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