KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > internal > ui > SearchPreferencePage


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.search.internal.ui;
12
13 import com.ibm.icu.text.Collator;
14
15 import java.util.Arrays JavaDoc;
16 import java.util.Comparator JavaDoc;
17
18 import org.eclipse.swt.graphics.RGB;
19 import org.eclipse.swt.widgets.Composite;
20
21 import org.eclipse.jface.preference.BooleanFieldEditor;
22 import org.eclipse.jface.preference.ColorFieldEditor;
23 import org.eclipse.jface.preference.ComboFieldEditor;
24 import org.eclipse.jface.preference.FieldEditorPreferencePage;
25 import org.eclipse.jface.preference.IPreferenceStore;
26 import org.eclipse.jface.preference.PreferenceConverter;
27 import org.eclipse.jface.util.PropertyChangeEvent;
28
29 import org.eclipse.ui.IPerspectiveDescriptor;
30 import org.eclipse.ui.IPerspectiveRegistry;
31 import org.eclipse.ui.IWorkbench;
32 import org.eclipse.ui.IWorkbenchPreferencePage;
33 import org.eclipse.ui.PlatformUI;
34
35 import org.eclipse.search.internal.core.text.TextSearchEngineRegistry;
36
37
38 /*
39  * The page for setting the Search preferences.
40  */

41 public class SearchPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
42
43     public static final String JavaDoc PAGE_ID= "org.eclipse.search.preferences.SearchPreferencePage"; //$NON-NLS-1$
44

45     
46     public static final String JavaDoc IGNORE_POTENTIAL_MATCHES= "org.eclipse.search.potentialMatch.ignore"; //$NON-NLS-1$
47
public static final String JavaDoc EMPHASIZE_POTENTIAL_MATCHES= "org.eclipse.search.potentialMatch.emphasize"; //$NON-NLS-1$
48
public static final String JavaDoc POTENTIAL_MATCH_FG_COLOR= "org.eclipse.search.potentialMatch.fgColor"; //$NON-NLS-1$
49
public static final String JavaDoc REUSE_EDITOR= "org.eclipse.search.reuseEditor"; //$NON-NLS-1$
50
public static final String JavaDoc DEFAULT_PERSPECTIVE= "org.eclipse.search.defaultPerspective"; //$NON-NLS-1$
51
private static final String JavaDoc NO_DEFAULT_PERSPECTIVE= "org.eclipse.search.defaultPerspective.none"; //$NON-NLS-1$
52
public static final String JavaDoc BRING_VIEW_TO_FRONT= "org.eclipse.search.bringToFront"; //$NON-NLS-1$
53
public static final String JavaDoc TEXT_SEARCH_ENGINE = "org.eclipse.search.textSearchEngine"; //$NON-NLS-1$
54
public static final String JavaDoc TEXT_SEARCH_QUERY_PROVIDER = "org.eclipse.search.textSearchQueryProvider"; //$NON-NLS-1$
55
public static final String JavaDoc LIMIT_HISTORY= "org.eclipse.search.limitHistory"; //$NON-NLS-1$
56

