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