1 package net.sourceforge.phpeclipse.xdebug.ui.php.launching;
3 import java.text.MessageFormat;
4 //import java.util.HashMap;
5 import java.util.Iterator;
7 //import java.util.Map;
8 import java.util.Vector;
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;
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;
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;
72 protected String[] fPathMapTableColumnHeaders = { "Local", "Remote" };
74 protected ColumnLayoutData[] fPathMapTableColumnLayouts = {
75 new ColumnWeightData(50), new ColumnWeightData(50) };
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 };
83 * Content provider for the environment table
85 protected class PathMapContentProvider implements IStructuredContentProvider {
86 public Object[] getElements(Object inputElement) {
87 PathMapItem[] elements = new PathMapItem[0];
88 ILaunchConfiguration config = (ILaunchConfiguration) inputElement;
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$
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));
108 public void dispose() {
111 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
116 * Label provider for the environment table
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) {
126 result = var.getLocalPath().toOSString();
129 result = var.getRemotePath().toString();
136 public Image getColumnImage(Object element, int columnIndex) {
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());
155 createPathMapTable(mainComposite);
156 createTableButtons(mainComposite);
158 Dialog.applyDialogFont(mainComposite);
162 * Creates the add/edit/remove buttons for the environment table
165 * the composite in which the buttons should be created
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());
180 createVerticalSpacer(buttonComposite, 1);
182 fAddButton = createPushButton(buttonComposite, "New", null);
183 fAddButton.addSelectionListener(new SelectionAdapter() {
184 public void widgetSelected(SelectionEvent event) {
185 handleAddButtonSelected();
189 fEditButton = createPushButton(buttonComposite, "Edit", null);
190 fEditButton.addSelectionListener(new SelectionAdapter() {
191 public void widgetSelected(SelectionEvent event) {
192 handleEditButtonSelected();
195 fEditButton.setEnabled(false);
197 fRemoveButton = createPushButton(buttonComposite, "Remove", null);
198 fRemoveButton.addSelectionListener(new SelectionAdapter() {
199 public void widgetSelected(SelectionEvent event) {
200 handleRemoveButtonSelected();
203 fRemoveButton.setEnabled(false);
205 fUpButton = createPushButton(buttonComposite, "Up", null);
206 fUpButton.addSelectionListener(new SelectionAdapter() {
207 public void widgetSelected(SelectionEvent event) {
208 handleUpButtonSelected();
211 fUpButton.setEnabled(false);
213 fDownButton = createPushButton(buttonComposite, "Down", null);
214 fDownButton.addSelectionListener(new SelectionAdapter() {
215 public void widgetSelected(SelectionEvent event) {
216 handleDownButtonSelected();
219 fDownButton.setEnabled(false);
224 * Creates and configures the table that displayed the key/value pairs that
225 * comprise the environment.
228 * the composite in which the table should be created
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);
244 Label label = new Label(tableComposite, SWT.NONE);
246 label.setText("&Map remote path to local path");
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);
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);
265 fPathMapTable.addDoubleClickListener(new IDoubleClickListener() {
266 public void doubleClick(DoubleClickEvent event) {
267 if (!fPathMapTable.getSelection().isEmpty()) {
268 handleEditButtonSelected();
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]);
282 * Responds to a selection changed event in the environment table
283 * @param event the selection change event
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();
290 fEditButton.setEnabled(idx>0);
291 fUpButton.setEnabled(idx>0);
292 fDownButton.setEnabled((idx>=0)&&(idx<count-1));
295 fRemoveButton.setEnabled(size > 0);
299 * Creates an editor for the value of the selected environment variable.
301 private void handleUpButtonSelected() {
302 IStructuredSelection sel = (IStructuredSelection) fPathMapTable.getSelection();
303 PathMapItem mapItem = (PathMapItem) sel.getFirstElement();
305 if (mapItem == null) {
308 IPath local = mapItem.getLocalPath();
309 TableItem[] items = fPathMapTable.getTable().getItems();
311 for (i = 0; i < items.length; i++) {
312 PathMapItem item = (PathMapItem) items[i].getData();
313 if (item.getLocalPath().equals(local)) {
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();
328 private void handleDownButtonSelected() {
329 IStructuredSelection sel = (IStructuredSelection) fPathMapTable.getSelection();
330 PathMapItem mapItem = (PathMapItem) sel.getFirstElement();
332 if (mapItem == null) {
335 IPath local = mapItem.getLocalPath();
336 TableItem[] items = fPathMapTable.getTable().getItems();
338 for (i = 0; i < items.length; i++) {
339 PathMapItem item = (PathMapItem) items[i].getData();
340 if (item.getLocalPath().equals(local)) {
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();
357 * Creates an editor for the value of the selected environment variable.
359 private void handleEditButtonSelected() {
360 IStructuredSelection sel = (IStructuredSelection) fPathMapTable.getSelection();
361 PathMapItem item = (PathMapItem) sel.getFirstElement();
366 EditPathMapDialog dialog = new EditPathMapDialog(getShell(), "Edit pathmap", new String[] { item.getLocalPath().toString(),item.getRemotePath().toString() });
368 if (dialog.open() != EditPathMapDialog.OK) {
371 String[] pathPair = dialog.getPathPair();
372 String newLocalPath=pathPair[0];
373 String newRemotePath=pathPair[1];
375 if (!item.getLocalPath().toString().equals(newLocalPath)) {
376 if (addVariable(new PathMapItem(newLocalPath,newRemotePath))) {
377 fPathMapTable.remove(item);
380 item.setRemotePath(newRemotePath);
381 fPathMapTable.update(item, null);
382 updateLaunchConfigurationDialog();
387 * Adds a new environment variable to the table.
389 protected void handleAddButtonSelected() {
390 EditPathMapDialog dialog = new EditPathMapDialog(getShell(), "Edit File Map", new String[] { "", "" });
391 if (dialog.open() != EditPathMapDialog.OK) {
394 String[] pathPair = dialog.getPathPair();
396 Path local = new Path(pathPair[0]);
397 Path remote = new Path(pathPair[1]);
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));
407 * Removes the selected environment variable from the table.
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);
416 fPathMapTable.getControl().setRedraw(true);
417 updateLaunchConfigurationDialog();
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
425 * @param variable the variable to add
426 * @return whether the variable was added
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() }));
439 fPathMapTable.remove(item);
443 fPathMapTable.add(mapItem);
444 updateLaunchConfigurationDialog();
450 public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
451 // TODO Auto-generated method stub
455 public void initializeFrom(ILaunchConfiguration configuration) {
456 fPathMapTable.setInput(configuration);
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());
468 if (vec.size() == 0) {
469 configuration.setAttribute(IXDebugConstants.ATTR_PHP_PATHMAP, (List) null);
471 configuration.setAttribute(IXDebugConstants.ATTR_PHP_PATHMAP, vec);
476 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#isValid(org.eclipse.debug.core.ILaunchConfiguration)
478 public boolean isValid(ILaunchConfiguration launchConfig) {
479 // need to use exception here!
480 setErrorMessage(null);
482 if (fPathMapTable.getTable().getItems().length == 0) {
483 setErrorMessage("Mappath empty!");
489 public String getName() {