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;
40 import org.eclipse.ui.PlatformUI;
42 public class PHPMainTab extends AbstractLaunchConfigurationTab {
45 protected Text fProjText;
46 protected Button fProjButton;
48 // Main class UI widgets
49 protected Text fMainText;
50 protected Button fSearchButton;
51 protected PHPProjectSelector projectSelector;
52 protected PHPFileSelector fileSelector;
53 private Button fUseDefaultInterpreterButton;
54 private Button fInterpreterButton;
55 private Text fInterpreterText;
61 public void createControl(Composite parent) {
62 Font font = parent.getFont();
64 Composite comp = new Composite(parent, SWT.NONE);
66 // PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaDebugHelpContextIds.LAUNCH_CONFIGURATION_DIALOG_MAIN_TAB);
67 GridLayout topLayout = new GridLayout();
68 topLayout.verticalSpacing = 0;
69 comp.setLayout(topLayout);
72 createProjectEditor(comp);
73 createVerticalSpacer(comp, 1);
74 createMainTypeEditor(comp);
75 createVerticalSpacer(comp, 1);
76 createInterpreterEditor(comp);
80 * Creates the widgets for specifying a main type.
82 * @param parent the parent composite
84 private void createProjectEditor(Composite parent) {
85 Font font= parent.getFont();
86 Group group= new Group(parent, SWT.NONE);
87 group.setText("Project:");
88 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
89 group.setLayoutData(gd);
90 GridLayout layout = new GridLayout();
91 layout.numColumns = 2;
92 group.setLayout(layout);
95 projectSelector = new PHPProjectSelector(group);
96 projectSelector.setBrowseDialogMessage("Choose the project containing the application entry point:");
97 projectSelector.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
98 projectSelector.addModifyListener(new ModifyListener() {
99 public void modifyText(ModifyEvent evt) {
100 updateLaunchConfigurationDialog();
107 * Creates the widgets for specifying a php file.
109 * @param parent the parent composite
111 private void createMainTypeEditor(Composite parent) {
112 Font font= parent.getFont();
113 Group mainGroup= new Group(parent, SWT.NONE);
114 mainGroup.setText("File: ");
115 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
116 mainGroup.setLayoutData(gd);
117 GridLayout layout = new GridLayout();
118 layout.numColumns = 2;
119 mainGroup.setLayout(layout);
120 mainGroup.setFont(font);
122 fileSelector = new PHPFileSelector(mainGroup, projectSelector);
123 fileSelector.setBrowseDialogMessage("Choose the PHP file that represents the application entry point:");
124 fileSelector.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
125 fileSelector.addModifyListener(new ModifyListener() {
126 public void modifyText(ModifyEvent evt) {
127 updateLaunchConfigurationDialog();
133 * Creates the widgets for specifying debug settings.
135 * @param parent the parent composite
137 private void createInterpreterEditor(Composite parent) {
138 Font font= parent.getFont();
139 Group interpreterGroup= new Group(parent, SWT.NONE);
140 interpreterGroup.setText("Interpreter: ");
141 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
142 interpreterGroup.setLayoutData(gd);
143 GridLayout layout = new GridLayout();
144 layout.numColumns = 2;
145 interpreterGroup.setLayout(layout);
146 interpreterGroup.setFont(font);
148 fInterpreterText= new Text(interpreterGroup, SWT.SINGLE | SWT.BORDER);
149 gd= new GridData(GridData.FILL_HORIZONTAL);
150 fInterpreterText.setLayoutData(gd);
151 fInterpreterText.setFont(font);
152 fInterpreterText.addModifyListener(new ModifyListener() {
153 public void modifyText(ModifyEvent evt) {
154 updateLaunchConfigurationDialog();
159 fInterpreterButton= createPushButton(interpreterGroup,"Browse..", null);
160 fInterpreterButton.addSelectionListener(new SelectionAdapter() {
161 public void widgetSelected(SelectionEvent event) {
162 handleBrowseSellected(event);
166 fUseDefaultInterpreterButton = new Button(interpreterGroup,SWT.CHECK);
167 fUseDefaultInterpreterButton.setText("Use default interpreter");
168 gd = new GridData(GridData.FILL_HORIZONTAL);
169 fUseDefaultInterpreterButton.setLayoutData(gd);
170 fUseDefaultInterpreterButton.setFont(font);
171 fUseDefaultInterpreterButton.addSelectionListener(new SelectionAdapter() {
172 public void widgetSelected(SelectionEvent event) {
173 handleDefaultSellected(event);
180 * Set the appropriate enabled state for the appletviewqer text widget.
182 protected void setInterpreterTextEnabledState() {
183 if (isDefaultInterpreter()) {
184 fInterpreterText.setEnabled(false);
185 fInterpreterButton.setEnabled(false);
187 fInterpreterText.setEnabled(true);
188 fInterpreterButton.setEnabled(true);
193 * Returns whether the default appletviewer is to be used
195 protected boolean isDefaultInterpreter() {
196 return fUseDefaultInterpreterButton.getSelection();
201 protected void handleDefaultSellected(SelectionEvent event) {
202 setInterpreterTextEnabledState();
203 updateLaunchConfigurationDialog();
204 // if (isDefaultInterpreter()) {
205 // fInterpreterText.setText("default Interpreter");
210 protected void handleBrowseSellected(SelectionEvent event) {
211 FileDialog dlg=new FileDialog(getShell(),SWT.OPEN);
212 String fileName=dlg.open();
213 if (fileName!=null) {
214 fInterpreterText.setText(fileName);
215 updateLaunchConfigurationDialog();
219 protected IProject getContext() {
220 IWorkbenchPage page= PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
222 ISelection selection = page.getSelection();
223 if (selection instanceof IStructuredSelection) {
224 IStructuredSelection ss = (IStructuredSelection) selection;
226 Object obj = ss.getFirstElement();
227 if (obj instanceof IResource)
228 return ((IResource) obj).getProject();
231 IEditorPart part = page.getActiveEditor();
233 IEditorInput input = part.getEditorInput();
234 IResource file = (IResource) input.getAdapter(IResource.class);
236 return file.getProject();
244 public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
245 IProject project = getContext();
247 configuration.setAttribute(IXDebugConstants.ATTR_PHP_PROJECT, project.getName());
251 public void initializeFrom(ILaunchConfiguration configuration) {
253 String project = configuration.getAttribute(IXDebugConstants.ATTR_PHP_PROJECT, (String)null);
254 if (project != null) {
255 projectSelector.setSelectionText(project);
257 String file = configuration.getAttribute(IXDebugConstants.ATTR_PHP_FILE, (String)null);
259 fileSelector.setSelectionText(file);
262 String interpreterFile=configuration.getAttribute(IXDebugConstants.ATTR_PHP_INTERPRETER, (String) null);
263 if(interpreterFile!=null)
264 fInterpreterText.setText(interpreterFile);
265 boolean selection=configuration.getAttribute(IXDebugConstants.ATTR_PHP_DEFAULT_INTERPRETER, true);
266 fUseDefaultInterpreterButton.setSelection(selection);
267 setInterpreterTextEnabledState();
269 } catch (CoreException e) {
270 setErrorMessage(e.getMessage());
274 public void performApply(ILaunchConfigurationWorkingCopy configuration) {
275 String project = projectSelector.getSelectionText().trim();
276 if (project.length() == 0) {
279 configuration.setAttribute(IXDebugConstants.ATTR_PHP_PROJECT, project);
281 IFile file = fileSelector.getSelection();
282 configuration.setAttribute(IXDebugConstants.ATTR_PHP_FILE, file == null ? "" : file.getProjectRelativePath()
284 configuration.setAttribute(IXDebugConstants.ATTR_PHP_DEFAULT_INTERPRETER, this.fUseDefaultInterpreterButton.getSelection());
285 configuration.setAttribute(IXDebugConstants.ATTR_PHP_INTERPRETER, this.fInterpreterText.getText());
290 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#isValid(org.eclipse.debug.core.ILaunchConfiguration)
292 public boolean isValid(ILaunchConfiguration launchConfig) {
293 setErrorMessage(null);
294 String projectName=projectSelector.getSelectionText().trim();
295 IProject project=ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
296 if (!project.exists()) {
297 setErrorMessage("Project does not exist");
300 String fileString=fileSelector.getSelectionText().trim();
301 if (!"".equals(fileString)) {
302 IFile file=project.getFile(fileSelector.getSelectionText().trim());
303 if (!file.exists()) {
304 setErrorMessage("File does not exist");
308 setErrorMessage("File does not exist");
311 if (!fUseDefaultInterpreterButton.getSelection()) {
312 File exe = new File(fInterpreterText.getText());
313 System.out.println(exe.toString());
315 setErrorMessage("Invalid Interpreter");
322 public Image getImage() {
323 return PHPUiImages.get(PHPUiImages.IMG_CTOOLS_PHP_PAGE);
326 public String getName() {