Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 42e1a7dcab4519dd0995e0afd7d50e09699c071b (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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
/*******************************************************************************
 * Copyright (c) 2003, 2011 IBM 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:
 * IBM Rational Software - Initial API and implementation
 * ARM Ltd. - basic tooltip support
 * Miwako Tokugawa (Intel Corporation) - Fixed-location tooltip support
 * Baltasar Belyavsky (Texas Instruments) - custom field-editor support
 *******************************************************************************/
package org.eclipse.cdt.managedbuilder.ui.properties;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.eclipse.cdt.core.settings.model.MultiItemsHolder;
import org.eclipse.cdt.managedbuilder.core.BuildException;
import org.eclipse.cdt.managedbuilder.core.IBuildObject;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IHoldsOptions;
import org.eclipse.cdt.managedbuilder.core.IManagedOptionValueHandler;
import org.eclipse.cdt.managedbuilder.core.IOption;
import org.eclipse.cdt.managedbuilder.core.IOption.ITreeOption;
import org.eclipse.cdt.managedbuilder.core.IOption.ITreeRoot;
import org.eclipse.cdt.managedbuilder.core.IOptionApplicability;
import org.eclipse.cdt.managedbuilder.core.IOptionCategory;
import org.eclipse.cdt.managedbuilder.core.IResourceInfo;
import org.eclipse.cdt.managedbuilder.core.ITool;
import org.eclipse.cdt.managedbuilder.core.IToolChain;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
import org.eclipse.cdt.managedbuilder.internal.core.MultiResourceInfo;
import org.eclipse.cdt.managedbuilder.internal.ui.Messages;
import org.eclipse.cdt.managedbuilder.macros.BuildMacroException;
import org.eclipse.cdt.managedbuilder.macros.IBuildMacroProvider;
import org.eclipse.cdt.ui.newui.AbstractPage;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.DirectoryFieldEditor;
import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.jface.preference.FileFieldEditor;
import org.eclipse.jface.preference.StringButtonFieldEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
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.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.FilteredTree;
import org.eclipse.ui.dialogs.PatternFilter;

/**
 * Option settings page in project properties Build Settings under Tool Settings tab.
 */
public class BuildOptionSettingsUI extends AbstractToolSettingUI {
	private Map<String, FieldEditor> fieldsMap =
		new HashMap<String, FieldEditor>();
	private IOptionCategory category;
	private IHoldsOptions optionHolder;
	/** Option Holders involved */
	private IHoldsOptions[] ohs;
	/** The index of the current IHoldsOptions in ohs */
	private int curr = -1;
	private Map<String, CustomFieldEditorDescriptor> customFieldEditorDescriptorIndex;
	private Map<FieldEditor, Composite> fieldEditorsToParentMap =
		new HashMap<FieldEditor, Composite>();
	/** True if the user selected "Display tool option tips at a fixed location" in Preferences */
	private boolean displayFixedTip;
	/** type of mouse action the displayFixedTip responds to.
	 ** currently set to Enter rather than Hover since the former seems more responsive **/
	private final static int selectAction = SWT.MouseEnter;

	private final class TreeBrowseFieldEditor extends StringButtonFieldEditor {
		private final String nameStr;
		private final IOption option;
		private String contextId;

		private TreeBrowseFieldEditor(String name, String labelText, Composite parent, String nameStr,
				IOption option, String contextId) {
			super(name, labelText, parent);
			this.nameStr = nameStr;
			this.option = option;
			this.contextId = contextId;
		}

		@Override
		protected String changePressed() {
			ITreeRoot treeRoot;
			try {
				treeRoot = option.getTreeRoot();
				TreeSelectionDialog dlg = new TreeSelectionDialog(getShell(), treeRoot, nameStr, contextId);
				if (dlg.open() == Window.OK) {
					ITreeOption selected = dlg.getSelection();
					return selected.getName();
				}
			} catch (BuildException e) {
			}
			return null;
		}
	}

	private class TipInfo {
		private String name;
		private String tip;

		public TipInfo(String name, String tip) {
			this.name = name;
			this.tip = tip;
		}
		protected String getName() {
			return name;
		}
		protected String getTip() {
			return tip;
		}
	}


	public BuildOptionSettingsUI(AbstractCBuildPropertyTab page,
			IResourceInfo info, IHoldsOptions optionHolder,
			IOptionCategory _category) {
		this(page, info, optionHolder, _category, false);
	}

	/**
	 * @param page - parent page
	 * @param info - resource info
	 * @param optionHolder - option holder (i.e. tool)
	 * @param cat - option category
	 * @param displayFixedTip - {@code true} if tooltips for the option are
	 *    displayed at fixed area on the bottom of the dialog or
	 *    {@code false} as a regular tooltip hover
	 *
	 * @since 7.0
	 */
	public BuildOptionSettingsUI(AbstractCBuildPropertyTab page,
			IResourceInfo info, IHoldsOptions optionHolder,
			IOptionCategory cat, boolean displayFixedTip) {
		super(info);
		this.category = cat;
		this.displayFixedTip = displayFixedTip;
		this.optionHolder = optionHolder;
		buildPropPage = page;
		if (info instanceof MultiItemsHolder) {
			MultiResourceInfo mri = (MultiResourceInfo)info;
			IResourceInfo[] ris = (IResourceInfo[])mri.getItems();
			String id = category.getId();

			/*
			 * Collect together all the IHoldsOptions (ITools & IToolChains)
			 * from the MultiResourceInfo's set of selected configs
			 * which contain the option category and accept the input type
			 * of this option holder.
			 */
			ArrayList<IHoldsOptions> lst = new ArrayList<IHoldsOptions>();
			if (optionHolder instanceof ITool) {
				String ext = ((ITool)optionHolder).getDefaultInputExtension();
				for (int i=0; i<ris.length; i++) {
					ITool[] ts = ris[i].getTools();
					for (int j=0; j<ts.length; j++) {
						IOptionCategory op = ts[j].getOptionCategory(id);
						if (op != null) {
							if (ext.equals(ts[j].getDefaultInputExtension())) {
								lst.add(ts[j]);
							}
						}
					}
				}
			} else if (optionHolder instanceof IToolChain) {
				for (int i=0; i<ris.length; i++) {
					IToolChain tc = ris[i].getParent().getToolChain();
					IOptionCategory op = tc.getOptionCategory(id);
					if (op != null)
						lst.add(tc);
				}
			}

			ohs = lst.toArray(new IHoldsOptions[lst.size()]);
			for (int i=0; i<ohs.length; i++) {
				if (ohs[i].equals(optionHolder)) {
					curr = i;
					break;
				}
			}
		} else {
			ohs = null;
			curr = 0;
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.IPreferencePage#computeSize()
	 */
	@Override
	public Point computeSize() {
		return super.computeSize();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.FieldEditorPreferencePage#Editors()
	 */
	@Override
	protected void createFieldEditors() {
		// true if the user selected "Display tool option tips at a fixed location" in Preferences AND
		// and we are displaying the tool tip box on this page because one or more option has non-empty tool tip.
		boolean pageHasToolTipBox = isToolTipBoxNeeded();

		// 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 = category.getOptions(fInfo, optionHolder);

		for (int index = 0; index < options.length; ++index) {
			// Get the option
			IHoldsOptions holder = (IHoldsOptions)options[index][0];
			if (holder == 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();
			IBuildObject config = fInfo;

			if (applicabilityCalculator == null || applicabilityCalculator.isOptionVisible(config, holder, opt)) {

				String optId = getToolSettingsPrefStore().getOptionId(opt);
				final String nameStr = TextProcessor.process(opt.getName());
				String tipStr = TextProcessor.process(opt.getToolTip());
				String contextId = opt.getContextId();

				if (pageHasToolTipBox && (tipStr==null || tipStr.trim().length()==0)) {
					tipStr = Messages.BuildOptionSettingsUI_0;
				}

				try {
					// Figure out which type the option is and add a proper field
					// editor for it
					Composite fieldEditorParent = getFieldEditorParent();
					FieldEditor fieldEditor = null;

					String customFieldEditorId = opt.getFieldEditorId();
					if(customFieldEditorId != null) {
						fieldEditor = createCustomFieldEditor(customFieldEditorId);
						if(fieldEditor != null) {
							ICustomBuildOptionEditor customFieldEditor = (ICustomBuildOptionEditor)fieldEditor;
					        if(customFieldEditor.init(opt, opt.getFieldEditorExtraArgument(), optId, fieldEditorParent)) {
								Control[] toolTipSources = customFieldEditor.getToolTipSources();
								if(toolTipSources != null) {
									for(Control control : toolTipSources) {
										if(pageHasToolTipBox) {
											control.setData(new TipInfo(nameStr,tipStr));
											control.addListener(selectAction, tipSetListener);
										}
										else {
											control.setToolTipText(tipStr);
										}
									}
								}
					        }
					        else {
					        	fieldEditor = null;
					        }
						}
					}

					if(fieldEditor == null) {
						switch (opt.getValueType()) {
						case IOption.STRING: {
							StringFieldEditor stringField;

							// If browsing is set, use a field editor that has a
							// browse button of the appropriate type.
							switch (opt.getBrowseType()) {
								case IOption.BROWSE_DIR: {
									stringField = new DirectoryFieldEditor(optId, nameStr, fieldEditorParent);
									if(opt.getBrowseFilterPath() != null) {
										try {
											String filterPath = ManagedBuildManager.getBuildMacroProvider().resolveValue(opt.getBrowseFilterPath(),
													null, null, IBuildMacroProvider.CONTEXT_OPTION, opt.getOptionContextData(holder));
											((DirectoryFieldEditor)stringField).setFilterPath(new File(filterPath));
										} catch(BuildMacroException bmx) {
											ManagedBuilderUIPlugin.log(bmx);
										}
									}
								} break;

								case IOption.BROWSE_FILE: {
									stringField = new FileFieldEditor(optId, nameStr, fieldEditorParent) {
										/**
										 * Do not perform validity check on the file name due to losing focus,
										 * see http://bugs.eclipse.org/289448
										 */
										@Override
										protected boolean checkState() {
											clearErrorMessage();
											return true;
										}
									};
									if(opt.getBrowseFilterPath() != null) {
										try {
											String filterPath = ManagedBuildManager.getBuildMacroProvider().resolveValue(opt.getBrowseFilterPath(),
													null, null, IBuildMacroProvider.CONTEXT_OPTION, opt.getOptionContextData(holder));
											((FileFieldEditor)stringField).setFilterPath(new File(filterPath));
										} catch(BuildMacroException bmx) {
											ManagedBuilderUIPlugin.log(bmx);
										}
									}
									((FileFieldEditor)stringField).setFileExtensions(opt.getBrowseFilterExtensions());
								} break;

								case IOption.BROWSE_NONE: {
									final StringFieldEditorM local = new StringFieldEditorM(optId, nameStr, fieldEditorParent);
									stringField = local;
									local.getTextControl().addModifyListener(new ModifyListener() {
							            @Override
										public void modifyText(ModifyEvent e) {
							            	local.valueChanged();
							            }
									});
								} break;

								default: {
									throw new BuildException(null);
								}
							}
							Label label = stringField.getLabelControl(fieldEditorParent);
							Text text = stringField.getTextControl(fieldEditorParent);
							if (pageHasToolTipBox) {
								label.setData(new TipInfo(nameStr,tipStr));
								label.addListener(selectAction, tipSetListener);
								text.setData(new TipInfo(nameStr,tipStr));
								text.addListener(selectAction, tipSetListener);
							} else {
								label.setToolTipText(tipStr);
								text.setToolTipText(tipStr);
							}
							if (!contextId.equals(AbstractPage.EMPTY_STR)) {
								PlatformUI.getWorkbench().getHelpSystem().setHelp(text, contextId);
							}
							fieldEditor = stringField;
						} break;

						case IOption.BOOLEAN: {
							fieldEditor = new TriStateBooleanFieldEditor(
									optId,
									nameStr,
									tipStr,
									fieldEditorParent,
									contextId,
									ohs,
									curr);
							// tipStr is handled in TriStateBooleanFieldEditor constructor
						} break;

						case IOption.ENUMERATED: {
							String selId = opt.getSelectedEnum();
							String sel = opt.getEnumName(selId);

							// Get all applicable values for this enumerated Option, But display
							// only the enumerated values that are valid (static set of enumerated values defined
							// in the plugin.xml file) in the UI Combobox. This refrains the user from selecting an
							// invalid value and avoids issuing an error message.
							String[] enumNames = opt.getApplicableValues();
							Vector<String> enumValidList = new Vector<String>();
							for (int i = 0; i < enumNames.length; ++i) {
								if (opt.getValueHandler().isEnumValueAppropriate(config,
										opt.getOptionHolder(), opt, opt.getValueHandlerExtraArgument(), enumNames[i])) {
									enumValidList.add(enumNames[i]);
								}
							}
							String[] enumValidNames = new String[enumValidList.size()];
							enumValidList.copyInto(enumValidNames);

							// if (displayFixedTip==false), tooltip was already set in BuildOptionComboFieldEditor constructor.
							String tooltipHoverStr = displayFixedTip ? null : tipStr;
							fieldEditor = new BuildOptionComboFieldEditor(optId, nameStr,
									tooltipHoverStr, contextId, enumValidNames, sel, fieldEditorParent);

							if (pageHasToolTipBox) {
								Combo combo = ((BuildOptionComboFieldEditor)fieldEditor).getComboControl();
								Label label = fieldEditor.getLabelControl(fieldEditorParent);
								combo.setData(new TipInfo(nameStr,tipStr));
								combo.addListener(selectAction, tipSetListener);
								label.setData(new TipInfo(nameStr,tipStr));
								label.addListener(selectAction, tipSetListener);
							}
						} break;

						case IOption.TREE:
							fieldEditor = new TreeBrowseFieldEditor(optId, nameStr, fieldEditorParent, nameStr, opt, contextId);
							((StringButtonFieldEditor)fieldEditor).setChangeButtonText("..."); //$NON-NLS-1$

							if (pageHasToolTipBox) {
								Text text = ((StringButtonFieldEditor)fieldEditor).getTextControl(fieldEditorParent);
								Label label = fieldEditor.getLabelControl(fieldEditorParent);
								text.setData(new TipInfo(nameStr,tipStr));
								text.addListener(selectAction, tipSetListener);
								label.setData(new TipInfo(nameStr,tipStr));
								label.addListener(selectAction, tipSetListener);
							}
							break;

						case IOption.INCLUDE_PATH:
						case IOption.STRING_LIST:
						case IOption.PREPROCESSOR_SYMBOLS:
						case IOption.LIBRARIES:
						case IOption.OBJECTS:
						case IOption.INCLUDE_FILES:
						case IOption.LIBRARY_PATHS:
						case IOption.LIBRARY_FILES:
						case IOption.MACRO_FILES:
						case IOption.UNDEF_INCLUDE_PATH:
						case IOption.UNDEF_PREPROCESSOR_SYMBOLS:
						case IOption.UNDEF_INCLUDE_FILES:
						case IOption.UNDEF_LIBRARY_PATHS:
						case IOption.UNDEF_LIBRARY_FILES:
						case IOption.UNDEF_MACRO_FILES:
						{
							 // if (displayFixedTip==false), tooltip was already set in FileListControlFieldEditor constructor.
							String tooltipHoverStr = displayFixedTip ? null : tipStr;
							fieldEditor = new FileListControlFieldEditor(optId, nameStr,
									tooltipHoverStr, contextId, fieldEditorParent, opt.getBrowseType());
							if(opt.getBrowseFilterPath() != null) {
								try {
									String filterPath = ManagedBuildManager.getBuildMacroProvider().resolveValue(opt.getBrowseFilterPath(),
											null, null, IBuildMacroProvider.CONTEXT_OPTION, opt.getOptionContextData(holder));
									((FileListControlFieldEditor)fieldEditor).setFilterPath(filterPath);
								} catch(BuildMacroException bmx) {
									ManagedBuilderUIPlugin.log(bmx);
								}
							}
							((FileListControlFieldEditor)fieldEditor).setFilterExtensions(opt.getBrowseFilterExtensions());

							if (pageHasToolTipBox) {
								Label label = fieldEditor.getLabelControl(fieldEditorParent);
								label.setData(new TipInfo(nameStr,tipStr));
								label.addListener(selectAction, tipSetListener);
							}
						} break;

						default:
							throw new BuildException(null);
						}
					}

					setFieldEditorEnablement(holder, opt, applicabilityCalculator, fieldEditor, fieldEditorParent);

					addField(fieldEditor);
					fieldsMap.put(optId, fieldEditor);
					fieldEditorsToParentMap.put(fieldEditor, fieldEditorParent);

				} catch (BuildException e) {
				}
			}
		}
	}

	/**
	 * Instantiates the custom-field editor registered under the given id.
	 */
	private FieldEditor createCustomFieldEditor(String customFieldEditorId) {
		if(this.customFieldEditorDescriptorIndex == null) {
			loadCustomFieldEditorDescriptors();
		}

		CustomFieldEditorDescriptor editorDescriptor = this.customFieldEditorDescriptorIndex.get(customFieldEditorId);
		if(editorDescriptor != null) {
			return editorDescriptor.createEditor();
		}

		return null;
	}

	/**
	 * Holds all the information necessary to instantiate a custom field-editor.
	 * Also acts as a factory - instantiates and returns a non-initialized field-editor.
	 */
	private class CustomFieldEditorDescriptor
	{
		private final IConfigurationElement element;

		public CustomFieldEditorDescriptor(IConfigurationElement providerElement) {
			this.element = providerElement;
		}

		FieldEditor createEditor() {
			try {
				Object editor = element.createExecutableExtension("class"); //$NON-NLS-1$
				if(editor instanceof FieldEditor && editor instanceof ICustomBuildOptionEditor) {
					return (FieldEditor)editor;
				}
			}
			catch(Exception x) {
				ManagedBuilderUIPlugin.log(x);
			}

			return null;
		}
	}

	/**
	 * Loads all the registered custom field-editor descriptors.
	 * Synchronization is not necessary as this would always be invoked on the UI thread.
	 */
	private void loadCustomFieldEditorDescriptors() {
		if(this.customFieldEditorDescriptorIndex != null)
			return;

		this.customFieldEditorDescriptorIndex = new HashMap<String, CustomFieldEditorDescriptor>();

		IExtensionPoint ep = Platform.getExtensionRegistry().getExtensionPoint(
				ManagedBuilderUIPlugin.getUniqueIdentifier() + ".buildDefinitionsUI"); //$NON-NLS-1$

		for(IExtension e : ep.getExtensions()) {
			for(IConfigurationElement providerElement : e.getConfigurationElements()) {
				String editorId = providerElement.getAttribute("id"); //$NON-NLS-1$

				this.customFieldEditorDescriptorIndex.put(editorId, new CustomFieldEditorDescriptor(providerElement));
			}
		}
	}

	/**
	 * Answers <code>true</code> if the settings page has been created for the
	 * option category specified in the argument.
	 *
	 * @see org.eclipse.cdt.managedbuilder.ui.properties.AbstractToolSettingUI#isFor(java.lang.Object, java.lang.Object)
	 */
	@Override
	public boolean isFor(Object holder, Object cat) {
		if (holder instanceof IHoldsOptions && cat != null && cat instanceof IOptionCategory) {
			if (holder == this.optionHolder && cat.equals(this.category))
				return true;
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.IPreferencePage#performOk()
	 */
	@Override
	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[][] clonedOptions;
//		IResourceConfiguration realRcCfg = null;
		IConfiguration realCfg = null;
		IBuildObject handler = null;

		realCfg = buildPropPage.getCfg(); //.getRealConfig(clonedConfig);
		if(realCfg == null)	return false;
		handler = realCfg;
		clonedOptions = category.getOptions(fInfo, optionHolder);

		for (int i = 0; i < clonedOptions.length; i++) {
			IHoldsOptions clonedHolder = (IHoldsOptions)clonedOptions[i][0];
			if (clonedHolder == null) break;	//  The array may not be full
			IOption clonedOption = (IOption)clonedOptions[i][1];

			IHoldsOptions realHolder = clonedHolder; // buildPropPage.getRealHoldsOptions(clonedHolder);
			IOption realOption = clonedOption; // buildPropPage.getRealOption(clonedOption, clonedHolder);
			if(realOption == null) continue;

			try {
				// Transfer value from preference store to options
				IOption setOption = null;
				switch (clonedOption.getValueType()) {
					case IOption.BOOLEAN :
						boolean boolVal = clonedOption.getBooleanValue();
						setOption = ManagedBuildManager.setOption(realCfg, realHolder, realOption, boolVal);
						// Reset the preference store since the Id may have changed
//						if (setOption != option) {
//							getToolSettingsPrefStore().setValue(setOption.getId(), boolVal);
//							FieldEditor fe = (FieldEditor)fieldsMap.get(option.getId());
//							fe.setPreferenceName(setOption.getId());
//						}
						break;
					case IOption.ENUMERATED :
					case IOption.TREE :
						String enumVal = clonedOption.getStringValue();
						String enumId = clonedOption.getId(enumVal);
						setOption = ManagedBuildManager.setOption(realCfg, realHolder, realOption,
								(enumId != null && enumId.length() > 0) ? enumId : enumVal);
						// Reset the preference store since the Id may have changed
//						if (setOption != option) {
//							getToolSettingsPrefStore().setValue(setOption.getId(), enumVal);
//							FieldEditor fe = (FieldEditor)fieldsMap.get(option.getId());
//							fe.setPreferenceName(setOption.getId());
//					}
						break;
					case IOption.STRING :
						String strVal = clonedOption.getStringValue();
						setOption = ManagedBuildManager.setOption(realCfg, realHolder, realOption, strVal);
						// Reset the preference store since the Id may have changed
//						if (setOption != option) {
//							getToolSettingsPrefStore().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 :
					case IOption.INCLUDE_FILES:
					case IOption.LIBRARY_PATHS:
					case IOption.LIBRARY_FILES:
					case IOption.MACRO_FILES:
					case IOption.UNDEF_INCLUDE_PATH:
					case IOption.UNDEF_PREPROCESSOR_SYMBOLS:
					case IOption.UNDEF_INCLUDE_FILES:
					case IOption.UNDEF_LIBRARY_PATHS:
					case IOption.UNDEF_LIBRARY_FILES:
					case IOption.UNDEF_MACRO_FILES:
						@SuppressWarnings("unchecked")
						String[] listVal = ((List<String>)clonedOption.getValue()).toArray(new String[0]);
						setOption = ManagedBuildManager.setOption(realCfg, realHolder, realOption, listVal);

						// Reset the preference store since the Id may have changed
//						if (setOption != option) {
//							getToolSettingsPrefStore().setValue(setOption.getId(), listStr);
//							FieldEditor fe = (FieldEditor)fieldsMap.get(option.getId());
//							fe.setPreferenceName(setOption.getId());
//						}
						break;
					default :
						break;
				}

				// Call an MBS CallBack function to inform that Settings related to Apply/OK button
				// press have been applied.
				if (setOption == null)
					setOption = realOption;

				if (setOption.getValueHandler().handleValue(
						handler,
						setOption.getOptionHolder(),
						setOption,
						setOption.getValueHandlerExtraArgument(),
						IManagedOptionValueHandler.EVENT_APPLY)) {
					// TODO : Event is handled successfully and returned true.
					// May need to do something here say log a message.
				} else {
					// Event handling Failed.
				}

			} catch (BuildException e) {
			} catch (ClassCastException e) {
			}


		}
		return ok;
	}

	/**
	 * Update field editors in this page when the page is loaded.
	 */
	@Override
	public void updateFields() {
		Object[][] options = category.getOptions(fInfo, optionHolder);
		// some option has changed on this page... update enabled/disabled state for all options

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

			// is the option on this page?
			if (fieldsMap.containsKey(prefName)) {
				FieldEditor fieldEditor = fieldsMap.get(prefName);
				try {
					if ( opt.getValueType() == IOption.ENUMERATED ) {
						updateEnumList( fieldEditor, opt, holder, fInfo );
					}
				} catch ( BuildException be ) {}

				// check to see if the option has an applicability calculator
				IOptionApplicability applicabilityCalculator = opt.getApplicabilityCalculator();
				if (applicabilityCalculator != null) {
					Composite parent = fieldEditorsToParentMap.get(fieldEditor);
					setFieldEditorEnablement(holder, opt, applicabilityCalculator, fieldEditor, parent);
				}
			}
		}

		Collection<FieldEditor> fieldsList = fieldsMap.values();
		for (FieldEditor editor : fieldsList) {
			if (editor instanceof TriStateBooleanFieldEditor)
				((TriStateBooleanFieldEditor)editor).set3(true);
			editor.load();
		}
	}

	private void setFieldEditorEnablement(IHoldsOptions holder, IOption option,
			IOptionApplicability optionApplicability, FieldEditor fieldEditor, Composite parent) {
		if (optionApplicability == null)
			return;

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

	private boolean hasStr(String tipStr) {
		return (tipStr!=null && tipStr.trim().length()>0);
	}

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

		Object source = event.getSource();
		IOption changedOption = null;
		IHoldsOptions changedHolder = null;
		String id = null;

		if(source instanceof FieldEditor){
			FieldEditor fe = (FieldEditor)source;

			if (fe instanceof TriStateBooleanFieldEditor)
				((TriStateBooleanFieldEditor)fe).set3(false);

			id = fe.getPreferenceName();

			Object[] option = this.getToolSettingsPrefStore().getOption(id);

			if (option == null) {
				int n = id.lastIndexOf('.');
				if (n > 0) {
					id = id.substring(0, n);
					option = getToolSettingsPrefStore().getOption(id);
				}
			}

			if(option != null){
				changedOption = (IOption)option[1];
				changedHolder = (IHoldsOptions)option[0];
				try {
					switch(changedOption.getValueType()){
					case IOption.STRING:
						if(fe instanceof StringFieldEditor){
							String val = ((StringFieldEditor)fe).getStringValue();
							ManagedBuildManager.setOption(fInfo,changedHolder,changedOption,val);
						}
						break;
					case IOption.BOOLEAN:
						if(fe instanceof BooleanFieldEditor){
							boolean val = ((BooleanFieldEditor)fe).getBooleanValue();
							ManagedBuildManager.setOption(fInfo,changedHolder,changedOption,val);
						}
						break;
					case IOption.ENUMERATED:
						if(fe instanceof BuildOptionComboFieldEditor){
							String name = ((BuildOptionComboFieldEditor)fe).getSelection();
							String enumId = changedOption.getEnumeratedId(name);
							ManagedBuildManager.setOption(fInfo,changedHolder,changedOption,
									(enumId != null && enumId.length() > 0) ? enumId : name);

						}
						break;
					case IOption.TREE:
						if(fe instanceof TreeBrowseFieldEditor){
							String name = ((TreeBrowseFieldEditor)fe).getStringValue();
							String treeId = changedOption.getId(name);
							ManagedBuildManager.setOption(fInfo,changedHolder,changedOption,
									(treeId != null && treeId.length() > 0) ? treeId : name);

						}
						break;
					case IOption.INCLUDE_PATH:
					case IOption.STRING_LIST:
					case IOption.PREPROCESSOR_SYMBOLS:
					case IOption.LIBRARIES:
					case IOption.OBJECTS:
					case IOption.INCLUDE_FILES:
					case IOption.LIBRARY_PATHS:
					case IOption.LIBRARY_FILES:
					case IOption.MACRO_FILES:
					case IOption.UNDEF_INCLUDE_PATH:
					case IOption.UNDEF_PREPROCESSOR_SYMBOLS:
					case IOption.UNDEF_INCLUDE_FILES:
					case IOption.UNDEF_LIBRARY_PATHS:
					case IOption.UNDEF_LIBRARY_FILES:
					case IOption.UNDEF_MACRO_FILES:
						if(fe instanceof FileListControlFieldEditor){
							String val[] =((FileListControlFieldEditor)fe).getStringListValue();
							ManagedBuildManager.setOption(fInfo, changedHolder, changedOption, val);
						}
						break;
					default:
						break;
					}
				} catch (BuildException e) {}
			}
		}

		Object[][] options = category.getOptions(fInfo, optionHolder);

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

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

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

				FieldEditor fieldEditor = fieldsMap.get(optId);
				try {
					if ( opt.getValueType() == IOption.ENUMERATED ) {
						// the item list of this enumerated option may have changed, update it
						updateEnumList( fieldEditor, opt, holder, fInfo );
					}
				} catch ( BuildException be ) {}

				if (applicabilityCalculator != null) {
					Composite parent = fieldEditorsToParentMap.get(fieldEditor);
					setFieldEditorEnablement(holder, opt, applicabilityCalculator, fieldEditor, parent);
				}
			}
		}

		Collection<FieldEditor> xxx = fieldsMap.values();
		for (FieldEditor editor : xxx) {
			if(id == null || !id.equals(editor.getPreferenceName()))
				editor.load();
		}
	}

	@Override
	public void setValues() {
		updateFields();
	}

	/**
	 * @param optionHolder - option holder such as {@link ITool}
	 * @param category - option category
	 *
	 * @return true if the page needs to have the tool tip box.
	 *
	 * @since 7.0
	 */
	protected boolean needToolTipBox(IHoldsOptions optionHolder, IOptionCategory category) {
		if (optionHolder instanceof ITool) { // option category page
			Object[][] options = category.getOptions(fInfo, optionHolder);
			for (int index = 0; index < options.length; ++index) {
				IHoldsOptions holder = (IHoldsOptions)options[index][0];
				if (holder == null) break; //  The array may not be full
				IOption opt = (IOption)options[index][1];
				String tipStr = TextProcessor.process(opt.getToolTip());

				// check to see if the option has an applicability calculator
				IOptionApplicability applicabilityCalculator = opt.getApplicabilityCalculator();
				IBuildObject config = fInfo;

				if (applicabilityCalculator == null || applicabilityCalculator.isOptionVisible(config, holder, opt)) {
					if (hasStr(tipStr)) {
						return true; // an option with a tip string was found.
					}
				}
			}
		}
		// A tool option summary page does not list individual options
		// so never should have the box
		return false;
	}

	/**
	 * The items shown in an enumerated option may depend on other option values.
	 * Whenever an option changes, check and update the valid enum values in
	 * the combo fieldeditor.
	 *
	 * See also https://bugs.eclipse.org/bugs/show_bug.cgi?id=154053
	 *
	 * @param fieldEditor enumerated combo fieldeditor
	 * @param opt         enumerated option type to update
	 * @param holder      the option holder
	 * @param config      project or resource info
	 * @throws BuildException
	 */
	protected void updateEnumList( FieldEditor fieldEditor, IOption opt, IHoldsOptions holder, IResourceInfo config ) throws BuildException	{
		// Get all applicable values for this enumerated Option, and filter out
		// the disable values
		String[] enumNames = opt.getApplicableValues();

		// get the currently selected enum value, the updated enum list may not contain
		// it, in that case a new value has to be selected
		String selectedEnum = opt.getSelectedEnum();
		String selectedEnumName = opt.getEnumName(selectedEnum);

		// get the default value for this enumerated option
		String defaultEnumId = (String)opt.getDefaultValue();
		String defaultEnumName = opt.getEnumName(defaultEnumId);

		boolean selectNewEnum = true;
		boolean selectDefault = false;

		Vector<String> enumValidList = new Vector<String>();
		for (int i = 0; i < enumNames.length; ++i) {
			if (opt.getValueHandler().isEnumValueAppropriate(config,
					opt.getOptionHolder(), opt, opt.getValueHandlerExtraArgument(), enumNames[i])) {
				if ( selectedEnumName.equals(enumNames[i]) ) {
					// the currently selected enum is part of the new item list, no need to select a new value.
					selectNewEnum = false;
				}
				if ( defaultEnumName.equals(enumNames[i]) ) {
					// the default enum value is part of new item list
					selectDefault = true;
				}
				enumValidList.add(enumNames[i]);
			}
		}
		String[] enumValidNames = new String[enumValidList.size()];
		enumValidList.copyInto(enumValidNames);

		if ( selectNewEnum ) {
			// apparently the currently selected enum value is not part anymore of the enum list
			// select a new value.
			String selection = null;
			if ( selectDefault ) {
				// the default enum value is part of the item list, use it
				selection = (String)opt.getDefaultValue();
			} else if ( enumValidNames.length > 0 ) {
				// select the first item in the item list
				selection = opt.getEnumeratedId(enumValidNames[0]);
			}
			ManagedBuildManager.setOption(config,holder,opt,selection);
		}
		((BuildOptionComboFieldEditor)fieldEditor).setOptions(enumValidNames);
		fieldEditor.load();
	}

	private final Listener tipSetListener = new Listener() {
		@Override
		public void handleEvent(Event event) {
			Object data = event.widget.getData();
			if (data!=null && buildPropPage!=null) {
				TipInfo obj = (TipInfo)data;
				((ToolSettingsTab)buildPropPage).updateTipText(obj.getName(), obj.getTip());
			}
		}
	};

	/**
	 *
	 *
	 *
	 */
	class TriStateBooleanFieldEditor extends BooleanFieldEditor {
		protected Button button = null;
		protected IHoldsOptions[] holders = null;
		private boolean enable3 = true;
		protected int current = 0;
		public TriStateBooleanFieldEditor(String name, String labelText, String tooltip, Composite parent, String contextId, IHoldsOptions[] ho, int curr) {
			super(name, labelText, parent);
			holders = ho;
			current = curr;
			button = getChangeControl(parent);
			if (displayFixedTip && isToolTipBoxNeeded()) {
				button.setData(new TipInfo(labelText,tooltip));
				button.addListener(selectAction, tipSetListener);
			} else {
				button.setToolTipText(tooltip);
			}
			if (!contextId.equals(AbstractPage.EMPTY_STR)) {
				PlatformUI.getWorkbench().getHelpSystem().setHelp(button, contextId);
			}

		}
		@Override
		protected void valueChanged(boolean oldValue, boolean newValue) {
			button.setGrayed(false);
			super.valueChanged(!newValue, newValue);
		}
		@Override
		protected void doLoad() {
			if (enable3 && holders != null && button != null) {
				String id = getPreferenceName();
				IOption op = holders[current].getOptionById(id);
				if (op != null) {
					if (op.getSuperClass() != null)
						id = op.getSuperClass().getId();
					int[] vals = new int[2];
					for (int i=0; i<holders.length; i++) {
						op = holders[i].getOptionBySuperClassId(id);
						try {
							if (op != null)
								vals[op.getBooleanValue() ? 1 : 0]++;
						} catch (BuildException e) {}
					}
					boolean value = false;
					boolean gray  = false;
					if (vals[1] > 0) {
						value = true;
						if (vals[0] > 0)
							gray = true;
					}
					button.setGrayed(gray);
					button.setSelection(value);
					return;
				}
			}
			super.doLoad(); // default case
		}

		void set3(boolean state) {
			enable3 = state;
		}
	}

	/**
	 * @since 8.1
	 */
	public static class TreeSelectionDialog extends TitleAreaDialog {
		private final ITreeRoot treeRoot;
		private ITreeOption selected;
		private final String name;
		private String contextId;
		private String baseMessage = ""; //$NON-NLS-1$

		public TreeSelectionDialog(Shell parentShell, ITreeRoot root, String name, String contextId) {
			super(parentShell);
			treeRoot = root;
			setShellStyle(getShellStyle() | SWT.RESIZE);
			if (root.getIcon() != null) {
				Image img = createImage(root.getIcon());
				if (img != null) {
					setTitleImage(img);
				}
			}
			this.name = name;
			this.contextId = contextId;
			if (contextId != null && contextId.length() > 0) {
				setHelpAvailable(true);
			}
		}

		@Override
		protected Control createDialogArea(Composite parent) {
			if (contextId != null && contextId.length() > 0) {
				PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, contextId);
			}

			Composite control = new Composite(parent, SWT.NULL);
			GridData gd= new GridData(GridData.FILL_BOTH);
			GridLayout topLayout = new GridLayout();
			topLayout.numColumns = 1;
			control.setLayout(topLayout);
			control.setLayoutData(gd);

			PatternFilter filter = new PatternFilter();
			filter.setIncludeLeadingWildcard(true);
			FilteredTree tree = new FilteredTree(control,
					SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER,
					filter, true);
			final TreeViewer viewer = tree.getViewer();
			viewer.setContentProvider(new ITreeContentProvider() {

				@Override
				public void dispose() {
				}

				@Override
				public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
				}

				@Override
				public Object[] getElements(Object inputElement) {
					return getChildren(inputElement);
				}

				@Override
				public Object[] getChildren(Object parentElement) {
					if (parentElement instanceof ITreeOption) {
						ITreeOption[] children = ((ITreeOption)parentElement).getChildren();

						// Not entirely sure whether this method is allowed to return null,
						// but let's play safe.
						if (children == null)
							return null;

						List<ITreeOption> childrenList = new ArrayList<ITreeOption>(Arrays.asList(children));

						// Check if any of the children has empty name
						List<ITreeOption> toRemove = null;
						for (ITreeOption child : children) {
							if (child.getName() == null || child.getName().trim().length() == 0) {
								if (toRemove == null) {
									toRemove = new ArrayList<ITreeOption>();
								}
								toRemove.add(child);
							}
						}
						if (toRemove != null) {
							childrenList.removeAll(toRemove);
						}

						// Sort the children.
						Collections.sort(childrenList, new Comparator<ITreeOption>() {
							@Override
							public int compare(ITreeOption arg0, ITreeOption arg1) {
								if (arg0.getOrder() == arg1.getOrder()) {
									return arg0.getName().compareToIgnoreCase(arg1.getName());
								} else {
									return arg0.getOrder() - arg1.getOrder();
								}
							}
						});

						return childrenList.toArray(new ITreeOption[0]);
					}
					return null;
				}

				@Override
				public Object getParent(Object element) {
					if (element instanceof ITreeOption) {
						return ((ITreeOption)element).getParent();
					}
					return null;
				}

				@Override
				public boolean hasChildren(Object element) {
					Object[] children = getChildren(element);
					return children != null && children.length > 0;
				}

			});

			viewer.setLabelProvider(new LabelProvider() {

				@Override
				public String getText(Object element) {
					if (element instanceof ITreeOption) {
						return ((ITreeOption)element).getName();
					}
					return super.getText(element);
				}

				@Override
				public Image getImage(Object element) {
					if (element instanceof ITreeOption) {
						String icon = ((ITreeOption)element).getIcon();
						return createImage(icon);
					}
					return super.getImage(element);
				}
			});

			viewer.addSelectionChangedListener(new ISelectionChangedListener() {

				@Override
				public void selectionChanged(SelectionChangedEvent event) {
					ISelection selection = event.getSelection();
					if (selection instanceof IStructuredSelection) {
						Object selectedObj = ((IStructuredSelection)selection).getFirstElement();
						if (selectedObj instanceof ITreeOption) {
							selected = (ITreeOption) selectedObj;

							updateOKButton(selected);

							// Adjust Message
							String description = selected.getDescription();
							if (description == null) {
								ITreeOption node = selected;
								description = ""; //$NON-NLS-1$
								String sep = ": "; //$NON-NLS-1$
								while (node != null && node.getParent() != null) { // ignore root
									description = sep + node.getName() + description;
									node = node.getParent();
								}
								description = description.substring(sep.length()); // remove the first separator.
							}
							setMessage(baseMessage + description);
						}
					}
				}
			});

			viewer.addDoubleClickListener(new IDoubleClickListener() {

				@Override
				public void doubleClick(DoubleClickEvent event) {
					ISelection selection = event.getSelection();
					if (!selection.isEmpty() && selection instanceof IStructuredSelection && ((IStructuredSelection)selection).size() == 1) {
						Object selectedNode = ((IStructuredSelection)selection).getFirstElement();
						if (selectedNode instanceof ITreeOption) {
							if (updateOKButton((ITreeOption)selectedNode)) {
								TreeSelectionDialog.this.okPressed();
							} else { // if doubleclick is not on selectable item, expand/collapse
								viewer.setExpandedState(selectedNode,!viewer.getExpandedState(selectedNode));
							}
						}
					}
				}
			});

			viewer.setInput(treeRoot);

			String msg = "Select " + name; //$NON-NLS-1$
			getShell().setText(msg);
			setTitle(msg);
			if (treeRoot.getDescription() != null) {
				baseMessage = treeRoot.getDescription();
				setMessage(baseMessage);
				baseMessage += "\nCurrent Selection: "; //$NON-NLS-1$
			} else {
				setMessage(msg);
			}

			return control;
		}

		public ITreeOption getSelection() {
			return selected;
		}

		private Image createImage(String icon) {
			if (icon != null) {
				URL url = null;
				try {
					url = FileLocator.find(new URL(icon));
				} catch (Exception e) {
				}
				if (url != null) {
					ImageDescriptor desc = ImageDescriptor.createFromURL(url);
					return desc.createImage();
				}
			}
			return null;
		}

		private boolean updateOKButton(ITreeOption selection) {
			// Check if Valid selection (only allow selecting leaf nodes)
			if (treeRoot.isSelectLeafsOnly()) {
				boolean enableOK = !selection.isContainer();
				getButton(IDialogConstants.OK_ID).setEnabled(enableOK);
				return enableOK;
			}
			return false;
		}
	}
}

Back to the top