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