Importing the XDebugProxy code in the HEAD. The repo was tagged with T_BEFORE_XDEBUGP...
[phpeclipse.git] / net.sourceforge.phpeclipse.xdebug.ui / src / net / sourceforge / phpeclipse / xdebug / ui / php / launching / PHPPathMapTab.java
1 package net.sourceforge.phpeclipse.xdebug.ui.php.launching;
2
3 import java.text.MessageFormat;
4 //import java.util.HashMap;
5 import java.util.Iterator;
6 import java.util.List;
7 //import java.util.Map;
8 import java.util.Vector;
9
10 import net.sourceforge.phpeclipse.xdebug.core.PathMapItem;
11 import net.sourceforge.phpeclipse.xdebug.core.XDebugCorePlugin;
12 import net.sourceforge.phpeclipse.xdebug.php.launching.IXDebugConstants;
13 import net.sourceforge.phpeclipse.xdebug.ui.EditPathMapDialog;
14 /*import net.sourceforge.phpeclipse.xdebug.ui.EnvironmentVariable;
15 import net.sourceforge.phpeclipse.xdebug.ui.MultipleInputDialog;
16 import net.sourceforge.phpeclipse.xdebug.ui.php.launching.PHPEnvironmentTab.EnvironmentVariableContentProvider;
17 import net.sourceforge.phpeclipse.xdebug.ui.php.launching.PHPEnvironmentTab.EnvironmentVariableLabelProvider;
18 */
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.debug.core.ILaunchConfiguration;
25 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
26 //import org.eclipse.debug.core.ILaunchManager;
27 import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
28 import org.eclipse.jface.dialogs.Dialog;
29 import org.eclipse.jface.dialogs.MessageDialog;
30 import org.eclipse.jface.viewers.ColumnLayoutData;
31 import org.eclipse.jface.viewers.ColumnWeightData;
32 import org.eclipse.jface.viewers.DoubleClickEvent;
33 import org.eclipse.jface.viewers.IDoubleClickListener;
34 import org.eclipse.jface.viewers.ISelectionChangedListener;
35 import org.eclipse.jface.viewers.IStructuredContentProvider;
36 import org.eclipse.jface.viewers.IStructuredSelection;
37 import org.eclipse.jface.viewers.ITableLabelProvider;
38 import org.eclipse.jface.viewers.LabelProvider;
39 import org.eclipse.jface.viewers.SelectionChangedEvent;
40 import org.eclipse.jface.viewers.StructuredSelection;
41 import org.eclipse.jface.viewers.TableLayout;
42 import org.eclipse.jface.viewers.TableViewer;
43 import org.eclipse.jface.viewers.Viewer;
44 //import org.eclipse.jface.viewers.ViewerSorter;
45 //import org.eclipse.jface.window.Window;
46 import org.eclipse.swt.SWT;
47 import org.eclipse.swt.events.SelectionAdapter;
48 import org.eclipse.swt.events.SelectionEvent;
49 import org.eclipse.swt.graphics.Font;
50 import org.eclipse.swt.graphics.Image;
51 import org.eclipse.swt.layout.GridData;
52 import org.eclipse.swt.layout.GridLayout;
53 import org.eclipse.swt.widgets.Button;
54 import org.eclipse.swt.widgets.Composite;
55 import org.eclipse.swt.widgets.Label;
56 import org.eclipse.swt.widgets.Table;
57 import org.eclipse.swt.widgets.TableColumn;
58 import org.eclipse.swt.widgets.TableItem;
59
60 public class PHPPathMapTab extends AbstractLaunchConfigurationTab {
61         protected TableViewer fPathMapTable;
62         protected Button fAddButton;
63         protected Button envAddCGIButton;
64         protected Button fEditButton;
65         protected Button fRemoveButton;
66         protected Button fUpButton;
67         protected Button fDownButton;
68
69         
70         protected String[] fPathMapTableColumnHeaders = { "Local", "Remote" };
71
72         protected ColumnLayoutData[] fPathMapTableColumnLayouts = {
73                         new ColumnWeightData(50), new ColumnWeightData(50) };
74         
75         protected static final String P_REMOTE = "remote"; //$NON-NLS-1$
76         protected static final String P_LOCAL = "local"; //$NON-NLS-1$
77         protected static String[] fPathMapTableColumnProperties = { P_REMOTE, P_LOCAL };
78
79         
80         /**
81          * Content provider for the environment table
82          */
83         protected class PathMapContentProvider implements IStructuredContentProvider {
84                 public Object[] getElements(Object inputElement) {
85                         PathMapItem[] elements = new PathMapItem[0];
86                         ILaunchConfiguration config = (ILaunchConfiguration) inputElement;
87                         List l;
88                         try {
89                                 l = config.getAttribute(IXDebugConstants.ATTR_PHP_PATHMAP, (List) null);
90                         } catch (CoreException e) {
91                                 XDebugCorePlugin.log(new Status(IStatus.ERROR,
92                                                 XDebugCorePlugin.PLUGIN_ID, IStatus.ERROR,
93                                                 "Error reading configuration", e)); //$NON-NLS-1$
94                                 return elements;
95                         }
96                         if (l != null && !l.isEmpty()) {
97                                 elements = new PathMapItem[l.size()];
98                                 for (int i = 0; i < l.size(); i++) {
99                                         elements[i] = new PathMapItem((String) l.get(i));
100                                 }
101                         }
102
103                         return elements;
104                 }
105
106                 public void dispose() {
107                 }
108
109                 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
110                 }
111         }
112         
113         /**
114          * Label provider for the environment table
115          */
116         public class PathMapItemLabelProvider extends LabelProvider
117                         implements ITableLabelProvider {
118                 public String getColumnText(Object element, int columnIndex) {
119                         String result = null;
120                         if (element != null) {
121                                 PathMapItem var = (PathMapItem) element;
122                                 switch (columnIndex) {
123                                 case 0: // local
124                                         result = var.getLocalPath().toOSString();
125                                         break;
126                                 case 1: // remote
127                                         result = var.getRemotePath().toString();
128                                         break;
129                                 }
130                         }
131                         return result;
132                 }
133
134                 public Image getColumnImage(Object element, int columnIndex) {
135                         return null;
136                 }
137         }
138
139
140         public void createControl(Composite parent) {
141                 // Create main composite
142                 Composite mainComposite = new Composite(parent, SWT.NONE);
143                 setControl(mainComposite);
144                 // WorkbenchHelp.setHelp(getControl(),
145                 // IDebugHelpContextIds.LAUNCH_CONFIGURATION_DIALOG_ENVIRONMENT_TAB);
146                 GridLayout layout = new GridLayout();
147                 layout.numColumns = 2;
148                 GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
149                 mainComposite.setLayout(layout);
150                 mainComposite.setLayoutData(gridData);
151                 mainComposite.setFont(parent.getFont());
152
153                 createPathMapTable(mainComposite);
154                 createTableButtons(mainComposite);
155
156                 Dialog.applyDialogFont(mainComposite);
157         }
158         
159         /**
160          * Creates the add/edit/remove buttons for the environment table
161          * 
162          * @param parent
163          *            the composite in which the buttons should be created
164          */
165         protected void createTableButtons(Composite parent) {
166                 // Create button composite
167                 Composite buttonComposite = new Composite(parent, SWT.NONE);
168                 GridLayout glayout = new GridLayout();
169                 glayout.marginHeight = 0;
170                 glayout.marginWidth = 0;
171                 glayout.numColumns = 1;
172                 GridData gdata = new GridData(GridData.VERTICAL_ALIGN_BEGINNING
173                                 | GridData.HORIZONTAL_ALIGN_END);
174                 buttonComposite.setLayout(glayout);
175                 buttonComposite.setLayoutData(gdata);
176                 buttonComposite.setFont(parent.getFont());
177
178                 createVerticalSpacer(buttonComposite, 1);
179                 // Create buttons
180                 fAddButton = createPushButton(buttonComposite, "New", null);
181                 fAddButton.addSelectionListener(new SelectionAdapter() {
182                         public void widgetSelected(SelectionEvent event) {
183                                 handleAddButtonSelected();
184                         }
185                 });
186
187                 fEditButton = createPushButton(buttonComposite, "Edit", null);
188                 fEditButton.addSelectionListener(new SelectionAdapter() {
189                         public void widgetSelected(SelectionEvent event) {
190                                 handleEditButtonSelected();
191                         }
192                 });
193                 fEditButton.setEnabled(false);
194
195                 fRemoveButton = createPushButton(buttonComposite, "Remove", null);
196                 fRemoveButton.addSelectionListener(new SelectionAdapter() {
197                         public void widgetSelected(SelectionEvent event) {
198                                 handleRemoveButtonSelected();
199                         }
200                 });
201                 fRemoveButton.setEnabled(false);
202                 
203                 fUpButton = createPushButton(buttonComposite, "Up", null);
204                 fUpButton.addSelectionListener(new SelectionAdapter() {
205                         public void widgetSelected(SelectionEvent event) {
206                                 handleUpButtonSelected();
207                         }
208                 });
209                 fUpButton.setEnabled(false);
210                 
211                 fDownButton = createPushButton(buttonComposite, "Down", null);
212                 fDownButton.addSelectionListener(new SelectionAdapter() {
213                         public void widgetSelected(SelectionEvent event) {
214                                 handleDownButtonSelected();
215                         }
216                 });
217                 fDownButton.setEnabled(false);
218         }
219
220         
221         /**
222          * Creates and configures the table that displayed the key/value pairs that
223          * comprise the environment.
224          * 
225          * @param parent
226          *            the composite in which the table should be created
227          */
228         protected void createPathMapTable(Composite parent) {
229                 Font font = parent.getFont();
230                 // Create table composite
231                 Composite tableComposite = new Composite(parent, SWT.NONE);
232                 GridLayout layout = new GridLayout();
233                 layout.marginHeight = 0;
234                 layout.marginWidth = 0;
235                 layout.numColumns = 1;
236                 GridData gridData = new GridData(GridData.FILL_BOTH);
237                 gridData.heightHint = 150;
238                 tableComposite.setLayout(layout);
239                 tableComposite.setLayoutData(gridData);
240                 tableComposite.setFont(font);
241                 // Create label
242                 Label label = new Label(tableComposite, SWT.NONE);
243                 label.setFont(font);
244                 label.setText("&Map remote path to local path");
245                 // Create table
246                 fPathMapTable = new TableViewer(tableComposite, SWT.BORDER
247                                 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI | SWT.FULL_SELECTION);
248                 Table table = fPathMapTable.getTable();
249                 TableLayout tableLayout = new TableLayout();
250                 table.setLayout(tableLayout);
251                 table.setHeaderVisible(true);
252                 table.setFont(font);
253                 gridData = new GridData(GridData.FILL_BOTH);
254                 fPathMapTable.getControl().setLayoutData(gridData);
255                 fPathMapTable.setContentProvider(new PathMapContentProvider());
256                 fPathMapTable.setLabelProvider(new PathMapItemLabelProvider());
257                 fPathMapTable.setColumnProperties(fPathMapTableColumnProperties);
258                 fPathMapTable.addSelectionChangedListener(new ISelectionChangedListener() {
259                         public void selectionChanged(SelectionChangedEvent event) {
260                                 handleTableSelectionChanged(event);
261                         }
262                 });
263                 fPathMapTable.addDoubleClickListener(new IDoubleClickListener() {
264                         public void doubleClick(DoubleClickEvent event) {
265                                 if (!fPathMapTable.getSelection().isEmpty()) {
266                                         handleEditButtonSelected();
267                                 }
268                         }
269                 });
270                 // Create columns
271                 for (int i = 0; i < fPathMapTableColumnHeaders.length; i++) {
272                         tableLayout.addColumnData(fPathMapTableColumnLayouts[i]);
273                         TableColumn tc = new TableColumn(table, SWT.NONE, i);
274                         tc.setResizable(fPathMapTableColumnLayouts[i].resizable);
275                         tc.setText(fPathMapTableColumnHeaders[i]);
276                 }
277         }
278         
279         /**
280          * Responds to a selection changed event in the environment table
281          * @param event the selection change event
282          */
283         protected void handleTableSelectionChanged(SelectionChangedEvent event) {
284                 int size = ((IStructuredSelection)event.getSelection()).size();
285                 int idx = fPathMapTable.getTable().getSelectionIndex();
286                 int count = fPathMapTable.getTable().getItemCount();
287                 if (size==1) {
288                         fEditButton.setEnabled(idx>0);
289                         fUpButton.setEnabled(idx>0);
290                         fDownButton.setEnabled((idx>=0)&&(idx<count-1));
291                 }
292                 
293                 fRemoveButton.setEnabled(size > 0);
294         }
295         
296         /**
297          * Creates an editor for the value of the selected environment variable.
298          */
299         private void handleUpButtonSelected() {
300                 IStructuredSelection sel = (IStructuredSelection) fPathMapTable.getSelection();
301                 PathMapItem mapItem = (PathMapItem) sel.getFirstElement();
302                 boolean found=false;
303                 if (mapItem == null) {
304                         return;
305                 }
306                 IPath local = mapItem.getLocalPath();
307                 TableItem[] items = fPathMapTable.getTable().getItems();
308                 int i;
309                 for (i = 0; i < items.length; i++) {
310                         PathMapItem item = (PathMapItem) items[i].getData();
311                         if (item.getLocalPath().equals(local)) {
312                                 found=true;
313                                 break;
314                         }
315                 }
316                 if ((i>0) && found) {
317                         fPathMapTable.getControl().setRedraw(false);
318                         fPathMapTable.remove(mapItem);
319                         fPathMapTable.insert(mapItem,i-1);
320                         fPathMapTable.getControl().setRedraw(true);
321                         fPathMapTable.setSelection(new StructuredSelection(mapItem),true);
322                         updateLaunchConfigurationDialog();
323                 }
324         }
325         
326         private void handleDownButtonSelected() {
327                 IStructuredSelection sel = (IStructuredSelection) fPathMapTable.getSelection();
328                 PathMapItem mapItem = (PathMapItem) sel.getFirstElement();
329                 boolean found=false;
330                 if (mapItem == null) {
331                         return;
332                 }
333                 IPath local = mapItem.getLocalPath();
334                 TableItem[] items = fPathMapTable.getTable().getItems();
335                 int i;
336                 for (i = 0; i < items.length; i++) {
337                         PathMapItem item = (PathMapItem) items[i].getData();
338                         if (item.getLocalPath().equals(local)) {
339                                 found=true;
340                                 break;
341                         }
342                 }
343
344                 if ((i<items.length-1) && found) {
345                         fPathMapTable.getControl().setRedraw(false);
346                         fPathMapTable.remove(mapItem);
347                         fPathMapTable.insert(mapItem,i+1);
348                         fPathMapTable.getControl().setRedraw(true);
349                         fPathMapTable.setSelection(new StructuredSelection(mapItem),true);
350                         updateLaunchConfigurationDialog();
351                 }
352         }
353         
354         /**
355          * Creates an editor for the value of the selected environment variable.
356          */
357         private void handleEditButtonSelected() {
358                 IStructuredSelection sel = (IStructuredSelection) fPathMapTable.getSelection();
359                 PathMapItem item = (PathMapItem) sel.getFirstElement();
360                 if (item == null) {
361                         return;
362                 }
363
364                 EditPathMapDialog dialog = new EditPathMapDialog(getShell(), "Edit pathmap", new String[] { item.getLocalPath().toString(),item.getRemotePath().toString() });
365
366                 if (dialog.open() != EditPathMapDialog.OK) {
367                         return;
368                 }
369                 String[] pathPair = dialog.getPathPair();
370                 String newLocalPath=pathPair[0];
371                 String newRemotePath=pathPair[1];
372                 
373                 if (!item.getLocalPath().toString().equals(newLocalPath)) {
374                         if (addVariable(new PathMapItem(newLocalPath,newRemotePath))) {
375                                 fPathMapTable.remove(item);
376                         }
377                 } else {
378                         item.setRemotePath(newRemotePath);
379                         fPathMapTable.update(item, null);
380                         updateLaunchConfigurationDialog();
381                 }
382         }
383         
384         /**
385          * Adds a new environment variable to the table.
386          */
387         protected void handleAddButtonSelected() {
388             EditPathMapDialog dialog = new EditPathMapDialog(getShell(), "Edit File Map", new String[] { "", "" });
389                 if (dialog.open() != EditPathMapDialog.OK) {
390                         return;
391                 }
392                 String[] pathPair = dialog.getPathPair();
393                 
394                 Path local = new Path(pathPair[0]);
395                 Path remote = new Path(pathPair[1]);
396                 
397                 String strlocal = local.toString();
398                 String strremote = remote.toString();
399                 if (strlocal != null && strremote != null && strlocal.length() > 0 && strremote.length() > 0) {
400                                 addVariable(new PathMapItem(strlocal,strremote));
401                 }
402         }
403         
404         /**
405          * Removes the selected environment variable from the table.
406          */
407         private void handleRemoveButtonSelected() {
408                 IStructuredSelection sel = (IStructuredSelection) fPathMapTable.getSelection();
409                 fPathMapTable.getControl().setRedraw(false);
410                 for (Iterator i = sel.iterator(); i.hasNext();) {
411                         PathMapItem item = (PathMapItem) i.next();
412                         fPathMapTable.remove(item);
413                 }
414                 fPathMapTable.getControl().setRedraw(true);
415                 updateLaunchConfigurationDialog();
416         }
417
418         /**
419          * Attempts to add the given variable. Returns whether the variable was
420          * added or not (as when the user answers not to overwrite an existing
421          * variable).
422          * 
423          * @param variable the variable to add
424          * @return whether the variable was added
425          */
426         protected boolean addVariable(PathMapItem mapItem) {
427                 IPath local = mapItem.getLocalPath();
428                 TableItem[] items = fPathMapTable.getTable().getItems();
429                 for (int i = 0; i < items.length; i++) {
430                         PathMapItem item = (PathMapItem) items[i].getData();
431                         if (item.getLocalPath().equals(local)) {
432                                 boolean overWrite = MessageDialog.openQuestion(getShell(),"Overwrite variable?",
433                                                 MessageFormat.format("A local path named {0} already exists. Overwrite?",new String[] { local.toString() }));
434                                 if (!overWrite) {
435                                         return false;
436                                 }
437                                 fPathMapTable.remove(item);
438                                 break;
439                         }
440                 }
441                 fPathMapTable.add(mapItem);
442                 updateLaunchConfigurationDialog();
443                 return true;
444         }
445         
446
447
448         public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
449                 // TODO Auto-generated method stub
450
451         }
452
453         public void initializeFrom(ILaunchConfiguration configuration) {
454                 fPathMapTable.setInput(configuration);
455         }
456
457         public void performApply(ILaunchConfigurationWorkingCopy configuration) {
458                 // Convert the table's items into a List so that this can be saved in the
459                 // configuration's attributes.
460                 TableItem[] items = fPathMapTable.getTable().getItems();
461                 List vec = new Vector(items.length);
462                 for (int i = 0; i < items.length; i++) {
463                         PathMapItem item = (PathMapItem) items[i].getData();
464                         vec.add(item.getStringData());
465                 }
466                 if (vec.size() == 0) {
467                         configuration.setAttribute(IXDebugConstants.ATTR_PHP_PATHMAP, (List) null);
468                 } else {
469                         configuration.setAttribute(IXDebugConstants.ATTR_PHP_PATHMAP, vec);
470                 }
471         }
472
473         public String getName() {
474                 return "Pathmap";
475         }
476 }