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