1 package net.sourceforge.phpdt.internal.debug.ui.launcher;
3 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
4 import org.eclipse.core.resources.IFile;
5 import org.eclipse.core.resources.IProject;
6 import org.eclipse.core.resources.IResource;
7 import org.eclipse.core.runtime.CoreException;
8 import org.eclipse.core.runtime.Path;
9 import org.eclipse.debug.core.ILaunchConfiguration;
10 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
11 import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
12 import org.eclipse.jface.viewers.ISelection;
13 import org.eclipse.jface.viewers.IStructuredSelection;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.ModifyEvent;
16 import org.eclipse.swt.events.ModifyListener;
17 import org.eclipse.swt.graphics.Image;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Label;
22 import org.eclipse.ui.IEditorInput;
23 import org.eclipse.ui.IEditorPart;
24 import org.eclipse.ui.IWorkbenchPage;
25 import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiMessages;
26 import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiPlugin;
27 import net.sourceforge.phpdt.internal.launching.PHPLaunchConfigurationAttribute;
28 import net.sourceforge.phpdt.internal.ui.util.PHPFileSelector;
29 import net.sourceforge.phpdt.internal.ui.util.PHPProjectSelector;
31 public class PHPEntryPointTab extends AbstractLaunchConfigurationTab {
32 protected String originalFileName, originalProjectName;
33 protected PHPProjectSelector projectSelector;
34 protected PHPFileSelector fileSelector;
36 public PHPEntryPointTab() {
40 public void createControl(Composite parent) {
41 Composite composite = createPageRoot(parent);
43 new Label(composite, SWT.NONE).setText(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEntryPoint.projectLabel"));
44 projectSelector = new PHPProjectSelector(composite);
45 projectSelector.setBrowseDialogMessage(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEntryPoint.projectSelectorMessage"));
46 projectSelector.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
47 projectSelector.addModifyListener(new ModifyListener() {
48 public void modifyText(ModifyEvent evt) {
49 updateLaunchConfigurationDialog();
53 new Label(composite, SWT.NONE).setText(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEntryPoint.fileLabel"));
54 fileSelector = new PHPFileSelector(composite, projectSelector);
55 fileSelector.setBrowseDialogMessage(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEntryPoint.fileSelectorMessage"));
56 fileSelector.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
57 fileSelector.addModifyListener(new ModifyListener() {
58 public void modifyText(ModifyEvent evt) {
59 updateLaunchConfigurationDialog();
64 protected IProject getContext() {
65 IWorkbenchPage page = PHPDebugUiPlugin.getActivePage();
67 ISelection selection = page.getSelection();
68 if (selection instanceof IStructuredSelection) {
69 IStructuredSelection ss = (IStructuredSelection) selection;
71 Object obj = ss.getFirstElement();
72 if (obj instanceof IResource)
73 return ((IResource) obj).getProject();
76 IEditorPart part = page.getActiveEditor();
78 IEditorInput input = part.getEditorInput();
79 IResource file = (IResource) input.getAdapter(IResource.class);
80 return file.getProject();
86 public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
87 IProject project = getContext();
89 configuration.setAttribute(PHPLaunchConfigurationAttribute.PROJECT_NAME, project.getName());
92 public void initializeFrom(ILaunchConfiguration configuration) {
94 originalProjectName = configuration.getAttribute(PHPLaunchConfigurationAttribute.PROJECT_NAME, "");
95 originalFileName = configuration.getAttribute(PHPLaunchConfigurationAttribute.FILE_NAME, "");
96 } catch (CoreException e) {
100 projectSelector.setSelectionText(originalProjectName);
101 if (!"".equals(originalFileName))
102 fileSelector.setSelectionText(new Path(originalFileName).toOSString());
105 public void performApply(ILaunchConfigurationWorkingCopy configuration) {
106 configuration.setAttribute(PHPLaunchConfigurationAttribute.PROJECT_NAME, projectSelector.getSelectionText());
107 IFile file = fileSelector.getSelection();
108 configuration.setAttribute(PHPLaunchConfigurationAttribute.FILE_NAME, file == null ? "" : file.getProjectRelativePath().toString());
111 protected Composite createPageRoot(Composite parent) {
112 Composite composite = new Composite(parent, SWT.NONE);
113 GridLayout layout = new GridLayout();
114 layout.marginWidth = 0;
115 composite.setLayout(layout);
117 setControl(composite);
121 public String getName() {
122 return PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEntryPoint.name");
125 public boolean isValid(ILaunchConfiguration launchConfig) {
128 String projectName = launchConfig.getAttribute(PHPLaunchConfigurationAttribute.PROJECT_NAME, "");
129 if (projectName.length() == 0) {
130 setErrorMessage(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEntryPoint.invalidProjectSelectionMessage"));
134 String fileName = launchConfig.getAttribute(PHPLaunchConfigurationAttribute.FILE_NAME, "");
135 if (fileName.length() == 0) {
136 setErrorMessage(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEntryPoint.invalidFileSelectionMessage"));
139 } catch (CoreException e) {
143 setErrorMessage(null);
147 protected void log(Throwable t) {
148 PHPDebugUiPlugin.getDefault().log(t);
151 public boolean canSave() {
152 return getErrorMessage() == null;
155 public Image getImage() {
156 return PHPUiImages.get(PHPUiImages.IMG_CTOOLS_PHP_PAGE);