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