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