Minor changes in pages design.
[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.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.Group;
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                 Group grpProject = new Group(composite, SWT.NONE);
50                 grpProject
51                                 .setText(PHPDebugUiMessages
52                                                 .getString("LaunchConfigurationTab.PHPEntryPoint.projectLabel"));
53                 grpProject.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
54                 grpProject.setLayout(new GridLayout());
55                 projectSelector = new PHPProjectSelector(grpProject);
56                 projectSelector
57                                 .setBrowseDialogMessage(PHPDebugUiMessages
58                                                 .getString("LaunchConfigurationTab.PHPEntryPoint.projectSelectorMessage"));
59                 projectSelector.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
60                 projectSelector.addModifyListener(new ModifyListener() {
61                         public void modifyText(ModifyEvent evt) {
62                                 updateLaunchConfigurationDialog();
63                         }
64                 });
65
66 //              new Label(composite, SWT.NONE).setText(PHPDebugUiMessages
67 //                              .getString("LaunchConfigurationTab.PHPEntryPoint.fileLabel"));
68 //              fileSelector = new PHPFileSelector(composite, projectSelector);
69                 Group grpFile = new Group(composite, SWT.NONE);
70                 grpFile.setText(PHPDebugUiMessages
71                                 .getString("LaunchConfigurationTab.PHPEntryPoint.fileLabel"));
72                 grpFile.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
73                 grpFile.setLayout(new GridLayout());
74                 fileSelector = new PHPFileSelector(grpFile, projectSelector);
75                 fileSelector
76                                 .setBrowseDialogMessage(PHPDebugUiMessages
77                                                 .getString("LaunchConfigurationTab.PHPEntryPoint.fileSelectorMessage"));
78                 fileSelector.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
79                 fileSelector.addModifyListener(new ModifyListener() {
80                         public void modifyText(ModifyEvent evt) {
81                                 updateLaunchConfigurationDialog();
82                         }
83                 });
84         }
85
86         protected IResource getContext() {
87                 IWorkbenchPage page = PHPDebugUiPlugin.getActivePage();
88                 if (page != null) {
89                         ISelection selection = page.getSelection();
90                         if (selection instanceof IStructuredSelection) {
91                                 IStructuredSelection ss = (IStructuredSelection) selection;
92                                 if (!ss.isEmpty()) {
93                                         Object obj = ss.getFirstElement();
94                                         if (obj instanceof IResource)
95                                                 return ((IResource) obj);
96                                 }
97                         }
98                         IEditorPart part = page.getActiveEditor();
99                         if (part != null) {
100                                 IEditorInput input = part.getEditorInput();
101                                 IResource file = (IResource) input.getAdapter(IResource.class);
102                                 if (file != null) {
103                                         return file;
104                                 }
105                         }
106                 }
107                 return null;
108         }
109
110         public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
111                 IResource file = getContext();
112                 if (file != null) {
113                         configuration.setAttribute(
114                                         PHPLaunchConfigurationAttribute.PROJECT_NAME, file
115                                                         .getProject().getName());
116                         configuration.setAttribute(
117                                         PHPLaunchConfigurationAttribute.FILE_NAME, file
118                                                         .getProjectRelativePath().toOSString());
119                 }
120         }
121
122         public void initializeFrom(ILaunchConfiguration configuration) {
123                 try {
124                         originalProjectName = configuration.getAttribute(
125                                         PHPLaunchConfigurationAttribute.PROJECT_NAME, "");
126                         originalFileName = configuration.getAttribute(
127                                         PHPLaunchConfigurationAttribute.FILE_NAME, "");
128                 } catch (CoreException e) {
129                         log(e);
130                 }
131
132                 projectSelector.setSelectionText(originalProjectName);
133                 if (!"".equals(originalFileName))
134                         fileSelector.setSelectionText(new Path(originalFileName)
135                                         .toOSString());
136         }
137
138         public void performApply(ILaunchConfigurationWorkingCopy configuration) {
139                 configuration.setAttribute(
140                                 PHPLaunchConfigurationAttribute.PROJECT_NAME, projectSelector
141                                                 .getSelectionText());
142                 IFile file = fileSelector.getSelection();
143                 configuration.setAttribute(PHPLaunchConfigurationAttribute.FILE_NAME,
144                                 file == null ? "" : file.getProjectRelativePath().toString());
145         }
146
147         protected Composite createPageRoot(Composite parent) {
148                 Composite composite = new Composite(parent, SWT.NONE);
149                 GridLayout layout = new GridLayout();
150 //              layout.marginWidth = 0;
151                 composite.setLayout(layout);
152
153                 setControl(composite);
154                 return composite;
155         }
156
157         public String getName() {
158                 return PHPDebugUiMessages
159                                 .getString("LaunchConfigurationTab.PHPEntryPoint.name");
160         }
161
162         public boolean isValid(ILaunchConfiguration launchConfig) {
163                 try {
164
165                         String projectName = launchConfig.getAttribute(
166                                         PHPLaunchConfigurationAttribute.PROJECT_NAME, "");
167                         if (projectName.length() == 0) {
168                                 setErrorMessage(PHPDebugUiMessages
169                                                 .getString("LaunchConfigurationTab.PHPEntryPoint.invalidProjectSelectionMessage"));
170                                 return false;
171                         }
172
173                         String fileName = launchConfig.getAttribute(
174                                         PHPLaunchConfigurationAttribute.FILE_NAME, "");
175                         if (fileName.length() == 0) {
176                                 setErrorMessage(PHPDebugUiMessages
177                                                 .getString("LaunchConfigurationTab.PHPEntryPoint.invalidFileSelectionMessage"));
178                                 return false;
179                         }
180                 } catch (CoreException e) {
181                         log(e);
182                 }
183
184                 setErrorMessage(null);
185                 return true;
186         }
187
188         protected void log(Throwable t) {
189                 PHPDebugUiPlugin.log(t);
190         }
191
192         public boolean canSave() {
193                 return getErrorMessage() == null;
194         }
195
196         public Image getImage() {
197                 return PHPUiImages.get(PHPUiImages.IMG_CTOOLS_PHP_PAGE);
198         }
199
200 }