Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 782d7d013859db08f9ebd4c5f70666c7038e1039 (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
/*******************************************************************************
 * Copyright (c) 2009, 2015 Red Hat Inc.
 * 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:
 *     Red Hat Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.autotools.ui.properties;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.cdt.internal.autotools.core.configure.AutotoolsConfiguration;
import org.eclipse.cdt.internal.autotools.core.configure.IAConfiguration;
import org.eclipse.cdt.internal.autotools.core.configure.IConfigureOption;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.jface.preference.ListEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
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.Text;

public class AutotoolsCategoryPropertyOptionPage extends
		AbstractConfigurePropertyOptionsPage {

	private static final int MARGIN = 3;
	private String catName = "";
	private IAConfiguration cfg;
	//  Label class for a preference page.
	static class LabelFieldEditor extends FieldEditor {
		private String fTitle;
		private Label fTitleLabel;

		public LabelFieldEditor( Composite parent, String title ) {
			fTitle = title;
			this.createControl( parent );
		}

		@Override
		protected void adjustForNumColumns( int numColumns ) {
			((GridData)fTitleLabel.getLayoutData()).horizontalSpan = 2;
		}

		@Override
		protected void doFillIntoGrid( Composite parent, int numColumns ) {
			fTitleLabel = new Label( parent, SWT.WRAP );
			fTitleLabel.setText( fTitle );
			GridData gd = new GridData();
			gd.verticalAlignment = SWT.TOP;
			gd.grabExcessHorizontalSpace = false;
			gd.horizontalSpan = 2;
			fTitleLabel.setLayoutData( gd );
		}

		@Override
		public int getNumberOfControls() {	return 1; }
		/**
		 * The label field editor is only used to present a text label on a preference page.
		 */
		@Override
		protected void doLoad() {}
		@Override
		protected void doLoadDefault() {}
		@Override
		protected void doStore() {}
	}

	static class VariableListEditor extends ListEditor {
		Composite fParent;
		String fName;
		String fLabelText;
		boolean isLoaded;

		public VariableListEditor(String name, String labelText, Composite parent) {
			fName = name;
			fLabelText = labelText;
			fParent = parent;
			isLoaded = false;
			init(fName, fLabelText);
			createControl(fParent);
		}

		@Override
		protected void selectionChanged() {
			super.selectionChanged();
			super.fireValueChanged(getPreferenceName(), "", ""); //$NON-NLS-1$ //$NON-NLS-2$
		}

		@Override
		protected String createList(String[] arg0) {
			StringBuffer sb = new StringBuffer();
			for (String item : arg0) {
				sb.append(item);
				sb.append("\\s"); //$NON-NLS-1$
			}
			return sb.toString();
		}

		@Override
		protected void doLoad() {
			if (!isLoaded) {
				super.doLoad();
				isLoaded = true;
			}
		};

		public void setToolTipText(String toolTip) {
			this.getLabelControl().setToolTipText(toolTip);
		}

		/**
		 * Dialog user inputs variable's name and value.
		 */
		class DialogNewVar extends Dialog {
			private String name;
			private Text fTextName;
			private String value;
			private Text fTextValue;
			private Button fOkButton;

			public DialogNewVar(Shell shell) {
				super(shell);
			}

			@Override
			protected void configureShell(Shell newShell) {
				super.configureShell(newShell);
				newShell.setText(AutotoolsPropertyMessages.getString("NewEnvVarDialog.title"));
			};

			@Override
			protected Control createDialogArea(Composite parent) {
				Composite composite = new Composite(parent, SWT.NONE);
				GridLayout layout = new GridLayout(2, false);
				layout.marginWidth = 5;
				layout.numColumns = 2;
				composite.setLayout(layout);

				GC gc = new GC(composite);
				gc.setFont(composite.getFont());
				FontMetrics metrics = gc.getFontMetrics();
				gc.dispose();
				int fieldWidthHint = convertWidthInCharsToPixels(metrics, 50);

				Label label = new Label(composite, SWT.NONE);
				label.setText(AutotoolsPropertyMessages.getString("NewEnvVarDialog.name_field"));
				fTextName = new Text(composite, SWT.SINGLE | SWT.BORDER);
				GridData gd = new GridData(GridData.FILL_BOTH);
				gd.grabExcessHorizontalSpace = true;
				gd.widthHint = fieldWidthHint;
				fTextName.setLayoutData(gd);
				// Name field cannot be empty.
				fTextName.addModifyListener(new ModifyListener() {

					@Override
					public void modifyText(ModifyEvent e) {
						if (fOkButton != null) {
							fOkButton.setEnabled(fTextName.getText().length() > 0);
						}
					}
				});
				label = new Label(composite, SWT.NONE);
				label.setText(AutotoolsPropertyMessages.getString("NewEnvVarDialog.value_field"));
				fTextValue = new Text(composite, SWT.SINGLE | SWT.BORDER);
				gd = new GridData(GridData.FILL_BOTH);
				gd.grabExcessHorizontalSpace = true;
				gd.widthHint = fieldWidthHint;
				fTextValue.setLayoutData(gd);
				return composite;
			};

			// Obtain instance of OK button and set disabled.
			@Override
			protected Control createButtonBar(Composite parent) {
				Control control = super.createButtonBar(parent);
				fOkButton = getButton(IDialogConstants.OK_ID);
				fOkButton.setEnabled(false);
				return control;
			}

			@Override
			protected void okPressed() {
				name = fTextName.getText().trim();
				value = fTextValue.getText();
				if (value != null) {
					value = value.trim();
				} else {
					value = ""; //$NON-NLS-1$
				}
				super.okPressed();
			}

			public String getName() {
				return name;
			}

			public String getValue() {
				return value;
			}
		};

		@Override
		protected String getNewInputObject() {
			DialogNewVar newDialog = new DialogNewVar(getShell());
			newDialog.open();
			String name = newDialog.getName();

			// Create quoted string like CFLAGS="-q -O3"
			if (name != null) {
				String quote = "\""; //$NON-NLS-1$
				StringBuilder sb = new StringBuilder(name.trim());
				sb.append("="); //$NON-NLS-1$
				String value = newDialog.getValue();
				if (value != null) {
					value = value.trim();
					if (value.length() == 0) {
						// Check empty value
						sb.append(quote);
						sb.append(quote);
					} else if (value.charAt(0) == '"' && value.charAt(value.length() - 1) == '"') {
						// Check user has already quoted it.
						sb.append(value);
					} else {
						sb.append(quote);
						sb.append(value);
						sb.append(quote);
					}
				}
				return sb.toString();
			}
			return null;
		}

		/*
		 * Expect string with format: VAR1="VALUE1" VAR2="VALUE2". Count quotes
		 * to mark end of a variable.
		 *
		 * @see
		 * org.eclipse.jface.preference.ListEditor#parseString(java.lang.String)
		 */
		@Override
		protected String[] parseString(String str) {
			if (str == null) {
				return new String[] {};
			}
			ArrayList<String> variables = new ArrayList<>();
			StringBuilder sb = new StringBuilder();
			int i = 0;
			int quote = 0; // 0 = begin variable,
							// 1 = looking for end of variable.
			while (i < str.length()) {
				char c = str.charAt(i);
				sb.append(c);
				if (c == '"') {
					quote++;
				}
				if (quote == 2) {
					// Found end of variable.
					quote = 0;
					variables.add(sb.toString());
					sb.delete(0, sb.length());
					i++; // Skip whitespace char separating variables.
				}
				i++;
			}

			return variables.toArray(new String[0]);
		}

		/**
		 * Get the list of environment variables in a single line.
		 *
		 * @return environment variables
		 */
		public String getVariablesValue() {
			org.eclipse.swt.widgets.List list = super.getList();
			StringBuilder sb = new StringBuilder();
			for (String var : list.getItems()) {
				sb.append(var);
				sb.append(" "); //$NON-NLS-1$
			}
			return sb.toString().trim();
		}
	};

	private List<FieldEditor> fieldEditors;
	
	public AutotoolsCategoryPropertyOptionPage(ToolListElement element, IAConfiguration cfg) {
 		super(element.getName());
 		this.catName = element.getName();
 		this.cfg = cfg;
 		fieldEditors = new ArrayList<>();
	}

	@Override
	protected void createFieldEditors() {
		super.createFieldEditors();
		Composite parent = getFieldEditorParent();
		
		// Add margin
		parent.setLayout(new GridLayout(1, false));
		Composite area = new Composite(parent, SWT.NONE);
		GridLayout gl = new GridLayout(1, false);
		gl.marginTop = MARGIN;
		gl.marginLeft = MARGIN;
		gl.marginRight = MARGIN;
		area.setLayout(gl);
		area.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		
//		FontMetrics fm = AbstractCPropertyTab.getFontMetrics(parent);
		AutotoolsConfiguration.Option[] options = AutotoolsConfiguration.getChildOptions(catName);
		for (int i = 0; i < options.length; ++i) {
			AutotoolsConfiguration.Option option = options[i];
			switch (option.getType()) {
			case IConfigureOption.STRING:
			case IConfigureOption.INTERNAL:
			case IConfigureOption.MULTIARG:
				StringFieldEditor f = new StringFieldEditor(option.getName(), option.getDescription(), area);
				f.getLabelControl(area).setToolTipText(option.getToolTip());
				addField(f);
				fieldEditors.add(f);
				break;
			case IConfigureOption.BIN:
			case IConfigureOption.FLAGVALUE:
				BooleanFieldEditor b = new BooleanFieldEditor(option.getName(), option.getDescription(), area);
				b.getDescriptionControl(area).setToolTipText(option.getToolTip());
				addField(b);
				fieldEditors.add(b);
				break;
			case IConfigureOption.FLAG:
				FieldEditor l = new LabelFieldEditor(area, option.getDescription());
				addField(l);
				fieldEditors.add(l);
				break;
			case IConfigureOption.ENVVAR:
				VariableListEditor listEditor = new VariableListEditor(option.getName(), option.getDescription(), area);
				listEditor.setToolTipText(option.getToolTip());
				addField(listEditor);
				fieldEditors.add(listEditor);
				break;
			}
		}
	}

	/**
	 * Update the field editor that displays all the build options
	 */
	@Override
	public void updateFields() {
		setValues();
	}
	
	@Override
	public void setValues() {
		for (int i = 0; i < fieldEditors.size(); ++i) {
			fieldEditors.get(i).load();
		}
	}
	
	@Override
	public void propertyChange(PropertyChangeEvent event) {
		// allow superclass to handle as well
		super.propertyChange(event);

		if (event.getSource() instanceof StringFieldEditor) {
			StringFieldEditor f = (StringFieldEditor)event.getSource();
			cfg.setOption(f.getPreferenceName(), f.getStringValue());
		} else if (event.getSource() instanceof BooleanFieldEditor) {
			BooleanFieldEditor b = (BooleanFieldEditor)event.getSource();
			cfg.setOption(b.getPreferenceName(), Boolean.toString(b.getBooleanValue()));
		} else if (event.getSource() instanceof VariableListEditor) {
			VariableListEditor v = (VariableListEditor) event.getSource();
			cfg.setOption(v.getPreferenceName(), v.getVariablesValue());
		}
	}
	
}

Back to the top