Importing the XDebugProxy code in the HEAD. The repo was tagged with T_BEFORE_XDEBUGP...
[phpeclipse.git] / net.sourceforge.phpeclipse.xdebug.ui / src / net / sourceforge / phpeclipse / xdebug / ui / php / launching / 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.openQuestion(getShell(),"Overwrite variable?",
423                                         MessageFormat.format("A variable named {0} already exists. Overwrite?",new String[] { name }));
424                                 if (!overWrite) {
425                                         return false;
426                                 }
427                                 environmentTable.remove(existingVariable);
428                                 break;
429                         }
430                 }
431                 environmentTable.add(variable);
432                 updateLaunchConfigurationDialog();
433                 return true;
434         }
435
436         /**
437          * Displays a dialog that allows user to select native environment variables
438          * to add to the table.
439          */
440         private void handleEnvSelectButtonSelected() {
441                 // get Environment Variables from the OS
442                 Map envVariables = getNativeEnvironment();
443
444                 // get Environment Variables from the table
445                 TableItem[] items = environmentTable.getTable().getItems();
446                 for (int i = 0; i < items.length; i++) {
447                         EnvironmentVariable var = (EnvironmentVariable) items[i].getData();
448                         envVariables.remove(var.getName());
449                 }
450
451                 ListSelectionDialog dialog = new NativeEnvironmentDialog(getShell(),
452                                 envVariables, createSelectionDialogContentProvider(),
453                                 createSelectionDialogLabelProvider(),
454                                 "Select &environment variables to add:");
455                 dialog.setTitle("Select Environment Variables");
456
457                 int button = dialog.open();
458                 if (button == Window.OK) {
459                         Object[] selected = dialog.getResult();
460                         for (int i = 0; i < selected.length; i++) {
461                                 environmentTable.add(selected[i]);
462                         }
463                 }
464
465                 updateAppendReplace();
466                 updateLaunchConfigurationDialog();
467         }
468
469         /**
470          * Displays a dialog that allows user to select native environment variables
471          * to add to the table.
472          */
473         private void handleEnvAddCGIButtonSelected() {
474
475                 Map envVariables = new HashMap();
476
477                 envVariables.put("HTTP_COOKIE", new EnvironmentVariable("HTTP_COOKIE",
478                                 "TestCookie=1"));
479                 envVariables.put("REDIRECT_QUERY_STRING", new EnvironmentVariable(
480                                 "REDIRECT_QUERY_STRING", ""));
481                 envVariables.put("REDIRECT_STATUS", new EnvironmentVariable(
482                                 "REDIRECT_STATUS", "200"));
483                 envVariables.put("REDIRECT_URL", new EnvironmentVariable(
484                                 "REDIRECT_URL", ""));
485                 envVariables.put("SERVER_SOFTWARE", new EnvironmentVariable(
486                                 "SERVER_SOFTWARE", "DBG / 2.1"));
487                 envVariables.put("SERVER_NAME", new EnvironmentVariable("SERVER_NAME",
488                                 "localhost"));
489                 envVariables.put("SERVER_ADDR", new EnvironmentVariable("SERVER_ADDR",
490                                 "127.0.0.1"));
491                 envVariables.put("SERVER_PORT", new EnvironmentVariable("SERVER_PORT",
492                                 "80"));
493                 envVariables.put("REMOTE_ADDR", new EnvironmentVariable("REMOTE_ADDR",
494                                 "127.0.0.1"));
495                 envVariables.put("GATEWAY_INTERFACE", new EnvironmentVariable(
496                                 "GATEWAY_INTERFACE", "CGI / 1.1"));
497                 envVariables.put("SERVER_PROTOCOL", new EnvironmentVariable(
498                                 "SERVER_PROTOCOL", "HTTP / 1.1"));
499                 envVariables.put("REQUEST_METHOD", new EnvironmentVariable(
500                                 "REQUEST_METHOD", "GET"));
501                 envVariables.put("QUERY_STRING", new EnvironmentVariable(
502                                 "QUERY_STRING", ""));
503                 envVariables.put("REDIRECT_QUERY_STRING", new EnvironmentVariable(
504                                 "REDIRECT_QUERY_STRING", ""));
505                 // envVariables.put("REQUEST_URI" + OSFilePath;
506                 // envVariables.put("PATH_INFO=" + OSFilePath;
507                 // envVariables.put("PATH_TRANSLATED=" + OSFilePath;
508
509                 // get Environment Variables from the table
510                 TableItem[] items = environmentTable.getTable().getItems();
511                 for (int i = 0; i < items.length; i++) {
512                         EnvironmentVariable var = (EnvironmentVariable) items[i].getData();
513                         envVariables.remove(var.getName());
514                 }
515
516                 ListSelectionDialog dialog = new NativeEnvironmentDialog(getShell(),
517                                 envVariables, createSelectionDialogContentProvider(),
518                                 createSelectionDialogLabelProvider(),
519                                 "Select &environment variables to add:");
520                 dialog.setTitle("Select Environment Variables");
521
522                 int button = dialog.open();
523                 if (button == Window.OK) {
524                         Object[] selected = dialog.getResult();
525                         for (int i = 0; i < selected.length; i++) {
526                                 environmentTable.add(selected[i]);
527                         }
528                 }
529
530                 updateAppendReplace();
531                 updateLaunchConfigurationDialog();
532         }
533
534         /**
535          * Creates a label provider for the native native environment variable
536          * selection dialog.
537          * 
538          * @return A label provider for the native native environment variable
539          *         selection dialog.
540          */
541         private ILabelProvider createSelectionDialogLabelProvider() {
542                 return new ILabelProvider() {
543                         public Image getImage(Object element) {
544                                 return null;
545                         }
546
547                         public String getText(Object element) {
548                                 EnvironmentVariable var = (EnvironmentVariable) element;
549                                 return var.getName() + " [" + var.getValue() + "]"; //$NON-NLS-1$ //$NON-NLS-2$
550                         }
551
552                         public void addListener(ILabelProviderListener listener) {
553                         }
554
555                         public void dispose() {
556                         }
557
558                         public boolean isLabelProperty(Object element, String property) {
559                                 return false;
560                         }
561
562                         public void removeListener(ILabelProviderListener listener) {
563                         }
564                 };
565         }
566
567         /**
568          * Creates a content provider for the native native environment variable
569          * selection dialog.
570          * 
571          * @return A content provider for the native native environment variable
572          *         selection dialog.
573          */
574         private IStructuredContentProvider createSelectionDialogContentProvider() {
575                 return new IStructuredContentProvider() {
576                         public Object[] getElements(Object inputElement) {
577                                 EnvironmentVariable[] elements = null;
578                                 if (inputElement instanceof HashMap) {
579                                         Comparator comparator = new Comparator() {
580                                                 public int compare(Object o1, Object o2) {
581                                                         String s1 = (String) o1;
582                                                         String s2 = (String) o2;
583                                                         return s1.compareTo(s2);
584                                                 }
585
586                                         };
587                                         TreeMap envVars = new TreeMap(comparator);
588                                         envVars.putAll((Map) inputElement);
589                                         elements = new EnvironmentVariable[envVars.size()];
590                                         int index = 0;
591                                         for (Iterator iterator = envVars.keySet().iterator(); iterator
592                                                         .hasNext(); index++) {
593                                                 Object key = iterator.next();
594                                                 elements[index] = (EnvironmentVariable) envVars
595                                                                 .get(key);
596                                         }
597                                 }
598                                 return elements;
599                         }
600
601                         public void dispose() {
602                         }
603
604                         public void inputChanged(Viewer viewer, Object oldInput,
605                                         Object newInput) {
606                         }
607                 };
608         }
609
610         /**
611          * Gets native environment variable from the LaunchManager. Creates
612          * EnvironmentVariable objects.
613          * 
614          * @return Map of name - EnvironmentVariable pairs based on native
615          *         environment.
616          */
617         private Map getNativeEnvironment() {
618                 Map stringVars = DebugPlugin.getDefault().getLaunchManager()
619                                 .getNativeEnvironment();
620                 HashMap vars = new HashMap();
621                 for (Iterator i = stringVars.keySet().iterator(); i.hasNext();) {
622                         String key = (String) i.next();
623                         String value = (String) stringVars.get(key);
624                         vars.put(key, new EnvironmentVariable(key, value));
625                 }
626                 return vars;
627         }
628
629         /**
630          * Creates an editor for the value of the selected environment variable.
631          */
632         private void handleEnvEditButtonSelected() {
633                 IStructuredSelection sel = (IStructuredSelection) environmentTable
634                                 .getSelection();
635                 EnvironmentVariable var = (EnvironmentVariable) sel.getFirstElement();
636                 if (var == null) {
637                         return;
638                 }
639                 String originalName = var.getName();
640                 String value = var.getValue();
641                 MultipleInputDialog dialog = new MultipleInputDialog(getShell(),
642                                 "Edit Environment Variable");
643                 dialog.addTextField(NAME_LABEL, originalName, false);
644                 dialog.addVariablesField(VALUE_LABEL, value, true);
645
646                 if (dialog.open() != Window.OK) {
647                         return;
648                 }
649                 String name = dialog.getStringValue(NAME_LABEL);
650                 value = dialog.getStringValue(VALUE_LABEL);
651                 if (!originalName.equals(name)) {
652                         if (addVariable(new EnvironmentVariable(name, value))) {
653                                 environmentTable.remove(var);
654                         }
655                 } else {
656                         var.setValue(value);
657                         environmentTable.update(var, null);
658                         updateLaunchConfigurationDialog();
659                 }
660         }
661
662         /**
663          * Removes the selected environment variable from the table.
664          */
665         private void handleEnvRemoveButtonSelected() {
666                 IStructuredSelection sel = (IStructuredSelection) environmentTable
667                                 .getSelection();
668                 environmentTable.getControl().setRedraw(false);
669                 for (Iterator i = sel.iterator(); i.hasNext();) {
670                         EnvironmentVariable var = (EnvironmentVariable) i.next();
671                         environmentTable.remove(var);
672                 }
673                 environmentTable.getControl().setRedraw(true);
674                 updateAppendReplace();
675                 updateLaunchConfigurationDialog();
676         }
677
678         /**
679          * Updates the environment table for the given launch configuration
680          * 
681          * @param configuration
682          */
683         protected void updateEnvironment(ILaunchConfiguration configuration) {
684                 environmentTable.setInput(configuration);
685         }
686
687         /*
688          * (non-Javadoc)
689          * 
690          * @see org.eclipse.debug.ui.ILaunchConfigurationTab#setDefaults(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
691          */
692         public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
693         }
694
695         /*
696          * (non-Javadoc)
697          * 
698          * @see org.eclipse.debug.ui.ILaunchConfigurationTab#initializeFrom(org.eclipse.debug.core.ILaunchConfiguration)
699          */
700         public void initializeFrom(ILaunchConfiguration configuration) {
701                 boolean append = true;
702                 try {
703                         append = configuration.getAttribute(
704                                         ILaunchManager.ATTR_APPEND_ENVIRONMENT_VARIABLES, true);
705                 } catch (CoreException e) {
706                         XDebugCorePlugin.log(e.getStatus());
707                 }
708                 if (append) {
709                         appendEnvironment.setSelection(true);
710                         replaceEnvironment.setSelection(false);
711                 } else {
712                         replaceEnvironment.setSelection(true);
713                         appendEnvironment.setSelection(false);
714                 }
715                 updateEnvironment(configuration);
716                 updateAppendReplace();
717         }
718
719         /**
720          * Stores the environment in the given configuration
721          * 
722          * @see org.eclipse.debug.ui.ILaunchConfigurationTab#performApply(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
723          */
724         public void performApply(ILaunchConfigurationWorkingCopy configuration) {
725                 // Convert the table's items into a Map so that this can be saved in the
726                 // configuration's attributes.
727                 TableItem[] items = environmentTable.getTable().getItems();
728                 Map map = new HashMap(items.length);
729                 for (int i = 0; i < items.length; i++) {
730                         EnvironmentVariable var = (EnvironmentVariable) items[i].getData();
731                         map.put(var.getName(), var.getValue());
732                 }
733                 if (map.size() == 0) {
734                         configuration.setAttribute(
735                                         ILaunchManager.ATTR_ENVIRONMENT_VARIABLES, (Map) null);
736                 } else {
737                         configuration.setAttribute(
738                                         ILaunchManager.ATTR_ENVIRONMENT_VARIABLES, map);
739                 }
740                 configuration.setAttribute(
741                                 ILaunchManager.ATTR_APPEND_ENVIRONMENT_VARIABLES,
742                                 appendEnvironment.getSelection());
743         }
744
745         /*
746          * (non-Javadoc)
747          * 
748          * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getName()
749          */
750         public String getName() {
751                 return "Environment";
752         }
753
754         /*
755          * (non-Javadoc)
756          * 
757          * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getImage()
758          */
759         public Image getImage() {       
760                 return XDebugUIPluginImages.get(XDebugUIPluginImages.IMG_EVIEW_ENVIROMENT_TAB);
761         }
762
763         /*
764          * (non-Javadoc)
765          * 
766          * @see org.eclipse.debug.ui.ILaunchConfigurationTab#activated(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
767          */
768         public void activated(ILaunchConfigurationWorkingCopy workingCopy) {
769                 // do nothing when activated
770         }
771
772         /*
773          * (non-Javadoc)
774          * 
775          * @see org.eclipse.debug.ui.ILaunchConfigurationTab#deactivated(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
776          */
777         public void deactivated(ILaunchConfigurationWorkingCopy workingCopy) {
778                 // do nothing when deactivated
779         }
780
781         private class NativeEnvironmentDialog extends ListSelectionDialog {
782                 public NativeEnvironmentDialog(Shell parentShell, Object input,
783                                 IStructuredContentProvider contentProvider,
784                                 ILabelProvider labelProvider, String message) {
785                         super(parentShell, input, contentProvider, labelProvider, message);
786                         setShellStyle(getShellStyle() | SWT.RESIZE);
787                 }
788
789                 protected IDialogSettings getDialogSettings() {
790 //                      IDialogSettings settings = XDebugCorePlugin.getDefault().getDialogSettings();
791                         IDialogSettings settings = getDialogSettings();
792                         IDialogSettings section = settings
793                                         .getSection(getDialogSettingsSectionName());
794                         if (section == null) {
795                                 section = settings
796                                                 .addNewSection(getDialogSettingsSectionName());
797                         }
798                         return section;
799                 }
800
801                 /**
802                  * Returns the name of the section that this dialog stores its settings
803                  * in
804                  * 
805                  * @return String
806                  */
807                 protected String getDialogSettingsSectionName() {
808                         return IDebugUIConstants.PLUGIN_ID
809                                         + ".ENVIRONMENT_TAB.NATIVE_ENVIROMENT_DIALOG"; //$NON-NLS-1$
810                 }
811
812                 // /* (non-Javadoc)
813                 // * @see
814                 // org.eclipse.jface.window.Window#getInitialLocation(org.eclipse.swt.graphics.Point)
815                 // */
816                 // protected Point getInitialLocation(Point initialSize) {
817                 // Point initialLocation=
818                 // DialogSettingsHelper.getInitialLocation(getDialogSettingsSectionName());
819                 // if (initialLocation != null) {
820                 // return initialLocation;
821                 // }
822                 // return super.getInitialLocation(initialSize);
823                 // }
824
825                 // /* (non-Javadoc)
826                 // * @see org.eclipse.jface.window.Window#getInitialSize()
827                 // */
828                 // protected Point getInitialSize() {
829                 //              Point size = super.getInitialSize();
830                 //              return DialogSettingsHelper.getInitialSize(getDialogSettingsSectionName(), size);
831                 //              }
832
833                 //              /* (non-Javadoc)
834                 //              * @see org.eclipse.jface.window.Window#close()
835                 //              */
836                 //              public boolean close() {
837                 //              DialogSettingsHelper.persistShellGeometry(getShell(), getDialogSettingsSectionName());
838                 //              return super.close();
839                 //              }
840         }
841 }