57     private ColorFieldEditor fColorEditor;
58     private BooleanFieldEditor fEmphasizedCheckbox;
59     private BooleanFieldEditor fIgnorePotentialMatchesCheckbox;
60
61
62     private static class PerspectiveDescriptorComparator implements Comparator JavaDoc {
63         /*
64          * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
65          */

66         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
67             if (o1 instanceof IPerspectiveDescriptor && o2 instanceof IPerspectiveDescriptor) {
68                 String JavaDoc id1= ((IPerspectiveDescriptor)o1).getLabel();
69                 String JavaDoc id2= ((IPerspectiveDescriptor)o2).getLabel();
70                 return Collator.getInstance().compare(id1, id2);
71             }
72             return 0;
73         }
74     }
75
76
77
78     public SearchPreferencePage() {
79         super(GRID);
80         setPreferenceStore(SearchPlugin.getDefault().getPreferenceStore());
81     }
82
83     public static void initDefaults(IPreferenceStore store) {
84         RGB gray= new RGB(85, 85, 85);
85         store.setDefault(EMPHASIZE_POTENTIAL_MATCHES, true);
86         store.setDefault(IGNORE_POTENTIAL_MATCHES, false);
87         PreferenceConverter.setDefault(store, POTENTIAL_MATCH_FG_COLOR, gray);
88         store.setDefault(REUSE_EDITOR, true);
89         store.setDefault(BRING_VIEW_TO_FRONT, true);
90         store.setDefault(DEFAULT_PERSPECTIVE, NO_DEFAULT_PERSPECTIVE);
91         store.setDefault(TEXT_SEARCH_ENGINE, ""); //default search engine is empty string //$NON-NLS-1$
92
store.setDefault(TEXT_SEARCH_QUERY_PROVIDER, ""); // default query provider is empty string //$NON-NLS-1$
93
store.setDefault(LIMIT_HISTORY, 10);
94     }
95
96
97     public void createControl(Composite parent) {
98         super.createControl(parent);
99         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), ISearchHelpContextIds.SEARCH_PREFERENCE_PAGE);
100     }
101     
102     protected void createFieldEditors() {
103         BooleanFieldEditor boolEditor= new BooleanFieldEditor(
104             REUSE_EDITOR,
105             SearchMessages.SearchPreferencePage_reuseEditor,
106             getFieldEditorParent()
107         );
108         addField(boolEditor);
109
110         boolEditor= new BooleanFieldEditor(
111                 BRING_VIEW_TO_FRONT,
112                 SearchMessages.SearchPreferencePage_bringToFront,
113                 getFieldEditorParent()
114                 );
115         addField(boolEditor);
116         
117         fIgnorePotentialMatchesCheckbox= new BooleanFieldEditor(
118             IGNORE_POTENTIAL_MATCHES,
119             SearchMessages.SearchPreferencePage_ignorePotentialMatches,
120             getFieldEditorParent());
121         addField(fIgnorePotentialMatchesCheckbox);
122
123         fEmphasizedCheckbox= new BooleanFieldEditor(
124             EMPHASIZE_POTENTIAL_MATCHES,
125             SearchMessages.SearchPreferencePage_emphasizePotentialMatches,
126             getFieldEditorParent());
127         addField(fEmphasizedCheckbox);
128
129         fColorEditor= new ColorFieldEditor(
130             POTENTIAL_MATCH_FG_COLOR,
131             SearchMessages.SearchPreferencePage_potentialMatchFgColor,
132             getFieldEditorParent()
133         );
134         addField(fColorEditor);
135         
136         fEmphasizedCheckbox.setEnabled(!arePotentialMatchesIgnored(), getFieldEditorParent());
137         fColorEditor.setEnabled(!arePotentialMatchesIgnored() && arePotentialMatchesEmphasized(), getFieldEditorParent());
138
139         handleDeletedPerspectives();
140         String JavaDoc[][] perspectiveNamesAndIds = getPerspectiveNamesAndIds();
141         ComboFieldEditor comboEditor= new ComboFieldEditor(
142             DEFAULT_PERSPECTIVE,
143             SearchMessages.SearchPreferencePage_defaultPerspective,
144             perspectiveNamesAndIds,
145             getFieldEditorParent());
146         addField(comboEditor);
147         
148         // in case we have a contributed engine, let the user choose.
149
TextSearchEngineRegistry reg= SearchPlugin.getDefault().getTextSearchEngineRegistry();
150         String JavaDoc[][] engineNamesAndIds= reg.getAvailableEngines();
151         if (engineNamesAndIds.length > 1) {
152             comboEditor= new ComboFieldEditor(
153                     TEXT_SEARCH_ENGINE,
154                     SearchMessages.SearchPreferencePage_textSearchEngine,
155                     engineNamesAndIds,
156                     getFieldEditorParent());
157             addField(comboEditor);
158         }
159     }
160
161     public void setVisible(boolean state) {
162         handleDeletedPerspectives();
163         super.setVisible(state);
164     }
165
166     public void propertyChange(PropertyChangeEvent event) {
167         updateFieldEnablement();
168     }
169
170     public void init(IWorkbench workbench) {
171     }
172
173     protected void performDefaults() {
174         super.performDefaults();
175         updateFieldEnablement();
176     }
177     
178     
179     private void updateFieldEnablement() {
180         boolean arePotentialMatchesIgnored= fIgnorePotentialMatchesCheckbox.getBooleanValue();
181         fEmphasizedCheckbox.setEnabled(!arePotentialMatchesIgnored, getFieldEditorParent());
182         fColorEditor.setEnabled(!arePotentialMatchesIgnored && fEmphasizedCheckbox.getBooleanValue(), getFieldEditorParent());
183     }
184
185     /*
186      * Return a 2-dimensional array of perspective names and ids.
187      */

