1 package net.sourceforge.phpdt.internal.debug.ui.launcher;
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;
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;
32 public class PHPEntryPointTab extends AbstractLaunchConfigurationTab {
33 protected String originalFileName, originalProjectName;
34 protected PHPProjectSelector projectSelector;
35 protected PHPFileSelector fileSelector;
37 public PHPEntryPointTab() {
41 public void createControl(Composite parent) {
42 Composite composite = createPageRoot(parent);
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();
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();
65 protected IProject getContext() {
66 IWorkbenchPage page = PHPDebugUiPlugin.getActivePage();
68 ISelection selection = page.getSelection();
69 if (selection instanceof IStructuredSelection) {
70 IStructuredSelection ss = (IStructuredSelection) selection;
72 Object obj = ss.getFirstElement();
73 if (obj instanceof IResource)
74 return ((IResource) obj).getProject();
77 IEditorPart part = page.getActiveEditor();
79 IEditorInput input = part.getEditorInput();
80 IResource file = (IResource) input.getAdapter(IResource.class);
81 return file.getProject();
87 public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
88 IProject project = getContext();
90 configuration.setAttribute(PHPLaunchConfigurationAttribute.PROJECT_NAME, project.getName());
93 public void initializeFrom(ILaunchConfiguration configuration) {
95 originalProjectName = configuration.getAttribute(PHPLaunchConfigurationAttribute.PROJECT_NAME, "");
96 originalFileName = configuration.getAttribute(PHPLaunchConfigurationAttribute.FILE_NAME, "");
97 } catch (CoreException e) {
101 projectSelector.setSelectionText(originalProjectName);
102 if (!"".equals(originalFileName))
103 fileSelector.setSelectionText(new Path(originalFileName).toOSString());
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());
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);
118 setControl(composite);
122 public String getName() {
123 return PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEntryPoint.name");
126 public boolean isValid(ILaunchConfiguration launchConfig) {
129 String projectName = launchConfig.getAttribute(PHPLaunchConfigurationAttribute.PROJECT_NAME, "");
130 if (projectName.length() == 0) {
131 setErrorMessage(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEntryPoint.invalidProjectSelectionMessage"));
135 String fileName = launchConfig.getAttribute(PHPLaunchConfigurationAttribute.FILE_NAME, "");
136 if (fileName.length() == 0) {
137 setErrorMessage(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEntryPoint.invalidFileSelectionMessage"));
140 } catch (CoreException e) {
144 setErrorMessage(null);
148 protected void log(Throwable t) {
149 PHPDebugUiPlugin.getDefault().log(t);
152 public boolean canSave() {
153 return getErrorMessage() == null;
156 public Image getImage() {
157 return PHPUiImages.get(PHPUiImages.IMG_CTOOLS_PHP_PAGE);