1
/*******************************************************************************
2
* Copyright (c) 2007, 2009 Intel Corporation and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* Intel Corporation - initial API and implementation
11
*******************************************************************************/
12
package org.eclipse.cdt.ui.newui;
14
import java.util.ArrayList;
15
import java.util.Arrays;
16
import java.util.Collections;
17
import java.util.Iterator;
20
import org.eclipse.jface.resource.JFaceResources;
21
import org.eclipse.jface.viewers.IColorProvider;
22
import org.eclipse.jface.viewers.IFontProvider;
23
import org.eclipse.jface.viewers.IStructuredContentProvider;
24
import org.eclipse.jface.viewers.ITableFontProvider;
25
import org.eclipse.jface.viewers.ITableLabelProvider;
26
import org.eclipse.jface.viewers.LabelProvider;
27
import org.eclipse.jface.viewers.TableViewer;
28
import org.eclipse.jface.viewers.Viewer;
29
import org.eclipse.jface.window.Window;
30
import org.eclipse.osgi.util.TextProcessor;
31
import org.eclipse.swt.SWT;
32
import org.eclipse.swt.events.MouseAdapter;
33
import org.eclipse.swt.events.MouseEvent;
34
import org.eclipse.swt.events.SelectionAdapter;
35
import org.eclipse.swt.events.SelectionEvent;
36
import org.eclipse.swt.events.SelectionListener;
37
import org.eclipse.swt.graphics.Color;
38
import org.eclipse.swt.graphics.Font;
39
import org.eclipse.swt.graphics.Image;
40
import org.eclipse.swt.layout.GridData;
41
import org.eclipse.swt.layout.GridLayout;
42
import org.eclipse.swt.widgets.Button;
43
import org.eclipse.swt.widgets.Composite;
44
import org.eclipse.swt.widgets.Control;
45
import org.eclipse.swt.widgets.Label;
46
import org.eclipse.swt.widgets.Shell;
47
import org.eclipse.swt.widgets.Table;
48
import org.eclipse.swt.widgets.TableColumn;
49
import org.eclipse.ui.dialogs.ListSelectionDialog;
51
import org.eclipse.cdt.core.envvar.IEnvironmentVariable;
52
import org.eclipse.cdt.core.model.util.CDTListComparator;
53
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
54
import org.eclipse.cdt.core.settings.model.ICResourceDescription;
55
import org.eclipse.cdt.utils.envvar.StorableEnvironment;
56
import org.eclipse.cdt.utils.spawner.EnvironmentReader;
58
import org.eclipse.cdt.internal.core.envvar.EnvVarDescriptor;
59
import org.eclipse.cdt.internal.core.envvar.EnvironmentVariableManager;
60
import org.eclipse.cdt.internal.core.envvar.UserDefinedEnvironmentSupplier;
63
* @noextend This class is not intended to be subclassed by clients.
65
public class EnvironmentTab extends AbstractCPropertyTab {
66
private static final String SEPARATOR = System.getProperty("path.separator", ";"); //$NON-NLS-1$ //$NON-NLS-2$
67
private static final String LBR = " ["; //$NON-NLS-1$
68
private static final String RBR = "]"; //$NON-NLS-1$
69
private static final UserDefinedEnvironmentSupplier fUserSupplier = EnvironmentVariableManager.fUserSupplier;
71
private final MultiCfgContributedEnvironment ce = new MultiCfgContributedEnvironment();
74
private TableViewer tv;
75
private ArrayList<TabData> data = new ArrayList<TabData>();
76
private Button b1, b2;
77
private Label lb1, lb2;
79
private ICConfigurationDescription cfgd = null;
80
private StorableEnvironment vars = null;
82
private class TabData implements Comparable<TabData> {
83
IEnvironmentVariable var;
85
TabData(IEnvironmentVariable _var, boolean _changed) {
89
public int compareTo(TabData a) {
90
String s = var.getName();
91
if (a != null && s != null && a.var != null)
92
return (s.compareTo(a.var.getName()));
97
private class EnvironmentLabelProvider extends LabelProvider implements ITableLabelProvider, IFontProvider , ITableFontProvider, IColorProvider{
98
public EnvironmentLabelProvider(boolean user){
101
public Image getImage(Object element) {
102
return null; // JavaPluginImages.get(JavaPluginImages.IMG_OBJS_REFACTORING_INFO);
105
public String getText(Object element) {
106
return getColumnText(element, 0);
108
public Image getColumnImage(Object element, int columnIndex) {
111
public String getColumnText(Object element, int columnIndex) {
112
TabData td = (TabData)element;
115
return td.var.getName();
117
if(td.var.getOperation() == IEnvironmentVariable.ENVVAR_REMOVE)
118
return UIMessages.getString(UIMessages.getString("EnvironmentTab.20")); //$NON-NLS-1$
119
return td.var.getValue();
121
return ce.getOrigin(td.var);
126
public Font getFont(Object element) {
127
return getFont(element, 0);
130
public Font getFont(Object element, int columnIndex) {
131
TabData td = (TabData)element;
135
return JFaceResources.getFontRegistry().getBold(JFaceResources.DIALOG_FONT);
143
public Color getForeground(Object element){
147
public Color getBackground(Object element){
148
TabData td = (TabData)element;
150
return BACKGROUND_FOR_USER_VAR;
156
public void createControls(Composite parent) {
157
super.createControls(parent);
158
usercomp.setLayout(new GridLayout(3, true));
159
Label l1 = new Label(usercomp, SWT.LEFT);
160
l1.setText(UIMessages.getString("EnvironmentTab.0")); //$NON-NLS-1$
161
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
162
gd.horizontalSpan = 3;
163
l1.setLayoutData(gd);
164
table = new Table(usercomp, SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.H_SCROLL | SWT.FULL_SELECTION);
165
table.setHeaderVisible(true);
166
table.setLinesVisible(true);
167
table.addSelectionListener(new SelectionListener() {
168
public void widgetSelected(SelectionEvent e) {
171
public void widgetDefaultSelected(SelectionEvent e) {
172
if (buttonIsEnabled(2) && table.getSelectionIndex() != -1)
176
tv = new TableViewer(table);
177
tv.setContentProvider(new IStructuredContentProvider() {
179
@SuppressWarnings("unchecked")
180
public Object[] getElements(Object inputElement) {
181
if (inputElement != null && inputElement instanceof ArrayList) {
182
ArrayList<Object> ar = (ArrayList)inputElement;
183
return ar.toArray(new TabData[0]);
187
public void dispose() {}
188
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
190
tv.setLabelProvider(new EnvironmentLabelProvider(true));
192
TableColumn tc = new TableColumn(table, SWT.LEFT);
193
tc.setText(UIMessages.getString("EnvironmentTab.1")); //$NON-NLS-1$
195
tc = new TableColumn(table, SWT.LEFT);
196
tc.setText(UIMessages.getString("EnvironmentTab.2")); //$NON-NLS-1$
198
if (this.getResDesc() != null) {
199
tc = new TableColumn(table, SWT.LEFT);
200
tc.setText(UIMessages.getString("EnvironmentTab.16")); //$NON-NLS-1$
204
gd = new GridData(GridData.FILL_BOTH);
205
gd.horizontalSpan = 3;
206
table.setLayoutData(gd);
208
b1 = new Button(usercomp, SWT.RADIO);
209
b1.setText(UIMessages.getString("EnvironmentTab.3")); //$NON-NLS-1$
210
b1.setToolTipText(UIMessages.getString("EnvironmentTab.3")); //$NON-NLS-1$
211
gd = new GridData(GridData.FILL_HORIZONTAL);
212
gd.horizontalSpan = 3;
213
b1.setLayoutData(gd);
214
b1.addSelectionListener(new SelectionAdapter() {
216
public void widgetSelected(SelectionEvent e) {
218
ce.setAppendEnvironment(true, cfgd);
220
vars.setAppendContributedEnvironment(true);
224
b2 = new Button(usercomp, SWT.RADIO);
225
b2.setText(UIMessages.getString("EnvironmentTab.4")); //$NON-NLS-1$
226
b2.setToolTipText(UIMessages.getString("EnvironmentTab.4")); //$NON-NLS-1$
227
gd = new GridData(GridData.FILL_HORIZONTAL);
228
gd.horizontalSpan = 3;
229
b2.setLayoutData(gd);
230
b2.addSelectionListener(new SelectionAdapter() {
232
public void widgetSelected(SelectionEvent e) {
234
ce.setAppendEnvironment(false, cfgd);
236
vars.setAppendContributedEnvironment(false);
240
if (!page.isForPrefs()) {
242
new Label(usercomp, SWT.NONE).setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
244
lb1 = new Label(usercomp, SWT.BORDER | SWT.CENTER);
245
lb1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
246
lb1.setToolTipText(UIMessages.getString("EnvironmentTab.15")); //$NON-NLS-1$
247
lb1.addMouseListener(new MouseAdapter() {
249
public void mouseDoubleClick(MouseEvent e) {
250
CDTPrefUtil.spinDMode();
254
lb2 = new Label(usercomp, SWT.BORDER | SWT.CENTER);
255
lb2.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
256
lb2.setToolTipText(UIMessages.getString("EnvironmentTab.23")); //$NON-NLS-1$
257
lb2.addMouseListener(new MouseAdapter() {
259
public void mouseDoubleClick(MouseEvent e) {
260
CDTPrefUtil.spinWMode();
261
updateLbs(null, lb2);
264
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$
268
public void buttonPressed(int i) {
271
handleEnvAddButtonSelected();
274
handleEnvSelectButtonSelected();
277
handleEnvEditButtonSelected(table.getSelectionIndex());
280
handleEnvDelButtonSelected(table.getSelectionIndex());
283
handleEnvUndefButtonSelected(table.getSelectionIndex());
290
protected void updateButtons() {
291
if (table == null || table.isDisposed()) return;
293
boolean canEdit = table.getSelectionCount() == 1;
294
boolean canDel = false;
295
boolean canUndef = table.getSelectionCount() >= 1;
297
for (int i : table.getSelectionIndices()) {
298
IEnvironmentVariable var = ((TabData)tv.getElementAt(i)).var;
300
// if (cfgd == null || !wse.getVariable(var.))
306
buttonSetEnabled(2, canEdit); // edit
307
buttonSetEnabled(3, canDel); // delete
308
buttonSetEnabled(4, canUndef); // undefine
312
protected void updateData(ICResourceDescription _cfgd) {
313
// null means preference configuration
314
cfgd = (_cfgd != null) ? _cfgd.getConfiguration() : null;
315
if (cfgd == null && vars == null)
316
vars = fUserSupplier.getWorkspaceEnvironmentCopy();
318
ce.setMulti(page.isMultiCfg());
322
private void updateData() {
323
IEnvironmentVariable[] _vars = null;
325
b1.setSelection(ce.appendEnvironment(cfgd));
326
b2.setSelection(!ce.appendEnvironment(cfgd));
327
_vars = ce.getVariables(cfgd);
330
vars = fUserSupplier.getWorkspaceEnvironmentCopy();
331
b1.setSelection(vars.appendContributedEnvironment());
332
b2.setSelection(!vars.appendContributedEnvironment());
333
_vars = vars.getVariables() ;
338
for (IEnvironmentVariable _var : _vars) {
339
data.add(new TabData(_var, false));
342
Collections.sort(data);
350
protected void performApply(ICResourceDescription _src, ICResourceDescription _dst) {
351
ICConfigurationDescription src = _src.getConfiguration();
352
ICConfigurationDescription dst = _dst.getConfiguration();
354
ce.setAppendEnvironment(ce.appendEnvironment(src), dst);
355
IEnvironmentVariable[] v = ce.getVariables(dst);
356
for (IEnvironmentVariable element : v)
357
ce.removeVariable(element.getName(), dst);
358
v = ce.getVariables(src);
359
for (IEnvironmentVariable element : v) {
360
if (ce.isUserVariable(src, element))
361
ce.addVariable(element.getName(), element.getValue(),
362
element.getOperation(), element.getDelimiter(), dst);
369
private class MyListSelectionDialog extends ListSelectionDialog {
370
public boolean toAll = false;
371
public MyListSelectionDialog(Shell parentShell, Object input, IStructuredContentProvider contentProvider) {
372
super(parentShell, input, contentProvider, new LabelProvider() {}, UIMessages.getString("EnvironmentTab.12")); //$NON-NLS-1$
375
protected Control createDialogArea(Composite parent) {
376
Composite composite = (Composite) super.createDialogArea(parent);
377
Button b = new Button(composite, SWT.CHECK);
378
b.setText(UIMessages.getString("EnvironmentTab.13")); //$NON-NLS-1$
379
b.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
383
b.addSelectionListener(new SelectionAdapter() {
385
public void widgetSelected(SelectionEvent e) {
386
toAll = ((Button)e.widget).getSelection();
392
private void handleEnvEditButtonSelected(int n) {
395
IEnvironmentVariable var = ((TabData)tv.getElementAt(n)).var;
396
EnvDialog dlg = new EnvDialog(usercomp.getShell(),
398
UIMessages.getString("EnvironmentTab.11"), //$NON-NLS-1$
402
if (dlg.open() == Window.OK) {
404
ce.addVariable( var.getName(), dlg.t2.trim(),
405
IEnvironmentVariable.ENVVAR_REPLACE,
406
var.getDelimiter(), cfgd);
408
vars.createVariable(dlg.t1.trim(), dlg.t2.trim(),
409
IEnvironmentVariable.ENVVAR_REPLACE, var.getDelimiter());
411
table.setSelection(n);
416
private void handleEnvUndefButtonSelected(int n) {
419
for (int i : table.getSelectionIndices()) {
420
IEnvironmentVariable var = ((TabData)tv.getElementAt(i)).var;
425
IEnvironmentVariable.ENVVAR_REMOVE,
431
IEnvironmentVariable.ENVVAR_REMOVE,
432
var.getDelimiter(), cfgd);
435
table.setSelection(n);
439
private void handleEnvDelButtonSelected(int n) {
442
for (int i : table.getSelectionIndices()) {
443
IEnvironmentVariable var = ((TabData)tv.getElementAt(i)).var;
445
vars.deleteVariable(var.getName());
447
ce.removeVariable(var.getName(), cfgd);
450
int x = table.getItemCount() - 1;
452
table.setSelection(Math.min(x, n));
457
private void handleEnvAddButtonSelected() {
458
IEnvironmentVariable var = null;
459
EnvDialog dlg = new EnvDialog(usercomp.getShell(),
461
UIMessages.getString("EnvironmentTab.10"), //$NON-NLS-1$
465
if (dlg.open() == Window.OK) {
466
String name = dlg.t1.trim();
467
if (name.length() > 0) {
468
ICConfigurationDescription[] cfgs;
470
cfgs = page.getCfgsEditable();
472
cfgs = new ICConfigurationDescription[] {cfgd};
474
vars.createVariable(name, dlg.t2.trim(),
475
IEnvironmentVariable.ENVVAR_APPEND, SEPARATOR);
477
for (ICConfigurationDescription cfg : cfgs) {
478
ce.addVariable(name, dlg.t2.trim(),
479
IEnvironmentVariable.ENVVAR_APPEND,
488
private void setPos(String name) {
489
if (name == null || name.length() == 0)
491
for (int i=0; i<table.getItemCount(); i++) {
492
if (name.equals(table.getItem(i).getText())) {
493
table.setSelection(i);
500
private void handleEnvSelectButtonSelected() {
501
// get Environment Variables from the OS
502
Map<?,?> v = EnvironmentReader.getEnvVars();
503
MyListSelectionDialog dialog = new MyListSelectionDialog(usercomp.getShell(), v, createSelectionDialogContentProvider());
505
dialog.setTitle(UIMessages.getString("EnvironmentTab.14")); //$NON-NLS-1$
506
if (dialog.open() == Window.OK) {
507
Object[] selected = dialog.getResult();
508
ICConfigurationDescription[] cfgs;
510
cfgs = page.getCfgsEditable();
512
cfgs = new ICConfigurationDescription[] {cfgd};
515
for (Object element : selected) {
516
name = (String)element;
517
String value = EMPTY_STR;
518
int x = name.indexOf(LBR);
520
value = name.substring(x + 2, name.length() - 1);
521
name = name.substring(0, x);
525
vars.createVariable(name, value);
527
for (ICConfigurationDescription cfg : cfgs) {
530
IEnvironmentVariable.ENVVAR_APPEND,
539
private IStructuredContentProvider createSelectionDialogContentProvider() {
540
return new IStructuredContentProvider() {
542
public Object[] getElements(Object inputElement) {
544
if (inputElement instanceof Map<?, ?>) {
545
@SuppressWarnings("unchecked")
546
Map<String,?> m = (Map)inputElement;
547
els = new String[m.size()];
549
for (Iterator<String> iterator = m.keySet().iterator(); iterator.hasNext(); index++) {
550
String k = iterator.next();
551
els[index] = TextProcessor.process(k + LBR + m.get(k) + RBR);
554
Arrays.sort(els, CDTListComparator.getInstance());
557
public void dispose() {}
558
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
561
// This page can be displayed for project only
563
public boolean canBeVisible() {
564
return page.isForProject() || page.isForPrefs();
568
protected void performOK() {
570
if (fUserSupplier.setWorkspaceEnvironment(vars))
571
if (page instanceof PrefPage_Abstract)
572
PrefPage_Abstract.isChanged = true;
579
protected void performCancel() {
581
super.performCancel();
585
protected void performDefaults() {
586
ce.restoreDefaults(cfgd); // both for proj & prefs
591
private boolean isUsers(IEnvironmentVariable var) {
592
return cfgd == null ||
593
(ce.isUserVariable(cfgd, var) &&
594
((EnvVarDescriptor)var).getContextInfo().getContext() != null);