Skip to main content
summaryrefslogtreecommitdiffstats
blob: d1139800da35d02a802c05bda220a498590d46d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package org.eclipse.debug.ui;
/*******************************************************************************
 * Copyright (c) 2000, 2003 Keith Seitz and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     Keith Seitz (keiths@redhat.com) - initial implementation
 *     IBM Corporation - integration and code cleanup
 *******************************************************************************/

import java.util.HashMap;
import java.util.Map;

import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.jface.viewers.ColumnLayoutData;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

/**
 * Launch configuration tab for configuring the environment passed
 * into Runtime.exec(...) when a config is launched.
 */
public class EnvironmentTab extends AbstractLaunchConfigurationTab {

	private TableViewer environmentTable;
	private String[] envTableColumnHeaders =
	{
		"Variable",
		"Value",
	};
	private ColumnLayoutData[] envTableColumnLayouts =
	{
		new ColumnWeightData(50),
		new ColumnWeightData(50)
	};
	private static final String P_VARIABLE = "variable"; //$NON-NLS-1$
	private static final String P_VALUE = "value"; //$NON-NLS-1$
	private static String[] envTableColumnProperties =
	{
		P_VARIABLE,
		P_VALUE
	};
	private Button envAddButton;
	private Button envEditButton;
	private Button envRemoveButton;

	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#createControl(org.eclipse.swt.widgets.Composite)
	 */
	public void createControl(Composite parent) {
		// Create main composite
		Composite mainComposite = new Composite(parent, SWT.NONE);
		setControl(mainComposite);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		mainComposite.setLayout(layout);
		mainComposite.setLayoutData(gridData);
		mainComposite.setFont(parent.getFont());
		// Create table composite
		Composite tableComposite = new Composite(mainComposite, SWT.NONE);
		GridLayout glayout = new GridLayout();
		glayout.marginHeight = 0;
		glayout.marginWidth = 0;
		glayout.numColumns = 1;
		GridData gdata = new GridData(GridData.FILL_HORIZONTAL);
		gdata.heightHint = 150;
		tableComposite.setLayout(glayout);
		tableComposite.setLayoutData(gdata);
		// Create label
		Label label = new Label(tableComposite, SWT.NONE);
		label.setText("Environment");
		// Create table
		environmentTable = new TableViewer(tableComposite);
		Table table = environmentTable.getTable();
		TableLayout tableLayout = new TableLayout();
		table.setLayout(tableLayout);
		table.setHeaderVisible(true);
		gdata = new GridData(GridData.FILL_BOTH);
		environmentTable.getControl().setLayoutData(gdata);
		environmentTable.setContentProvider(new EnvironmentVariableContentProvider());
		environmentTable.setLabelProvider(new EnvironmentVariableLabelProvider());
		environmentTable.setColumnProperties(envTableColumnProperties);
		environmentTable.addSelectionChangedListener(new ISelectionChangedListener()
		{
			public void selectionChanged(SelectionChangedEvent event)
			{
				boolean enabled = !(environmentTable.getSelection().isEmpty());
				envEditButton.setEnabled(enabled);
				envRemoveButton.setEnabled(enabled);
			}
		});
		// Create columns
		for (int i = 0; i < envTableColumnHeaders.length; i++)
		{
			tableLayout.addColumnData(envTableColumnLayouts[i]);
			TableColumn tc = new TableColumn(table, SWT.NONE, i);
			tc.setResizable(envTableColumnLayouts[i].resizable);
			tc.setText(envTableColumnHeaders[i]);
		}
		// Cell Editors
		TextCellEditor[] cellEditors = new TextCellEditor[envTableColumnHeaders.length];
		cellEditors[0] = new TextCellEditor(table);
		cellEditors[1] = new TextCellEditor(table);
		environmentTable.setCellEditors(cellEditors);
		environmentTable.setCellModifier(new ICellModifier()
		{
			public boolean canModify(Object element, String property) { return true; }
			public Object getValue(Object element, String property)
			{
				String result = null;
				EnvironmentVariable var = (EnvironmentVariable) element;
				if (property.equals(P_VARIABLE))
					result = var.getName();
				else if (property.equals(P_VALUE))
					result = var.getValue();
				return result;
			}
			public void modify(Object element, String property, Object value)
			{
				TableItem ti = (TableItem) element;
				EnvironmentVariable var = (EnvironmentVariable) ti.getData();
				if (property.equals(P_VARIABLE))
					var.setName((String) value);
				else if (property.equals(P_VALUE))
					var.setValue((String) value);
				else return;
				// update viewer's display and update the dialog
				String properties[] = new String[1];
				properties[0] = property;
				environmentTable.update(var, properties);
				updateLaunchConfigurationDialog();
			}
		});
		// Create button composite
		Composite buttonComposite = new Composite(mainComposite, SWT.NONE);
		glayout = new GridLayout();
		glayout.marginHeight = 0;
		glayout.marginWidth = 0;
		glayout.numColumns = 1;
		gdata = new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.HORIZONTAL_ALIGN_END);
		buttonComposite.setLayout(glayout);
		buttonComposite.setLayoutData(gdata);

