Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 264727916f2e2a9d22954f9f8c0083a7d5a5d2b9 (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
/**********************************************************************
 * Copyright (c) 2003, 2005 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: 
 * IBM Rational Software - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.managedbuilder.ui.properties;

import java.util.Iterator;
import java.util.Map;
import java.util.HashMap;
import java.util.Collection;

import org.eclipse.cdt.managedbuilder.core.BuildException;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IOptionApplicability;
import org.eclipse.cdt.managedbuilder.core.IOption;
import org.eclipse.cdt.managedbuilder.core.IOptionCategory;
import org.eclipse.cdt.managedbuilder.core.IResourceConfiguration;
import org.eclipse.cdt.managedbuilder.core.ITool;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.jface.preference.DirectoryFieldEditor;
import org.eclipse.jface.preference.FileFieldEditor;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import java.lang.AssertionError;

public class BuildOptionSettingsPage extends BuildSettingsPage {
	private Map fieldsMap = new HashMap();
	private IOptionCategory category;
	private boolean isItResourceConfigPage;
	private Map fieldEditorsToParentMap = new HashMap();

	public BuildOptionSettingsPage(IConfiguration configuration, IOptionCategory category) {
		// Cache the configuration and option category this page is created for
		super(configuration);
		this.category = category;
		isItResourceConfigPage = false;
	}
	
	public BuildOptionSettingsPage(IResourceConfiguration resConfig, IOptionCategory category) {
		// Cache the configuration and option category this page is created for
		super(resConfig);
		this.category = category;
		isItResourceConfigPage = true;
	}
	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.IPreferencePage#computeSize()
	 */
	public Point computeSize() {
		return super.computeSize();
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.FieldEditorPreferencePage#createFieldEditors()
	 */
	protected void createFieldEditors() {
		// Get the preference store for the build settings
		super.createFieldEditors();
		// Iterate over the options in the category and create a field editor
		// for each
		Object[][] options;
		if ( isItResourceConfigPage ) {
			options = category.getOptions(resConfig);
		} else {
			options = category.getOptions(configuration);
		}
		
		for (int index = 0; index < options.length; ++index) {
			// Get the option
			ITool tool = (ITool)options[index][0];
			if (tool == null) break;	//  The array may not be full
			IOption opt = (IOption)options[index][1];
			
			// check to see if the option has an applicability calculator
			IOptionApplicability applicabilityCalculator = opt.getApplicabilityCalculator();
			
			// is the option visible?
			if (applicabilityCalculator == null || applicabilityCalculator.isOptionVisible(tool)) {
		
			try {
				// Figure out which type the option is and add a proper field
				// editor for it
				switch (opt.getValueType()) {
					case IOption.STRING:
						// fix for PR 63973
						// Check browse type.
						// If browsing is set, use a field editor that has a
						// browse button of
						// the appropriate type.
						// Otherwise, use a regular text field.
						switch (opt.getBrowseType()) {
						case IOption.BROWSE_DIR:
							Composite fieldEditorParent2 = getFieldEditorParent();
							DirectoryFieldEditor dirFieldEditor = new DirectoryFieldEditor(
									opt.getId(), opt.getName(),	fieldEditorParent2);

							setFieldEditorEnablement(tool,
									applicabilityCalculator, dirFieldEditor, fieldEditorParent2);

							addField(dirFieldEditor);
							fieldsMap.put(opt.getId(), dirFieldEditor);
							fieldEditorsToParentMap.put(dirFieldEditor,	fieldEditorParent2);

							break;

						case IOption.BROWSE_FILE:
							Composite fieldEditorParent3 = getFieldEditorParent();
							FileFieldEditor fileFieldEditor = new FileFieldEditor(
									opt.getId(), opt.getName(),	fieldEditorParent3);

							setFieldEditorEnablement(tool,
									applicabilityCalculator, fileFieldEditor, fieldEditorParent3);

							addField(fileFieldEditor);
							fieldsMap.put(opt.getId(), fileFieldEditor);
							fieldEditorsToParentMap.put(fileFieldEditor, fieldEditorParent3);
							break;

						case IOption.BROWSE_NONE:
							Composite fieldEditorParent4 = getFieldEditorParent();
							StringFieldEditor stringField = new StringFieldEditor(
									opt.getId(), opt.getName(),	fieldEditorParent4);

							setFieldEditorEnablement(tool,
									applicabilityCalculator, stringField, fieldEditorParent4);

							addField(stringField);
							fieldsMap.put(opt.getId(), stringField);
							fieldEditorsToParentMap.put(stringField, fieldEditorParent4);
							break;

						default:
							// should not be possible
							throw (new AssertionError());
						}
						// end fix for 63973
						break;
					case IOption.BOOLEAN:
						Composite fieldEditorParent5 = getFieldEditorParent();
						BooleanFieldEditor booleanField = new BooleanFieldEditor(
								opt.getId(), opt.getName(), fieldEditorParent5);

						setFieldEditorEnablement(tool, 
								applicabilityCalculator, booleanField, fieldEditorParent5);

						addField(booleanField);
						fieldsMap.put(opt.getId(), booleanField);
						fieldEditorsToParentMap.put(booleanField, fieldEditorParent5);
						break;
					case IOption.ENUMERATED:
						String selId;
						String sel;
						try {
							selId = opt.getSelectedEnum();
							sel = opt.getEnumName(selId);
						} catch (BuildException e) {
							// If we get this exception, then the option type is
							// wrong
							break;
						}

						Composite fieldEditorParent6 = getFieldEditorParent();
						BuildOptionComboFieldEditor comboField = new BuildOptionComboFieldEditor(
								opt.getId(), opt.getName(), opt.getApplicableValues(), sel,	fieldEditorParent6);

						setFieldEditorEnablement(tool, 
								applicabilityCalculator, comboField, fieldEditorParent6);

						addField(comboField);
						fieldsMap.put(opt.getId(), comboField);
						fieldEditorsToParentMap.put(comboField,	fieldEditorParent6);
						break;
					case IOption.INCLUDE_PATH:
					case IOption.STRING_LIST:
					case IOption.PREPROCESSOR_SYMBOLS:
					case IOption.LIBRARIES:
					case IOption.OBJECTS:

						Composite fieldEditorParent7 = getFieldEditorParent();
						FileListControlFieldEditor listField = new FileListControlFieldEditor(
								opt.getId(), opt.getName(), fieldEditorParent7,	opt.getBrowseType());

						setFieldEditorEnablement(tool, 
								applicabilityCalculator, listField, fieldEditorParent7);

						addField(listField);
						fieldsMap.put(opt.getId(), listField);
						fieldEditorsToParentMap.put(listField, fieldEditorParent7);
						break;
					default:
						break;
					}
			} catch (BuildException e) {
			}
			}
		}
	}
	
	/**
	 * Answers <code>true</code> if the settings page has been created for the
	 * option category specified in the argument.
	 * 
	 * @param category
	 * @return
	 */
	public boolean isForCategory(IOptionCategory category) {
		if (category != null) {
			return category.equals(this.category);
		}
		return false;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.IPreferencePage#performOk()
	 */
	public boolean performOk() {
		// Write the field editor contents out to the preference store
		boolean ok = super.performOk();
		// Write the preference store values back to the build model
		
		Object[][] options;
		if (isItResourceConfigPage){
			options = category.getOptions(resConfig);
		} else {
			options = category.getOptions(configuration);
		}
		
		for (int i = 0; i < options.length; i++) {
			ITool tool = (ITool)options[i][0];
			if (tool == null) break;	//  The array may not be full
			IOption option = (IOption)options[i][1];
			try {
				// Transfer value from preference store to options
				IOption setOption;
				switch (option.getValueType()) {
					case IOption.BOOLEAN :
						boolean boolVal = getToolSettingsPreferenceStore().getBoolean(option.getId());
						if(isItResourceConfigPage) {
							setOption = ManagedBuildManager.setOption(resConfig, tool, option, boolVal);
						} else {
							setOption = ManagedBuildManager.setOption(configuration, tool, option, boolVal);
						}
						// Reset the preference store since the Id may have changed
						if (setOption != option) {
							getToolSettingsPreferenceStore().setValue(setOption.getId(), boolVal);
							FieldEditor fe = (FieldEditor)fieldsMap.get(option.getId());
							fe.setPreferenceName(setOption.getId());
						}
						break;
					case IOption.ENUMERATED :
						String enumVal = getToolSettingsPreferenceStore().getString(option.getId());
						String enumId = option.getEnumeratedId(enumVal);
						if(isItResourceConfigPage) {
							setOption = ManagedBuildManager.setOption(resConfig, tool, option, 
									(enumId != null && enumId.length() > 0) ? enumId : enumVal);
						} else {
							setOption = ManagedBuildManager.setOption(configuration, tool, option, 
									(enumId != null && enumId.length() > 0) ? enumId : enumVal);
						}
						// Reset the preference store since the Id may have changed
						if (setOption != option) {
							getToolSettingsPreferenceStore().setValue(setOption.getId(), enumVal);
							FieldEditor fe = (FieldEditor)fieldsMap.get(option.getId());
							fe.setPreferenceName(setOption.getId());
						}
						break;
					case IOption.STRING :
						String strVal = getToolSettingsPreferenceStore().getString(option.getId());
						if(isItResourceConfigPage){
							setOption = ManagedBuildManager.setOption(resConfig, tool, option, strVal);
						} else {
							setOption = ManagedBuildManager.setOption(configuration, tool, option, strVal);	
						}
						
						// Reset the preference store since the Id may have changed
						if (setOption != option) {
							getToolSettingsPreferenceStore().setValue(setOption.getId(), strVal);
							FieldEditor fe = (FieldEditor)fieldsMap.get(option.getId());
							fe.setPreferenceName(setOption.getId());
						}
						break;
					case IOption.STRING_LIST :
					case IOption.INCLUDE_PATH :
					case IOption.PREPROCESSOR_SYMBOLS :
					case IOption.LIBRARIES :
					case IOption.OBJECTS :
						String listStr = getToolSettingsPreferenceStore().getString(option.getId());
						String[] listVal = BuildToolsSettingsStore.parseString(listStr);
						if( isItResourceConfigPage){
							setOption = ManagedBuildManager.setOption(resConfig, tool, option, listVal);
						}else {
							setOption = ManagedBuildManager.setOption(configuration, tool, option, listVal);	
						}
						
						// Reset the preference store since the Id may have changed
						if (setOption != option) {
							getToolSettingsPreferenceStore().setValue(setOption.getId(), listStr);
							FieldEditor fe = (FieldEditor)fieldsMap.get(option.getId());
							fe.setPreferenceName(setOption.getId());
						}
						break;
					default :
						break;
				}
			} catch (BuildException e) {}
		}
		return ok;
	}
	
	/**
	 * Update field editors in this page when the page is loaded.
	 */
	public void updateFields() {
		Collection fieldsList = fieldsMap.values();
		Iterator iter = fieldsList.iterator();
		while (iter.hasNext()) {
			FieldEditor editor = (FieldEditor) iter.next();
			//  TODO: Why was loadDefault called before load?  It results in String fields 
			//        setting the "dirty" flag
			//editor.loadDefault();
			editor.load();
		}
	}
	
	/**
	 * saves all field editors
	 */
	public void storeSettings() {
		super.performOk();
	}
	
	private void setFieldEditorEnablement(ITool tool, IOptionApplicability optionApplicability,
			FieldEditor fieldEditor, Composite parent) {
		if (optionApplicability == null)
			return;

		// if the option is not enabled then disable it
		if (!optionApplicability.isOptionEnabled(tool)) {
			fieldEditor.setEnabled(false, parent);
		} else {
			fieldEditor.setEnabled(true, parent);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
	 */
	public void propertyChange(PropertyChangeEvent event) {
		// allow superclass to handle as well
		super.propertyChange(event);

		// some option has changed on this page... update enabled/disabled state for all options

		Object[][] options;
		if (isItResourceConfigPage) {
			options = category.getOptions(resConfig);
		} else {
			options = category.getOptions(configuration);
		}

		for (int index = 0; index < options.length; ++index) {
			// Get the option
			ITool tool = (ITool) options[index][0];
			if (tool == null)
				break; //  The array may not be full
			IOption opt = (IOption) options[index][1];

			// is the option on this page?
			if (fieldsMap.containsKey(opt.getId())) {
				// check to see if the option has an applicability calculator
				IOptionApplicability applicabilityCalculator = opt.getApplicabilityCalculator();

				if (applicabilityCalculator != null) {
					FieldEditor fieldEditor = (FieldEditor) fieldsMap.get(opt.getId());
					Composite parent = (Composite) fieldEditorsToParentMap.get(fieldEditor);
					setFieldEditorEnablement(tool, applicabilityCalculator, fieldEditor, parent);
				}
			}

		}
	}

}

Back to the top