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