Skip to main content
summaryrefslogtreecommitdiffstats
blob: ad8033555a30d834b7999a1d706af7367ce7d80e (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
/*******************************************************************************
 * Copyright (c) 2017 QNX Software Systems 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
 *******************************************************************************/
package org.eclipse.cdt.build.gcc.ui.internal;

import org.eclipse.cdt.core.envvar.EnvironmentVariable;
import org.eclipse.cdt.core.envvar.IEnvironmentVariable;
import org.eclipse.jface.dialogs.Dialog;
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.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class NewEnvVarDialog extends Dialog {

	private Text nameText;
	private Label valueLabel;
	private Text valueText;
	private Label delimiterLabel;
	private Text delimiterText;
	private Button replaceButton;
	private Button prependButton;
	private Button appendButton;
	private Button removeButton;

	private IEnvironmentVariable envvar;

	public NewEnvVarDialog(Shell parentShell) {
		super(parentShell);
	}

	public NewEnvVarDialog(Shell parentShell, IEnvironmentVariable var) {
		super(parentShell);
		this.envvar = var;
	}

	@Override
	protected boolean isResizable() {
		return true;
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		if (envvar == null) {
			getShell().setText(Messages.NewEnvVarDialog_New);
		} else {
			getShell().setText(Messages.NewEnvVarDialog_Edit);
		}

		Composite comp = new Composite(parent, SWT.NONE);
		GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
		layoutData.widthHint = 400;
		comp.setLayoutData(layoutData);
		comp.setLayout(new GridLayout(3, false));

		Group opGroup = new Group(comp, SWT.NONE);
		opGroup.setText(Messages.NewEnvVarDialog_Operation);
		opGroup.setLayout(new GridLayout(4, false));
		layoutData = new GridData(SWT.FILL, SWT.FILL, true, false);
		layoutData.horizontalSpan = 3;
		opGroup.setLayoutData(layoutData);

		replaceButton = new Button(opGroup, SWT.RADIO);
		replaceButton.setText(Messages.NewEnvVarDialog_Replace);

		prependButton = new Button(opGroup, SWT.RADIO);
		prependButton.setText(Messages.NewEnvVarDialog_Prepend);
		prependButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				updateEnablement();
			}
		});

		appendButton = new Button(opGroup, SWT.RADIO);
		appendButton.setText(Messages.NewEnvVarDialog_Append);
		appendButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				updateEnablement();
			}
		});

		removeButton = new Button(opGroup, SWT.RADIO);
		removeButton.setText(Messages.NewEnvVarDialog_Unset);
		removeButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				updateEnablement();
			}
		});

		if (envvar == null) {
			replaceButton.setSelection(true);
		} else {
			switch (envvar.getOperation()) {
			case IEnvironmentVariable.ENVVAR_REPLACE:
				replaceButton.setSelection(true);
				break;
			case IEnvironmentVariable.ENVVAR_PREPEND:
				prependButton.setSelection(true);
				break;
			case IEnvironmentVariable.ENVVAR_APPEND:
				appendButton.setSelection(true);
				break;
			default:
				removeButton.setSelection(true);
			}
		}

		Label label = new Label(comp, SWT.NONE);
		label.setText(Messages.NewEnvVarDialog_Name);

		nameText = new Text(comp, SWT.BORDER);
		layoutData = new GridData(SWT.FILL, SWT.CENTER, true, false);
		layoutData.horizontalSpan = 2;
		nameText.setLayoutData(layoutData);
		if (envvar != null) {
			nameText.setText(envvar.getName());
		}

		// TODO
		// Button selectButton = new Button(comp, SWT.PUSH);
		// selectButton.setText(Messages.NewEnvVarDialog_Select);

		valueLabel = new Label(comp, SWT.NONE);
		valueLabel.setText(Messages.NewEnvVarDialog_Value);

		valueText = new Text(comp, SWT.BORDER);
		layoutData = new GridData(SWT.FILL, SWT.CENTER, true, false);
		layoutData.horizontalSpan = 2;
		valueText.setLayoutData(layoutData);
		if (envvar != null && envvar.getValue() != null) {
			valueText.setText(envvar.getValue());
		}

		delimiterLabel = new Label(comp, SWT.NONE);
		delimiterLabel.setText(Messages.NewEnvVarDialog_Delimiter);

		delimiterText = new Text(comp, SWT.BORDER);
		layoutData = new GridData(SWT.FILL, SWT.CENTER, true, false);
		layoutData.horizontalSpan = 2;
		delimiterText.setLayoutData(layoutData);
		if (envvar != null && envvar.getDelimiter() != null) {
			delimiterText.setText(envvar.getDelimiter());
		}

		updateEnablement();
		return comp;
	}

	private void updateEnablement() {
		valueLabel.setEnabled(!removeButton.getSelection());
		valueText.setEnabled(!removeButton.getSelection());

		delimiterLabel.setEnabled(prependButton.getSelection() || appendButton.getSelection());
		delimiterText.setEnabled(prependButton.getSelection() || appendButton.getSelection());
	}

	@Override
	protected void okPressed() {
		String name = nameText.getText().trim();

		int operation;
		if (replaceButton.getSelection()) {
			operation = IEnvironmentVariable.ENVVAR_REPLACE;
		} else if (prependButton.getSelection()) {
			operation = IEnvironmentVariable.ENVVAR_PREPEND;
		} else if (appendButton.getSelection()) {
			operation = IEnvironmentVariable.ENVVAR_APPEND;
		} else {
			operation = IEnvironmentVariable.ENVVAR_REMOVE;
		}

		String value;
		if (valueText.isEnabled()) {
			value = valueText.getText().trim();
			if (value.isEmpty()) {
				value = null;
			}
		} else {
			value = null;
		}

		String delimiter;
		if (delimiterText.isEnabled()) {
			delimiter = delimiterText.getText().trim();
			if (delimiter.isEmpty()) {
				delimiter = null;
			}
		} else {
			delimiter = null;
		}

		envvar = new EnvironmentVariable(name, value, operation, delimiter);

		super.okPressed();
	}

	public IEnvironmentVariable getEnvVar() {
		return envvar;
	}
}

Back to the top