188     private String JavaDoc[][] getPerspectiveNamesAndIds() {
189         
190         IPerspectiveRegistry registry= PlatformUI.getWorkbench().getPerspectiveRegistry();
191         IPerspectiveDescriptor[] perspectiveDescriptors= registry.getPerspectives();
192         
193         Arrays.sort(perspectiveDescriptors, new PerspectiveDescriptorComparator());
194         
195         String JavaDoc[][] table = new String JavaDoc[perspectiveDescriptors.length + 1][2];
196         table[0][0] = SearchMessages.SearchPreferencePage_defaultPerspective_none;
197         table[0][1] = NO_DEFAULT_PERSPECTIVE;
198         for (int i = 0; i < perspectiveDescriptors.length; i++) {
199             table[i + 1][0] = perspectiveDescriptors[i].getLabel();
200             table[i + 1][1] = perspectiveDescriptors[i].getId();
201         }
202         return table;
203     }
204
205     private static void handleDeletedPerspectives() {
206         IPreferenceStore store= SearchPlugin.getDefault().getPreferenceStore();
207         String JavaDoc id= store.getString(DEFAULT_PERSPECTIVE);
208         if (PlatformUI.getWorkbench().getPerspectiveRegistry().findPerspectiveWithId(id) == null) {
209             store.putValue(DEFAULT_PERSPECTIVE, NO_DEFAULT_PERSPECTIVE);
210         }
211     }
212     
213     
214     // Accessors to preference values
215
public static String JavaDoc getDefaultPerspectiveId() {
216         handleDeletedPerspectives();
217         IPreferenceStore store= SearchPlugin.getDefault().getPreferenceStore();
218         String JavaDoc id= store.getString(DEFAULT_PERSPECTIVE);
219         if (id == null || id.length() == 0 || id.equals(NO_DEFAULT_PERSPECTIVE))
220             return null;
221         else if (PlatformUI.getWorkbench().getPerspectiveRegistry().findPerspectiveWithId(id) == null) {
222             store.putValue(DEFAULT_PERSPECTIVE, id);
223             return null;
224         }
225         return id;
226     }
227
228     public static boolean isEditorReused() {
229         IPreferenceStore store= SearchPlugin.getDefault().getPreferenceStore();
230         return store.getBoolean(REUSE_EDITOR);
231     }
232     
233     public static boolean isViewBroughtToFront() {
234         IPreferenceStore store= SearchPlugin.getDefault().getPreferenceStore();
235         return store.getBoolean(BRING_VIEW_TO_FRONT);
236     }
237
238     public static boolean arePotentialMatchesIgnored() {
239         IPreferenceStore store= SearchPlugin.getDefault().getPreferenceStore();
240         return store.getBoolean(IGNORE_POTENTIAL_MATCHES);
241     }
242
243     public static boolean arePotentialMatchesEmphasized() {
244         IPreferenceStore store= SearchPlugin.getDefault().getPreferenceStore();
245         return store.getBoolean(EMPHASIZE_POTENTIAL_MATCHES);
246     }
247
248     public static RGB getPotentialMatchForegroundColor() {
249         IPreferenceStore store= SearchPlugin.getDefault().getPreferenceStore();
250         return PreferenceConverter.getColor(store, POTENTIAL_MATCH_FG_COLOR);
251     }
252     
253     public static int getHistoryLimit() {
254         IPreferenceStore store= SearchPlugin.getDefault().getPreferenceStore();
255         return store.getInt(LIMIT_HISTORY);
256     }
257     
258 }
259
Popular Tags