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