1 11 package org.eclipse.search.internal.ui; 12 13 import com.ibm.icu.text.Collator; 14 15 import java.util.Arrays ; 16 import java.util.Comparator ; 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 41 public class SearchPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage { 42 43 public static final String PAGE_ID= "org.eclipse.search.preferences.SearchPreferencePage"; 45 46 public static final String IGNORE_POTENTIAL_MATCHES= "org.eclipse.search.potentialMatch.ignore"; public static final String EMPHASIZE_POTENTIAL_MATCHES= "org.eclipse.search.potentialMatch.emphasize"; public static final String POTENTIAL_MATCH_FG_COLOR= "org.eclipse.search.potentialMatch.fgColor"; public static final String REUSE_EDITOR= "org.eclipse.search.reuseEditor"; public static final String DEFAULT_PERSPECTIVE= "org.eclipse.search.defaultPerspective"; private static final String NO_DEFAULT_PERSPECTIVE= "org.eclipse.search.defaultPerspective.none"; public static final String BRING_VIEW_TO_FRONT= "org.eclipse.search.bringToFront"; public static final String TEXT_SEARCH_ENGINE = "org.eclipse.search.textSearchEngine"; public static final String TEXT_SEARCH_QUERY_PROVIDER = "org.eclipse.search.textSearchQueryProvider"; public static final String LIMIT_HISTORY= "org.eclipse.search.limitHistory"; 57 private ColorFieldEditor fColorEditor; 58 private BooleanFieldEditor fEmphasizedCheckbox; 59 private BooleanFieldEditor fIgnorePotentialMatchesCheckbox; 60 61 62 private static class PerspectiveDescriptorComparator implements Comparator { 63 66 public int compare(Object o1, Object o2) { 67 if (o1 instanceof IPerspectiveDescriptor && o2 instanceof IPerspectiveDescriptor) { 68 String id1= ((IPerspectiveDescriptor)o1).getLabel(); 69 String 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, ""); store.setDefault(TEXT_SEARCH_QUERY_PROVIDER, ""); 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 [][] perspectiveNamesAndIds = getPerspectiveNamesAndIds(); 141 ComboFieldEditor comboEditor= new ComboFieldEditor( 142 DEFAULT_PERSPECTIVE, 143 SearchMessages.SearchPreferencePage_defaultPerspective, 144 perspectiveNamesAndIds, 145 getFieldEditorParent()); 146 addField(comboEditor); 147 148 TextSearchEngineRegistry reg= SearchPlugin.getDefault().getTextSearchEngineRegistry(); 150 String [][] 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 188 private String [][] getPerspectiveNamesAndIds() { 189 190 IPerspectiveRegistry registry= PlatformUI.getWorkbench().getPerspectiveRegistry(); 191 IPerspectiveDescriptor[] perspectiveDescriptors= registry.getPerspectives(); 192 193 Arrays.sort(perspectiveDescriptors, new PerspectiveDescriptorComparator()); 194 195 String [][] table = new String [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 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 public static String getDefaultPerspectiveId() { 216 handleDeletedPerspectives(); 217 IPreferenceStore store= SearchPlugin.getDefault().getPreferenceStore(); 218 String 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 |