1) Although dbg will be dropped from being supported or bundled with PHPeclipse,...
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.ui / src / net / sourceforge / phpdt / internal / debug / ui / launcher / PHPEnvironmentTab.java
1 package net.sourceforge.phpdt.internal.debug.ui.launcher;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.Iterator;
7 import java.util.Map;
8
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.EditPathMapDialog;
12 import net.sourceforge.phpdt.internal.debug.ui.preferences.PHPInterpreterPreferencePage;
13 import net.sourceforge.phpdt.internal.launching.PHPInterpreter;
14 import net.sourceforge.phpdt.internal.launching.PHPLaunchConfigurationAttribute;
15 import net.sourceforge.phpdt.internal.launching.PHPRuntime;
16 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
17
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.resources.ResourcesPlugin;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IPath;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.debug.core.ILaunchConfiguration;
24 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
25 import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
26 import org.eclipse.jface.viewers.ColumnWeightData;
27 import org.eclipse.jface.viewers.ListViewer;
28 import org.eclipse.jface.viewers.TableLayout;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.events.ModifyEvent;
31 import org.eclipse.swt.events.ModifyListener;
32 import org.eclipse.swt.events.MouseAdapter;
33 import org.eclipse.swt.events.MouseEvent;
34 import org.eclipse.swt.events.SelectionAdapter;
35 import org.eclipse.swt.events.SelectionEvent;
36 //import org.eclipse.swt.events.SelectionListener;
37 import org.eclipse.swt.graphics.Color;
38 import org.eclipse.swt.graphics.Image;
39 import org.eclipse.swt.layout.GridData;
40 import org.eclipse.swt.layout.GridLayout;
41 import org.eclipse.swt.widgets.Button;
42 import org.eclipse.swt.widgets.Combo;
43 import org.eclipse.swt.widgets.Composite;
44 import org.eclipse.swt.widgets.Label;
45 import org.eclipse.swt.widgets.TabFolder;
46 import org.eclipse.swt.widgets.TabItem;
47 import org.eclipse.swt.widgets.Table;
48 import org.eclipse.swt.widgets.TableColumn;
49 import org.eclipse.swt.widgets.TableItem;
50 import org.eclipse.swt.widgets.Text;
51
52 public class PHPEnvironmentTab extends AbstractLaunchConfigurationTab {
53         protected ListViewer loadPathListViewer;
54
55         protected java.util.List installedInterpretersWorkingCopy;
56
57         protected Combo     interpreterCombo;
58
59         // protected Button loadPathDefaultButton;
60         protected Button    fRemoteDebugCheckBox;
61         protected Button    fRelaunchOnScriptTerminationCheckBox;
62         protected Button    fRemoteDebugTranslate;
63         protected Button    fOpenDBGSessionInBrowserCheckBox;
64         protected Button    fOpenDBGSessionInExternalBrowserCheckBox;
65         protected Button    fPathMapRemoveButton;
66         protected Button    fPathMapAddButton;
67         protected Button    fPathMapEditButton;
68         protected Text      fRemoteSourcePath;
69         protected Table     fRemoteDebugPathMapTable;
70         protected TabFolder tabFolder;
71
72         private Text targetFile;
73
74         private String originalFileName = "";
75
76         private class RemoteDebugTabListener extends SelectionAdapter implements
77                         ModifyListener {
78
79                 /*
80                  * (non-Javadoc)
81                  *
82                  * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
83                  */
84                 public void modifyText(ModifyEvent e) {
85                         updateLaunchConfigurationDialog();
86                         makeupTargetFile();
87                 }
88
89                 /*
90                  * (non-Javadoc)
91                  *
92                  * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
93                  */
94                 public void widgetSelected(SelectionEvent e) {
95                         Object source = e.getSource();
96                         if (source == fRemoteDebugPathMapTable) {
97                                 setPathMapButtonsEnableState();
98                         } else if (source == fPathMapAddButton) {
99                                 handlePathMapAddButtonSelected();
100                         } else if (source == fPathMapEditButton) {
101                                 handlePathMapEditButtonSelected();
102                         } else if (source == fPathMapRemoveButton) {
103                                 handlePathMapRemoveButtonSelected();
104                         } else if (source == fRemoteDebugCheckBox) {
105                                 setRemoteTabEnableState();
106                         } else if (source == fRelaunchOnScriptTerminationCheckBox) {
107                                 setRemoteTabEnableState();
108                         } else if (source == fRemoteDebugTranslate) {
109                                 setRemoteTabEnableState();
110                         } else if (source == fOpenDBGSessionInBrowserCheckBox) {
111                                 setRemoteTabEnableState();
112                         } else {
113                                 updateLaunchConfigurationDialog();
114                         }
115                         makeupTargetFile();
116                 }
117
118         }
119
120         private static final String                 EMPTY_STRING                                = ""; //$NON-NLS-1$
121         private              RemoteDebugTabListener fListener                                   = new RemoteDebugTabListener();
122         private static final boolean                DEFAULT_REMOTE_DEBUG                        = false;
123         private static final boolean                DEFAULT_REMOTE_DEBUG_TRANSLATE              = false;
124         private static final boolean                DEFAULT_OPEN_DBGSESSION_IN_BROWSER          = true;
125         private static final boolean                DEFAULT_OPEN_DBGSESSION_IN_EXTERNAL_BROWSER = false;
126         private static final boolean                DEFAULT_RELAUNCH_ON_SCRIPT_TERMINATION      = true;
127
128         static String[] columnTitles = {
129                         PHPDebugUiMessages
130                                         .getString("LaunchConfigurationTab.PHPEnvironment.remoteDebugTab.PathMapTableTitle.local"),
131                         PHPDebugUiMessages
132                                         .getString("LaunchConfigurationTab.PHPEnvironment.remoteDebugTab.PathMapTableTitle.remote") };
133
134         public PHPEnvironmentTab() {
135                 super();
136         }
137
138         public void createControl(Composite parent) {
139                 Composite composite = createPageRoot(parent);
140
141                 tabFolder = new TabFolder(composite, SWT.NONE);
142                 GridData gridData = new GridData(GridData.FILL_BOTH);
143                 tabFolder.setLayoutData(gridData);
144
145                 // addLoadPathTab(tabFolder);
146                 addRemoteDebugTab(tabFolder);
147                 addInterpreterTab(tabFolder);
148         }
149
150         protected void addRemoteDebugTab(TabFolder tabFolder) {
151                 Label label;
152
153                 TabItem remoteDebugTab = new TabItem(tabFolder, SWT.NONE, 0);
154                 remoteDebugTab
155                                 .setText(PHPDebugUiMessages
156                                                 .getString("LaunchConfigurationTab.PHPEnvironment.remoteDebugTab.label"));
157
158                 Composite comp = new Composite(tabFolder, SWT.NONE);
159                 comp.setLayout(new GridLayout());
160                 remoteDebugTab.setControl(comp);
161                 GridData gd;
162
163                 fRemoteDebugCheckBox = new Button(comp, SWT.CHECK);
164                 fRemoteDebugCheckBox
165                                 .setText(PHPDebugUiMessages
166                                                 .getString("LaunchConfigurationTab.PHPEnvironment.remoteDebugTab.RemoteCheckBox.label"));
167                 fRemoteDebugCheckBox.setLayoutData(new GridData(
168                                 GridData.HORIZONTAL_ALIGN_BEGINNING));
169                 fRemoteDebugCheckBox.addSelectionListener(fListener);
170
171                 fRemoteDebugTranslate = new Button(comp, SWT.CHECK);
172                 fRemoteDebugTranslate
173                                 .setText(PHPDebugUiMessages
174                                                 .getString("LaunchConfigurationTab.PHPEnvironment.remoteDebugTab.RemoteTranslate.label"));
175                 fRemoteDebugTranslate.setLayoutData(new GridData(
176                                 GridData.HORIZONTAL_ALIGN_BEGINNING));
177                 fRemoteDebugTranslate.addSelectionListener(fListener);
178
179                 fOpenDBGSessionInBrowserCheckBox = new Button(comp, SWT.CHECK);
180                 fOpenDBGSessionInBrowserCheckBox
181                                 .setText(PHPDebugUiMessages
182                                                 .getString("LaunchConfigurationTab.PHPEnvironment.remoteDebugTab.OpenDBGSessionInBrowserCheckBox.label"));
183                 fOpenDBGSessionInBrowserCheckBox.setLayoutData(new GridData(
184                                 GridData.HORIZONTAL_ALIGN_BEGINNING));
185                 fOpenDBGSessionInBrowserCheckBox.addSelectionListener(fListener);
186
187                 // addendum
188                 fOpenDBGSessionInExternalBrowserCheckBox = new Button(comp, SWT.CHECK);
189                 fOpenDBGSessionInExternalBrowserCheckBox
190                                 .setText(PHPDebugUiMessages
191                                                 .getString("LaunchConfigurationTab.PHPEnvironment.remoteDebugTab.OpenDBGSessionInExternalBrowserCheckBox.label"));
192                 fOpenDBGSessionInExternalBrowserCheckBox.setLayoutData(new GridData(
193                                 SWT.BEGINNING));
194                 ((GridData) fOpenDBGSessionInExternalBrowserCheckBox.getLayoutData()).horizontalIndent = 16;
195                 fOpenDBGSessionInExternalBrowserCheckBox
196                                 .addSelectionListener(fListener);
197                 // addendum
198
199                 fRelaunchOnScriptTerminationCheckBox = new Button (comp, SWT.CHECK);
200                 fRelaunchOnScriptTerminationCheckBox
201                                 .setText (PHPDebugUiMessages
202                                                 .getString ("LaunchConfigurationTab.PHPEnvironment.remoteDebugTab.RelaunchOnScriptTermination.label"));
203                 fRelaunchOnScriptTerminationCheckBox.setLayoutData (new GridData (GridData.HORIZONTAL_ALIGN_BEGINNING));
204                 fRelaunchOnScriptTerminationCheckBox.addSelectionListener (fListener);
205
206                 label = new Label(comp, SWT.NONE);
207                 label
208                                 .setText(PHPDebugUiMessages
209                                                 .getString("LaunchConfigurationTab.PHPEnvironment.remoteDebugTab.RemoteSourcePath.label"));
210                 fRemoteSourcePath = new Text(comp, SWT.BORDER | SWT.SINGLE);
211                 gd = new GridData(GridData.FILL_HORIZONTAL);
212                 fRemoteSourcePath.setLayoutData(gd);
213                 fRemoteSourcePath.addModifyListener(fListener);
214
215                 // addendum - make an effect of RemoteSourcePath clear
216                 Composite targetComp = new Composite(comp, SWT.NONE);
217                 targetComp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
218                 GridLayout targetLayout = new GridLayout(4, false);
219                 targetLayout.marginHeight = 0;
220                 targetLayout.marginWidth = 3;
221                 targetComp.setLayout(targetLayout);
222                 Color targetColor = new Color(null, 160, 160, 160);
223                 Label label_lp = new Label(targetComp, SWT.NONE);
224                 label_lp.setText("(");
225                 label_lp.setForeground(targetColor);
226                 label_lp.setLayoutData(new GridData(GridData.BEGINNING));
227                 Label targetLabel = new Label(targetComp, SWT.NONE);
228                 targetLabel
229                                 .setText(PHPDebugUiMessages
230                                                 .getString("LaunchConfigurationTab.PHPEnvironment.remoteDebugTab.TargetFile.label"));
231                 targetLabel.setForeground(targetColor);
232                 targetLabel.setLayoutData(new GridData(GridData.BEGINNING));
233                 targetFile = new Text(targetComp, SWT.SINGLE);
234                 targetFile.setForeground(targetColor);
235                 targetFile.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
236                 targetFile.setEditable(false);
237                 Label label_rp = new Label(targetComp, SWT.NONE);
238                 label_rp.setText(")");
239                 label_rp.setForeground(targetColor);
240                 label_rp.setLayoutData(new GridData(GridData.END));
241                 // addendum
242
243                 createVerticalSpacer(comp, 1);
244
245                 Composite pathMapComp = new Composite(comp, SWT.NONE);
246                 gd = new GridData(GridData.FILL_BOTH);
247                 pathMapComp.setLayoutData(gd);
248                 GridLayout parametersLayout = new GridLayout();
249                 parametersLayout.numColumns = 2;
250                 parametersLayout.marginHeight = 0;
251                 parametersLayout.marginWidth = 0;
252                 pathMapComp.setLayout(parametersLayout);
253
254                 Label pathMapLabel = new Label(pathMapComp, SWT.NONE);
255                 pathMapLabel
256                                 .setText(PHPDebugUiMessages
257                                                 .getString("LaunchConfigurationTab.PHPEnvironment.remoteDebugTab.PathMap.label"));
258                 gd = new GridData();
259                 gd.horizontalSpan = 2;
260                 pathMapLabel.setLayoutData(gd);
261
262                 fRemoteDebugPathMapTable = new Table(pathMapComp, SWT.BORDER
263                                 | SWT.MULTI);
264                 TableLayout tableLayout = new TableLayout();
265                 fRemoteDebugPathMapTable.setLayout(tableLayout);
266
267                 gd = new GridData(GridData.FILL_BOTH);
268                 fRemoteDebugPathMapTable.setLayoutData(gd);
269                 TableColumn column1 = new TableColumn(this.fRemoteDebugPathMapTable,
270                                 SWT.NONE);
271                 column1
272                                 .setText(PHPDebugUiMessages
273                                                 .getString("LaunchConfigurationTab.PHPEnvironment.remoteDebugTab.PathMap.Table.Title.local")); //$NON-NLS-1$
274                 TableColumn column2 = new TableColumn(this.fRemoteDebugPathMapTable,
275                                 SWT.NONE);
276                 column2
277                                 .setText(PHPDebugUiMessages
278                                                 .getString("LaunchConfigurationTab.PHPEnvironment.remoteDebugTab.PathMap.Table.Title.remote")); //$NON-NLS-1$
279                 tableLayout.addColumnData(new ColumnWeightData(100));
280                 tableLayout.addColumnData(new ColumnWeightData(100));
281                 fRemoteDebugPathMapTable.setHeaderVisible(true);
282                 fRemoteDebugPathMapTable.setLinesVisible(true);
283                 fRemoteDebugPathMapTable.addSelectionListener(fListener);
284                 fRemoteDebugPathMapTable.addMouseListener(new MouseAdapter() {
285                         public void mouseDoubleClick(MouseEvent e) {
286                                 setPathMapButtonsEnableState();
287                                 if (fPathMapEditButton.isEnabled()) {
288                                         handlePathMapEditButtonSelected();
289                                 }
290                         }
291                 });
292                 // fRemoteDebugPathMapTable.setEnabled(false);
293
294                 Composite envButtonComp = new Composite(pathMapComp, SWT.NONE);
295                 GridLayout envButtonLayout = new GridLayout();
296                 envButtonLayout.marginHeight = 0;
297                 envButtonLayout.marginWidth = 0;
298                 envButtonComp.setLayout(envButtonLayout);
299                 gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING
300                                 | GridData.HORIZONTAL_ALIGN_FILL);
301                 envButtonComp.setLayoutData(gd);
302
303                 fPathMapAddButton = createPushButton(
304                                 envButtonComp,
305                                 PHPDebugUiMessages
306                                                 .getString("LaunchConfigurationTab.PHPEnvironment.remoteDebugTab.PathMap.Button.Add.label"), null); //$NON-NLS-1$
307                 fPathMapAddButton.addSelectionListener(fListener);
308                 // fPathMapAddButton.setEnabled(false);
309
310                 fPathMapEditButton = createPushButton(
311                                 envButtonComp,
312                                 PHPDebugUiMessages
313                                                 .getString("LaunchConfigurationTab.PHPEnvironment.remoteDebugTab.PathMap.Button.Edit.label"), null); //$NON-NLS-1$
314                 fPathMapEditButton.addSelectionListener(fListener);
315                 // fPathMapEditButton.setEnabled(false);
316
317                 fPathMapRemoveButton = createPushButton(
318                                 envButtonComp,
319                                 PHPDebugUiMessages
320                                                 .getString("LaunchConfigurationTab.PHPEnvironment.remoteDebugTab.PathMap.Button.Remove.label"), null); //$NON-NLS-1$
321                 fPathMapRemoveButton.addSelectionListener(fListener);
322                 // fPathMapRemoveButton.setEnabled(false);
323
324         }
325
326         void handlePathMapAddButtonSelected() {
327                 EditPathMapDialog dialog = new EditPathMapDialog(getShell(),
328                                 "Edit File Map", new String[] { EMPTY_STRING, EMPTY_STRING });
329                 openNewPathMapDialog(dialog, null);
330                 // dialog.create();
331                 // if (dialog.open()==EditPathMapDialog.OK)
332                 // {
333                 // TableItem item = new TableItem (fRemoteDebugPathMapTable, SWT.NONE);
334                 // item.setText(0,dialog.getLocalPath());
335                 // item.setText(1,dialog.getRemotePath());
336                 // updateLaunchConfigurationDialog();
337                 // }
338                 // updateLaunchConfigurationDialog();
339                 setPathMapButtonsEnableState();
340         }
341
342         void handlePathMapRemoveButtonSelected() {
343                 int[] selectedIndices = this.fRemoteDebugPathMapTable
344                                 .getSelectionIndices();
345                 this.fRemoteDebugPathMapTable.remove(selectedIndices);
346                 setPathMapButtonsEnableState();
347                 updateLaunchConfigurationDialog();
348         }
349
350         void handlePathMapEditButtonSelected() {
351                 TableItem selectedItem = this.fRemoteDebugPathMapTable.getSelection()[0];
352                 String local = selectedItem.getText(0);
353                 String remote = selectedItem.getText(1);
354                 EditPathMapDialog dialog = new EditPathMapDialog(getShell(),
355                                 "Edit File Map", new String[] { local, remote });
356                 openNewPathMapDialog(dialog, selectedItem);
357         }
358
359         /**
360          * Set the enabled state of whole tab.
361          */
362         private void setRemoteTabEnableState() {
363                 boolean state = fRemoteDebugCheckBox.getSelection();
364
365                 fRemoteSourcePath.setEnabled (state);
366                 fRemoteDebugTranslate.setEnabled (state);
367                 fRemoteDebugPathMapTable.setEnabled (state);
368
369                 if (!state) {
370                         fPathMapEditButton.setEnabled (false);
371                         fPathMapRemoveButton.setEnabled (false);
372                         fPathMapAddButton.setEnabled (false);
373                         fOpenDBGSessionInBrowserCheckBox.setEnabled (false);
374                         fOpenDBGSessionInExternalBrowserCheckBox.setEnabled (false);
375                 } else {
376                         setPathMapButtonsEnableState ();
377                 }
378
379                 updateLaunchConfigurationDialog ();
380         }
381
382         /**
383          * Set the enabled state of the three environment variable-related buttons
384          * based on the selection in the PathMapTable widget.
385          */
386         private void setPathMapButtonsEnableState() {
387                 // just do nothing for now
388                 //
389                 if (fRemoteDebugCheckBox.getSelection ()) {
390                         fOpenDBGSessionInBrowserCheckBox.setEnabled (true);
391                         fOpenDBGSessionInExternalBrowserCheckBox
392                                         .setEnabled (fOpenDBGSessionInBrowserCheckBox.getSelection ());
393                         fRemoteDebugTranslate.setEnabled (true);
394
395                         int selectCount = this.fRemoteDebugPathMapTable
396                                         .getSelectionIndices ().length;
397
398                         if (selectCount < 1) {
399                                 fPathMapEditButton.setEnabled (false);
400                                 fPathMapRemoveButton.setEnabled (false);
401                         } else {
402                                 fPathMapRemoveButton.setEnabled (true);
403
404                                 if (selectCount == 1) {
405                                         fPathMapEditButton.setEnabled (true);
406                                 } else {
407                                         fPathMapEditButton.setEnabled (false);
408                                 }
409                         }
410
411                         fPathMapAddButton.setEnabled (true);
412                 }
413         }
414
415         /**
416          * Show the specified dialog and update the pathMapTable table based on its
417          * results.
418          *
419          * @param updateItem
420          *            the item to update, or <code>null</code> if adding a new
421          *            item
422          */
423         private void openNewPathMapDialog(EditPathMapDialog dialog,
424                         TableItem updateItem) {
425                 if (dialog.open() != EditPathMapDialog.OK) {
426                         return;
427                 }
428                 String[] pathPair = dialog.getPathPair();
429                 TableItem tableItem = updateItem;
430                 if (tableItem == null) {
431                         tableItem = getTableItemForName(pathPair[0]);
432                         if (tableItem == null) {
433                                 tableItem = new TableItem(this.fRemoteDebugPathMapTable,
434                                                 SWT.NONE);
435                         }
436                 }
437                 tableItem.setText(pathPair);
438                 this.fRemoteDebugPathMapTable
439                                 .setSelection(new TableItem[] { tableItem });
440                 updateLaunchConfigurationDialog();
441         }
442
443         /**
444          * Helper method that indicates whether the specified parameter name is
445          * already present in the parameters table.
446          */
447         private TableItem getTableItemForName(String candidateName) {
448                 TableItem[] items = this.fRemoteDebugPathMapTable.getItems();
449                 for (int i = 0; i < items.length; i++) {
450                         String name = items[i].getText(0);
451                         if (name.equals(candidateName)) {
452                                 return items[i];
453                         }
454                 }
455                 return null;
456         }
457
458         // protected void addLoadPathTab(TabFolder tabFolder) {
459         // Composite loadPathComposite = new Composite(tabFolder, SWT.NONE);
460         // loadPathComposite.setLayout(new GridLayout());
461         //
462         // loadPathListViewer = new ListViewer(loadPathComposite, SWT.BORDER |
463         // SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
464         // loadPathListViewer.setContentProvider(new ListContentProvider());
465         // loadPathListViewer.setLabelProvider(new LoadPathEntryLabelProvider());
466         // loadPathListViewer.getList().setLayoutData(new
467         // GridData(GridData.FILL_BOTH));
468         //
469         // TabItem loadPathTab = new TabItem(tabFolder, SWT.NONE, 0);
470         // loadPathTab.setText(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.loadPathTab.label"));
471         // loadPathTab.setControl(loadPathComposite);
472         // loadPathTab.setData(loadPathListViewer);
473
474         // loadPathDefaultButton = new Button(loadPathComposite, SWT.CHECK);
475         // loadPathDefaultButton.setText(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.loadPathDefaultButton.label"));
476         // loadPathDefaultButton.setLayoutData(new
477         // GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
478         // loadPathDefaultButton.addSelectionListener(getLoadPathDefaultButtonSelectionListener());
479         //
480         // loadPathDefaultButton.setEnabled(false); //for now, until the load path
481         // is customizable on the configuration
482         // }
483
484 //      protected SelectionListener getLoadPathSelectionListener() {
485 //              return new SelectionAdapter() {
486 //                      public void widgetSelected(SelectionEvent e) {
487 //                              System.out.println("Loadpath list selection occurred: "
488 //                                              + e.getSource());
489 //                      }
490 //              };
491 //      }
492
493 //      protected SelectionListener getLoadPathDefaultButtonSelectionListener() {
494 //              return new SelectionAdapter() {
495 //                      public void widgetSelected(SelectionEvent e) {
496 //                              setUseLoadPathDefaults(((Button) e.getSource()).getSelection());
497 //                      }
498 //              };
499 //      }
500
501         protected void addInterpreterTab(TabFolder tabFolder) {
502                 Composite interpreterComposite = new Composite(tabFolder, SWT.NONE);
503                 GridLayout layout = new GridLayout();
504                 layout.numColumns = 2;
505                 // layout.marginHeight = 0;
506                 // layout.marginWidth = 0;
507                 interpreterComposite.setLayout(layout);
508                 interpreterComposite.setLayoutData(new GridData(
509                                 GridData.FILL_HORIZONTAL));
510
511                 createVerticalSpacer(interpreterComposite, 2);
512
513                 interpreterCombo = new Combo(interpreterComposite, SWT.READ_ONLY);
514                 interpreterCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
515                 initializeInterpreterCombo(interpreterCombo);
516                 interpreterCombo.addModifyListener(getInterpreterComboModifyListener());
517
518                 Button interpreterAddButton = new Button(interpreterComposite, SWT.PUSH);
519                 interpreterAddButton
520                                 .setText(PHPDebugUiMessages
521                                                 .getString("LaunchConfigurationTab.PHPEnvironment.interpreterAddButton.label"));
522                 interpreterAddButton.addSelectionListener(new SelectionAdapter() {
523                         public void widgetSelected(SelectionEvent evt) {
524                                 PHPInterpreter newInterpreter = new PHPInterpreter(null);
525                                 File phpRuntime = PHPInterpreterPreferencePage.getFile(
526                                                 getShell(), null);
527                                 if (phpRuntime != null) {
528                                         newInterpreter.setInstallLocation(phpRuntime);
529                                         PHPRuntime.getDefault().addInstalledInterpreter(
530                                                         newInterpreter);
531                                         interpreterCombo.add(newInterpreter.getInstallLocation()
532                                                         .toString());
533                                         interpreterCombo.select(interpreterCombo
534                                                         .indexOf(newInterpreter.getInstallLocation()
535                                                                         .toString()));
536                                 }
537                         }
538                 });
539
540                 TabItem interpreterTab = new TabItem(tabFolder, SWT.NONE);
541                 interpreterTab
542                                 .setText(PHPDebugUiMessages
543                                                 .getString("LaunchConfigurationTab.PHPEnvironment.interpreterTab.label"));
544                 interpreterTab.setControl(interpreterComposite);
545         }
546
547         protected ModifyListener getInterpreterComboModifyListener() {
548                 return new ModifyListener() {
549                         public void modifyText(ModifyEvent evt) {
550                                 updateLaunchConfigurationDialog();
551                         }
552                 };
553         }
554
555         protected void createVerticalSpacer(Composite comp, int colSpan) {
556                 Label label = new Label(comp, SWT.NONE);
557                 GridData gd = new GridData();
558                 gd.horizontalSpan = colSpan;
559                 label.setLayoutData(gd);
560         }
561
562         public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
563                 PHPInterpreter selectedInterpreter = PHPRuntime.getDefault()
564                                 .getSelectedInterpreter();
565                 if (selectedInterpreter != null) {
566                         String interpreterLocation = selectedInterpreter
567                                         .getInstallLocation().toString();
568                         configuration.setAttribute(
569                                         PHPLaunchConfigurationAttribute.SELECTED_INTERPRETER,
570                                         interpreterLocation);
571                 }
572                 try {
573                         String projectName = configuration.getAttribute(
574                                         PHPLaunchConfigurationAttribute.PROJECT_NAME, "");
575                         if (projectName != "") {
576                                 IProject project = ResourcesPlugin.getWorkspace().getRoot()
577                                                 .getProject(projectName);
578                                 if (project != null) {
579                                         IPath remotePath = project.getFullPath();
580                                         String fileName = configuration.getAttribute(
581                                                         PHPLaunchConfigurationAttribute.FILE_NAME, "");
582                                         if (fileName != "") {
583                                                 Path filePath = new Path(fileName);
584                                                 remotePath = remotePath.append(filePath
585                                                                 .removeLastSegments(1));
586                                         }
587                                         configuration.setAttribute(
588                                                         PHPLaunchConfigurationAttribute.REMOTE_PATH,
589                                                         remotePath.toOSString());
590                                 }
591                         }
592                 } catch (CoreException e) {
593                         log(e);
594                 }
595         }
596
597         public void initializeFrom(ILaunchConfiguration configuration) {
598                 // initializeLoadPath(configuration);
599                 initializeInterpreterSelection(configuration);
600                 initializeRemoteDebug(configuration);
601         }
602
603         protected void initializeRemoteDebug(ILaunchConfiguration configuration) {
604                 try {
605                         fRemoteDebugCheckBox.setSelection(configuration.getAttribute(
606                                         PHPLaunchConfigurationAttribute.REMOTE_DEBUG,
607                                         DEFAULT_REMOTE_DEBUG));
608                 } catch (CoreException ce) {
609                         fRemoteDebugCheckBox.setSelection(DEFAULT_REMOTE_DEBUG);
610                 }
611
612                 tabFolder.setSelection(fRemoteDebugCheckBox.getSelection() ? 0 : 1);
613
614                 try {
615                         fRemoteDebugTranslate.setSelection(configuration.getAttribute(
616                                         PHPLaunchConfigurationAttribute.REMOTE_DEBUG_TRANSLATE,
617                                         DEFAULT_REMOTE_DEBUG_TRANSLATE));
618                 } catch (CoreException ce) {
619                         fRemoteDebugTranslate.setSelection(DEFAULT_REMOTE_DEBUG_TRANSLATE);
620                 }
621
622                 try {
623                         fOpenDBGSessionInBrowserCheckBox
624                                         .setSelection(configuration
625                                                         .getAttribute(
626                                                                         PHPLaunchConfigurationAttribute.OPEN_DBGSESSION_IN_BROWSER,
627                                                                         DEFAULT_OPEN_DBGSESSION_IN_BROWSER));
628                 } catch (CoreException ce) {
629                         fOpenDBGSessionInBrowserCheckBox
630                                         .setSelection(DEFAULT_OPEN_DBGSESSION_IN_BROWSER);
631                 }
632
633                 try {
634                         fOpenDBGSessionInExternalBrowserCheckBox
635                                         .setSelection(configuration
636                                                         .getAttribute(
637                                                                         PHPLaunchConfigurationAttribute.OPEN_DBGSESSION_IN_EXTERNAL_BROWSER,
638                                                                         DEFAULT_OPEN_DBGSESSION_IN_EXTERNAL_BROWSER));
639                 } catch (CoreException ce) {
640                         fOpenDBGSessionInExternalBrowserCheckBox
641                                         .setSelection (DEFAULT_OPEN_DBGSESSION_IN_EXTERNAL_BROWSER);
642                 }
643
644                 try {
645                         fRelaunchOnScriptTerminationCheckBox
646                                         .setSelection (configuration
647                                                         .getAttribute (
648                                                                         PHPLaunchConfigurationAttribute.RELAUNCH_ON_SCRIPT_TERMINATION,
649                                                                         DEFAULT_RELAUNCH_ON_SCRIPT_TERMINATION));
650                 } catch (CoreException ce) {
651                         fRelaunchOnScriptTerminationCheckBox
652                                         .setSelection (DEFAULT_RELAUNCH_ON_SCRIPT_TERMINATION);
653                 }
654
655
656                 setRemoteTabEnableState();
657
658                 try {
659                         fRemoteSourcePath.setText(configuration.getAttribute(
660                                         PHPLaunchConfigurationAttribute.REMOTE_PATH, ""));
661                 } catch (CoreException ce) {
662                         fRemoteSourcePath.setText("");
663                 }
664
665                 updatePathMapFromConfig(configuration);
666
667                 try {
668                         originalFileName = configuration.getAttribute(
669                                         PHPLaunchConfigurationAttribute.FILE_NAME, "");
670                         makeupTargetFile();
671                 } catch (CoreException ce) {
672                         originalFileName = "";
673                 }
674
675         }
676
677         private void updatePathMapFromConfig(ILaunchConfiguration config) {
678                 Map envVars = null;
679                 try {
680                         if (config != null) {
681                                 envVars = config.getAttribute(
682                                                 PHPLaunchConfigurationAttribute.FILE_MAP, (Map) null);
683                         }
684                         updatePathMapTable(envVars, this.fRemoteDebugPathMapTable);
685                         setPathMapButtonsEnableState();
686                 } catch (CoreException ce) {
687                         log(ce);
688                 }
689         }
690
691         private void updatePathMapTable(Map map, Table tableWidget) {
692                 tableWidget.removeAll();
693                 if (map == null) {
694                         return;
695                 }
696                 Iterator iterator = map.keySet().iterator();
697                 while (iterator.hasNext()) {
698                         String key = (String) iterator.next();
699                         String value = (String) map.get(key);
700                         TableItem tableItem = new TableItem(tableWidget, SWT.NONE);
701                         tableItem.setText(new String[] { key, value });
702                 }
703         }
704
705         // protected void initializeLoadPath(ILaunchConfiguration configuration) {
706         // boolean useDefaultLoadPath = true;
707         // try {
708         // useDefaultLoadPath =
709         // configuration.getAttribute(PHPLaunchConfigurationAttribute.USE_DEFAULT_LOAD_PATH,
710         // true);
711         // setUseLoadPathDefaults(useDefaultLoadPath);
712         // if (useDefaultLoadPath) {
713         // String projectName =
714         // configuration.getAttribute(PHPLaunchConfigurationAttribute.PROJECT_NAME,
715         // "");
716         // if (projectName != "") {
717         // IProject aProject =
718         // PHPeclipsePlugin.getWorkspace().getRoot().getProject(projectName);
719         // if ((aProject != null) && JavaCore.isPHPProject(aProject)) {
720         // JavaProject thePHPProject = new JavaProject();
721         // thePHPProject.setProject(aProject);
722         // List loadPathEntries = thePHPProject.getLoadPathEntries();
723         // loadPathListViewer.setInput(loadPathEntries);
724         // }
725         // }
726         // }
727         // } catch (CoreException e) {
728         // log(e);
729         // }
730         // }
731
732 //      protected void setUseLoadPathDefaults(boolean useDefaults) {
733 //              loadPathListViewer.getList().setEnabled(!useDefaults);
734 //              // loadPathDefaultButton.setSelection(useDefaults);
735 //      }
736
737         protected void initializeInterpreterSelection(
738                         ILaunchConfiguration configuration) {
739                 String interpreterName = null;
740                 try {
741                         interpreterName = configuration.getAttribute(
742                                         PHPLaunchConfigurationAttribute.SELECTED_INTERPRETER, "");
743                 } catch (CoreException e) {
744                         log(e);
745                 }
746                 if (interpreterName != null && !interpreterName.equals("")) {
747                         interpreterCombo.select(interpreterCombo.indexOf(interpreterName));
748                 }
749                 if (interpreterCombo.getSelectionIndex() < 0) {
750                         // previous definition had been deleted
751                         ((ILaunchConfigurationWorkingCopy) configuration).setAttribute(
752                                         PHPLaunchConfigurationAttribute.SELECTED_INTERPRETER, "");
753                         setErrorMessage(PHPDebugUiMessages
754                                         .getString("LaunchConfigurationTab.PHPEnvironment.interpreter_not_selected_error_message"));
755                 }
756         }
757
758         protected void initializeInterpreterCombo(Combo interpreterCombo) {
759                 installedInterpretersWorkingCopy = new ArrayList();
760                 installedInterpretersWorkingCopy.addAll(PHPRuntime.getDefault()
761                                 .getInstalledInterpreters());
762
763                 String[] interpreterNames = new String[installedInterpretersWorkingCopy
764                                 .size()];
765                 for (int interpreterIndex = 0; interpreterIndex < installedInterpretersWorkingCopy
766                                 .size(); interpreterIndex++) {
767                         PHPInterpreter interpreter = (PHPInterpreter) installedInterpretersWorkingCopy
768                                         .get(interpreterIndex);
769                         interpreterNames[interpreterIndex] = interpreter
770                                         .getInstallLocation().toString();
771                 }
772                 interpreterCombo.setItems(interpreterNames);
773
774                 PHPInterpreter selectedInterpreter = PHPRuntime.getDefault()
775                                 .getSelectedInterpreter();
776                 if (selectedInterpreter != null)
777                         interpreterCombo.select(interpreterCombo
778                                         .indexOf(selectedInterpreter.getInstallLocation()
779                                                         .toString()));
780         }
781
782         public void performApply(ILaunchConfigurationWorkingCopy configuration) {
783                 int selectionIndex = interpreterCombo.getSelectionIndex();
784                 if (selectionIndex >= 0)
785                         configuration.setAttribute(
786                                         PHPLaunchConfigurationAttribute.SELECTED_INTERPRETER,
787                                         interpreterCombo.getItem(selectionIndex));
788
789                 // configuration.setAttribute(PHPLaunchConfigurationAttribute.USE_DEFAULT_LOAD_PATH,
790                 // loadPathDefaultButton.getSelection());
791
792                 // if (!loadPathDefaultButton.getSelection()) {
793                 // List loadPathEntries = (List) loadPathListViewer.getInput();
794                 // List loadPathStrings = new ArrayList();
795                 // for (Iterator iterator = loadPathEntries.iterator();
796                 // iterator.hasNext();) {
797                 // LoadPathEntry entry = (LoadPathEntry) iterator.next();
798                 // loadPathStrings.add(entry.getPath().toString());
799                 // }
800                 // configuration.setAttribute(PHPLaunchConfigurationAttribute.CUSTOM_LOAD_PATH,
801                 // loadPathStrings);
802                 // }
803
804                 configuration.setAttribute(
805                                 PHPLaunchConfigurationAttribute.REMOTE_DEBUG,
806                                 fRemoteDebugCheckBox.getSelection());
807                 configuration.setAttribute(
808                                 PHPLaunchConfigurationAttribute.REMOTE_DEBUG_TRANSLATE,
809                                 fRemoteDebugTranslate.getSelection());
810                 configuration.setAttribute(PHPLaunchConfigurationAttribute.FILE_MAP,
811                                 getMapFromPathMapTable());
812                 configuration.setAttribute(PHPLaunchConfigurationAttribute.REMOTE_PATH,
813                                 fRemoteSourcePath.getText());
814                 configuration.setAttribute(
815                                 PHPLaunchConfigurationAttribute.OPEN_DBGSESSION_IN_BROWSER,
816                                 fOpenDBGSessionInBrowserCheckBox.getSelection());
817                 configuration.setAttribute(
818                                 PHPLaunchConfigurationAttribute.OPEN_DBGSESSION_IN_EXTERNAL_BROWSER,
819                                 fOpenDBGSessionInExternalBrowserCheckBox.getSelection());
820                 configuration.setAttribute(
821                                 PHPLaunchConfigurationAttribute.RELAUNCH_ON_SCRIPT_TERMINATION,
822                                 fRelaunchOnScriptTerminationCheckBox.getSelection());
823         }
824
825         protected Composite createPageRoot(Composite parent) {
826                 Composite composite = new Composite(parent, SWT.NULL);
827                 GridLayout layout = new GridLayout();
828                 layout.numColumns = 2;
829                 composite.setLayout(layout);
830                 createVerticalSpacer(composite, 2);
831                 setControl(composite);
832
833                 return composite;
834         }
835
836         private Map getMapFromPathMapTable() {
837                 TableItem[] items = fRemoteDebugPathMapTable.getItems();
838                 if (items.length == 0) {
839                         return null;
840                 }
841                 Map map = new HashMap(items.length);
842                 for (int i = 0; i < items.length; i++) {
843                         TableItem item = items[i];
844                         String key = item.getText(0);
845                         String value = item.getText(1);
846                         map.put(key, value);
847                 }
848                 return map;
849         }
850
851         public String getName() {
852                 return PHPDebugUiMessages
853                                 .getString("LaunchConfigurationTab.PHPEnvironment.name");
854         }
855
856         public boolean isValid(ILaunchConfiguration launchConfig) {
857                 try {
858                         if (launchConfig.getAttribute(
859                                         PHPLaunchConfigurationAttribute.SELECTED_INTERPRETER, "")
860                                         .equals("")) {
861                                 if (!launchConfig.getAttribute(
862                                                 PHPLaunchConfigurationAttribute.REMOTE_DEBUG, false)) {
863                                         setErrorMessage(PHPDebugUiMessages
864                                                         .getString("LaunchConfigurationTab.PHPEnvironment.interpreter_not_selected_error_message"));
865                                         return false;
866                                 }
867                         }
868                 } catch (CoreException e) {
869                         log(e);
870                 }
871
872                 setErrorMessage(null);
873                 return true;
874         }
875
876         protected void log(Throwable t) {
877                 PHPDebugUiPlugin.log(t);
878         }
879
880         public Image getImage() {
881                 return PHPUiImages.get(PHPUiImages.IMG_CTOOLS_PHP);
882         }
883
884         private void makeupTargetFile() {
885                 if (!fRemoteDebugCheckBox.getSelection() || originalFileName.equals("")) {
886                         targetFile.setText("");
887                         return;
888                 }
889
890                 // see net.sourceforge.phpdt.internal.debug.core.PHPDBGProxy.MapPath(PHPLineBreakpoint)
891
892                 IPath remoteSourcePath = new Path(fRemoteSourcePath.getText());
893                 IPath filename = new Path(originalFileName);
894                 filename = remoteSourcePath.append(filename);
895                 String path = filename.toOSString();
896                 Map pathmap = getMapFromPathMapTable();
897
898                 if (pathmap != null) {
899                         Iterator it = pathmap.keySet().iterator();
900                         while (it.hasNext()) {
901                                 String k = (String) it.next();
902                                 if (path.startsWith(k)) {
903                                         path = pathmap.get(k) + path.substring(k.length());
904                                         break;
905                                 }
906                         }
907                 }
908
909                 if (remoteSourcePath.isEmpty()) {
910                         if (pathmap != null) {
911                                 Iterator it = pathmap.keySet().iterator();
912                                 while (it.hasNext()) {
913                                         String local = (String) it.next();
914                                         IPath remotePath = new Path((String) pathmap.get(local));
915                                         IPath localPath = new Path(local);
916                                         if (localPath.isPrefixOf(filename)) {
917                                                 IPath newpath = filename.removeFirstSegments(localPath
918                                                                 .matchingFirstSegments(filename));
919                                                 newpath = remotePath.append(newpath);
920                                                 path = newpath.toString();
921                                                 if (path.substring(0, 1).equals("/")) {
922                                                         path = path.replace('\\', '/');
923                                                 } else {
924                                                         path = path.replace('/', '\\');
925                                                 }
926                                                 break;
927                                         }
928                                 }
929                         }
930                 } else {
931                         if (fRemoteDebugTranslate.getSelection()) {
932                                 if (remoteSourcePath.toString().substring(0, 1).equals("/")) {
933                                         path = path.replace('\\', '/');
934                                 } else {
935                                         path = path.replace('/', '\\');
936                                 }
937                         }
938                 }
939
940                 targetFile.setText(path);
941         }
942
943 }