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