KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > OptionsConfigurationBlock


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.preferences;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.IdentityHashMap JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.StringTokenizer JavaDoc;
19
20 import org.eclipse.core.runtime.preferences.DefaultScope;
21 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
22 import org.eclipse.core.runtime.preferences.IScopeContext;
23 import org.eclipse.core.runtime.preferences.InstanceScope;
24
25 import org.eclipse.core.resources.IProject;
26 import org.eclipse.core.resources.ProjectScope;
27
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.events.ModifyEvent;
30 import org.eclipse.swt.events.ModifyListener;
31 import org.eclipse.swt.events.SelectionEvent;
32 import org.eclipse.swt.events.SelectionListener;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.widgets.Button;
36 import org.eclipse.swt.widgets.Combo;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Control;
39 import org.eclipse.swt.widgets.Label;
40 import org.eclipse.swt.widgets.Link;
41 import org.eclipse.swt.widgets.Shell;
42 import org.eclipse.swt.widgets.Text;
43 import org.eclipse.swt.widgets.Widget;
44
45 import org.eclipse.jface.dialogs.IDialogConstants;
46 import org.eclipse.jface.dialogs.IDialogSettings;
47 import org.eclipse.jface.dialogs.MessageDialog;
48 import org.eclipse.jface.resource.JFaceResources;
49
50 import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
51 import org.eclipse.ui.preferences.IWorkingCopyManager;
52 import org.eclipse.ui.preferences.WorkingCopyManager;
53
54 import org.eclipse.ui.forms.events.ExpansionAdapter;
55 import org.eclipse.ui.forms.events.ExpansionEvent;
56 import org.eclipse.ui.forms.widgets.ExpandableComposite;
57
58 import org.eclipse.jdt.core.JavaCore;
59
60 import org.eclipse.jdt.ui.JavaUI;
61
62 import org.eclipse.jdt.internal.ui.JavaPlugin;
63 import org.eclipse.jdt.internal.ui.util.CoreUtility;
64 import org.eclipse.jdt.internal.ui.wizards.IStatusChangeListener;
65
66 import org.osgi.service.prefs.BackingStoreException;
67
68 /**
69  * Abstract options configuration block providing a general implementation for setting up
70  * an options configuration page.
71  *
72  * @since 2.1
73  */

