cd7ae8f1151d78423ec6b880e2ca0e10245495ba
[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                 tabFolder.setSelection(fRemoteDebugCheckBox.getSelection() ? 0 : 1);
610                 try {
611                         fRemoteDebugTranslate.setSelection(configuration.getAttribute(
612                                         PHPLaunchConfigurationAttribute.REMOTE_DEBUG_TRANSLATE,
613                                         DEFAULT_REMOTE_DEBUG_TRANSLATE));
614                 } catch (CoreException ce) {
615                         fRemoteDebugTranslate.setSelection(DEFAULT_REMOTE_DEBUG_TRANSLATE);
616                 }
617                 try {
618                         fOpenDBGSessionInBrowserCheckBox
619                                         .setSelection(configuration
620                                                         .getAttribute(
621                                                                         PHPLaunchConfigurationAttribute.OPEN_DBGSESSION_IN_BROWSER,
622                                                                         DEFAULT_OPEN_DBGSESSION_IN_BROWSER));
623                 } catch (CoreException ce) {
624                         fOpenDBGSessionInBrowserCheckBox
625                                         .setSelection(DEFAULT_OPEN_DBGSESSION_IN_BROWSER);
626                 }
627                 try {
628                         fOpenDBGSessionInExternalBrowserCheckBox
629                                         .setSelection(configuration
630                                                         .getAttribute(
631                                                                         PHPLaunchConfigurationAttribute.OPEN_DBGSESSION_IN_EXTERNAL_BROWSER,
632                                                                         DEFAULT_OPEN_DBGSESSION_IN_EXTERNAL_BROWSER));
633                 } catch (CoreException ce) {
634                         fOpenDBGSessionInExternalBrowserCheckBox
635                                         .setSelection(DEFAULT_OPEN_DBGSESSION_IN_EXTERNAL_BROWSER);
636                 }
637
638                 setRemoteTabEnableState();
639
640                 try {
641                         fRemoteSourcePath.setText(configuration.getAttribute(
642                                         PHPLaunchConfigurationAttribute.REMOTE_PATH, ""));
643                 } catch (CoreException ce) {
644                         fRemoteSourcePath.setText("");
645                 }
646
647                 updatePathMapFromConfig(configuration);
648
649                 try {
650                         originalFileName = configuration.getAttribute(
651                                         PHPLaunchConfigurationAttribute.FILE_NAME, "");
652                         makeupTargetFile();
653                 } catch (CoreException ce) {
654                         originalFileName = "";
655                 }
656
657         }
658
659         private void updatePathMapFromConfig(ILaunchConfiguration config) {
660                 Map envVars = null;
661                 try {
662                         if (config != null) {
663                                 envVars = config.getAttribute(
664                                                 PHPLaunchConfigurationAttribute.FILE_MAP, (Map) null);
665                         }
666                         updatePathMapTable(envVars, this.fRemoteDebugPathMapTable);
667                         setPathMapButtonsEnableState();
668                 } catch (CoreException ce) {
669                         log(ce);
670                 }
671         }
672
673         private void updatePathMapTable(Map map, Table tableWidget) {
674                 tableWidget.removeAll();
675                 if (map == null) {
676                         return;
677                 }
678                 Iterator iterator = map.keySet().iterator();
679                 while (iterator.hasNext()) {
680                         String key = (String) iterator.next();
681                         String value = (String) map.get(key);
682                         TableItem tableItem = new TableItem(tableWidget, SWT.NONE);
683                         tableItem.setText(new String[] { key, value });
684                 }
685         }
686
687         // protected void initializeLoadPath(ILaunchConfiguration configuration) {
688         // boolean useDefaultLoadPath = true;
689         // try {
690         // useDefaultLoadPath =
691         // configuration.getAttribute(PHPLaunchConfigurationAttribute.USE_DEFAULT_LOAD_PATH,
692         // true);
693         // setUseLoadPathDefaults(useDefaultLoadPath);
694         // if (useDefaultLoadPath) {
695         // String projectName =
696         // configuration.getAttribute(PHPLaunchConfigurationAttribute.PROJECT_NAME,
697         // "");
698         // if (projectName != "") {
699         // IProject aProject =
700         // PHPeclipsePlugin.getWorkspace().getRoot().getProject(projectName);
701         // if ((aProject != null) && JavaCore.isPHPProject(aProject)) {
702         // JavaProject thePHPProject = new JavaProject();
703         // thePHPProject.setProject(aProject);
704         // List loadPathEntries = thePHPProject.getLoadPathEntries();
705         // loadPathListViewer.setInput(loadPathEntries);
706         // }
707         // }
708         // }
709         // } catch (CoreException e) {
710         // log(e);
711         // }
712         // }
713
714         protected void setUseLoadPathDefaults(boolean useDefaults) {
715                 loadPathListViewer.getList().setEnabled(!useDefaults);
716                 // loadPathDefaultButton.setSelection(useDefaults);
717         }
718
719         protected void initializeInterpreterSelection(
720                         ILaunchConfiguration configuration) {
721                 String interpreterName = null;
722                 try {
723                         interpreterName = configuration.getAttribute(
724                                         PHPLaunchConfigurationAttribute.SELECTED_INTERPRETER, "");
725                 } catch (CoreException e) {
726                         log(e);
727                 }
728                 if (interpreterName != null && !interpreterName.equals("")) {
729                         interpreterCombo.select(interpreterCombo.indexOf(interpreterName));
730                 }
731                 if (interpreterCombo.getSelectionIndex() < 0) {
732                         // previous definition had been deleted
733                         ((ILaunchConfigurationWorkingCopy) configuration).setAttribute(
734                                         PHPLaunchConfigurationAttribute.SELECTED_INTERPRETER, "");
735                         setErrorMessage(PHPDebugUiMessages
736                                         .getString("LaunchConfigurationTab.PHPEnvironment.interpreter_not_selected_error_message"));
737                 }
738         }
739
740         protected void initializeInterpreterCombo(Combo interpreterCombo) {
741                 installedInterpretersWorkingCopy = new ArrayList();
742                 installedInterpretersWorkingCopy.addAll(PHPRuntime.getDefault()
743                                 .getInstalledInterpreters());
744
745                 String[] interpreterNames = new String[installedInterpretersWorkingCopy
746                                 .size()];
747                 for (int interpreterIndex = 0; interpreterIndex < installedInterpretersWorkingCopy
748                                 .size(); interpreterIndex++) {
749                         PHPInterpreter interpreter = (PHPInterpreter) installedInterpretersWorkingCopy
750                                         .get(interpreterIndex);
751                         interpreterNames[interpreterIndex] = interpreter
752                                         .getInstallLocation().toString();
753                 }
754                 interpreterCombo.setItems(interpreterNames);
755
756                 PHPInterpreter selectedInterpreter = PHPRuntime.getDefault()
757                                 .getSelectedInterpreter();
758                 if (selectedInterpreter != null)
759                         interpreterCombo.select(interpreterCombo
760                                         .indexOf(selectedInterpreter.getInstallLocation()
761                                                         .toString()));
762         }
763
764         public void performApply(ILaunchConfigurationWorkingCopy configuration) {
765                 int selectionIndex = interpreterCombo.getSelectionIndex();
766                 if (selectionIndex >= 0)
767                         configuration.setAttribute(
768                                         PHPLaunchConfigurationAttribute.SELECTED_INTERPRETER,
769                                         interpreterCombo.getItem(selectionIndex));
770
771                 // configuration.setAttribute(PHPLaunchConfigurationAttribute.USE_DEFAULT_LOAD_PATH,
772                 // loadPathDefaultButton.getSelection());
773
774                 // if (!loadPathDefaultButton.getSelection()) {
775                 // List loadPathEntries = (List) loadPathListViewer.getInput();
776                 // List loadPathStrings = new ArrayList();
777                 // for (Iterator iterator = loadPathEntries.iterator();
778                 // iterator.hasNext();) {
779                 // LoadPathEntry entry = (LoadPathEntry) iterator.next();
780                 // loadPathStrings.add(entry.getPath().toString());
781                 // }
782                 // configuration.setAttribute(PHPLaunchConfigurationAttribute.CUSTOM_LOAD_PATH,
783                 // loadPathStrings);
784                 // }
785
786                 configuration.setAttribute(
787                                 PHPLaunchConfigurationAttribute.REMOTE_DEBUG,
788                                 fRemoteDebugCheckBox.getSelection());
789                 configuration.setAttribute(
790                                 PHPLaunchConfigurationAttribute.REMOTE_DEBUG_TRANSLATE,
791                                 fRemoteDebugTranslate.getSelection());
792                 configuration.setAttribute(PHPLaunchConfigurationAttribute.FILE_MAP,
793                                 getMapFromPathMapTable());
794                 configuration.setAttribute(PHPLaunchConfigurationAttribute.REMOTE_PATH,
795                                 fRemoteSourcePath.getText());
796                 configuration.setAttribute(
797                                 PHPLaunchConfigurationAttribute.OPEN_DBGSESSION_IN_BROWSER,
798                                 fOpenDBGSessionInBrowserCheckBox.getSelection());
799                 configuration
800                                 .setAttribute(
801                                                 PHPLaunchConfigurationAttribute.OPEN_DBGSESSION_IN_EXTERNAL_BROWSER,
802                                                 fOpenDBGSessionInExternalBrowserCheckBox.getSelection());
803         }
804
805         protected Composite createPageRoot(Composite parent) {
806                 Composite composite = new Composite(parent, SWT.NULL);
807                 GridLayout layout = new GridLayout();
808                 layout.numColumns = 2;
809                 composite.setLayout(layout);
810                 createVerticalSpacer(composite, 2);
811                 setControl(composite);
812
813                 return composite;
814         }
815
816         private Map getMapFromPathMapTable() {
817                 TableItem[] items = fRemoteDebugPathMapTable.getItems();
818                 if (items.length == 0) {
819                         return null;
820                 }
821                 Map map = new HashMap(items.length);
822                 for (int i = 0; i < items.length; i++) {
823                         TableItem item = items[i];
824                         String key = item.getText(0);
825                         String value = item.getText(1);
826                         map.put(key, value);
827                 }
828                 return map;
829         }
830
831         public String getName() {
832                 return PHPDebugUiMessages
833                                 .getString("LaunchConfigurationTab.PHPEnvironment.name");
834         }
835
836         public boolean isValid(ILaunchConfiguration launchConfig) {
837                 try {
838                         if (launchConfig.getAttribute(
839                                         PHPLaunchConfigurationAttribute.SELECTED_INTERPRETER, "")
840                                         .equals("")) {
841                                 if (!launchConfig.getAttribute(
842                                                 PHPLaunchConfigurationAttribute.REMOTE_DEBUG, false)) {
843                                         setErrorMessage(PHPDebugUiMessages
844                                                         .getString("LaunchConfigurationTab.PHPEnvironment.interpreter_not_selected_error_message"));
845                                         return false;
846                                 }
847                         }
848                 } catch (CoreException e) {
849                         log(e);
850                 }
851
852                 setErrorMessage(null);
853                 return true;
854         }
855
856         protected void log(Throwable t) {
857                 PHPDebugUiPlugin.log(t);
858         }
859
860         public Image getImage() {
861                 return PHPUiImages.get(PHPUiImages.IMG_CTOOLS_PHP);
862         }
863
864         private void makeupTargetFile() {
865                 if (!fRemoteDebugCheckBox.getSelection() || originalFileName.equals("")) {
866                         targetFile.setText("");
867                         return;
868                 }
869
870                 // see net.sourceforge.phpdt.internal.debug.core.PHPDBGProxy.MapPath(PHPLineBreakpoint)
871
872                 IPath remoteSourcePath = new Path(fRemoteSourcePath.getText());
873                 IPath filename = new Path(originalFileName);
874                 filename = remoteSourcePath.append(filename);
875                 String path = filename.toOSString();
876                 Map pathmap = getMapFromPathMapTable();
877
878                 if (pathmap != null) {
879                         Iterator it = pathmap.keySet().iterator();
880                         while (it.hasNext()) {
881                                 String k = (String) it.next();
882                                 if (path.startsWith(k)) {
883                                         path = pathmap.get(k) + path.substring(k.length());
884                                         break;
885                                 }
886                         }
887                 }
888
889                 if (remoteSourcePath.isEmpty()) {
890                         if (pathmap != null) {
891                                 Iterator it = pathmap.keySet().iterator();
892                                 while (it.hasNext()) {
893                                         String local = (String) it.next();
894                                         IPath remotePath = new Path((String) pathmap.get(local));
895                                         IPath localPath = new Path(local);
896                                         if (localPath.isPrefixOf(filename)) {
897                                                 IPath newpath = filename.removeFirstSegments(localPath
898                                                                 .matchingFirstSegments(filename));
899                                                 newpath = remotePath.append(newpath);
900                                                 path = newpath.toString();
901                                                 if (path.substring(0, 1).equals("/")) {
902                                                         path = path.replace('\\', '/');
903                                                 } else {
904                                                         path = path.replace('/', '\\');
905                                                 }
906                                                 break;
907                                         }
908                                 }
909                         }
910                 } else {
911                         if (fRemoteDebugTranslate.getSelection()) {
912                                 if (remoteSourcePath.toString().substring(0, 1).equals("/")) {
913                                         path = path.replace('\\', '/');
914                                 } else {
915                                         path = path.replace('/', '\\');
916                                 }
917                         }
918                 }
919
920                 targetFile.setText(path);
921         }
922
923 }