1 package net.sourceforge.phpeclipse.xdebug.ui.php.launching;
4 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
5 import net.sourceforge.phpdt.internal.ui.util.PHPFileSelector;
6 import net.sourceforge.phpdt.internal.ui.util.PHPProjectSelector;
7 import net.sourceforge.phpeclipse.xdebug.core.XDebugCorePlugin;
8 import net.sourceforge.phpeclipse.xdebug.php.launching.IXDebugConstants;
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.resources.ResourcesPlugin;
14 import org.eclipse.core.runtime.CoreException;
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.events.SelectionAdapter;
24 import org.eclipse.swt.events.SelectionEvent;
25 import org.eclipse.swt.graphics.Font;
26 import org.eclipse.swt.graphics.Image;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Button;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.FileDialog;
32 import org.eclipse.swt.widgets.Group;
33 import org.eclipse.swt.widgets.Text;
34 import org.eclipse.ui.IEditorInput;
35 import org.eclipse.ui.IEditorPart;
36 import org.eclipse.ui.IWorkbenchPage;
38 public class PHPMainTab extends AbstractLaunchConfigurationTab {
41 protected Text fProjText;
42 protected Button fProjButton;
44 // Main class UI widgets
45 protected Text fMainText;
46 protected Button fSearchButton;
47 protected PHPProjectSelector projectSelector;
48 protected PHPFileSelector fileSelector;
49 private Button fUseDefaultInterpreterButton;
50 private Button fInterpreterButton;
51 private Text fInterpreterText;
57 public void createControl(Composite parent) {
58 Font font = parent.getFont();
60 Composite comp = new Composite(parent, SWT.NONE);
62 // PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaDebugHelpContextIds.LAUNCH_CONFIGURATION_DIALOG_MAIN_TAB);
63 GridLayout topLayout = new GridLayout();
64 topLayout.verticalSpacing = 0;
65 comp.setLayout(topLayout);
68 createProjectEditor(comp);
69 createVerticalSpacer(comp, 1);
70 createMainTypeEditor(comp);
71 createVerticalSpacer(comp, 1);
72 createInterpreterEditor(comp);
76 * Creates the widgets for specifying a main type.
78 * @param parent the parent composite
80 private void createProjectEditor(Composite parent) {
81 Font font= parent.getFont();
82 Group group= new Group(parent, SWT.NONE);
83 group.setText("Project:");
84 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
85 group.setLayoutData(gd);
86 GridLayout layout = new GridLayout();
87 layout.numColumns = 2;
88 group.setLayout(layout);
91 projectSelector = new PHPProjectSelector(group);
92 projectSelector.setBrowseDialogMessage("Choose the project containing the application entry point:");
93 projectSelector.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
94 projectSelector.addModifyListener(new ModifyListener() {
95 public void modifyText(ModifyEvent evt) {
96 updateLaunchConfigurationDialog();
103 * Creates the widgets for specifying a php file.
105 * @param parent the parent composite
107 private void createMainTypeEditor(Composite parent) {
108 Font font= parent.getFont();
109 Group mainGroup= new Group(parent, SWT.NONE);
110 mainGroup.setText("File: ");
111 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
112 mainGroup.setLayoutData(gd);
113 GridLayout layout = new GridLayout();
114 layout.numColumns = 2;
115 mainGroup.setLayout(layout);
116 mainGroup.setFont(font);
118 fileSelector = new PHPFileSelector(mainGroup, projectSelector);
119 fileSelector.setBrowseDialogMessage("Choose the PHP file that represents the application entry point:");
120 fileSelector.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
121 fileSelector.addModifyListener(new ModifyListener() {
122 public void modifyText(ModifyEvent evt) {
123 updateLaunchConfigurationDialog();
129 * Creates the widgets for specifying debug settings.
131 * @param parent the parent composite
133 private void createInterpreterEditor(Composite parent) {
134 Font font= parent.getFont();
135 Group interpreterGroup= new Group(parent, SWT.NONE);
136 interpreterGroup.setText("Interpreter: ");
137 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
138 interpreterGroup.setLayoutData(gd);
139 GridLayout layout = new GridLayout();
140 layout.numColumns = 2;
141 interpreterGroup.setLayout(layout);
142 interpreterGroup.setFont(font);
144 fInterpreterText= new Text(interpreterGroup, SWT.SINGLE | SWT.BORDER);
145 gd= new GridData(GridData.FILL_HORIZONTAL);
146 fInterpreterText.setLayoutData(gd);
147 fInterpreterText.setFont(font);
148 fInterpreterText.addModifyListener(new ModifyListener() {
149 public void modifyText(ModifyEvent evt) {
150 updateLaunchConfigurationDialog();
155 fInterpreterButton= createPushButton(interpreterGroup,"Browse..", null);
156 fInterpreterButton.addSelectionListener(new SelectionAdapter() {
157 public void widgetSelected(SelectionEvent event) {
158 handleBrowseSellected(event);
162 fUseDefaultInterpreterButton = new Button(interpreterGroup,SWT.CHECK);
163 fUseDefaultInterpreterButton.setText("Use default interpreter");
164 gd = new GridData(GridData.FILL_HORIZONTAL);
165 fUseDefaultInterpreterButton.setLayoutData(gd);
166 fUseDefaultInterpreterButton.setFont(font);
167 fUseDefaultInterpreterButton.addSelectionListener(new SelectionAdapter() {
168 public void widgetSelected(SelectionEvent event) {
169 handleDefaultSellected(event);
176 * Set the appropriate enabled state for the appletviewqer text widget.
178 protected void setInterpreterTextEnabledState() {
179 if (isDefaultInterpreter()) {
180 fInterpreterText.setEnabled(false);
181 fInterpreterButton.setEnabled(false);
183 fInterpreterText.setEnabled(true);
184 fInterpreterButton.setEnabled(true);
189 * Returns whether the default appletviewer is to be used
191 protected boolean isDefaultInterpreter() {
192 return fUseDefaultInterpreterButton.getSelection();
197 protected void handleDefaultSellected(SelectionEvent event) {
198 setInterpreterTextEnabledState();
199 updateLaunchConfigurationDialog();
200 // if (isDefaultInterpreter()) {
201 // fInterpreterText.setText("default Interpreter");
206 protected void handleBrowseSellected(SelectionEvent event) {
207 FileDialog dlg=new FileDialog(getShell(),SWT.OPEN);
208 String fileName=dlg.open();
209 if (fileName!=null) {
210 fInterpreterText.setText(fileName);
211 updateLaunchConfigurationDialog();
215 protected IProject getContext() {
216 IWorkbenchPage page= XDebugCorePlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
218 ISelection selection = page.getSelection();
219 if (selection instanceof IStructuredSelection) {
220 IStructuredSelection ss = (IStructuredSelection) selection;
222 Object obj = ss.getFirstElement();
223 if (obj instanceof IResource)
224 return ((IResource) obj).getProject();
227 IEditorPart part = page.getActiveEditor();
229 IEditorInput input = part.getEditorInput();
230 IResource file = (IResource) input.getAdapter(IResource.class);
232 return file.getProject();
239 public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
240 IProject project = getContext();
242 configuration.setAttribute(IXDebugConstants.ATTR_PHP_PROJECT, project.getName());
246 public void initializeFrom(ILaunchConfiguration configuration) {
248 String project = configuration.getAttribute(IXDebugConstants.ATTR_PHP_PROJECT, (String)null);
249 if (project != null) {
250 projectSelector.setSelectionText(project);
252 String file = configuration.getAttribute(IXDebugConstants.ATTR_PHP_FILE, (String)null);
254 fileSelector.setSelectionText(file);
257 String interpreterFile=configuration.getAttribute(IXDebugConstants.ATTR_PHP_INTERPRETER, (String) null);
258 if(interpreterFile!=null)
259 fInterpreterText.setText(interpreterFile);
260 boolean selection=configuration.getAttribute(IXDebugConstants.ATTR_PHP_DEFAULT_INTERPRETER, true);
261 fUseDefaultInterpreterButton.setSelection(selection);
262 setInterpreterTextEnabledState();
264 } catch (CoreException e) {
265 setErrorMessage(e.getMessage());
269 public void performApply(ILaunchConfigurationWorkingCopy configuration) {
270 String project = projectSelector.getSelectionText().trim();
271 if (project.length() == 0) {
274 configuration.setAttribute(IXDebugConstants.ATTR_PHP_PROJECT, project);
276 IFile file = fileSelector.getSelection();
277 configuration.setAttribute(IXDebugConstants.ATTR_PHP_FILE, file == null ? "" : file.getProjectRelativePath()
279 configuration.setAttribute(IXDebugConstants.ATTR_PHP_DEFAULT_INTERPRETER, this.fUseDefaultInterpreterButton.getSelection());
280 configuration.setAttribute(IXDebugConstants.ATTR_PHP_INTERPRETER, this.fInterpreterText.getText());
285 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#isValid(org.eclipse.debug.core.ILaunchConfiguration)
287 public boolean isValid(ILaunchConfiguration launchConfig) {
288 setErrorMessage(null);
289 String projectName=projectSelector.getSelectionText().trim();
290 IProject project=ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
291 if (!project.exists()) {
292 setErrorMessage("Project does not exist");
295 String fileName=fileSelector.getSelectionText().trim();
296 if (fileName.equals("")) {
297 setErrorMessage("No file selected.");
300 IFile file=project.getFile(fileName);
301 if (!file.exists()) {
302 setErrorMessage("File does not exist");
305 if (!fUseDefaultInterpreterButton.getSelection()) {
306 File exe = new File(fInterpreterText.getText());
307 System.out.println(exe.toString());
309 setErrorMessage("Invalid Interpreter");
316 public Image getImage() {
317 return PHPUiImages.get(PHPUiImages.IMG_CTOOLS_PHP_PAGE);
320 public String getName() {