Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7ab157eec0eb65f9644f1907e2bfb2bb8c99ec3f (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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*******************************************************************************
 * Copyright (c) 2007 Intel Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Intel Corporation - initial API and implementation
 *     IBM Corporation
 *******************************************************************************/
package org.eclipse.cdt.ui.newui;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.IColorProvider;
import org.eclipse.jface.viewers.IFontProvider;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableFontProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.window.Window;
import org.eclipse.osgi.util.TextProcessor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
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.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.dialogs.ListSelectionDialog;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.envvar.IContributedEnvironment;
import org.eclipse.cdt.core.envvar.IEnvironmentVariable;
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
import org.eclipse.cdt.core.settings.model.ICResourceDescription;
import org.eclipse.cdt.utils.envvar.StorableEnvironment;
import org.eclipse.cdt.utils.spawner.EnvironmentReader;

import org.eclipse.cdt.internal.core.envvar.EnvironmentVariableManager;
import org.eclipse.cdt.internal.core.envvar.UserDefinedEnvironmentSupplier;


public class EnvironmentTab extends AbstractCPropertyTab {
	private static final String PREFIX = "EnvironmentBlock";	//$NON-NLS-1$
	private static final String LABEL = PREFIX + ".label";	//$NON-NLS-1$
	private static final String VALUE_UNDEF = LABEL + ".value.undef";	//$NON-NLS-1$
	private static final String SEMI = ";"; //$NON-NLS-1$
	private static final String LBR = " ["; //$NON-NLS-1$
	private static final String RBR = "]"; //$NON-NLS-1$
	private static final UserDefinedEnvironmentSupplier fUserSupplier = EnvironmentVariableManager.fUserSupplier;
	

	private Table table;
	private TableViewer tv;
	private ArrayList data = new ArrayList();
	private Button b1, b2;
	
	private static final IContributedEnvironment ce = CCorePlugin.getDefault().getBuildEnvironmentManager().getContributedEnvironment();
	private ICConfigurationDescription cfgd = null;
	private StorableEnvironment vars = null;

	private class TabData {
		IEnvironmentVariable var;
		boolean changed;
		TabData(IEnvironmentVariable _var, boolean _changed) {
			var = _var;
			changed = _changed;
		}
	}
	
	private class EnvironmentLabelProvider extends LabelProvider implements ITableLabelProvider, IFontProvider , ITableFontProvider, IColorProvider{
		public EnvironmentLabelProvider(boolean user){
		}
		public Image getImage(Object element) {
			return null; // JavaPluginImages.get(JavaPluginImages.IMG_OBJS_REFACTORING_INFO);
		}
		public String getText(Object element) {
			return getColumnText(element, 0);
		}
		public Image getColumnImage(Object element, int columnIndex) {
			return null;
		}
		public String getColumnText(Object element, int columnIndex) {
			TabData td = (TabData)element;
			switch(columnIndex){
			case 0:
				return td.var.getName();
			case 1:
				if(td.var.getOperation() == IEnvironmentVariable.ENVVAR_REMOVE)
					return UIMessages.getString(VALUE_UNDEF);
				return td.var.getValue();
			}
			return EMPTY_STR;
		}

		public Font getFont(Object element) {
			return getFont(element, 0);
		}

		public Font getFont(Object element, int columnIndex) {
			TabData td = (TabData)element;
			Font f = null;
			if (cfgd == null || ce.isUserVariable(cfgd, td.var))
				f = JFaceResources.getFontRegistry().getBold(JFaceResources.DIALOG_FONT);

			switch(columnIndex){
			case 0:
				break;
			case 1:
				if(td.var.getOperation() == IEnvironmentVariable.ENVVAR_REMOVE)
					f = JFaceResources.getFontRegistry().getItalic(JFaceResources.DIALOG_FONT);
				break;
			}
			return f;
		}
		
	    /* (non-Javadoc)
	     * @see org.eclipse.jface.viewers.IColorProvider#getForeground(java.lang.Object)
	     */
	    public Color getForeground(Object element){
			return null;
	    }
		public Color getBackground(Object element){
			return null;
	    }
	}
	
	public void createControls(Composite parent) {
		super.createControls(parent);
		usercomp.setLayout(new GridLayout(1, false));
		Label l1 = new Label(usercomp, SWT.LEFT);
		l1.setText(UIMessages.getString("EnvironmentTab.0")); //$NON-NLS-1$
		l1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		table = new Table(usercomp, SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.H_SCROLL | SWT.FULL_SELECTION);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		table.addSelectionListener(new SelectionListener() {
			public void widgetSelected(SelectionEvent e) {
				updateButtons();
			}
			public void widgetDefaultSelected(SelectionEvent e) {
	    		if (buttonIsEnabled(2) && table.getSelectionIndex() != -1)
    				buttonPressed(2);
			}});
		
		tv = new TableViewer(table);
		tv.setContentProvider(new IStructuredContentProvider() {

			public Object[] getElements(Object inputElement) {
				if (inputElement != null && inputElement instanceof ArrayList) {
					ArrayList ar = (ArrayList)inputElement;
					return ar.toArray(new TabData[0]);
				}
				return null;
			}
			public void dispose() {}
			public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
			});
		tv.setLabelProvider(new EnvironmentLabelProvider(true));
		// add headers
		TableColumn tc = new TableColumn(table, SWT.LEFT);
		tc.setText(UIMessages.getString("EnvironmentTab.1")); //$NON-NLS-1$
		tc.setWidth(200);
		tc = new TableColumn(table, SWT.LEFT);
		tc.setText(UIMessages.getString("EnvironmentTab.2")); //$NON-NLS-1$
		tc.setWidth(200);
		                      
	    table.setLayoutData(new GridData(GridData.FILL_BOTH));
	    
	    b1 = new Button(usercomp, SWT.RADIO);
	    b1.setText(UIMessages.getString("EnvironmentTab.3")); //$NON-NLS-1$
	    b1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
	    b1.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (cfgd != null)
					ce.setAppendEnvironment(true, cfgd);
				else
					vars.setAppendContributedEnvironment(true);
				updateData();
			}});

	    b2 = new Button(usercomp, SWT.RADIO);
	    b2.setText(UIMessages.getString("EnvironmentTab.4")); //$NON-NLS-1$
	    b2.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
	    b2.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (cfgd != null)
					ce.setAppendEnvironment(false, cfgd);
				else
					vars.setAppendContributedEnvironment(false);
				updateData();
			}});
	    
	    initButtons(new String[] {UIMessages.getString("EnvironmentTab.5"),UIMessages.getString("EnvironmentTab.6"),UIMessages.getString("EnvironmentTab.7"),UIMessages.getString("EnvironmentTab.8"),UIMessages.getString("EnvironmentTab.9")}); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
	}

	public void buttonPressed(int i) {
		IEnvironmentVariable var = null;
		EnvDialog dlg;
		int n = table.getSelectionIndex();
		int[] idx;
		switch (i) {
		case 0:
			dlg = new EnvDialog(usercomp.getShell(), var, UIMessages.getString("EnvironmentTab.10"), true, cfgd); //$NON-NLS-1$
			if (dlg.open() == Window.OK) {
				if (dlg.t1.trim().length() > 0) {
					ICConfigurationDescription[] cfgs;
					if (dlg.toAll)
						cfgs = page.getCfgsEditable();
					else 
						cfgs = new ICConfigurationDescription[] {cfgd};
					if (cfgd == null)
						vars.createVariable(dlg.t1.trim(), dlg.t2.trim(), 
								IEnvironmentVariable.ENVVAR_APPEND,	SEMI);
					else	
						for (int x=0; x<cfgs.length; x++) { 
							ce.addVariable(dlg.t1.trim(), dlg.t2.trim(), 
							IEnvironmentVariable.ENVVAR_APPEND, 
							SEMI, cfgs[x]);
					}
					updateData();
				}
			}
			break;
		case 1: // select
			handleEnvSelectButtonSelected();
			updateData();
			break;
		case 2: // edit
			if (n == -1) return;
			var = ((TabData)tv.getElementAt(n)).var;
			dlg = new EnvDialog(usercomp.getShell(), var, UIMessages.getString("EnvironmentTab.11"), false, cfgd); //$NON-NLS-1$
			if (dlg.open() == Window.OK) {
				if (cfgd != null)
					ce.addVariable(	dlg.t1.trim(), dlg.t2.trim(), 
						IEnvironmentVariable.ENVVAR_REPLACE, 
						var.getDelimiter(), cfgd);
				else
					vars.createVariable(dlg.t1.trim(), dlg.t2.trim(),
						IEnvironmentVariable.ENVVAR_REPLACE, var.getDelimiter());
				updateData();
			}
			break;
		case 3: // remove
			if (n == -1) return;
			idx = table.getSelectionIndices();
			for (int j=0; j<idx.length; j++) {
				var = ((TabData)tv.getElementAt(idx[j])).var;
				if (cfgd == null) 
					vars.deleteVariable(var.getName());
				else
					ce.removeVariable(var.getName(), cfgd);
			}
			updateData();
			break;
		case 4: // Undefine
			if (n == -1) return;
			idx = table.getSelectionIndices();
			for (int j=0; j<idx.length; j++) {
				var = ((TabData)tv.getElementAt(idx[j])).var;
				if (cfgd == null)
					vars.createVariable(
							var.getName(), 
							null, 
							IEnvironmentVariable.ENVVAR_REMOVE, 
							var.getDelimiter());
				else 
					ce.addVariable(
						var.getName(), 
						null, 
						IEnvironmentVariable.ENVVAR_REMOVE, 
						var.getDelimiter(), cfgd);
			}
			updateData();
			break;
		}
		updateButtons();
	}
	
	public void updateButtons() {
		if (table == null || table.isDisposed()) return;
		
		int pos = table.getSelectionIndex();
		buttonSetEnabled(2, pos != -1);
		buttonSetEnabled(3, pos != -1);
		buttonSetEnabled(4, pos != -1);
	}

	public void updateData(ICResourceDescription _cfgd) {
		// null means preference configuration 
		cfgd = (_cfgd != null) ? _cfgd.getConfiguration() : null;
		if (cfgd == null && vars == null)
			vars = fUserSupplier.getWorkspaceEnvironmentCopy();
		updateData();
	}
	private void updateData() {
		IEnvironmentVariable[] _vars = null;
		if (cfgd != null) {
			b1.setSelection(ce.appendEnvironment(cfgd));
			b2.setSelection(!ce.appendEnvironment(cfgd));
			 _vars = ce.getVariables(cfgd);
		} else {
			b1.setSelection(vars.appendContributedEnvironment());
			b2.setSelection(!vars.appendContributedEnvironment());
			_vars = vars.getVariables() ;
		}
		data.clear();
		if (_vars != null) {
			for (int i=0; i<_vars.length; i++) {
				data.add(new TabData(_vars[i], false));
			}
		}
		tv.setInput(data);
		updateButtons();
	}

	public void performApply(ICResourceDescription _src, ICResourceDescription _dst) {
		ICConfigurationDescription src = _src.getConfiguration();
		ICConfigurationDescription dst = _dst.getConfiguration();
		
		ce.setAppendEnvironment(ce.appendEnvironment(src), dst);
		IEnvironmentVariable[] v = ce.getVariables(dst);
		for (int i=0; i<v.length; i++) ce.removeVariable(v[i].getName(), dst);
		v = ce.getVariables(src);
		for (int i=0; i<v.length; i++) {
			if (ce.isUserVariable(src, v[i]))
					ce.addVariable(v[i].getName(), v[i].getValue(), 
							v[i].getOperation(), v[i].getDelimiter(), dst);
		}
	}

	/**
	 * 
	 */
	private class MyListSelectionDialog extends ListSelectionDialog {
		public boolean toAll = false;
	    public MyListSelectionDialog(Shell parentShell, Object input, IStructuredContentProvider contentProvider) {
	        super(parentShell, input, contentProvider, new LabelProvider() {}, UIMessages.getString("EnvironmentTab.12")); //$NON-NLS-1$
	    }
	    protected Control createDialogArea(Composite parent) {
	    	Composite composite = (Composite) super.createDialogArea(parent);
	    	Button b = new Button(composite, SWT.CHECK);
	    	b.setText(UIMessages.getString("EnvironmentTab.13")); //$NON-NLS-1$
	    	b.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
	    	if (cfgd == null)
	    		b.setVisible(false);
	    	else
	    		b.addSelectionListener(new SelectionAdapter() {
	    			public void widgetSelected(SelectionEvent e) {
	    				toAll = ((Button)e.widget).getSelection();
	    			}});
	    	return composite;
	    }
	}
	
	private void handleEnvSelectButtonSelected() {
		// get Environment Variables from the OS
		Map v = EnvironmentReader.getEnvVars();
		MyListSelectionDialog dialog = new MyListSelectionDialog(usercomp.getShell(), v, createSelectionDialogContentProvider());
		
		dialog.setTitle(UIMessages.getString("EnvironmentTab.14")); //$NON-NLS-1$
		if (dialog.open() == Window.OK) {
			Object[] selected = dialog.getResult();
			ICConfigurationDescription[] cfgs;
			if (dialog.toAll)
				cfgs = page.getCfgsEditable();
			else 
				cfgs = new ICConfigurationDescription[] {cfgd};
			
			for (int i = 0; i < selected.length; i++) {
				String name = (String)selected[i];
				String value = EMPTY_STR;
				int x = name.indexOf(LBR);
				if (x >= 0) {
					value = name.substring(x + 2, name.length() - 1);
					name = name.substring(0, x);
				}
				
				if (cfgd == null) 
					vars.createVariable(name, value); 
				else
					for (int y=0; y<cfgs.length; y++) { 
						ce.addVariable(
								name, value, 
								IEnvironmentVariable.ENVVAR_APPEND, 
								SEMI, cfgs[y]);
				}
			}
		}
	}
	
	private IStructuredContentProvider createSelectionDialogContentProvider() {
		return new IStructuredContentProvider() {

			public Object[] getElements(Object inputElement) {
				String[] els = null;
				if (inputElement instanceof Map) {
					Map m = (Map)inputElement;
					els = new String[m.size()];  
					int index = 0;
					for (Iterator iterator = m.keySet().iterator(); iterator.hasNext(); index++) {
						String k = (String)iterator.next();
						els[index] = TextProcessor.process(k + LBR + (String)m.get(k) + RBR);  
					}
				}
				Arrays.sort(els, CDTListComparator.getInstance());
				return els;
			}
			public void dispose() {}
			public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
		};
	}
	// This page can be displayed for project only
	public boolean canBeVisible() {
		return page.isForProject() || page.isForPrefs();
	}

	protected void performOK() {
		if (vars != null) {
			if (fUserSupplier.setWorkspaceEnvironment(vars))
				if (page instanceof PrefPage_Abstract)
					PrefPage_Abstract.isChanged = true;
		}
		vars = null;
		super.performOK();
	}
	
	protected void performCancel() {
		vars = null;
		super.performCancel();
	}
	
	protected void performDefaults() {
		ce.restoreDefaults(cfgd); // both for proj & prefs
		vars = null;
		updateData();
	}
}

Back to the top