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