74 public abstract class OptionsConfigurationBlock {
75     
76     public static final class Key {
77         
78         private String JavaDoc fQualifier;
79         private String JavaDoc fKey;
80         
81         public Key(String JavaDoc qualifier, String JavaDoc key) {
82             fQualifier= qualifier;
83             fKey= key;
84         }
85         
86         public String JavaDoc getName() {
87             return fKey;
88         }
89         
90         private IEclipsePreferences getNode(IScopeContext context, IWorkingCopyManager manager) {
91             IEclipsePreferences node= context.getNode(fQualifier);
92             if (manager != null) {
93                 return manager.getWorkingCopy(node);
94             }
95             return node;
96         }
97         
98         public String JavaDoc getStoredValue(IScopeContext context, IWorkingCopyManager manager) {
99             return getNode(context, manager).get(fKey, null);
100         }
101         
102         public String JavaDoc getStoredValue(IScopeContext[] lookupOrder, boolean ignoreTopScope, IWorkingCopyManager manager) {
103             for (int i= ignoreTopScope ? 1 : 0; i < lookupOrder.length; i++) {
104                 String JavaDoc value= getStoredValue(lookupOrder[i], manager);
105                 if (value != null) {
106                     return value;
107                 }
108             }
109             return null;
110         }
111         
112         public void setStoredValue(IScopeContext context, String JavaDoc value, IWorkingCopyManager manager) {
113             if (value != null) {
114                 getNode(context, manager).put(fKey, value);
115             } else {
116                 getNode(context, manager).remove(fKey);
117             }
118         }
119             
120         /* (non-Javadoc)
121          * @see java.lang.Object#toString()
122          */

123         public String JavaDoc toString() {
124             return fQualifier + '/' + fKey;
125         }
126
127         public String JavaDoc getQualifier() {
128             return fQualifier;
129         }
130
131     }
132     
133
134     protected static class ControlData {
135         private Key fKey;
136         private String JavaDoc[] fValues;
137         
138         public ControlData(Key key, String JavaDoc[] values) {
139             fKey= key;
140             fValues= values;
141         }
142         
143         public Key getKey() {
144             return fKey;
145         }
146         
147         public String JavaDoc getValue(boolean selection) {
148             int index= selection ? 0 : 1;
149             return fValues[index];
150         }
151         
152         public String JavaDoc getValue(int index) {
153             return fValues[index];
154         }
155         
156         public int getSelection(String JavaDoc value) {
157             if (value != null) {
158                 for (int i= 0; i < fValues.length; i++) {
159                     if (value.equals(fValues[i])) {
160                         return i;
161                     }
162                 }
163             }
164             return fValues.length -1; // assume the last option is the least severe
165
}
166     }
167     
168     private static final String JavaDoc REBUILD_COUNT_KEY= "preferences_build_requested"; //$NON-NLS-1$
169

170     private static final String JavaDoc SETTINGS_EXPANDED= "expanded"; //$NON-NLS-1$
171

172     protected final ArrayList JavaDoc fCheckBoxes;
173     protected final ArrayList JavaDoc fComboBoxes;
174     protected final ArrayList JavaDoc fTextBoxes;
175     protected final HashMap JavaDoc fLabels;
176     protected final ArrayList JavaDoc fExpandedComposites;
177     
178     private SelectionListener fSelectionListener;
179     private ModifyListener fTextModifyListener;
180
181     protected IStatusChangeListener fContext;
182     protected final IProject fProject; // project or null
183
protected final Key[] fAllKeys;
184     
185     private IScopeContext[] fLookupOrder;
186     
187     private Shell fShell;
188
189     private final IWorkingCopyManager fManager;
190     private IWorkbenchPreferenceContainer fContainer;
191
192     private Map JavaDoc fDisabledProjectSettings; // null when project specific settings are turned off
193

194     private int fRebuildCount; /// used to prevent multiple dialogs that ask for a rebuild
195

196     public OptionsConfigurationBlock(IStatusChangeListener context, IProject project, Key[] allKeys, IWorkbenchPreferenceContainer container) {
197         fContext= context;
198         fProject= project;
199         fAllKeys= allKeys;
200         fContainer= container;
201         if (container == null) {
202             fManager= new WorkingCopyManager();
203         } else {
204             fManager= container.getWorkingCopyManager();
205         }
206         
207         if (fProject != null) {
208             fLookupOrder= new IScopeContext[] {
209                 new ProjectScope(fProject),
210                 new InstanceScope(),
211                 new DefaultScope()
212             };
213         } else {
214             fLookupOrder= new IScopeContext[] {
215                 new InstanceScope(),
216                 new DefaultScope()
217             };
218         }
219         
220         testIfOptionsComplete(allKeys);
221         if (fProject == null || hasProjectSpecificOptions(fProject)) {
222             fDisabledProjectSettings= null;
223         } else {
224             fDisabledProjectSettings= new IdentityHashMap JavaDoc();
225             for (int i= 0; i < allKeys.length; i++) {
226                 Key curr= allKeys[i];
227                 fDisabledProjectSettings.put(curr, curr.getStoredValue(fLookupOrder, false, fManager));
228             }
229         }
230         
231         settingsUpdated();
232         
233         fCheckBoxes= new ArrayList JavaDoc();
234         fComboBoxes= new ArrayList JavaDoc();
235         fTextBoxes= new ArrayList JavaDoc(2);
236         fLabels= new HashMap JavaDoc();
237         fExpandedComposites= new ArrayList JavaDoc();
238         
239         fRebuildCount= getRebuildCount();
240     }
241     
242     protected final IWorkbenchPreferenceContainer getPreferenceContainer() {
243         return fContainer;
244     }
245     
246     protected static Key getKey(String JavaDoc plugin, String JavaDoc key) {
247         return new Key(plugin, key);
248     }
249     
250     protected final static Key getJDTCoreKey(String JavaDoc key) {
251         return getKey(JavaCore.PLUGIN_ID, key);
252     }
253     
254     protected final static Key getJDTUIKey(String JavaDoc key) {
255         return getKey(JavaUI.ID_PLUGIN, key);
256     }
257     
258         
259     private void testIfOptionsComplete(Key[] allKeys) {
260         for (int i= 0; i < allKeys.length; i++) {
261             if (allKeys[i].getStoredValue(fLookupOrder, false, fManager) == null) {
262                 JavaPlugin.logErrorMessage("preference option missing: " + allKeys[i] + " (" + this.getClass().getName() +')'); //$NON-NLS-1$//$NON-NLS-2$
263
}
264         }
265     }
266     
267     private int getRebuildCount() {
268         return fManager.getWorkingCopy(new DefaultScope().getNode(JavaUI.ID_PLUGIN)).getInt(REBUILD_COUNT_KEY, 0);
269     }
270     
271     private void incrementRebuildCount() {
272         fRebuildCount++;
273         fManager.getWorkingCopy(new DefaultScope().getNode(JavaUI.ID_PLUGIN)).putInt(REBUILD_COUNT_KEY, fRebuildCount);
274     }
275     
276     
277     protected void settingsUpdated() {
278     }
279     
280     
281     public void selectOption(String JavaDoc key, String JavaDoc qualifier) {
282         for (int i= 0; i < fAllKeys.length; i++) {
283             Key curr= fAllKeys[i];
284             if (curr.getName().equals(key) && curr.getQualifier().equals(qualifier)) {
285                 selectOption(curr);
286             }
287         }
288     }
289     
290     public void selectOption(Key key) {
291         Control control= findControl(key);
292         if (control != null) {
293             if (!fExpandedComposites.isEmpty()) {
294                 ExpandableComposite expandable= getParentExpandableComposite(control);
295                 if (expandable != null) {
296                     for (int i= 0; i < fExpandedComposites.size(); i++) {
297                         ExpandableComposite curr= (ExpandableComposite) fExpandedComposites.get(i);
298                         curr.setExpanded(curr == expandable);
299                     }
300                     expandedStateChanged(expandable);
301                 }
302             }
303             control.setFocus();
304         }
305     }
306     
307     
308     public final boolean hasProjectSpecificOptions(IProject project) {
309         if (project != null) {
310             IScopeContext projectContext= new ProjectScope(project);
311             Key[] allKeys= fAllKeys;
312             for (int i= 0; i < allKeys.length; i++) {
313                 if (allKeys[i].getStoredValue(projectContext, fManager) != null) {
314                     return true;
315                 }
316             }
317         }
318         return false;
319     }
320             
321     protected Shell getShell() {
322         return fShell;
323     }
324     
325     protected void setShell(Shell shell) {
326         fShell= shell;
327     }
328     
329     protected abstract Control createContents(Composite parent);
330     
331     protected Button addCheckBox(Composite parent, String JavaDoc label, Key key, String JavaDoc[] values, int indent) {
332         ControlData data= new ControlData(key, values);
333         
334         GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
335         gd.horizontalSpan= 3;
336         gd.horizontalIndent= indent;
337         
338         Button checkBox= new Button(parent, SWT.CHECK);
339         checkBox.setFont(JFaceResources.getDialogFont());
340         checkBox.setText(label);
341         checkBox.setData(data);
342         checkBox.setLayoutData(gd);
343         checkBox.addSelectionListener(getSelectionListener());
344         
345         makeScrollableCompositeAware(checkBox);
346         
347         String JavaDoc currValue= getValue(key);
348         checkBox.setSelection(data.getSelection(currValue) == 0);
349         
350         fCheckBoxes.add(checkBox);
351         
352         return checkBox;
353     }
354     
355     protected Button addCheckBoxWithLink(Composite parent, String JavaDoc label, Key key, String JavaDoc[] values, int indent, int widthHint, SelectionListener listener) {
356         ControlData data= new ControlData(key, values);
357         
358         GridData gd= new GridData(GridData.FILL, GridData.FILL, true, false);
359         gd.horizontalSpan= 3;
360         gd.horizontalIndent= indent;
361         
362         Composite composite= new Composite(parent, SWT.NONE);
363         GridLayout layout= new GridLayout();
364         layout.marginHeight= 0;
365         layout.marginWidth= 0;
366         layout.numColumns= 2;
367         composite.setLayout(layout);
368         composite.setLayoutData(gd);
369         
370         Button checkBox= new Button(composite, SWT.CHECK);
371         checkBox.setFont(JFaceResources.getDialogFont());
372         checkBox.setData(data);
373         checkBox.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, false, false));
374         checkBox.addSelectionListener(getSelectionListener());
375         
376         gd= new GridData(GridData.FILL, GridData.CENTER, true, false);
377         gd.widthHint= widthHint;
378         
379         Link link= new Link(composite, SWT.NONE);
380         link.setText(label);
381         link.setLayoutData(gd);
382         if (listener != null) {
383             link.addSelectionListener(listener);
384         }
385         
386         makeScrollableCompositeAware(link);
387         makeScrollableCompositeAware(checkBox);
388         
389         String JavaDoc currValue= getValue(key);
390         checkBox.setSelection(data.getSelection(currValue) == 0);
391         
392         fCheckBoxes.add(checkBox);
393         
394         return checkBox;
395     }
396     
397     protected Combo addComboBox(Composite parent, String JavaDoc label, Key key, String JavaDoc[] values, String JavaDoc[] valueLabels, int indent) {
398         GridData gd= new GridData(GridData.FILL, GridData.CENTER, true, false, 2, 1);
399         gd.horizontalIndent= indent;
400                 
401         Label labelControl= new Label(parent, SWT.LEFT);
402         labelControl.setFont(JFaceResources.getDialogFont());
403         labelControl.setText(label);
404         labelControl.setLayoutData(gd);
405                 
406         Combo comboBox= newComboControl(parent, key, values, valueLabels);
407         comboBox.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
408
409         fLabels.put(comboBox, labelControl);
410         
411         return comboBox;
412     }
413     
414     protected Combo addInversedComboBox(Composite parent, String JavaDoc label, Key key, String JavaDoc[] values, String JavaDoc[] valueLabels, int indent) {
415         GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
416         gd.horizontalIndent= indent;
417         gd.horizontalSpan= 3;
418         
419         Composite composite= new Composite(parent, SWT.NONE);
420         GridLayout layout= new GridLayout();
421         layout.marginHeight= 0;
422         layout.marginWidth= 0;
423         layout.numColumns= 2;
424         composite.setLayout(layout);
425         composite.setLayoutData(gd);
426         
427         Combo comboBox= newComboControl(composite, key, values, valueLabels);
428         comboBox.setFont(JFaceResources.getDialogFont());
429         comboBox.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
430         
431         Label labelControl= new Label(composite, SWT.LEFT | SWT.WRAP);
432         labelControl.setText(label);
433         labelControl.setLayoutData(new GridData());
434         
435         fLabels.put(comboBox, labelControl);
436         return comboBox;
437     }
438     
439     protected Combo newComboControl(Composite composite, Key key, String JavaDoc[] values, String JavaDoc[] valueLabels) {
440         ControlData data= new ControlData(key, values);
441         
442         Combo comboBox= new Combo(composite, SWT.READ_ONLY);
443         comboBox.setItems(valueLabels);
444         comboBox.setData(data);
445         comboBox.addSelectionListener(getSelectionListener());
446         comboBox.setFont(JFaceResources.getDialogFont());
447             
448         makeScrollableCompositeAware(comboBox);
449         
450         String JavaDoc currValue= getValue(key);
451         comboBox.select(data.getSelection(currValue));
452         
453         fComboBoxes.add(comboBox);
454         return comboBox;
455     }
456
457     protected Text addTextField(Composite parent, String JavaDoc label, Key key, int indent, int widthHint) {
458         Label labelControl= new Label(parent, SWT.WRAP);
459         labelControl.setText(label);
460         labelControl.setFont(JFaceResources.getDialogFont());
461         labelControl.setLayoutData(new GridData());
462                 
463         Text textBox= new Text(parent, SWT.BORDER | SWT.SINGLE);
464         textBox.setData(key);
465         textBox.setLayoutData(new GridData());
466         
467         makeScrollableCompositeAware(textBox);
468         
469         fLabels.put(textBox, labelControl);
470         
471         String JavaDoc currValue= getValue(key);
472         if (currValue != null) {
473             textBox.setText(currValue);
474         }
475         textBox.addModifyListener(getTextModifyListener());
476
477         GridData data= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
478         if (widthHint != 0) {
479             data.widthHint= widthHint;
480         }
481         data.horizontalIndent= indent;
482         data.horizontalSpan= 2;
483         textBox.setLayoutData(data);
484
485         fTextBoxes.add(textBox);
486         return textBox;
487     }
488     
489     protected ScrolledPageContent getParentScrolledComposite(Control control) {
490         Control parent= control.getParent();
491         while (!(parent instanceof ScrolledPageContent) && parent != null) {
492             parent= parent.getParent();
493         }
494         if (parent instanceof ScrolledPageContent) {
495             return (ScrolledPageContent) parent;
496         }
497         return null;
498     }
499     
500     protected ExpandableComposite getParentExpandableComposite(Control control) {
501         Control parent= control.getParent();
502         while (!(parent instanceof ExpandableComposite) && parent != null) {
503             parent= parent.getParent();
504         }
505         if (parent instanceof ExpandableComposite) {
506             return (ExpandableComposite) parent;
507         }
508         return null;
509     }
510     
511     private void makeScrollableCompositeAware(Control control) {
512         ScrolledPageContent parentScrolledComposite= getParentScrolledComposite(control);
513         if (parentScrolledComposite != null) {
514             parentScrolledComposite.adaptChild(control);
515         }
516     }
517     
518     protected ExpandableComposite createStyleSection(Composite parent, String JavaDoc label, int nColumns) {
519         ExpandableComposite excomposite= new ExpandableComposite(parent, SWT.NONE, ExpandableComposite.TWISTIE | ExpandableComposite.CLIENT_INDENT);
520         excomposite.setText(label);
521         excomposite.setExpanded(false);
522         excomposite.setFont(JFaceResources.getFontRegistry().getBold(JFaceResources.DIALOG_FONT));
523         excomposite.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false, nColumns, 1));
524         excomposite.addExpansionListener(new ExpansionAdapter() {
525             public void expansionStateChanged(ExpansionEvent e) {
526                 expandedStateChanged((ExpandableComposite) e.getSource());
527             }
528         });
529         fExpandedComposites.add(excomposite);
530         makeScrollableCompositeAware(excomposite);
531         return excomposite;
532     }
533     
534     protected final void expandedStateChanged(ExpandableComposite expandable) {
535         ScrolledPageContent parentScrolledComposite= getParentScrolledComposite(expandable);
536         if (parentScrolledComposite != null) {
537             parentScrolledComposite.reflow(true);
538         }
539     }
540     
541     protected void restoreSectionExpansionStates(IDialogSettings settings) {
542         for (int i= 0; i < fExpandedComposites.size(); i++) {
543             ExpandableComposite excomposite= (ExpandableComposite) fExpandedComposites.get(i);
544             if (settings == null) {
545                 excomposite.setExpanded(i == 0); // only expand the first node by default
546
} else {
547                 excomposite.setExpanded(settings.getBoolean(SETTINGS_EXPANDED + String.valueOf(i)));
548             }
549         }
550     }
551     
552     protected void storeSectionExpansionStates(IDialogSettings settings) {
553         for (int i= 0; i < fExpandedComposites.size(); i++) {
554             ExpandableComposite curr= (ExpandableComposite) fExpandedComposites.get(i);
555             settings.put(SETTINGS_EXPANDED + String.valueOf(i), curr.isExpanded());
556         }
557     }
558     
559     protected SelectionListener getSelectionListener() {
560         if (fSelectionListener == null) {
561             fSelectionListener= new SelectionListener() {
562                 public void widgetDefaultSelected(SelectionEvent e) {}
563     
564                 public void widgetSelected(SelectionEvent e) {
565                     controlChanged(e.widget);
566                 }
567             };
568         }
569         return fSelectionListener;
570     }
571     
572     protected ModifyListener getTextModifyListener() {
573         if (fTextModifyListener == null) {
574             fTextModifyListener= new ModifyListener() {
575                 public void modifyText(ModifyEvent e) {
576                     textChanged((Text) e.widget);
577                 }
578             };
579         }
580         return fTextModifyListener;
581     }
582     
583     protected void controlChanged(Widget widget) {
584         ControlData data= (ControlData) widget.getData();
585         String JavaDoc newValue= null;
586         if (widget instanceof Button) {
587             newValue= data.getValue(((Button)widget).getSelection());
588         } else if (widget instanceof Combo) {
589             newValue= data.getValue(((Combo)widget).getSelectionIndex());
590         } else {
591             return;
592         }
593         String JavaDoc oldValue= setValue(data.getKey(), newValue);
594         validateSettings(data.getKey(), oldValue, newValue);
595     }
596     
597     protected void textChanged(Text textControl) {
598         Key key= (Key) textControl.getData();
599         String JavaDoc number= textControl.getText();
600         String JavaDoc oldValue= setValue(key, number);
601         validateSettings(key, oldValue, number);
602     }
603
604     protected boolean checkValue(Key key, String JavaDoc value) {
605         return value.equals(getValue(key));
606     }
607     
608     protected String JavaDoc getValue(Key key) {
609         if (fDisabledProjectSettings != null) {
610             return (String JavaDoc) fDisabledProjectSettings.get(key);
611         }
612         return key.getStoredValue(fLookupOrder, false, fManager);
613     }
614     
615     
616     protected boolean getBooleanValue(Key key) {
617         return Boolean.valueOf(getValue(key)).booleanValue();
618     }
619     
620     protected String JavaDoc setValue(Key key, String JavaDoc value) {
621         if (fDisabledProjectSettings != null) {
622             return (String JavaDoc) fDisabledProjectSettings.put(key, value);
623         }
624         String JavaDoc oldValue= getValue(key);
625         key.setStoredValue(fLookupOrder[0], value, fManager);
626         return oldValue;
627     }
628     
629     protected String JavaDoc setValue(Key key, boolean value) {
630         return setValue(key, String.valueOf(value));
631     }
632
633     /**
634      * Returns the value as actually stored in the preference store.
635      * @param key
636      * @return the value as actually stored in the preference store.
637      */

