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