1 package net.sourceforge.phpeclipse.xdebug.ui.php.launching;
5 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
6 import net.sourceforge.phpdt.internal.ui.util.PHPFileSelector;
7 import net.sourceforge.phpdt.internal.ui.util.PHPProjectSelector;
8 import net.sourceforge.phpeclipse.xdebug.core.XDebugCorePlugin;
9 import net.sourceforge.phpeclipse.xdebug.php.launching.IXDebugConstants;
11 import org.eclipse.core.resources.IFile;
12 import org.eclipse.core.resources.IProject;
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.core.resources.ResourcesPlugin;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.debug.core.ILaunchConfiguration;
17 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
18 import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.ModifyEvent;
23 import org.eclipse.swt.events.ModifyListener;
24 import org.eclipse.swt.events.SelectionAdapter;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.graphics.Font;
27 import org.eclipse.swt.graphics.Image;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Button;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.FileDialog;
33 import org.eclipse.swt.widgets.Group;
34 import org.eclipse.swt.widgets.Text;
35 import org.eclipse.ui.IEditorInput;
36 import org.eclipse.ui.IEditorPart;
37 import org.eclipse.ui.IWorkbenchPage;
39 public class PHPMainTab extends AbstractLaunchConfigurationTab {
42 protected Text fProjText;
44 protected Button fProjButton;
46 // Main class UI widgets
47 protected Text fMainText;
49 protected Button fSearchButton;
51 protected PHPProjectSelector projectSelector;
53 protected PHPFileSelector fileSelector;
55 private Button fUseDefaultInterpreterButton;
57 private Button fInterpreterButton;
59 private Text fInterpreterText;
65 public void createControl(Composite parent) {
66 Font font = parent.getFont();
68 Composite comp = new Composite(parent, SWT.NONE);
70 // PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(),
71 // IJavaDebugHelpContextIds.LAUNCH_CONFIGURATION_DIALOG_MAIN_TAB);
72 GridLayout topLayout = new GridLayout();
73 topLayout.verticalSpacing = 0;
74 comp.setLayout(topLayout);
77 createProjectEditor(comp);
78 createVerticalSpacer(comp, 1);
79 createMainTypeEditor(comp);
80 createVerticalSpacer(comp, 1);
81 createInterpreterEditor(comp);
85 * Creates the widgets for specifying a main type.
88 * the parent composite
90 private void createProjectEditor(Composite parent) {
91 Font font = parent.getFont();
92 Group group = new Group(parent, SWT.NONE);
93 group.setText("Project:");
94 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
95 group.setLayoutData(gd);
96 GridLayout layout = new GridLayout();
97 layout.numColumns = 2;
98 group.setLayout(layout);
101 projectSelector = new PHPProjectSelector(group);
103 .setBrowseDialogMessage("Choose the project containing the application entry point:");
104 projectSelector.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
105 projectSelector.addModifyListener(new ModifyListener() {
106 public void modifyText(ModifyEvent evt) {
107 updateLaunchConfigurationDialog();
113 * Creates the widgets for specifying a php file.
116 * the parent composite
118 private void createMainTypeEditor(Composite parent) {
119 Font font = parent.getFont();
120 Group mainGroup = new Group(parent, SWT.NONE);
121 mainGroup.setText("File: ");
122 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
123 mainGroup.setLayoutData(gd);
124 GridLayout layout = new GridLayout();
125 layout.numColumns = 2;
126 mainGroup.setLayout(layout);
127 mainGroup.setFont(font);
129 fileSelector = new PHPFileSelector(mainGroup, projectSelector);
131 .setBrowseDialogMessage("Choose the PHP file that represents the application entry point:");
132 fileSelector.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
133 fileSelector.addModifyListener(new ModifyListener() {
134 public void modifyText(ModifyEvent evt) {
135 updateLaunchConfigurationDialog();
141 * Creates the widgets for specifying debug settings.
144 * the parent composite
146 private void createInterpreterEditor(Composite parent) {
147 Font font = parent.getFont();
148 Group interpreterGroup = new Group(parent, SWT.NONE);
149 interpreterGroup.setText("Interpreter: ");
150 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
151 interpreterGroup.setLayoutData(gd);
152 GridLayout layout = new GridLayout();
153 layout.numColumns = 2;
154 interpreterGroup.setLayout(layout);
155 interpreterGroup.setFont(font);
157 fInterpreterText = new Text(interpreterGroup, SWT.SINGLE | SWT.BORDER);
158 gd = new GridData(GridData.FILL_HORIZONTAL);
159 fInterpreterText.setLayoutData(gd);
160 fInterpreterText.setFont(font);
161 fInterpreterText.addModifyListener(new ModifyListener() {
162 public void modifyText(ModifyEvent evt) {
163 updateLaunchConfigurationDialog();
167 fInterpreterButton = createPushButton(interpreterGroup, "Browse..",
169 fInterpreterButton.addSelectionListener(new SelectionAdapter() {
170 public void widgetSelected(SelectionEvent event) {
171 handleBrowseSellected(event);
175 fUseDefaultInterpreterButton = new Button(interpreterGroup, SWT.CHECK);
176 fUseDefaultInterpreterButton.setText("Use default interpreter");
177 gd = new GridData(GridData.FILL_HORIZONTAL);
178 fUseDefaultInterpreterButton.setLayoutData(gd);
179 fUseDefaultInterpreterButton.setFont(font);
180 fUseDefaultInterpreterButton
181 .addSelectionListener(new SelectionAdapter() {
182 public void widgetSelected(SelectionEvent event) {
183 handleDefaultSellected(event);
190 * Set the appropriate enabled state for the appletviewqer text widget.
192 protected void setInterpreterTextEnabledState() {
193 if (isDefaultInterpreter()) {
194 fInterpreterText.setEnabled(false);
195 fInterpreterButton.setEnabled(false);
197 fInterpreterText.setEnabled(true);
198 fInterpreterButton.setEnabled(true);
203 * Returns whether the default appletviewer is to be used
205 protected boolean isDefaultInterpreter() {
206 return fUseDefaultInterpreterButton.getSelection();
209 protected void handleDefaultSellected(SelectionEvent event) {
210 setInterpreterTextEnabledState();
211 updateLaunchConfigurationDialog();
212 // if (isDefaultInterpreter()) {
213 // fInterpreterText.setText("default Interpreter");
218 protected void handleBrowseSellected(SelectionEvent event) {
219 FileDialog dlg = new FileDialog(getShell(), SWT.OPEN);
220 String fileName = dlg.open();
221 if (fileName != null) {
222 fInterpreterText.setText(fileName);
223 updateLaunchConfigurationDialog();
227 protected IProject getContext() {
228 IWorkbenchPage page = XDebugCorePlugin.getDefault().getWorkbench()
229 .getActiveWorkbenchWindow().getActivePage();
231 ISelection selection = page.getSelection();
232 if (selection instanceof IStructuredSelection) {
233 IStructuredSelection ss = (IStructuredSelection) selection;
235 Object obj = ss.getFirstElement();
236 if (obj instanceof IResource)
237 return ((IResource) obj).getProject();
240 IEditorPart part = page.getActiveEditor();
242 IEditorInput input = part.getEditorInput();
243 IResource file = (IResource) input.getAdapter(IResource.class);
245 return file.getProject();
252 public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
253 IProject project = getContext();
255 configuration.setAttribute(IXDebugConstants.ATTR_PHP_PROJECT,
259 public void initializeFrom(ILaunchConfiguration configuration) {
261 String project = configuration.getAttribute(
262 IXDebugConstants.ATTR_PHP_PROJECT, (String) null);
263 if (project != null) {
264 projectSelector.setSelectionText(project);
266 String file = configuration.getAttribute(
267 IXDebugConstants.ATTR_PHP_FILE, (String) null);
269 fileSelector.setSelectionText(file);
272 String interpreterFile = configuration.getAttribute(
273 IXDebugConstants.ATTR_PHP_INTERPRETER, (String) null);
274 if (interpreterFile != null)
275 fInterpreterText.setText(interpreterFile);
276 boolean selection = configuration.getAttribute(
277 IXDebugConstants.ATTR_PHP_DEFAULT_INTERPRETER, true);
278 fUseDefaultInterpreterButton.setSelection(selection);
279 setInterpreterTextEnabledState();
281 } catch (CoreException e) {
282 setErrorMessage(e.getMessage());
286 public void performApply(ILaunchConfigurationWorkingCopy configuration) {
287 String project = projectSelector.getSelectionText().trim();
288 if (project.length() == 0) {
291 configuration.setAttribute(IXDebugConstants.ATTR_PHP_PROJECT, project);
293 IFile file = fileSelector.getSelection();
294 configuration.setAttribute(IXDebugConstants.ATTR_PHP_FILE,
295 file == null ? "" : file.getProjectRelativePath().toString());
296 configuration.setAttribute(
297 IXDebugConstants.ATTR_PHP_DEFAULT_INTERPRETER,
298 this.fUseDefaultInterpreterButton.getSelection());
299 configuration.setAttribute(IXDebugConstants.ATTR_PHP_INTERPRETER,
300 this.fInterpreterText.getText());
307 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#isValid(org.eclipse.debug.core.ILaunchConfiguration)
309 public boolean isValid(ILaunchConfiguration launchConfig) {
310 setErrorMessage(null);
311 String projectName = projectSelector.getSelectionText().trim();
312 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(
314 if (!project.exists()) {
315 setErrorMessage("Project does not exist");
318 String fileName = fileSelector.getSelectionText().trim();
319 if (fileName.equals("")) {
320 setErrorMessage("No file selected.");
323 IFile file = project.getFile(fileName);
324 if (!file.exists()) {
325 setErrorMessage("File does not exist");
328 if (!fUseDefaultInterpreterButton.getSelection()) {
329 File exe = new File(fInterpreterText.getText());
330 System.out.println(exe.toString());
332 setErrorMessage("Invalid Interpreter");
339 public Image getImage() {
340 return PHPUiImages.get(PHPUiImages.IMG_CTOOLS_PHP_PAGE);
343 public String getName() {