1 package net.sourceforge.phpdt.internal.debug.ui.launcher;
3 import java.util.ArrayList;
4 import java.util.Iterator;
6 import java.util.HashMap;
9 import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiMessages;
10 import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiPlugin;
11 import net.sourceforge.phpdt.internal.debug.ui.preferences.EditInterpreterDialog;
12 import net.sourceforge.phpdt.internal.launching.PHPInterpreter;
13 import net.sourceforge.phpdt.internal.launching.PHPLaunchConfigurationAttribute;
14 import net.sourceforge.phpdt.internal.launching.PHPRuntime;
15 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
16 import net.sourceforge.phpeclipse.LoadPathEntry;
17 import net.sourceforge.phpeclipse.PHPCore;
18 import net.sourceforge.phpeclipse.resourcesview.PHPProject;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.debug.core.ILaunchConfiguration;
22 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
23 import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
24 import org.eclipse.jface.viewers.ListViewer;
25 import org.eclipse.jface.viewers.TableLayout;
26 import org.eclipse.jface.viewers.ColumnWeightData;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.events.ModifyEvent;
29 import org.eclipse.swt.events.ModifyListener;
30 import org.eclipse.swt.events.SelectionAdapter;
31 import org.eclipse.swt.events.SelectionEvent;
32 import org.eclipse.swt.events.SelectionListener;
33 import org.eclipse.swt.graphics.Image;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.layout.GridLayout;
36 import org.eclipse.swt.widgets.Button;
37 import org.eclipse.swt.widgets.Combo;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Label;
40 import org.eclipse.swt.widgets.TabFolder;
41 import org.eclipse.swt.widgets.TabItem;
42 import org.eclipse.swt.widgets.Table;
43 import org.eclipse.swt.widgets.TableItem;
44 import org.eclipse.swt.widgets.Text;
45 import org.eclipse.swt.widgets.TableColumn;
46 import org.eclipse.ui.internal.dialogs.ListContentProvider;
48 public class PHPEnvironmentTab extends AbstractLaunchConfigurationTab {
49 protected ListViewer loadPathListViewer;
50 protected java.util.List installedInterpretersWorkingCopy;
51 protected Combo interpreterCombo;
52 protected Button loadPathDefaultButton;
53 protected Button fRemoteDebugCheckBox;
54 protected Button fFileMapRemoveButton;
55 protected Button fFileMapAddButton;
56 protected Button fFileMapEditButton;
57 protected Text fRemoteSourcePath;
58 protected Table fRemoteDebugFileMapTable;
59 protected TabFolder tabFolder;
61 private class RemoteDebugTabListener extends SelectionAdapter implements ModifyListener {
64 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
66 public void modifyText(ModifyEvent e) {
67 updateLaunchConfigurationDialog();
71 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
73 public void widgetSelected(SelectionEvent e) {
74 Object source= e.getSource();
75 if (source == fRemoteDebugFileMapTable) {
76 setFileMapButtonsEnableState();
77 } else if (source == fFileMapAddButton) {
78 handleFileMapAddButtonSelected();
79 } else if (source == fFileMapEditButton) {
80 handleFileMapEditButtonSelected();
81 } else if (source == fFileMapRemoveButton) {
82 handleFileMapRemoveButtonSelected();
83 } else if (source == fRemoteDebugCheckBox) {
84 setRemoteTabEnableState();
86 updateLaunchConfigurationDialog();;
93 private RemoteDebugTabListener fListener= new RemoteDebugTabListener();
95 private static final boolean DEFAULT_REMOTE_DEBUG= false;
96 static String [] columnTitles = { PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.remoteDebugTab.FileMapTableTitle.local"),
97 PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.remoteDebugTab.FileMapTableTitle.remote")
99 public PHPEnvironmentTab() {
103 public void createControl(Composite parent) {
104 Composite composite = createPageRoot(parent);
106 tabFolder = new TabFolder(composite, SWT.NONE);
107 GridData gridData = new GridData(GridData.FILL_BOTH);
108 tabFolder.setLayoutData(gridData);
110 addLoadPathTab(tabFolder);
111 addInterpreterTab(tabFolder);
112 addRemoteDebugTab(tabFolder);
115 protected void addRemoteDebugTab(TabFolder tabFolder)
119 TabItem remoteDebugTab = new TabItem(tabFolder, SWT.NONE, 0);
120 remoteDebugTab.setText(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.remoteDebugTab.label"));
122 Composite comp = new Composite(tabFolder, SWT.NONE);
123 comp.setLayout(new GridLayout());
124 remoteDebugTab.setControl(comp);
127 fRemoteDebugCheckBox = new Button(comp, SWT.CHECK);
128 fRemoteDebugCheckBox.setText(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.remoteDebugTab.RemoteCheckBox.label"));
129 fRemoteDebugCheckBox.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
130 fRemoteDebugCheckBox.addSelectionListener(fListener);
132 label = new Label(comp, SWT.NONE);
133 label.setText(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.remoteDebugTab.RemoteSourcePath.label"));
134 fRemoteSourcePath = new Text(comp, SWT.BORDER | SWT.SINGLE);
135 gd = new GridData(GridData.FILL_HORIZONTAL);
136 fRemoteSourcePath.setLayoutData(gd);
137 fRemoteSourcePath.addModifyListener(fListener);
139 createVerticalSpacer(comp,1);
141 Composite fileMapComp = new Composite(comp, SWT.NONE);
142 gd = new GridData(GridData.FILL_BOTH);
143 fileMapComp.setLayoutData(gd);
144 GridLayout parametersLayout = new GridLayout();
145 parametersLayout.numColumns = 2;
146 parametersLayout.marginHeight = 0;
147 parametersLayout.marginWidth = 0;
148 fileMapComp.setLayout(parametersLayout);
151 Label fileMapLabel = new Label(fileMapComp, SWT.NONE);
152 fileMapLabel.setText(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.remoteDebugTab.FileMap.label"));
154 gd.horizontalSpan = 2;
155 fileMapLabel.setLayoutData(gd);
158 fRemoteDebugFileMapTable = new Table(fileMapComp, SWT.BORDER | SWT.MULTI);
159 TableLayout tableLayout = new TableLayout();
160 fRemoteDebugFileMapTable.setLayout(tableLayout);
162 gd = new GridData(GridData.FILL_BOTH);
163 fRemoteDebugFileMapTable.setLayoutData(gd);
164 TableColumn column1 = new TableColumn(this.fRemoteDebugFileMapTable, SWT.NONE);
165 column1.setText(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.remoteDebugTab.FileMap.Table.Title.local")); //$NON-NLS-1$
166 TableColumn column2 = new TableColumn(this.fRemoteDebugFileMapTable, SWT.NONE);
167 column2.setText(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.remoteDebugTab.FileMap.Table.Title.remote")); //$NON-NLS-1$
168 tableLayout.addColumnData(new ColumnWeightData(100));
169 tableLayout.addColumnData(new ColumnWeightData(100));
170 fRemoteDebugFileMapTable.setHeaderVisible(true);
171 fRemoteDebugFileMapTable.setLinesVisible(true);
172 fRemoteDebugFileMapTable.addSelectionListener(fListener);
173 fRemoteDebugFileMapTable.setEnabled(false);
175 Composite envButtonComp = new Composite(fileMapComp, SWT.NONE);
176 GridLayout envButtonLayout = new GridLayout();
177 envButtonLayout.marginHeight = 0;
178 envButtonLayout.marginWidth = 0;
179 envButtonComp.setLayout(envButtonLayout);
180 gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.HORIZONTAL_ALIGN_FILL);
181 envButtonComp.setLayoutData(gd);
184 fFileMapAddButton = createPushButton(envButtonComp ,PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.remoteDebugTab.FileMap.Button.Add.label"), null); //$NON-NLS-1$
185 fFileMapAddButton.addSelectionListener(fListener);
186 fFileMapAddButton.setEnabled(false);
188 fFileMapEditButton = createPushButton(envButtonComp,PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.remoteDebugTab.FileMap.Button.Edit.label"), null); //$NON-NLS-1$
189 fFileMapEditButton.addSelectionListener(fListener);
190 fFileMapEditButton.setEnabled(false);
192 fFileMapRemoveButton = createPushButton(envButtonComp,PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.remoteDebugTab.FileMap.Button.Remove.label"), null); //$NON-NLS-1$
193 fFileMapRemoveButton.addSelectionListener(fListener);
194 fFileMapRemoveButton.setEnabled(false);
199 void handleFileMapAddButtonSelected()
201 TableItem item = new TableItem (fRemoteDebugFileMapTable, SWT.NONE);
202 updateLaunchConfigurationDialog();
205 void handleFileMapRemoveButtonSelected()
207 int idx=fRemoteDebugFileMapTable.getSelectionIndex();
209 fRemoteDebugFileMapTable.remove(idx);
210 updateLaunchConfigurationDialog();
213 void handleFileMapEditButtonSelected()
219 * Set the enabled state of whole tab.
221 private void setRemoteTabEnableState() {
222 boolean state=fRemoteDebugCheckBox.getSelection();
223 fRemoteSourcePath.setEnabled(state);
225 // TODO: (cperkonig) not implemented yet
226 // fRemoteDebugFileMapTable.setEnabled(state);
229 // fFileMapEditButton.setEnabled(false);
230 // fFileMapRemoveButton.setEnabled(false);
231 // fFileMapAddButton.setEnabled(false);
233 // setFileMapButtonsEnableState();
236 updateLaunchConfigurationDialog();
241 * Set the enabled state of the three environment variable-related buttons based on the
242 * selection in the FileMapTable widget.
244 private void setFileMapButtonsEnableState() {
245 // just do nothing for now
247 if(fRemoteDebugCheckBox.getSelection())
249 int selectCount = this.fRemoteDebugFileMapTable.getSelectionIndices().length;
250 if (selectCount < 1) {
251 fFileMapEditButton.setEnabled(false);
252 fFileMapRemoveButton.setEnabled(false);
254 fFileMapRemoveButton.setEnabled(true);
255 if (selectCount == 1) {
256 fFileMapEditButton.setEnabled(true);
258 fFileMapEditButton.setEnabled(false);
261 fFileMapAddButton.setEnabled(true);
267 protected void addLoadPathTab(TabFolder tabFolder) {
268 Composite loadPathComposite = new Composite(tabFolder, SWT.NONE);
269 loadPathComposite.setLayout(new GridLayout());
271 loadPathListViewer = new ListViewer(loadPathComposite, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
272 loadPathListViewer.setContentProvider(new ListContentProvider());
273 loadPathListViewer.setLabelProvider(new LoadPathEntryLabelProvider());
274 loadPathListViewer.getList().setLayoutData(new GridData(GridData.FILL_BOTH));
276 TabItem loadPathTab = new TabItem(tabFolder, SWT.NONE, 0);
277 loadPathTab.setText(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.loadPathTab.label"));
278 loadPathTab.setControl(loadPathComposite);
279 loadPathTab.setData(loadPathListViewer);
281 loadPathDefaultButton = new Button(loadPathComposite, SWT.CHECK);
282 loadPathDefaultButton.setText(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.loadPathDefaultButton.label"));
283 loadPathDefaultButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
284 loadPathDefaultButton.addSelectionListener(getLoadPathDefaultButtonSelectionListener());
286 loadPathDefaultButton.setEnabled(false); //for now, until the load path is customizable on the configuration
289 protected SelectionListener getLoadPathSelectionListener() {
290 return new SelectionAdapter() {
291 public void widgetSelected(SelectionEvent e) {
292 System.out.println("Loadpath list selection occurred: " + e.getSource());
299 protected SelectionListener getLoadPathDefaultButtonSelectionListener() {
300 return new SelectionAdapter() {
301 public void widgetSelected(SelectionEvent e) {
302 setUseLoadPathDefaults(((Button) e.getSource()).getSelection());
307 protected void addInterpreterTab(TabFolder tabFolder) {
308 Composite interpreterComposite = new Composite(tabFolder, SWT.NONE);
309 GridLayout layout = new GridLayout();
310 layout.numColumns = 2;
311 layout.marginHeight = 0;
312 layout.marginWidth = 0;
313 interpreterComposite.setLayout(layout);
314 interpreterComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
316 createVerticalSpacer(interpreterComposite, 2);
318 interpreterCombo = new Combo(interpreterComposite, SWT.READ_ONLY);
319 interpreterCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
320 initializeInterpreterCombo(interpreterCombo);
321 interpreterCombo.addModifyListener(getInterpreterComboModifyListener());
323 Button interpreterAddButton = new Button(interpreterComposite, SWT.PUSH);
324 interpreterAddButton.setText(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.interpreterAddButton.label"));
325 interpreterAddButton.addSelectionListener(new SelectionAdapter() {
326 public void widgetSelected(SelectionEvent evt) {
327 PHPInterpreter newInterpreter = new PHPInterpreter(null, null);
328 EditInterpreterDialog editor = new EditInterpreterDialog(getShell(), PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.editInterpreterDialog.title"));
330 editor.setInterpreterToEdit(newInterpreter);
331 if (EditInterpreterDialog.OK == editor.open()) {
332 PHPRuntime.getDefault().addInstalledInterpreter(newInterpreter);
333 interpreterCombo.add(newInterpreter.getName());
334 interpreterCombo.select(interpreterCombo.indexOf(newInterpreter.getName()));
339 TabItem interpreterTab = new TabItem(tabFolder, SWT.NONE);
340 interpreterTab.setText(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.interpreterTab.label"));
341 interpreterTab.setControl(interpreterComposite);
344 protected ModifyListener getInterpreterComboModifyListener() {
345 return new ModifyListener() {
346 public void modifyText(ModifyEvent evt) {
347 updateLaunchConfigurationDialog();
352 protected void createVerticalSpacer(Composite comp, int colSpan) {
353 Label label = new Label(comp, SWT.NONE);
354 GridData gd = new GridData();
355 gd.horizontalSpan = colSpan;
356 label.setLayoutData(gd);
359 public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
362 public void initializeFrom(ILaunchConfiguration configuration) {
363 initializeLoadPath(configuration);
364 initializeInterpreterSelection(configuration);
365 initializeRemoteDebug(configuration);
368 protected void initializeRemoteDebug(ILaunchConfiguration configuration)
374 fRemoteDebugCheckBox.setSelection(
375 configuration.getAttribute(PHPLaunchConfigurationAttribute.REMOTE_DEBUG,DEFAULT_REMOTE_DEBUG));
376 } catch(CoreException ce) {
377 fRemoteDebugCheckBox.setSelection(DEFAULT_REMOTE_DEBUG);
379 setRemoteTabEnableState();
381 fRemoteSourcePath.setText(
382 configuration.getAttribute(PHPLaunchConfigurationAttribute.REMOTE_PATH,""));
383 } catch(CoreException ce) {
384 fRemoteSourcePath.setText("");
387 updateFileMapFromConfig(configuration);
391 private void updateFileMapFromConfig(ILaunchConfiguration config) {
394 if (config != null) {
395 envVars = config.getAttribute(PHPLaunchConfigurationAttribute.FILE_MAP, (Map)null);
397 updateFileMapTable(envVars, this.fRemoteDebugFileMapTable);
398 setFileMapButtonsEnableState();
399 } catch (CoreException ce) {
404 private void updateFileMapTable(Map map, Table tableWidget) {
405 tableWidget.removeAll();
409 Iterator iterator = map.keySet().iterator();
410 while (iterator.hasNext()) {
411 String key = (String) iterator.next();
412 String value = (String) map.get(key);
413 TableItem tableItem = new TableItem(tableWidget, SWT.NONE);
414 tableItem.setText(new String[] {key, value});
418 protected void initializeLoadPath(ILaunchConfiguration configuration) {
419 boolean useDefaultLoadPath = true;
421 useDefaultLoadPath = configuration.getAttribute(PHPLaunchConfigurationAttribute.USE_DEFAULT_LOAD_PATH, true);
422 setUseLoadPathDefaults(useDefaultLoadPath);
423 if (useDefaultLoadPath) {
424 String projectName = configuration.getAttribute(PHPLaunchConfigurationAttribute.PROJECT_NAME, "");
425 if (projectName != "") {
426 PHPProject project = PHPCore.getPHPProject(projectName);
427 if (project != null) {
428 List loadPathEntries = project.getLoadPathEntries();
429 loadPathListViewer.setInput(loadPathEntries);
433 } catch (CoreException e) {
438 protected void setUseLoadPathDefaults(boolean useDefaults) {
439 loadPathListViewer.getList().setEnabled(!useDefaults);
440 loadPathDefaultButton.setSelection(useDefaults);
443 protected void initializeInterpreterSelection(ILaunchConfiguration configuration) {
444 String interpreterName = null;
446 interpreterName = configuration.getAttribute(PHPLaunchConfigurationAttribute.SELECTED_INTERPRETER, "");
447 } catch (CoreException e) {
450 if (interpreterName != null && !interpreterName.equals(""))
451 interpreterCombo.select(interpreterCombo.indexOf(interpreterName));
454 protected void initializeInterpreterCombo(Combo interpreterCombo) {
455 installedInterpretersWorkingCopy = new ArrayList();
456 installedInterpretersWorkingCopy.addAll(PHPRuntime.getDefault().getInstalledInterpreters());
458 String[] interpreterNames = new String[installedInterpretersWorkingCopy.size()];
459 for (int interpreterIndex = 0; interpreterIndex < installedInterpretersWorkingCopy.size(); interpreterIndex++) {
460 PHPInterpreter interpreter = (PHPInterpreter) installedInterpretersWorkingCopy.get(interpreterIndex);
461 interpreterNames[interpreterIndex] = interpreter.getName();
463 interpreterCombo.setItems(interpreterNames);
465 PHPInterpreter selectedInterpreter = PHPRuntime.getDefault().getSelectedInterpreter();
466 if (selectedInterpreter != null)
467 interpreterCombo.select(interpreterCombo.indexOf(selectedInterpreter.getName()));
470 public void performApply(ILaunchConfigurationWorkingCopy configuration) {
471 int selectionIndex = interpreterCombo.getSelectionIndex();
472 if (selectionIndex >= 0)
473 configuration.setAttribute(PHPLaunchConfigurationAttribute.SELECTED_INTERPRETER, interpreterCombo.getItem(selectionIndex));
475 configuration.setAttribute(PHPLaunchConfigurationAttribute.USE_DEFAULT_LOAD_PATH, loadPathDefaultButton.getSelection());
477 if (!loadPathDefaultButton.getSelection()) {
478 List loadPathEntries = (List) loadPathListViewer.getInput();
479 List loadPathStrings = new ArrayList();
480 for (Iterator iterator = loadPathEntries.iterator(); iterator.hasNext();) {
481 LoadPathEntry entry = (LoadPathEntry) iterator.next();
482 loadPathStrings.add(entry.getPath().toString());
484 configuration.setAttribute(PHPLaunchConfigurationAttribute.CUSTOM_LOAD_PATH, loadPathStrings);
487 configuration.setAttribute(PHPLaunchConfigurationAttribute.REMOTE_DEBUG, fRemoteDebugCheckBox.getSelection());
488 configuration.setAttribute(PHPLaunchConfigurationAttribute.FILE_MAP, getMapFromFileMapTable());
489 configuration.setAttribute(PHPLaunchConfigurationAttribute.REMOTE_PATH, fRemoteSourcePath.getText());
492 protected Composite createPageRoot(Composite parent) {
493 Composite composite = new Composite(parent, SWT.NULL);
494 GridLayout layout = new GridLayout();
495 layout.numColumns = 2;
496 composite.setLayout(layout);
497 createVerticalSpacer(composite, 2);
498 setControl(composite);
503 private Map getMapFromFileMapTable() {
504 TableItem[] items = fRemoteDebugFileMapTable.getItems();
505 if (items.length == 0) {
508 Map map = new HashMap(items.length);
509 for (int i = 0; i < items.length; i++) {
510 TableItem item = items[i];
511 String key = item.getText(0);
512 String value = item.getText(1);
518 public String getName() {
519 return PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.name");
522 public boolean isValid(ILaunchConfiguration launchConfig) {
524 String selectedInterpreter = launchConfig.getAttribute(PHPLaunchConfigurationAttribute.SELECTED_INTERPRETER, "");
525 if (selectedInterpreter.length() == 0) {
526 setErrorMessage(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.interpreter_not_selected_error_message"));
529 } catch (CoreException e) {
533 setErrorMessage(null);
537 protected void log(Throwable t) {
538 PHPDebugUiPlugin.log(t);
541 public Image getImage() {
542 return PHPUiImages.get(PHPUiImages.IMG_CTOOLS_PHP);