eb99160a897677eb7779c645b31ed90d14007e45
[phpeclipse.git] / net.sourceforge.phpeclipse.xdebug.ui / src / net / sourceforge / phpeclipse / xdebug / ui / php / launching / PHPEnvironmentTab.java
1 /*
2  * Created on 14.07.2004
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */
7 package net.sourceforge.phpeclipse.xdebug.ui.php.launching;
8
9 import java.text.MessageFormat;
10 import java.util.Comparator;
11 import java.util.HashMap;
12 import java.util.Iterator;
13 import java.util.Map;
14 import java.util.TreeMap;
15
16 import net.sourceforge.phpeclipse.xdebug.core.XDebugCorePlugin;
17 import net.sourceforge.phpeclipse.xdebug.ui.EnvironmentVariable;
18 import net.sourceforge.phpeclipse.xdebug.ui.MultipleInputDialog;
19 import net.sourceforge.phpeclipse.xdebug.ui.XDebugUIPluginImages;
20
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.debug.core.DebugPlugin;
25 import org.eclipse.debug.core.ILaunchConfiguration;
26 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
27 import org.eclipse.debug.core.ILaunchManager;
28 import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
29 import org.eclipse.debug.ui.IDebugUIConstants;
30 import org.eclipse.jface.dialogs.Dialog;
31 import org.eclipse.jface.dialogs.IDialogSettings;
32 import org.eclipse.jface.dialogs.MessageDialog;
33 import org.eclipse.jface.viewers.ColumnLayoutData;
34 import org.eclipse.jface.viewers.ColumnWeightData;
35 import org.eclipse.jface.viewers.DoubleClickEvent;
36 import org.eclipse.jface.viewers.IDoubleClickListener;
37 import org.eclipse.jface.viewers.ILabelProvider;
38 import org.eclipse.jface.viewers.ILabelProviderListener;
39 import org.eclipse.jface.viewers.ISelectionChangedListener;
40 import org.eclipse.jface.viewers.IStructuredContentProvider;
41 import org.eclipse.jface.viewers.IStructuredSelection;
42 import org.eclipse.jface.viewers.ITableLabelProvider;
43 import org.eclipse.jface.viewers.LabelProvider;
44 import org.eclipse.jface.viewers.SelectionChangedEvent;
45 import org.eclipse.jface.viewers.TableLayout;
46 import org.eclipse.jface.viewers.TableViewer;
47 import org.eclipse.jface.viewers.Viewer;
48 import org.eclipse.jface.viewers.ViewerSorter;
49 import org.eclipse.jface.window.Window;
50 import org.eclipse.swt.SWT;
51 import org.eclipse.swt.events.SelectionAdapter;
52 import org.eclipse.swt.events.SelectionEvent;
53 import org.eclipse.swt.graphics.Font;
54 import org.eclipse.swt.graphics.Image;
55 import org.eclipse.swt.layout.GridData;
56 import org.eclipse.swt.layout.GridLayout;
57 import org.eclipse.swt.widgets.Button;
58 import org.eclipse.swt.widgets.Composite;
59 import org.eclipse.swt.widgets.Label;
60 import org.eclipse.swt.widgets.Shell;
61 import org.eclipse.swt.widgets.Table;
62 import org.eclipse.swt.widgets.TableColumn;
63 import org.eclipse.swt.widgets.TableItem;
64 import org.eclipse.ui.dialogs.ListSelectionDialog;
65
66 /**
67  * @author Christian
68  * 
69  * TODO To change the template for this generated type comment go to Window -
70  * Preferences - Java - Code Style - Code Templates
71  */
72 public class PHPEnvironmentTab extends AbstractLaunchConfigurationTab {
73
74         protected TableViewer environmentTable;
75
76         protected String[] envTableColumnHeaders = { "Variable", "Value" };
77
78         protected ColumnLayoutData[] envTableColumnLayouts = {
79                         new ColumnWeightData(50), new ColumnWeightData(50) };
80
81         private static final String NAME_LABEL = "Name";
82
83         private static final String VALUE_LABEL = "Value";
84
85         protected static final String P_VARIABLE = "variable"; //$NON-NLS-1$
86
87         protected static final String P_VALUE = "value"; //$NON-NLS-1$
88
89         protected static String[] envTableColumnProperties = { P_VARIABLE, P_VALUE };
90
91         protected Button envAddButton;
92
93         protected Button envAddCGIButton;
94
95         protected Button envEditButton;
96
97         protected Button envRemoveButton;
98
99         protected Button appendEnvironment;
100
101         protected Button replaceEnvironment;
102
103         protected Button envSelectButton;
104
105         /**
106          * Content provider for the environment table
107          */
108         protected class EnvironmentVariableContentProvider implements
109                         IStructuredContentProvider {
110                 public Object[] getElements(Object inputElement) {
111                         EnvironmentVariable[] elements = new EnvironmentVariable[0];
112                         ILaunchConfiguration config = (ILaunchConfiguration) inputElement;
113                         Map m;
114                         try {
115                                 m = config.getAttribute(
116                                                 ILaunchManager.ATTR_ENVIRONMENT_VARIABLES, (Map) null);
117                         } catch (CoreException e) {
118                                 XDebugCorePlugin.log(new Status(IStatus.ERROR,
119                                                 XDebugCorePlugin.PLUGIN_ID, IStatus.ERROR,
120                                                 "Error reading configuration", e)); //$NON-NLS-1$
121                                 return elements;
122                         }
123                         if (m != null && !m.isEmpty()) {
124                                 elements = new EnvironmentVariable[m.size()];
125                                 String[] varNames = new String[m.size()];
126                                 m.keySet().toArray(varNames);
127                                 for (int i = 0; i < m.size(); i++) {
128                                         elements[i] = new EnvironmentVariable(varNames[i],
129                                                         (String) m.get(varNames[i]));
130                                 }
131                         }
132                         return elements;
133                 }
134
135                 public void dispose() {
136                 }
137
138                 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
139                         if (newInput == null) {
140                                 return;
141                         }
142                         if (viewer instanceof TableViewer) {
143                                 TableViewer tableViewer = (TableViewer) viewer;
144                                 if (tableViewer.getTable().isDisposed()) {
145                                         return;
146                                 }
147                                 tableViewer.setSorter(new ViewerSorter() {
148                                         public int compare(Viewer iviewer, Object e1, Object e2) {
149                                                 if (e1 == null) {
150                                                         return -1;
151                                                 } else if (e2 == null) {
152                                                         return 1;
153                                                 } else {
154                                                         return ((EnvironmentVariable) e1).getName()
155                                                                         .compareToIgnoreCase(
156                                                                                         ((EnvironmentVariable) e2)
157                                                                                                         .getName());
158                                                 }
159                                         }
160                                 });
161                         }
162                 }
163         }
164
165         /**
166          * Label provider for the environment table
167          */
168         public class EnvironmentVariableLabelProvider extends LabelProvider
169                         implements ITableLabelProvider {
170                 public String getColumnText(Object element, int columnIndex) {
171                         String result = null;
172                         if (element != null) {
173                                 EnvironmentVariable var = (EnvironmentVariable) element;
174                                 switch (columnIndex) {
175                                 case 0: // variable
176                                         result = var.getName();
177                                         break;
178                                 case 1: // value
179                                         result = var.getValue();
180                                         break;
181                                 }
182                         }
183                         return result;
184                 }
185
186                 public Image getColumnImage(Object element, int columnIndex) {
187                         return null;
188                 }
189         }
190
191         /*
192          * (non-Javadoc)
193          * 
194          * @see org.eclipse.debug.ui.ILaunchConfigurationTab#createControl(org.eclipse.swt.widgets.Composite)
195          */
196         public void createControl(Composite parent) {
197                 // Create main composite
198                 Composite mainComposite = new Composite(parent, SWT.NONE);
199                 setControl(mainComposite);
200                 // WorkbenchHelp.setHelp(getControl(),
201                 // IDebugHelpContextIds.LAUNCH_CONFIGURATION_DIALOG_ENVIRONMENT_TAB);
202                 GridLayout layout = new GridLayout();
203                 layout.numColumns = 2;
204                 GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
205                 mainComposite.setLayout(layout);
206                 mainComposite.setLayoutData(gridData);
207                 mainComposite.setFont(parent.getFont());
208
209                 createEnvironmentTable(mainComposite);
210                 createTableButtons(mainComposite);
211                 createAppendReplace(mainComposite);
212
213                 Dialog.applyDialogFont(mainComposite);
214         }
215
216         /**
217          * Creates and configures the widgets which allow the user to choose whether
218          * the specified environment should be appended to the native environment or
219          * if it should completely replace it.
220          * 
221          * @param parent
222          *            the composite in which the widgets should be created
223          */
224         protected void createAppendReplace(Composite parent) {
225                 Composite appendReplaceComposite = new Composite(parent, SWT.NONE);
226                 GridData gridData = new GridData();
227                 gridData.horizontalSpan = 2;
228                 GridLayout layout = new GridLayout();
229                 appendReplaceComposite.setLayoutData(gridData);
230                 appendReplaceComposite.setLayout(layout);
231                 appendReplaceComposite.setFont(parent.getFont());
232
233                 appendEnvironment = createRadioButton(appendReplaceComposite,
234                                 "&Append environment to native environment");
235                 appendEnvironment.addSelectionListener(new SelectionAdapter() {
236                         public void widgetSelected(SelectionEvent e) {
237                                 updateLaunchConfigurationDialog();
238                         }
239                 });
240                 replaceEnvironment = createRadioButton(appendReplaceComposite,
241                                 "Re&place native environment with specified environment");
242         }
243
244         /**
245          * Updates the enablement of the append/replace widgets. The widgets should
246          * disable when there are no environment variables specified.
247          */
248         protected void updateAppendReplace() {
249                 boolean enable = environmentTable.getTable().getItemCount() > 0;
250                 appendEnvironment.setEnabled(enable);
251                 replaceEnvironment.setEnabled(enable);
252         }
253
254         /**
255          * Creates and configures the table that displayed the key/value pairs that
256          * comprise the environment.
257          * 
258          * @param parent
259          *            the composite in which the table should be created
260          */
261         protected void createEnvironmentTable(Composite parent) {
262                 Font font = parent.getFont();
263                 // Create table composite
264                 Composite tableComposite = new Composite(parent, SWT.NONE);
265                 GridLayout layout = new GridLayout();
266                 layout.marginHeight = 0;
267                 layout.marginWidth = 0;
268                 layout.numColumns = 1;
269                 GridData gridData = new GridData(GridData.FILL_BOTH);
270                 gridData.heightHint = 150;
271                 tableComposite.setLayout(layout);
272                 tableComposite.setLayoutData(gridData);
273                 tableComposite.setFont(font);
274                 // Create label
275                 Label label = new Label(tableComposite, SWT.NONE);
276                 label.setFont(font);
277                 label.setText("Environment variables to &set");
278                 // Create table
279                 environmentTable = new TableViewer(tableComposite, SWT.BORDER
280                                 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI | SWT.FULL_SELECTION);
281                 Table table = environmentTable.getTable();
282                 TableLayout tableLayout = new TableLayout();
283                 table.setLayout(tableLayout);
284                 table.setHeaderVisible(true);
285                 table.setFont(font);
286                 gridData = new GridData(GridData.FILL_BOTH);
287                 environmentTable.getControl().setLayoutData(gridData);
288                 environmentTable
289                                 .setContentProvider(new EnvironmentVariableContentProvider());
290                 environmentTable
291                                 .setLabelProvider(new EnvironmentVariableLabelProvider());
292                 environmentTable.setColumnProperties(envTableColumnProperties);
293                 environmentTable
294                                 .addSelectionChangedListener(new ISelectionChangedListener() {
295                                         public void selectionChanged(SelectionChangedEvent event) {
296                                                 handleTableSelectionChanged(event);
297                                         }
298                                 });
299                 environmentTable.addDoubleClickListener(new IDoubleClickListener() {
300                         public void doubleClick(DoubleClickEvent event) {
301                                 if (!environmentTable.getSelection().isEmpty()) {
302                                         handleEnvEditButtonSelected();
303                                 }
304                         }
305                 });
306                 // Create columns
307                 for (int i = 0; i < envTableColumnHeaders.length; i++) {
308                         tableLayout.addColumnData(envTableColumnLayouts[i]);
309                         TableColumn tc = new TableColumn(table, SWT.NONE, i);
310                         tc.setResizable(envTableColumnLayouts[i].resizable);
311                         tc.setText(envTableColumnHeaders[i]);
312                 }
313         }
314
315         /**
316          * Responds to a selection changed event in the environment table
317          * 
318          * @param event
319          *            the selection change event
320          */
321         protected void handleTableSelectionChanged(SelectionChangedEvent event) {
322                 int size = ((IStructuredSelection) event.getSelection()).size();
323                 envEditButton.setEnabled(size == 1);
324                 envRemoveButton.setEnabled(size > 0);
325         }
326
327         /**
328          * Creates the add/edit/remove buttons for the environment table
329          * 
330          * @param parent
331          *            the composite in which the buttons should be created
332          */
333         protected void createTableButtons(Composite parent) {
334                 // Create button composite
335                 Composite buttonComposite = new Composite(parent, SWT.NONE);
336                 GridLayout glayout = new GridLayout();
337                 glayout.marginHeight = 0;
338                 glayout.marginWidth = 0;
339                 glayout.numColumns = 1;
340                 GridData gdata = new GridData(GridData.VERTICAL_ALIGN_BEGINNING
341                                 | GridData.HORIZONTAL_ALIGN_END);
342                 buttonComposite.setLayout(glayout);
343                 buttonComposite.setLayoutData(gdata);
344                 buttonComposite.setFont(parent.getFont());
345
346                 createVerticalSpacer(buttonComposite, 1);
347                 // Create buttons
348                 envAddButton = createPushButton(buttonComposite, "New", null);
349                 envAddButton.addSelectionListener(new SelectionAdapter() {
350                         public void widgetSelected(SelectionEvent event) {
351                                 handleEnvAddButtonSelected();
352                         }
353                 });
354                 envAddCGIButton = createPushButton(buttonComposite, "CGIButton", null);
355                 envAddCGIButton.addSelectionListener(new SelectionAdapter() {
356                         public void widgetSelected(SelectionEvent event) {
357                                 handleEnvAddCGIButtonSelected();
358                         }
359                 });
360
361                 envSelectButton = createPushButton(buttonComposite, "Se&lect...", null);
362                 envSelectButton.addSelectionListener(new SelectionAdapter() {
363                         public void widgetSelected(SelectionEvent event) {
364                                 handleEnvSelectButtonSelected();
365                         }
366                 });
367                 envEditButton = createPushButton(buttonComposite, "Edit", null);
368                 envEditButton.addSelectionListener(new SelectionAdapter() {
369                         public void widgetSelected(SelectionEvent event) {
370                                 handleEnvEditButtonSelected();
371                         }
372                 });
373                 envEditButton.setEnabled(false);
374                 envRemoveButton = createPushButton(buttonComposite, "Remove", null);
375                 envRemoveButton.addSelectionListener(new SelectionAdapter() {
376                         public void widgetSelected(SelectionEvent event) {
377                                 handleEnvRemoveButtonSelected();
378                         }
379                 });
380                 envRemoveButton.setEnabled(false);
381         }
382
383         /**
384          * Adds a new environment variable to the table.
385          */
386         protected void handleEnvAddButtonSelected() {
387                 MultipleInputDialog dialog = new MultipleInputDialog(getShell(),
388                                 "New Environment Variable");
389                 dialog.addTextField(NAME_LABEL, null, false);
390                 dialog.addVariablesField(VALUE_LABEL, null, true);
391
392                 if (dialog.open() != Window.OK) {
393                         return;
394                 }
395
396                 String name = dialog.getStringValue(NAME_LABEL);
397                 String value = dialog.getStringValue(VALUE_LABEL);
398
399                 if (name != null && value != null && name.length() > 0
400                                 && value.length() > 0) {
401                         addVariable(new EnvironmentVariable(name.trim(), value.trim()));
402                         updateAppendReplace();
403                 }
404         }
405
406         /**
407          * Attempts to add the given variable. Returns whether the variable was
408          * added or not (as when the user answers not to overwrite an existing
409          * variable).
410          * 
411          * @param variable
412          *            the variable to add
413          * @return whether the variable was added
414          */
415         protected boolean addVariable(EnvironmentVariable variable) {
416                 String name = variable.getName();
417                 TableItem[] items = environmentTable.getTable().getItems();
418                 for (int i = 0; i < items.length; i++) {
419                         EnvironmentVariable existingVariable = (EnvironmentVariable) items[i]
420                                         .getData();
421                         if (existingVariable.getName().equals(name)) {
422                                 boolean overWrite = MessageDialog
423                                                 .openQuestion(
424                                                                 getShell(),
425                                                                 "Overwrite variable?",
426                                                                 MessageFormat
427                                                                                 .format(
428                                                                                                 "A variable named {0} already exists. Overwrite?",
429                                                                                                 new String[] { name }));
430                                 if (!overWrite) {
431                                         return false;
432                                 }
433                                 environmentTable.remove(existingVariable);
434                                 break;
435                         }
436                 }
437                 environmentTable.add(variable);
438                 updateLaunchConfigurationDialog();
439                 return true;
440         }
441
442         /**
443          * Displays a dialog that allows user to select native environment variables
444          * to add to the table.
445          */
446         private void handleEnvSelectButtonSelected() {
447                 // get Environment Variables from the OS
448                 Map envVariables = getNativeEnvironment();
449
450                 // get Environment Variables from the table
451                 TableItem[] items = environmentTable.getTable().getItems();
452                 for (int i = 0; i < items.length; i++) {
453                         EnvironmentVariable var = (EnvironmentVariable) items[i].getData();
454                         envVariables.remove(var.getName());
455                 }
456
457                 ListSelectionDialog dialog = new NativeEnvironmentDialog(getShell(),
458                                 envVariables, createSelectionDialogContentProvider(),
459                                 createSelectionDialogLabelProvider(),
460                                 "Select &environment variables to add:");
461                 dialog.setTitle("Select Environment Variables");
462
463                 int button = dialog.open();
464                 if (button == Window.OK) {
465                         Object[] selected = dialog.getResult();
466                         for (int i = 0; i < selected.length; i++) {
467                                 environmentTable.add(selected[i]);
468                         }
469                 }
470
471                 updateAppendReplace();
472                 updateLaunchConfigurationDialog();
473         }
474
475         /**
476          * Displays a dialog that allows user to select native environment variables
477          * to add to the table.
478          */
479         private void handleEnvAddCGIButtonSelected() {
480
481                 Map envVariables = new HashMap();
482
483                 envVariables.put("HTTP_COOKIE", new EnvironmentVariable("HTTP_COOKIE",
484                                 "TestCookie=1"));
485                 envVariables.put("REDIRECT_QUERY_STRING", new EnvironmentVariable(
486                                 "REDIRECT_QUERY_STRING", ""));
487                 envVariables.put("REDIRECT_STATUS", new EnvironmentVariable(
488                                 "REDIRECT_STATUS", "200"));
489                 envVariables.put("REDIRECT_URL", new EnvironmentVariable(
490                                 "REDIRECT_URL", ""));
491                 envVariables.put("SERVER_SOFTWARE", new EnvironmentVariable(
492                                 "SERVER_SOFTWARE", "DBG / 2.1"));
493                 envVariables.put("SERVER_NAME", new EnvironmentVariable("SERVER_NAME",
494                                 "localhost"));
495                 envVariables.put("SERVER_ADDR", new EnvironmentVariable("SERVER_ADDR",
496                                 "127.0.0.1"));
497                 envVariables.put("SERVER_PORT", new EnvironmentVariable("SERVER_PORT",
498                                 "80"));
499                 envVariables.put("REMOTE_ADDR", new EnvironmentVariable("REMOTE_ADDR",
500                                 "127.0.0.1"));
501                 envVariables.put("GATEWAY_INTERFACE", new EnvironmentVariable(
502                                 "GATEWAY_INTERFACE", "CGI / 1.1"));
503                 envVariables.put("SERVER_PROTOCOL", new EnvironmentVariable(
504                                 "SERVER_PROTOCOL", "HTTP / 1.1"));
505                 envVariables.put("REQUEST_METHOD", new EnvironmentVariable(
506                                 "REQUEST_METHOD", "GET"));
507                 envVariables.put("QUERY_STRING", new EnvironmentVariable(
508                                 "QUERY_STRING", ""));
509                 envVariables.put("REDIRECT_QUERY_STRING", new EnvironmentVariable(
510                                 "REDIRECT_QUERY_STRING", ""));
511                 // envVariables.put("REQUEST_URI" + OSFilePath;
512                 // envVariables.put("PATH_INFO=" + OSFilePath;
513                 // envVariables.put("PATH_TRANSLATED=" + OSFilePath;
514
515                 // get Environment Variables from the table
516                 TableItem[] items = environmentTable.getTable().getItems();
517                 for (int i = 0; i < items.length; i++) {
518                         EnvironmentVariable var = (EnvironmentVariable) items[i].getData();
519                         envVariables.remove(var.getName());
520                 }
521
522                 ListSelectionDialog dialog = new NativeEnvironmentDialog(getShell(),
523                                 envVariables, createSelectionDialogContentProvider(),
524                                 createSelectionDialogLabelProvider(),
525                                 "Select &environment variables to add:");
526                 dialog.setTitle("Select Environment Variables");
527
528                 int button = dialog.open();
529                 if (button == Window.OK) {
530                         Object[] selected = dialog.getResult();
531                         for (int i = 0; i < selected.length; i++) {
532                                 environmentTable.add(selected[i]);
533                         }
534                 }
535
536                 updateAppendReplace();
537                 updateLaunchConfigurationDialog();
538         }
539
540         /**
541          * Creates a label provider for the native native environment variable
542          * selection dialog.
543          * 
544          * @return A label provider for the native native environment variable
545          *         selection dialog.
546          */
547         private ILabelProvider createSelectionDialogLabelProvider() {
548                 return new ILabelProvider() {
549                         public Image getImage(Object element) {
550                                 return null;
551                         }
552
553                         public String getText(Object element) {
554                                 EnvironmentVariable var = (EnvironmentVariable) element;
555                                 return var.getName() + " [" + var.getValue() + "]"; //$NON-NLS-1$ //$NON-NLS-2$
556                         }
557
558                         public void addListener(ILabelProviderListener listener) {
559                         }
560
561                         public void dispose() {
562                         }
563
564                         public boolean isLabelProperty(Object element, String property) {
565                                 return false;
566                         }
567
568                         public void removeListener(ILabelProviderListener listener) {
569                         }
570                 };
571         }
572
573         /**
574          * Creates a content provider for the native native environment variable
575          * selection dialog.
576          * 
577          * @return A content provider for the native native environment variable
578          *         selection dialog.
579          */
580         private IStructuredContentProvider createSelectionDialogContentProvider() {
581                 return new IStructuredContentProvider() {
582                         public Object[] getElements(Object inputElement) {
583                                 EnvironmentVariable[] elements = null;
584                                 if (inputElement instanceof HashMap) {
585                                         Comparator comparator = new Comparator() {
586                                                 public int compare(Object o1, Object o2) {
587                                                         String s1 = (String) o1;
588                                                         String s2 = (String) o2;
589                                                         return s1.compareTo(s2);
590                                                 }
591
592                                         };
593                                         TreeMap envVars = new TreeMap(comparator);
594                                         envVars.putAll((Map) inputElement);
595                                         elements = new EnvironmentVariable[envVars.size()];
596                                         int index = 0;
597                                         for (Iterator iterator = envVars.keySet().iterator(); iterator
598                                                         .hasNext(); index++) {
599                                                 Object key = iterator.next();
600                                                 elements[index] = (EnvironmentVariable) envVars
601                                                                 .get(key);
602                                         }
603                                 }
604                                 return elements;
605                         }
606
607                         public void dispose() {
608                         }
609
610                         public void inputChanged(Viewer viewer, Object oldInput,
611                                         Object newInput) {
612                         }
613                 };
614         }
615
616         /**
617          * Gets native environment variable from the LaunchManager. Creates
618          * EnvironmentVariable objects.
619          * 
620          * @return Map of name - EnvironmentVariable pairs based on native
621          *         environment.
622          */
623         private Map getNativeEnvironment() {
624                 Map stringVars = DebugPlugin.getDefault().getLaunchManager()
625                                 .getNativeEnvironment();
626                 HashMap vars = new HashMap();
627                 for (Iterator i = stringVars.keySet().iterator(); i.hasNext();) {
628                         String key = (String) i.next();
629                         String value = (String) stringVars.get(key);
630                         vars.put(key, new EnvironmentVariable(key, value));
631                 }
632                 return vars;
633         }
634
635         /**
636          * Creates an editor for the value of the selected environment variable.
637          */
638         private void handleEnvEditButtonSelected() {
639                 IStructuredSelection sel = (IStructuredSelection) environmentTable
640                                 .getSelection();
641                 EnvironmentVariable var = (EnvironmentVariable) sel.getFirstElement();
642                 if (var == null) {
643                         return;
644                 }
645                 String originalName = var.getName();
646                 String value = var.getValue();
647                 MultipleInputDialog dialog = new MultipleInputDialog(getShell(),
648                                 "Edit Environment Variable");
649                 dialog.addTextField(NAME_LABEL, originalName, false);
650                 dialog.addVariablesField(VALUE_LABEL, value, true);
651
652                 if (dialog.open() != Window.OK) {
653                         return;
654                 }
655                 String name = dialog.getStringValue(NAME_LABEL);
656                 value = dialog.getStringValue(VALUE_LABEL);
657                 if (!originalName.equals(name)) {
658                         if (addVariable(new EnvironmentVariable(name, value))) {
659                                 environmentTable.remove(var);
660                         }
661                 } else {
662                         var.setValue(value);
663                         environmentTable.update(var, null);
664                         updateLaunchConfigurationDialog();
665                 }
666         }
667
668         /**
669          * Removes the selected environment variable from the table.
670          */
671         private void handleEnvRemoveButtonSelected() {
672                 IStructuredSelection sel = (IStructuredSelection) environmentTable
673                                 .getSelection();
674                 environmentTable.getControl().setRedraw(false);
675                 for (Iterator i = sel.iterator(); i.hasNext();) {
676                         EnvironmentVariable var = (EnvironmentVariable) i.next();
677                         environmentTable.remove(var);
678                 }
679                 environmentTable.getControl().setRedraw(true);
680                 updateAppendReplace();
681                 updateLaunchConfigurationDialog();
682         }
683
684         /**
685          * Updates the environment table for the given launch configuration
686          * 
687          * @param configuration
688          */
689         protected void updateEnvironment(ILaunchConfiguration configuration) {
690                 environmentTable.setInput(configuration);
691         }
692
693         /*
694          * (non-Javadoc)
695          * 
696          * @see org.eclipse.debug.ui.ILaunchConfigurationTab#setDefaults(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
697          */
698         public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
699         }
700
701         /*
702          * (non-Javadoc)
703          * 
704          * @see org.eclipse.debug.ui.ILaunchConfigurationTab#initializeFrom(org.eclipse.debug.core.ILaunchConfiguration)
705          */
706         public void initializeFrom(ILaunchConfiguration configuration) {
707                 boolean append = true;
708                 try {
709                         append = configuration.getAttribute(
710                                         ILaunchManager.ATTR_APPEND_ENVIRONMENT_VARIABLES, true);
711                 } catch (CoreException e) {
712                         XDebugCorePlugin.log(e.getStatus());
713                 }
714                 if (append) {
715                         appendEnvironment.setSelection(true);
716                         replaceEnvironment.setSelection(false);
717                 } else {
718                         replaceEnvironment.setSelection(true);
719                         appendEnvironment.setSelection(false);
720                 }
721                 updateEnvironment(configuration);
722                 updateAppendReplace();
723         }
724
725         /**
726          * Stores the environment in the given configuration
727          * 
728          * @see org.eclipse.debug.ui.ILaunchConfigurationTab#performApply(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
729          */
730         public void performApply(ILaunchConfigurationWorkingCopy configuration) {
731                 // Convert the table's items into a Map so that this can be saved in the
732                 // configuration's attributes.
733                 TableItem[] items = environmentTable.getTable().getItems();
734                 Map map = new HashMap(items.length);
735                 for (int i = 0; i < items.length; i++) {
736                         EnvironmentVariable var = (EnvironmentVariable) items[i].getData();
737                         map.put(var.getName(), var.getValue());
738                 }
739                 if (map.size() == 0) {
740                         configuration.setAttribute(
741                                         ILaunchManager.ATTR_ENVIRONMENT_VARIABLES, (Map) null);
742                 } else {
743                         configuration.setAttribute(
744                                         ILaunchManager.ATTR_ENVIRONMENT_VARIABLES, map);
745                 }
746                 configuration.setAttribute(
747                                 ILaunchManager.ATTR_APPEND_ENVIRONMENT_VARIABLES,
748                                 appendEnvironment.getSelection());
749         }
750
751         /*
752          * (non-Javadoc)
753          * 
754          * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getName()
755          */
756         public String getName() {
757                 return "Environment";
758         }
759
760         /*
761          * (non-Javadoc)
762          * 
763          * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getImage()
764          */
765         public Image getImage() {
766                 return XDebugUIPluginImages
767                                 .get(XDebugUIPluginImages.IMG_EVIEW_ENVIROMENT_TAB);
768         }
769
770         /*
771          * (non-Javadoc)
772          * 
773          * @see org.eclipse.debug.ui.ILaunchConfigurationTab#activated(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
774          */
775         public void activated(ILaunchConfigurationWorkingCopy workingCopy) {
776                 // do nothing when activated
777         }
778
779         /*
780          * (non-Javadoc)
781          * 
782          * @see org.eclipse.debug.ui.ILaunchConfigurationTab#deactivated(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
783          */
784         public void deactivated(ILaunchConfigurationWorkingCopy workingCopy) {
785                 // do nothing when deactivated
786         }
787
788         private class NativeEnvironmentDialog extends ListSelectionDialog {
789                 public NativeEnvironmentDialog(Shell parentShell, Object input,
790                                 IStructuredContentProvider contentProvider,
791                                 ILabelProvider labelProvider, String message) {
792                         super(parentShell, input, contentProvider, labelProvider, message);
793                         setShellStyle(getShellStyle() | SWT.RESIZE);
794                 }
795
796                 protected IDialogSettings getDialogSettings() {
797                         IDialogSettings settings = XDebugCorePlugin.getDefault()
798                                         .getDialogSettings();
799                         IDialogSettings section = settings
800                                         .getSection(getDialogSettingsSectionName());
801                         if (section == null) {
802                                 section = settings
803                                                 .addNewSection(getDialogSettingsSectionName());
804                         }
805                         return section;
806                 }
807
808                 /**
809                  * Returns the name of the section that this dialog stores its settings
810                  * in
811                  * 
812                  * @return String
813                  */
814                 protected String getDialogSettingsSectionName() {
815                         return IDebugUIConstants.PLUGIN_ID
816                                         + ".ENVIRONMENT_TAB.NATIVE_ENVIROMENT_DIALOG"; //$NON-NLS-1$
817                 }
818
819                 // /* (non-Javadoc)
820                 // * @see
821                 // org.eclipse.jface.window.Window#getInitialLocation(org.eclipse.swt.graphics.Point)
822                 // */
823                 // protected Point getInitialLocation(Point initialSize) {
824                 // Point initialLocation=
825                 // DialogSettingsHelper.getInitialLocation(getDialogSettingsSectionName());
826                 // if (initialLocation != null) {
827                 // return initialLocation;
828                 // }
829                 // return super.getInitialLocation(initialSize);
830                 // }
831
832                 // /* (non-Javadoc)
833                 // * @see org.eclipse.jface.window.Window#getInitialSize()
834                 // */
835                 // protected Point getInitialSize() {
836                 // Point size = super.getInitialSize();
837                 // return
838                 // DialogSettingsHelper.getInitialSize(getDialogSettingsSectionName(),
839                 // size);
840                 // }
841
842                 // /* (non-Javadoc)
843                 // * @see org.eclipse.jface.window.Window#close()
844                 // */
845                 // public boolean close() {
846                 // DialogSettingsHelper.persistShellGeometry(getShell(),
847                 // getDialogSettingsSectionName());
848                 // return super.close();
849                 // }
850         }
851 }