638     protected String JavaDoc getStoredValue(Key key) {
639         return key.getStoredValue(fLookupOrder, false, fManager);
640     }
641     
642     /* (non-javadoc)
643      * Update fields and validate.
644      * @param changedKey Key that changed, or null, if all changed.
645      */

646     protected abstract void validateSettings(Key changedKey, String JavaDoc oldValue, String JavaDoc newValue);
647     
648     
649     protected String JavaDoc[] getTokens(String JavaDoc text, String JavaDoc separator) {
650         StringTokenizer JavaDoc tok= new StringTokenizer JavaDoc(text, separator);
651         int nTokens= tok.countTokens();
652         String JavaDoc[] res= new String JavaDoc[nTokens];
653         for (int i= 0; i < res.length; i++) {
654             res[i]= tok.nextToken().trim();
655         }
656         return res;
657     }
658
659     private boolean getChanges(IScopeContext currContext, List JavaDoc changedSettings) {
660         boolean needsBuild= false;
661         for (int i= 0; i < fAllKeys.length; i++) {
662             Key key= fAllKeys[i];
663             String JavaDoc oldVal= key.getStoredValue(currContext, null);
664             String JavaDoc val= key.getStoredValue(currContext, fManager);
665             if (val == null) {
666                 if (oldVal != null) {
667                     changedSettings.add(key);
668                     needsBuild |= !oldVal.equals(key.getStoredValue(fLookupOrder, true, fManager));
669                 }
670             } else if (!val.equals(oldVal)) {
671                 changedSettings.add(key);
672                 needsBuild |= oldVal != null || !val.equals(key.getStoredValue(fLookupOrder, true, fManager));
673             }
674         }
675         return needsBuild;
676     }
677     
678     public void useProjectSpecificSettings(boolean enable) {
679         boolean hasProjectSpecificOption= fDisabledProjectSettings == null;
680         if (enable != hasProjectSpecificOption && fProject != null) {
681             if (enable) {
682                 for (int i= 0; i < fAllKeys.length; i++) {
683                     Key curr= fAllKeys[i];
684                     String JavaDoc val= (String JavaDoc) fDisabledProjectSettings.get(curr);
685                     curr.setStoredValue(fLookupOrder[0], val, fManager);
686                 }
687                 fDisabledProjectSettings= null;
688                 updateControls();
689                 validateSettings(null, null, null);
690             } else {
691                 fDisabledProjectSettings= new IdentityHashMap JavaDoc();
692                 for (int i= 0; i < fAllKeys.length; i++) {
693                     Key curr= fAllKeys[i];
694                     String JavaDoc oldSetting= curr.getStoredValue(fLookupOrder, false, fManager);
695                     fDisabledProjectSettings.put(curr, oldSetting);
696                     curr.setStoredValue(fLookupOrder[0], null, fManager); // clear project settings
697
}
698             }
699         }
700     }
701     
702     public boolean areSettingsEnabled() {
703         return fDisabledProjectSettings == null || fProject == null;
704     }
705     
706     
707     public boolean performOk() {
708         return processChanges(fContainer);
709     }
710     
711     public boolean performApply() {
712         return processChanges(null); // apply directly
713
}
714     
715     protected boolean processChanges(IWorkbenchPreferenceContainer container) {
716         IScopeContext currContext= fLookupOrder[0];
717         
718         List JavaDoc /* <Key>*/ changedOptions= new ArrayList JavaDoc();
719         boolean needsBuild= getChanges(currContext, changedOptions);
720         if (changedOptions.isEmpty()) {
721             return true;
722         }
723         if (needsBuild) {
724             int count= getRebuildCount();
725             if (count > fRebuildCount) {
726                 needsBuild= false; // build already requested
727
fRebuildCount= count;
728             }
729         }
730
731         boolean doBuild= false;
732         if (needsBuild) {
733             String JavaDoc[] strings= getFullBuildDialogStrings(fProject == null);
734             if (strings != null) {
735                 MessageDialog dialog= new MessageDialog(getShell(), strings[0], null, strings[1], MessageDialog.QUESTION, new String JavaDoc[] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL }, 2);
736                 int res= dialog.open();
737                 if (res == 0) {
738                     doBuild= true;
739                 } else if (res != 1) {
740                     return false; // cancel pressed
741
}
742             }
743         }
744         if (container != null) {
745             // no need to apply the changes to the original store: will be done by the page container
746
if (doBuild) { // post build
747
incrementRebuildCount();
748                 container.registerUpdateJob(CoreUtility.getBuildJob(fProject));
749             }
750         } else {
751             // apply changes right away
752
try {
753                 fManager.applyChanges();
754             } catch (BackingStoreException e) {
755                 JavaPlugin.log(e);
756                 return false;
757             }
758             if (doBuild) {
759                 CoreUtility.getBuildJob(fProject).schedule();
760             }
761             
762         }
763         return true;
764     }
765     
766     protected abstract String JavaDoc[] getFullBuildDialogStrings(boolean workspaceSettings);
767             
768     
769     public void performDefaults() {
770         for (int i= 0; i < fAllKeys.length; i++) {
771             Key curr= fAllKeys[i];
772             String JavaDoc defValue= curr.getStoredValue(fLookupOrder, true, fManager);
773             setValue(curr, defValue);
774         }
775         
776         settingsUpdated();
777         updateControls();
778         validateSettings(null, null, null);
779     }
780
781     /**
782      * @since 3.1
783      */

