1 package net.sourceforge.phpeclipse.xdebug.ui.php.launching;
4 import java.text.MessageFormat;
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 import net.sourceforge.phpeclipse.xdebug.core.IXDebugPreferenceConstants;
10 import net.sourceforge.phpeclipse.xdebug.core.XDebugCorePlugin;
11 import net.sourceforge.phpeclipse.xdebug.php.launching.IXDebugConstants;
13 import org.eclipse.core.resources.IFile;
14 import org.eclipse.core.resources.IProject;
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.resources.ResourcesPlugin;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.debug.core.ILaunchConfiguration;
19 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
20 import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.ModifyEvent;
25 import org.eclipse.swt.events.ModifyListener;
26 import org.eclipse.swt.events.SelectionAdapter;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.graphics.Font;
29 import org.eclipse.swt.graphics.Image;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.layout.GridLayout;
32 import org.eclipse.swt.widgets.Button;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.FileDialog;
35 import org.eclipse.swt.widgets.Group;
36 import org.eclipse.swt.widgets.Text;
37 import org.eclipse.ui.IEditorInput;
38 import org.eclipse.ui.IEditorPart;
39 import org.eclipse.ui.IWorkbenchPage;
41 public class PHPMainTab extends AbstractLaunchConfigurationTab {
44 protected Text fProjText;
45 protected Button fProjButton;
47 // Main class UI widgets
48 protected Text fMainText;
49 protected Button fSearchButton;
50 protected PHPProjectSelector projectSelector;
51 protected PHPFileSelector fileSelector;
52 private Button fUseDefaultInterpreterButton;
53 private Button fInterpreterButton;
54 private Text fInterpreterText;
60 public void createControl(Composite parent) {
61 Font font = parent.getFont();
63 Composite comp = new Composite(parent, SWT.NONE);
65 // PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaDebugHelpContextIds.LAUNCH_CONFIGURATION_DIALOG_MAIN_TAB);
66 GridLayout topLayout = new GridLayout();
67 topLayout.verticalSpacing = 0;
68 comp.setLayout(topLayout);
71 createProjectEditor(comp);
72 createVerticalSpacer(comp, 1);
73 createMainTypeEditor(comp);
74 createVerticalSpacer(comp, 1);
75 createInterpreterEditor(comp);
79 * Creates the widgets for specifying a main type.
81 * @param parent the parent composite
83 private void createProjectEditor(Composite parent) {
84 Font font= parent.getFont();
85 Group group= new Group(parent, SWT.NONE);
86 group.setText("Project:");
87 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
88 group.setLayoutData(gd);
89 GridLayout layout = new GridLayout();
90 layout.numColumns = 2;
91 group.setLayout(layout);
94 projectSelector = new PHPProjectSelector(group);
95 projectSelector.setBrowseDialogMessage("Choose the project containing the application entry point:");
96 projectSelector.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
97 projectSelector.addModifyListener(new ModifyListener() {
98 public void modifyText(ModifyEvent evt) {
99 updateLaunchConfigurationDialog();
106 * Creates the widgets for specifying a php file.
108 * @param parent the parent composite
110 private void createMainTypeEditor(Composite parent) {
111 Font font= parent.getFont();
112 Group mainGroup= new Group(parent, SWT.NONE);
113 mainGroup.setText("File: ");
114 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
115 mainGroup.setLayoutData(gd);
116 GridLayout layout = new GridLayout();
117 layout.numColumns = 2;
118 mainGroup.setLayout(layout);
119 mainGroup.setFont(font);
121 fileSelector = new PHPFileSelector(mainGroup, projectSelector);
122 fileSelector.setBrowseDialogMessage("Choose the PHP file that represents the application entry point:");
123 fileSelector.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
124 fileSelector.addModifyListener(new ModifyListener() {
125 public void modifyText(ModifyEvent evt) {
126 updateLaunchConfigurationDialog();
132 * Creates the widgets for specifying debug settings.
134 * @param parent the parent composite
136 private void createInterpreterEditor(Composite parent) {
137 Font font= parent.getFont();
138 Group interpreterGroup= new Group(parent, SWT.NONE);
139 interpreterGroup.setText("Interpreter: ");
140 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
141 interpreterGroup.setLayoutData(gd);
142 GridLayout layout = new GridLayout();
143 layout.numColumns = 2;
144 interpreterGroup.setLayout(layout);
145 interpreterGroup.setFont(font);
147 fInterpreterText= new Text(interpreterGroup, SWT.SINGLE | SWT.BORDER);
148 gd= new GridData(GridData.FILL_HORIZONTAL);
149 fInterpreterText.setLayoutData(gd);
150 fInterpreterText.setFont(font);
151 fInterpreterText.addModifyListener(new ModifyListener() {
152 public void modifyText(ModifyEvent evt) {
153 updateLaunchConfigurationDialog();
158 fInterpreterButton= createPushButton(interpreterGroup,"Browse..", null);
159 fInterpreterButton.addSelectionListener(new SelectionAdapter() {
160 public void widgetSelected(SelectionEvent event) {
161 handleBrowseSellected(event);
165 fUseDefaultInterpreterButton = new Button(interpreterGroup,SWT.CHECK);
166 fUseDefaultInterpreterButton.setText("Use default interpreter");
167 gd = new GridData(GridData.FILL_HORIZONTAL);
168 fUseDefaultInterpreterButton.setLayoutData(gd);
169 fUseDefaultInterpreterButton.setFont(font);
170 fUseDefaultInterpreterButton.addSelectionListener(new SelectionAdapter() {
171 public void widgetSelected(SelectionEvent event) {
172 handleDefaultSellected(event);
179 * Set the appropriate enabled state for the appletviewqer text widget.
181 protected void setInterpreterTextEnabledState() {
182 if (isDefaultInterpreter()) {
183 fInterpreterText.setEnabled(false);
184 fInterpreterButton.setEnabled(false);
186 fInterpreterText.setEnabled(true);
187 fInterpreterButton.setEnabled(true);
192 * Returns whether the default appletviewer is to be used
194 protected boolean isDefaultInterpreter() {
195 return fUseDefaultInterpreterButton.getSelection();
200 protected void handleDefaultSellected(SelectionEvent event) {
201 setInterpreterTextEnabledState();
202 updateLaunchConfigurationDialog();
203 // if (isDefaultInterpreter()) {
204 // fInterpreterText.setText("default Interpreter");
209 protected void handleBrowseSellected(SelectionEvent event) {
210 FileDialog dlg=new FileDialog(getShell(),SWT.OPEN);
211 String fileName=dlg.open();
212 if (fileName!=null) {
213 fInterpreterText.setText(fileName);
214 updateLaunchConfigurationDialog();
218 protected IProject getContext() {
219 IWorkbenchPage page= XDebugCorePlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
221 ISelection selection = page.getSelection();
222 if (selection instanceof IStructuredSelection) {
223 IStructuredSelection ss = (IStructuredSelection) selection;
225 Object obj = ss.getFirstElement();
226 if (obj instanceof IResource)
227 return ((IResource) obj).getProject();
230 IEditorPart part = page.getActiveEditor();
232 IEditorInput input = part.getEditorInput();
233 IResource file = (IResource) input.getAdapter(IResource.class);
235 return file.getProject();
242 public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
243 IProject project = getContext();
245 configuration.setAttribute(IXDebugConstants.ATTR_PHP_PROJECT, project.getName());
249 public void initializeFrom(ILaunchConfiguration configuration) {
251 String project = configuration.getAttribute(IXDebugConstants.ATTR_PHP_PROJECT, (String)null);
252 if (project != null) {
253 projectSelector.setSelectionText(project);
255 String file = configuration.getAttribute(IXDebugConstants.ATTR_PHP_FILE, (String)null);
257 fileSelector.setSelectionText(file);
260 String interpreterFile=configuration.getAttribute(IXDebugConstants.ATTR_PHP_INTERPRETER, (String) null);
261 if(interpreterFile!=null)
262 fInterpreterText.setText(interpreterFile);
263 boolean selection=configuration.getAttribute(IXDebugConstants.ATTR_PHP_DEFAULT_INTERPRETER, true);
264 fUseDefaultInterpreterButton.setSelection(selection);
265 setInterpreterTextEnabledState();
267 } catch (CoreException e) {
268 setErrorMessage(e.getMessage());
272 public void performApply(ILaunchConfigurationWorkingCopy configuration) {
273 String project = projectSelector.getSelectionText().trim();
274 if (project.length() == 0) {
277 configuration.setAttribute(IXDebugConstants.ATTR_PHP_PROJECT, project);
279 IFile file = fileSelector.getSelection();
280 configuration.setAttribute(IXDebugConstants.ATTR_PHP_FILE, file == null ? "" : file.getProjectRelativePath()
282 configuration.setAttribute(IXDebugConstants.ATTR_PHP_DEFAULT_INTERPRETER, this.fUseDefaultInterpreterButton.getSelection());
283 configuration.setAttribute(IXDebugConstants.ATTR_PHP_INTERPRETER, this.fInterpreterText.getText());
288 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#isValid(org.eclipse.debug.core.ILaunchConfiguration)
290 public boolean isValid(ILaunchConfiguration launchConfig) {
291 setErrorMessage(null);
292 String projectName=projectSelector.getSelectionText().trim();
293 IProject project=ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
294 if (!project.exists()) {
295 setErrorMessage("Project does not exist");
298 IFile file=project.getFile(fileSelector.getSelectionText().trim());
299 if (!file.exists()) {
300 setErrorMessage("File does not exist");
303 if (!fUseDefaultInterpreterButton.getSelection()) {
304 File exe = new File(fInterpreterText.getText());
305 System.out.println(exe.toString());
307 setErrorMessage("Invalid Interpreter");
314 public Image getImage() {
315 return PHPUiImages.get(PHPUiImages.IMG_CTOOLS_PHP_PAGE);
318 public String getName() {