This commit was generated by cvs2svn to compensate for changes in r50,
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.ui / src / net / sourceforge / phpdt / internal / debug / ui / launcher / PHPEntryPointTab.java
1 package net.sourceforge.phpdt.internal.debug.ui.launcher;
2
3 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
4 import org.eclipse.core.resources.IFile;
5 import org.eclipse.core.resources.IProject;
6 import org.eclipse.core.resources.IResource;
7 import org.eclipse.core.runtime.CoreException;
8 import org.eclipse.core.runtime.Path;
9 import org.eclipse.debug.core.ILaunchConfiguration;
10 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
11 import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
12 import org.eclipse.jface.viewers.ISelection;
13 import org.eclipse.jface.viewers.IStructuredSelection;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.ModifyEvent;
16 import org.eclipse.swt.events.ModifyListener;
17 import org.eclipse.swt.graphics.Image;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Label;
22 import org.eclipse.ui.IEditorInput;
23 import org.eclipse.ui.IEditorPart;
24 import org.eclipse.ui.IWorkbenchPage;
25 import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiMessages;
26 import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiPlugin;
27 import net.sourceforge.phpdt.internal.launching.PHPLaunchConfigurationAttribute;
28 import net.sourceforge.phpdt.internal.ui.util.PHPFileSelector;
29 import net.sourceforge.phpdt.internal.ui.util.PHPProjectSelector;
30
31 public class PHPEntryPointTab extends AbstractLaunchConfigurationTab {
32         protected String originalFileName, originalProjectName;
33         protected PHPProjectSelector projectSelector;
34         protected PHPFileSelector fileSelector;
35
36         public PHPEntryPointTab() {
37                 super();
38         }
39
40         public void createControl(Composite parent) {
41                 Composite composite = createPageRoot(parent);
42
43                 new Label(composite, SWT.NONE).setText(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEntryPoint.projectLabel"));
44                 projectSelector = new PHPProjectSelector(composite);
45                 projectSelector.setBrowseDialogMessage(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEntryPoint.projectSelectorMessage"));
46                 projectSelector.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
47                 projectSelector.addModifyListener(new ModifyListener() {
48                         public void modifyText(ModifyEvent evt) {
49                                 updateLaunchConfigurationDialog();
50                         }
51                 });
52
53                 new Label(composite, SWT.NONE).setText(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEntryPoint.fileLabel"));
54                 fileSelector = new PHPFileSelector(composite, projectSelector);
55                 fileSelector.setBrowseDialogMessage(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEntryPoint.fileSelectorMessage"));
56                 fileSelector.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
57                 fileSelector.addModifyListener(new ModifyListener() {
58                         public void modifyText(ModifyEvent evt) {
59                                 updateLaunchConfigurationDialog();
60                         }
61                 });
62         }
63
64         protected IProject getContext() {
65                 IWorkbenchPage page = PHPDebugUiPlugin.getActivePage();
66                 if (page != null) {
67                         ISelection selection = page.getSelection();
68                         if (selection instanceof IStructuredSelection) {
69                                 IStructuredSelection ss = (IStructuredSelection) selection;
70                                 if (!ss.isEmpty()) {
71                                         Object obj = ss.getFirstElement();
72                                         if (obj instanceof IResource)
73                                                 return ((IResource) obj).getProject();
74                                 }
75                         }
76                         IEditorPart part = page.getActiveEditor();
77                         if (part != null) {
78                                 IEditorInput input = part.getEditorInput();
79                                 IResource file = (IResource) input.getAdapter(IResource.class);
80                                 return file.getProject();
81                         }
82                 }
83                 return null;
84         }
85
86         public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
87                 IProject project = getContext();
88                 if (project != null)
89                         configuration.setAttribute(PHPLaunchConfigurationAttribute.PROJECT_NAME, project.getName());
90         }
91
92         public void initializeFrom(ILaunchConfiguration configuration) {
93                 try {
94                         originalProjectName = configuration.getAttribute(PHPLaunchConfigurationAttribute.PROJECT_NAME, "");
95                         originalFileName = configuration.getAttribute(PHPLaunchConfigurationAttribute.FILE_NAME, "");
96                 } catch (CoreException e) {
97                         log(e);
98                 }
99
100                 projectSelector.setSelectionText(originalProjectName);
101                 if (!"".equals(originalFileName))
102                         fileSelector.setSelectionText(new Path(originalFileName).toOSString());
103         }
104
105         public void performApply(ILaunchConfigurationWorkingCopy configuration) {
106                 configuration.setAttribute(PHPLaunchConfigurationAttribute.PROJECT_NAME, projectSelector.getSelectionText());
107                 IFile file = fileSelector.getSelection();
108                 configuration.setAttribute(PHPLaunchConfigurationAttribute.FILE_NAME, file == null ? "" : file.getProjectRelativePath().toString());
109         }
110
111         protected Composite createPageRoot(Composite parent) {
112                 Composite composite = new Composite(parent, SWT.NONE);
113                 GridLayout layout = new GridLayout();
114                 layout.marginWidth = 0;
115                 composite.setLayout(layout);
116
117                 setControl(composite);
118                 return composite;
119         }
120
121         public String getName() {
122                 return PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEntryPoint.name");
123         }
124
125         public boolean isValid(ILaunchConfiguration launchConfig) {
126                 try {
127                                 
128                         String projectName = launchConfig.getAttribute(PHPLaunchConfigurationAttribute.PROJECT_NAME, "");
129                         if (projectName.length() == 0) {
130                                 setErrorMessage(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEntryPoint.invalidProjectSelectionMessage"));
131                                 return false;
132                         }
133
134                         String fileName = launchConfig.getAttribute(PHPLaunchConfigurationAttribute.FILE_NAME, "");
135                         if (fileName.length() == 0) {
136                                 setErrorMessage(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEntryPoint.invalidFileSelectionMessage"));
137                                 return false;
138                         }
139                 } catch (CoreException e) {
140                         log(e);
141                 }
142                 
143                 setErrorMessage(null);
144                 return true;
145         }
146
147         protected void log(Throwable t) {
148                 PHPDebugUiPlugin.getDefault().log(t);
149         }
150
151         public boolean canSave() {
152                 return getErrorMessage() == null;
153         }
154
155         public Image getImage() {
156                 return PHPUiImages.get(PHPUiImages.IMG_CTOOLS_PHP_PAGE);
157   }
158
159 }