		createVerticalSpacer(buttonComposite, 1);
		// Create buttons
		envAddButton = createPushButton(buttonComposite, "New", null);
		envAddButton.addSelectionListener(new SelectionAdapter()
		{
			public void widgetSelected(SelectionEvent event)
			{
				handleEnvAddButtonSelected();
			}
		});
		envEditButton = createPushButton(buttonComposite, "Edit", null);
		envEditButton.addSelectionListener(new SelectionAdapter()
		{
			public void widgetSelected(SelectionEvent event)
			{
				handleEnvEditButtonSelected();
			}
		});
		envEditButton.setEnabled(false);
		envRemoveButton = createPushButton(buttonComposite, "Remove", null);
		envRemoveButton.addSelectionListener(new SelectionAdapter()
		{
			public void widgetSelected(SelectionEvent event)
			{
				handleEnvRemoveButtonSelected();
			}
		});
		envRemoveButton.setEnabled(false);
	}
	
	/**
	 * Method handleEnvAddButtonSelected.
	 */
	private void handleEnvAddButtonSelected() {
		String name = new String("variable");
		String value = new String("value");
		EnvironmentVariable var = new EnvironmentVariable(name, value);
		environmentTable.add(var);
		environmentTable.editElement(var, 0);
	}

	/**
	 * Method handleEnvEditButtonSelected.
	 */
	private void handleEnvEditButtonSelected() {
		IStructuredSelection sel =
			(IStructuredSelection) environmentTable.getSelection();
		EnvironmentVariable var =
			(EnvironmentVariable) sel.getFirstElement();
		environmentTable.editElement(var, 1);
	}

	/**
	 * Method handleEnvRemoveButtonSelected.
	 */
	private void handleEnvRemoveButtonSelected() {
		IStructuredSelection sel =
			(IStructuredSelection) environmentTable.getSelection();
		EnvironmentVariable var =
			(EnvironmentVariable) sel.getFirstElement();
		environmentTable.remove(var);
	}

	/**
	 * Method updateEnvironment.
	 * @param configuration
	 */
	private void updateEnvironment(ILaunchConfiguration configuration) {
		environmentTable.setInput(configuration);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#setDefaults(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
	 */
	public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
		
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#initializeFrom(org.eclipse.debug.core.ILaunchConfiguration)
	 */
	public void initializeFrom(ILaunchConfiguration configuration) {
		updateEnvironment(configuration);
	}

	/**
	 * Stores the environment in the given configuration
	 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#performApply(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
	 */
	public void performApply(ILaunchConfigurationWorkingCopy configuration) {	
		// Convert the table's items into a Map so that this can be saved in the
		// configuration's attributes.
		HashMap map = new HashMap();
		TableItem[] items = environmentTable.getTable().getItems();
		for (int i = 0; i < items.length; i++)
		{
			EnvironmentVariable var = (EnvironmentVariable) items[i].getData();
			map.put(var.getName(), var.getValue());
		} 
		if (map.size() == 0) {
			configuration.setAttribute(IDebugUIConstants.ATTR_ENVIRONMENT_VARIABLES, (Map) null);
		} else {
			configuration.setAttribute(IDebugUIConstants.ATTR_ENVIRONMENT_VARIABLES, map);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getName()
	 */
	public String getName() {
		return "Environment";
	}

}

Back to the top