784     public void performRevert() {
785         for (int i= 0; i < fAllKeys.length; i++) {
786             Key curr= fAllKeys[i];
787             String JavaDoc origValue= curr.getStoredValue(fLookupOrder, false, null);
788             setValue(curr, origValue);
789         }
790         
791         settingsUpdated();
792         updateControls();
793         validateSettings(null, null, null);
794     }
795     
796     public void dispose() {
797     }
798     
799     protected void updateControls() {
800         // update the UI
801
for (int i= fCheckBoxes.size() - 1; i >= 0; i--) {
802             updateCheckBox((Button) fCheckBoxes.get(i));
803         }
804         for (int i= fComboBoxes.size() - 1; i >= 0; i--) {
805             updateCombo((Combo) fComboBoxes.get(i));
806         }
807         for (int i= fTextBoxes.size() - 1; i >= 0; i--) {
808             updateText((Text) fTextBoxes.get(i));
809         }
810     }
811     
812     protected void updateCombo(Combo curr) {
813         ControlData data= (ControlData) curr.getData();
814         
815         String JavaDoc currValue= getValue(data.getKey());
816         curr.select(data.getSelection(currValue));
817     }
818     
819     protected void updateCheckBox(Button curr) {
820         ControlData data= (ControlData) curr.getData();
821         
822         String JavaDoc currValue= getValue(data.getKey());
823         curr.setSelection(data.getSelection(currValue) == 0);
824     }
825     
826     protected void updateText(Text curr) {
827         Key key= (Key) curr.getData();
828         
829         String JavaDoc currValue= getValue(key);
830         if (currValue != null) {
831             curr.setText(currValue);
832         }
833     }
834     
835     protected Button getCheckBox(Key key) {
836         for (int i= fCheckBoxes.size() - 1; i >= 0; i--) {
837             Button curr= (Button) fCheckBoxes.get(i);
838             ControlData data= (ControlData) curr.getData();
839             if (key.equals(data.getKey())) {
840                 return curr;
841             }
842         }
843         return null;
844     }
845     
846     protected Combo getComboBox(Key key) {
847         for (int i= fComboBoxes.size() - 1; i >= 0; i--) {
848             Combo curr= (Combo) fComboBoxes.get(i);
849             ControlData data= (ControlData) curr.getData();
850             if (key.equals(data.getKey())) {
851                 return curr;
852             }
853         }
854         return null;
855     }
856     
857     protected Text getTextControl(Key key) {
858         for (int i= fTextBoxes.size() - 1; i >= 0; i--) {
859             Text curr= (Text) fTextBoxes.get(i);
860             ControlData data= (ControlData) curr.getData();
861             if (key.equals(data.getKey())) {
862                 return curr;
863             }
864         }
865         return null;
866     }
867     
868     protected Control findControl(Key key) {
869         Combo comboBox= getComboBox(key);
870         if (comboBox != null) {
871             return comboBox;
872         }
873         Button checkBox= getCheckBox(key);
874         if (checkBox != null) {
875             return checkBox;
876         }
877         Text text= getTextControl(key);
878         if (text != null) {
879             return text;
880         }
881         return null;
882     }
883     
884     protected void setComboEnabled(Key key, boolean enabled) {
885         Combo combo= getComboBox(key);
886         Label label= (Label) fLabels.get(combo);
887         combo.setEnabled(enabled);
888         label.setEnabled(enabled);
889     }
890 }
891
Popular Tags