1 14 package org.eclipse.search.internal.ui; 15 16 import java.util.ArrayList ; 17 import java.util.Arrays ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.StringTokenizer ; 21 22 import org.eclipse.core.runtime.CoreException; 23 import org.eclipse.core.runtime.IAdaptable; 24 import org.eclipse.core.runtime.IConfigurationElement; 25 import org.eclipse.core.runtime.Path; 26 import org.eclipse.core.runtime.Platform; 27 28 import org.eclipse.core.resources.IFile; 29 import org.eclipse.core.resources.IResource; 30 31 import org.eclipse.swt.SWT; 32 import org.eclipse.swt.graphics.Point; 33 34 import org.eclipse.jface.dialogs.IDialogSettings; 35 import org.eclipse.jface.resource.ImageDescriptor; 36 import org.eclipse.jface.resource.StringConverter; 37 38 import org.eclipse.ui.IPluginContribution; 39 40 import org.eclipse.search.ui.ISearchPage; 41 import org.eclipse.search.ui.ISearchPageContainer; 42 import org.eclipse.search.ui.ISearchPageScoreComputer; 43 44 import org.eclipse.search.internal.ui.util.ExceptionHandler; 45 46 import org.osgi.framework.Bundle; 47 48 51 class SearchPageDescriptor implements IPluginContribution, Comparable { 52 53 public final static String PAGE_TAG= "page"; private final static String ID_ATTRIBUTE= "id"; private final static String ICON_ATTRIBUTE= "icon"; private final static String CLASS_ATTRIBUTE= "class"; private final static String LABEL_ATTRIBUTE= "label"; private final static String SIZE_ATTRIBUTE= "sizeHint"; private final static String TAB_POSITION_ATTRIBUTE= "tabPosition"; private final static String EXTENSIONS_ATTRIBUTE= "extensions"; private final static String SHOW_SCOPE_SECTION_ATTRIBUTE= "showScopeSection"; private final static String CAN_SEARCH_ENCLOSING_PROJECTS= "canSearchEnclosingProjects"; private final static String ENABLED_ATTRIBUTE= "enabled"; private final static String SEARCH_VIEW_HELP_CONTEXT_ID_ATTRIBUTE= "searchViewHelpContextId"; 66 public final static Point UNKNOWN_SIZE= new Point(SWT.DEFAULT, SWT.DEFAULT); 67 68 private final static String SECTION_ID= "Search"; private final static String STORE_ENABLED_PAGE_IDS= SECTION_ID + ".enabledPageIds"; private final static String STORE_PROCESSED_PAGE_IDS= SECTION_ID + ".processedPageIds"; 73 private static List fgEnabledPageIds; 74 75 private static class ExtensionScorePair { 76 public String extension; 77 public int score; 78 public ExtensionScorePair(String extension, int score) { 79 this.extension= extension; 80 this.score= score; 81 } 82 } 83 84 private IConfigurationElement fElement; 85 private List fExtensionScorePairs; 86 private int fWildcardScore= ISearchPageScoreComputer.UNKNOWN; 87 private ISearchPage fCreatedPage; 88 89 93 public SearchPageDescriptor(IConfigurationElement element) { 94 fElement= element; 95 } 96 97 103 public ISearchPage createObject(ISearchPageContainer container) throws CoreException { 104 if (fCreatedPage == null) { 105 fCreatedPage= (ISearchPage) fElement.createExecutableExtension(CLASS_ATTRIBUTE); 106 fCreatedPage.setTitle(getLabel()); 107 fCreatedPage.setContainer(container); 108 } 109 return fCreatedPage; 110 } 111 112 public ISearchPage getPage() { 113 return fCreatedPage; 114 } 115 116 117 public void dispose() { 118 if (fCreatedPage != null) { 119 fCreatedPage.dispose(); 120 fCreatedPage= null; 121 } 122 } 123 124 126 130 public String getId() { 131 return fElement.getAttribute(ID_ATTRIBUTE); 132 } 133 134 138 public ImageDescriptor getImage() { 139 String imageName= fElement.getAttribute(ICON_ATTRIBUTE); 140 if (imageName == null) 141 return null; 142 Bundle bundle = Platform.getBundle(getPluginId()); 143 return SearchPluginImages.createImageDescriptor(bundle, new Path(imageName), true); 144 } 145 146 149 public String getLabel() { 150 return fElement.getAttribute(LABEL_ATTRIBUTE); 151 } 152 153 157 public boolean showScopeSection() { 158 return Boolean.valueOf(fElement.getAttribute(SHOW_SCOPE_SECTION_ATTRIBUTE)).booleanValue(); 159 } 160 161 168 public boolean isInitiallyEnabled() { 169 String strVal= fElement.getAttribute(ENABLED_ATTRIBUTE); 170 return strVal == null || Boolean.valueOf(strVal).booleanValue(); 171 } 172 173 181 public boolean canSearchInProjects() { 182 return Boolean.valueOf(fElement.getAttribute(CAN_SEARCH_ENCLOSING_PROJECTS)).booleanValue(); 183 } 184 185 188 public Point getPreferredSize() { 189 return StringConverter.asPoint( 190 fElement.getAttribute(SIZE_ATTRIBUTE), UNKNOWN_SIZE); 191 } 192 193 198 public int getTabPosition() { 199 int position= Integer.MAX_VALUE / 2; 200 String str= fElement.getAttribute(TAB_POSITION_ATTRIBUTE); 201 if (str != null) 202 try { 203 position= Integer.parseInt(str); 204 } catch (NumberFormatException ex) { 205 ExceptionHandler.log(ex, SearchMessages.Search_Error_createSearchPage_message); 206 } 208 return position; 209 } 210 211 boolean isEnabled() { 212 return getEnabledPageIds().contains(getId()); 213 } 214 215 220 public String getSearchViewHelpContextId() { 221 return fElement.getAttribute(SEARCH_VIEW_HELP_CONTEXT_ID_ATTRIBUTE); 222 } 223 224 static void setEnabled(Object [] enabledDescriptors) { 225 fgEnabledPageIds= new ArrayList (5); 226 for (int i= 0; i < enabledDescriptors.length; i++) { 227 if (enabledDescriptors[i] instanceof SearchPageDescriptor) 228 fgEnabledPageIds.add(((SearchPageDescriptor)enabledDescriptors[i]).getId()); 229 } 230 storeEnabledPageIds(); 231 } 232 233 private static List getEnabledPageIds() { 234 if (fgEnabledPageIds == null) { 235 List descriptors= SearchPlugin.getDefault().getSearchPageDescriptors(); 236 237 String [] enabledPageIds= getDialogSettings().getArray(STORE_ENABLED_PAGE_IDS); 238 if (enabledPageIds == null) 239 fgEnabledPageIds= new ArrayList (descriptors.size()); 240 else 241 fgEnabledPageIds= new ArrayList (Arrays.asList(enabledPageIds)); 242 243 244 List processedPageIds; 245 String [] processedPageIdsArr= getDialogSettings().getArray(STORE_PROCESSED_PAGE_IDS); 246 if (processedPageIdsArr == null) 247 processedPageIds= new ArrayList (descriptors.size()); 248 else 249 processedPageIds= new ArrayList (Arrays.asList(processedPageIdsArr)); 250 251 Iterator iter= descriptors.iterator(); 253 while (iter.hasNext()) { 254 SearchPageDescriptor desc= (SearchPageDescriptor)iter.next(); 255 if (processedPageIds.contains(desc.getId())) 256 continue; 257 258 processedPageIds.add(desc.getId()); 259 if (desc.isInitiallyEnabled()) 260 fgEnabledPageIds.add(desc.getId()); 261 } 262 263 getDialogSettings().put(STORE_PROCESSED_PAGE_IDS, (String [])processedPageIds.toArray(new String [processedPageIds.size()])); 264 storeEnabledPageIds(); 265 } 266 return fgEnabledPageIds; 267 } 268 269 private static void storeEnabledPageIds() { 270 getDialogSettings().put(STORE_ENABLED_PAGE_IDS, (String [])fgEnabledPageIds.toArray(new String [fgEnabledPageIds.size()])); 271 SearchPlugin.getDefault().savePluginPreferences(); 272 } 273 274 private static IDialogSettings getDialogSettings() { 275 IDialogSettings settings= SearchPlugin.getDefault().getDialogSettings(); 276 IDialogSettings section= settings.getSection(SECTION_ID); 277 if (section == null) 278 section= settings.addNewSection(SECTION_ID); 280 return section; 281 } 282 283 286 public int compareTo(Object o) { 287 int myPos= getTabPosition(); 288 int objsPos= ((SearchPageDescriptor)o).getTabPosition(); 289 if (myPos == Integer.MAX_VALUE && objsPos == Integer.MAX_VALUE || myPos == objsPos) 290 return getLabel().compareTo(((SearchPageDescriptor)o).getLabel()); 291 292 return myPos - objsPos; 293 } 294 295 297 302 public int computeScore(Object element) { 303 if (element instanceof IAdaptable) { 304 IResource resource= (IResource)((IAdaptable)element).getAdapter(IResource.class); 305 if (resource != null && resource.getType() == IResource.FILE) { 306 String extension= ((IFile)resource).getFileExtension(); 307 if (extension != null) 308 return getScoreForFileExtension(extension); 309 } else { 310 ISearchPageScoreComputer tester= 311 (ISearchPageScoreComputer)((IAdaptable)element).getAdapter(ISearchPageScoreComputer.class); 312 if (tester != null) 313 return tester.computeScore(getId(), element); 314 } 315 } 320 if (fWildcardScore != ISearchPageScoreComputer.UNKNOWN) 321 return fWildcardScore; 322 323 return ISearchPageScoreComputer.LOWEST; 324 } 325 326 private int getScoreForFileExtension(String extension) { 327 if (fExtensionScorePairs == null) 328 readExtensionScorePairs(); 329 330 int size= fExtensionScorePairs.size(); 331 for (int i= 0; i < size; i++) { 332 ExtensionScorePair p= (ExtensionScorePair)fExtensionScorePairs.get(i); 333 if (extension.equals(p.extension)) 334 return p.score; 335 } 336 if (fWildcardScore != ISearchPageScoreComputer.UNKNOWN) 337 return fWildcardScore; 338 339 return ISearchPageScoreComputer.LOWEST; 340 } 341 342 private void readExtensionScorePairs() { 343 fExtensionScorePairs= new ArrayList (3); 344 String content= fElement.getAttribute(EXTENSIONS_ATTRIBUTE); 345 if (content == null) 346 return; 347 StringTokenizer tokenizer= new StringTokenizer (content, ","); while (tokenizer.hasMoreElements()) { 349 String token= tokenizer.nextToken().trim(); 350 int pos= token.indexOf(':'); 351 if (pos != -1) { 352 String extension= token.substring(0, pos); 353 int score= StringConverter.asInt(token.substring(pos+1), ISearchPageScoreComputer.UNKNOWN); 354 if (extension.equals("*")) { fWildcardScore= score; 356 } else { 357 fExtensionScorePairs.add(new ExtensionScorePair(extension, score)); 358 } 359 } 360 } 361 } 362 363 366 public String getLocalId() { 367 return getId(); 368 } 369 370 373 public String getPluginId() { 374 return fElement.getContributor().getName(); 375 } 376 } 377 | Popular Tags |