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.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;
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;
70 protected String[] fPathMapTableColumnHeaders = { "Local", "Remote" };
72 protected ColumnLayoutData[] fPathMapTableColumnLayouts = {
73 new ColumnWeightData(50), new ColumnWeightData(50) };
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 };
81 * Content provider for the environment table
83 protected class PathMapContentProvider implements IStructuredContentProvider {
84 public Object[] getElements(Object inputElement) {
85 PathMapItem[] elements = new PathMapItem[0];
86 ILaunchConfiguration config = (ILaunchConfiguration) inputElement;
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$
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));
106 public void dispose() {
109 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
114 * Label provider for the environment table
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) {
124 result = var.getLocalPath().toOSString();
127 result = var.getRemotePath().toString();
134 public Image getColumnImage(Object element, int columnIndex) {
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());
153 createPathMapTable(mainComposite);
154 createTableButtons(mainComposite);
156 Dialog.applyDialogFont(mainComposite);
160 * Creates the add/edit/remove buttons for the environment table
163 * the composite in which the buttons should be created
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());
178 createVerticalSpacer(buttonComposite, 1);
180 fAddButton = createPushButton(buttonComposite, "New", null);
181 fAddButton.addSelectionListener(new SelectionAdapter() {
182 public void widgetSelected(SelectionEvent event) {
183 handleAddButtonSelected();
187 fEditButton = createPushButton(buttonComposite, "Edit", null);
188 fEditButton.addSelectionListener(new SelectionAdapter() {
189 public void widgetSelected(SelectionEvent event) {
190 handleEditButtonSelected();
193 fEditButton.setEnabled(false);
195 fRemoveButton = createPushButton(buttonComposite, "Remove", null);
196 fRemoveButton.addSelectionListener(new SelectionAdapter() {
197 public void widgetSelected(SelectionEvent event) {
198 handleRemoveButtonSelected();
201 fRemoveButton.setEnabled(false);
203 fUpButton = createPushButton(buttonComposite, "Up", null);
204 fUpButton.addSelectionListener(new SelectionAdapter() {
205 public void widgetSelected(SelectionEvent event) {
206 handleUpButtonSelected();
209 fUpButton.setEnabled(false);
211 fDownButton = createPushButton(buttonComposite, "Down", null);
212 fDownButton.addSelectionListener(new SelectionAdapter() {
213 public void widgetSelected(SelectionEvent event) {
214 handleDownButtonSelected();
217 fDownButton.setEnabled(false);
222 * Creates and configures the table that displayed the key/value pairs that
223 * comprise the environment.
226 * the composite in which the table should be created
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);
242 Label label = new Label(tableComposite, SWT.NONE);
244 label.setText("&Map remote path to local path");
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);
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);
263 fPathMapTable.addDoubleClickListener(new IDoubleClickListener() {
264 public void doubleClick(DoubleClickEvent event) {
265 if (!fPathMapTable.getSelection().isEmpty()) {
266 handleEditButtonSelected();
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]);
280 * Responds to a selection changed event in the environment table
281 * @param event the selection change event
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();
288 fEditButton.setEnabled(idx>0);
289 fUpButton.setEnabled(idx>0);
290 fDownButton.setEnabled((idx>=0)&&(idx<count-1));
293 fRemoveButton.setEnabled(size > 0);
297 * Creates an editor for the value of the selected environment variable.
299 private void handleUpButtonSelected() {
300 IStructuredSelection sel = (IStructuredSelection) fPathMapTable.getSelection();
301 PathMapItem mapItem = (PathMapItem) sel.getFirstElement();
303 if (mapItem == null) {
306 IPath local = mapItem.getLocalPath();
307 TableItem[] items = fPathMapTable.getTable().getItems();
309 for (i = 0; i < items.length; i++) {
310 PathMapItem item = (PathMapItem) items[i].getData();
311 if (item.getLocalPath().equals(local)) {
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();
326 private void handleDownButtonSelected() {
327 IStructuredSelection sel = (IStructuredSelection) fPathMapTable.getSelection();
328 PathMapItem mapItem = (PathMapItem) sel.getFirstElement();
330 if (mapItem == null) {
333 IPath local = mapItem.getLocalPath();
334 TableItem[] items = fPathMapTable.getTable().getItems();
336 for (i = 0; i < items.length; i++) {
337 PathMapItem item = (PathMapItem) items[i].getData();
338 if (item.getLocalPath().equals(local)) {
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();
355 * Creates an editor for the value of the selected environment variable.
357 private void handleEditButtonSelected() {
358 IStructuredSelection sel = (IStructuredSelection) fPathMapTable.getSelection();
359 PathMapItem item = (PathMapItem) sel.getFirstElement();
364 EditPathMapDialog dialog = new EditPathMapDialog(getShell(), "Edit pathmap", new String[] { item.getLocalPath().toString(),item.getRemotePath().toString() });
366 if (dialog.open() != EditPathMapDialog.OK) {
369 String[] pathPair = dialog.getPathPair();
370 String newLocalPath=pathPair[0];
371 String newRemotePath=pathPair[1];
373 if (!item.getLocalPath().toString().equals(newLocalPath)) {
374 if (addVariable(new PathMapItem(newLocalPath,newRemotePath))) {
375 fPathMapTable.remove(item);
378 item.setRemotePath(newRemotePath);
379 fPathMapTable.update(item, null);
380 updateLaunchConfigurationDialog();
385 * Adds a new environment variable to the table.
387 protected void handleAddButtonSelected() {
388 EditPathMapDialog dialog = new EditPathMapDialog(getShell(), "Edit File Map", new String[] { "", "" });
389 if (dialog.open() != EditPathMapDialog.OK) {
392 String[] pathPair = dialog.getPathPair();
394 Path local = new Path(pathPair[0]);
395 Path remote = new Path(pathPair[1]);
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));
405 * Removes the selected environment variable from the table.
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);
414 fPathMapTable.getControl().setRedraw(true);
415 updateLaunchConfigurationDialog();
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
423 * @param variable the variable to add
424 * @return whether the variable was added
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() }));
437 fPathMapTable.remove(item);
441 fPathMapTable.add(mapItem);
442 updateLaunchConfigurationDialog();
448 public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
449 // TODO Auto-generated method stub
453 public void initializeFrom(ILaunchConfiguration configuration) {
454 fPathMapTable.setInput(configuration);
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());
466 if (vec.size() == 0) {
467 configuration.setAttribute(IXDebugConstants.ATTR_PHP_PATHMAP, (List) null);
469 configuration.setAttribute(IXDebugConstants.ATTR_PHP_PATHMAP, vec);
473 public String getName() {