1 11 package org.eclipse.search.internal.ui; 12 13 import java.util.ArrayList ; 14 import java.util.Collections ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.core.runtime.Assert; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IConfigurationElement; 21 import org.eclipse.core.runtime.IStatus; 22 import org.eclipse.core.runtime.Platform; 23 import org.eclipse.core.runtime.Status; 24 25 import org.eclipse.core.resources.IWorkspace; 26 import org.eclipse.core.resources.IWorkspaceDescription; 27 import org.eclipse.core.resources.ResourcesPlugin; 28 29 import org.eclipse.swt.widgets.Control; 30 import org.eclipse.swt.widgets.Display; 31 import org.eclipse.swt.widgets.Shell; 32 33 import org.eclipse.jface.action.GroupMarker; 34 import org.eclipse.jface.action.IMenuManager; 35 import org.eclipse.jface.action.Separator; 36 import org.eclipse.jface.dialogs.IDialogSettings; 37 38 import org.eclipse.ui.IWorkbenchPage; 39 import org.eclipse.ui.IWorkbenchWindow; 40 import org.eclipse.ui.plugin.AbstractUIPlugin; 41 42 import org.eclipse.search.ui.IContextMenuConstants; 43 import org.eclipse.search.ui.NewSearchUI; 44 45 import org.eclipse.search.internal.core.text.TextSearchEngineRegistry; 46 import org.eclipse.search.internal.ui.util.ExceptionHandler; 47 48 import org.eclipse.search2.internal.ui.InternalSearchUI; 49 import org.eclipse.search2.internal.ui.text2.TextSearchQueryProviderRegistry; 50 51 import org.osgi.framework.BundleContext; 52 53 56 public class SearchPlugin extends AbstractUIPlugin { 57 58 public static final String SEARCH_PAGE_EXTENSION_POINT= "searchPages"; public static final String SORTER_EXTENSION_POINT= "searchResultSorters"; 61 66 public static final String FILTERED_SEARCH_MARKER= NewSearchUI.PLUGIN_ID + ".filteredsearchmarker"; 68 73 public static final String SEARCH_ANNOTATION_TYPE= NewSearchUI.PLUGIN_ID + ".results"; 75 80 public static final String FILTERED_SEARCH_ANNOTATION_TYPE= NewSearchUI.PLUGIN_ID + ".filteredResults"; 82 83 public static final int INTERNAL_ERROR= 1; 84 85 private static SearchPlugin fgSearchPlugin; 86 87 88 private List fPageDescriptors; 89 private List fSorterDescriptors; 90 private TextSearchEngineRegistry fTextSearchEngineRegistry; 91 private TextSearchQueryProviderRegistry fTextSearchQueryProviderRegistry; 92 93 public SearchPlugin() { 94 super(); 95 Assert.isTrue(fgSearchPlugin == null); 96 fgSearchPlugin= this; 97 fTextSearchEngineRegistry= null; 98 fTextSearchQueryProviderRegistry= null; 99 } 100 101 104 public static SearchPlugin getDefault() { 105 return fgSearchPlugin; 106 } 107 108 112 public static IWorkbenchWindow getActiveWorkbenchWindow() { 113 IWorkbenchWindow window= fgSearchPlugin.getWorkbench().getActiveWorkbenchWindow(); 114 if (window == null) { 115 final WindowRef windowRef= new WindowRef(); 116 Display.getDefault().syncExec(new Runnable () { 117 public void run() { 118 setActiveWorkbenchWindow(windowRef); 119 } 120 }); 121 return windowRef.window; 122 } 123 return window; 124 } 125 126 private static class WindowRef { 127 public IWorkbenchWindow window; 128 } 129 130 private static void setActiveWorkbenchWindow(WindowRef windowRef) { 131 windowRef.window= null; 132 Display display= Display.getCurrent(); 133 if (display == null) 134 return; 135 Control shell= display.getActiveShell(); 136 while (shell != null) { 137 Object data= shell.getData(); 138 if (data instanceof IWorkbenchWindow) { 139 windowRef.window= (IWorkbenchWindow)data; 140 return; 141 } 142 shell= shell.getParent(); 143 } 144 Shell shells[]= display.getShells(); 145 for (int i= 0; i < shells.length; i++) { 146 Object data= shells[i].getData(); 147 if (data instanceof IWorkbenchWindow) { 148 windowRef.window= (IWorkbenchWindow)data; 149 return; 150 } 151 } 152 } 153 154 157 public static Shell getActiveWorkbenchShell() { 158 IWorkbenchWindow window= getActiveWorkbenchWindow(); 159 if (window != null) 160 return window.getShell(); 161 return null; 162 } 163 164 167 public static void beep() { 168 getActiveWorkbenchShell().getDisplay().beep(); 169 } 170 171 174 public static IWorkbenchPage getActivePage() { 175 return getActiveWorkbenchWindow().getActivePage(); 176 } 177 178 181 public static IWorkspace getWorkspace() { 182 return ResourcesPlugin.getWorkspace(); 183 } 184 185 186 187 static boolean setAutoBuilding(boolean state) { 188 IWorkspaceDescription workspaceDesc= getWorkspace().getDescription(); 189 boolean isAutobuilding= workspaceDesc.isAutoBuilding(); 190 191 if (isAutobuilding != state) { 192 workspaceDesc.setAutoBuilding(state); 193 try { 194 getWorkspace().setDescription(workspaceDesc); 195 } 196 catch (CoreException ex) { 197 ExceptionHandler.handle(ex, SearchMessages.Search_Error_setDescription_title, SearchMessages.Search_Error_setDescription_message); 198 } 199 } 200 return isAutobuilding; 201 } 202 203 208 public void start(BundleContext context) throws Exception { 209 super.start(context); 210 } 211 212 217 public void stop(BundleContext context) throws Exception { 218 InternalSearchUI.shutdown(); 219 getWorkspace().removeResourceChangeListener(SearchManager.getDefault()); 220 super.stop(context); 221 fgSearchPlugin= null; 222 } 223 224 227 public List getSearchPageDescriptors() { 228 if (fPageDescriptors == null) { 229 IConfigurationElement[] elements= Platform.getExtensionRegistry().getConfigurationElementsFor(NewSearchUI.PLUGIN_ID, SEARCH_PAGE_EXTENSION_POINT); 230 fPageDescriptors= createSearchPageDescriptors(elements); 231 } 232 return fPageDescriptors; 233 } 234 235 239 public List getEnabledSearchPageDescriptors(String pageId) { 240 Iterator iter= getSearchPageDescriptors().iterator(); 241 List enabledDescriptors= new ArrayList (5); 242 while (iter.hasNext()) { 243 SearchPageDescriptor desc= (SearchPageDescriptor)iter.next(); 244 if (desc.isEnabled() || desc.getId().equals(pageId)) 245 enabledDescriptors.add(desc); 246 } 247 return enabledDescriptors; 248 } 249 250 257 public String getSearchViewHelpContextId() { 258 Search currentSearch= SearchManager.getDefault().getCurrentSearch(); 259 if (currentSearch != null) { 260 String pageId= currentSearch.getPageId(); 261 Iterator iter= getSearchPageDescriptors().iterator(); 262 while (iter.hasNext()) { 263 SearchPageDescriptor desc= (SearchPageDescriptor)iter.next(); 264 if (desc.getId().equals(pageId)) { 265 String helpId= desc.getSearchViewHelpContextId(); 266 if (helpId == null) 267 return ISearchHelpContextIds.SEARCH_VIEW; 268 return desc.getSearchViewHelpContextId(); 269 } 270 } 271 } 272 return ISearchHelpContextIds.SEARCH_VIEW; 273 } 274 275 280 private List createSearchPageDescriptors(IConfigurationElement[] elements) { 281 List result= new ArrayList (5); 282 for (int i= 0; i < elements.length; i++) { 283 IConfigurationElement element= elements[i]; 284 if (SearchPageDescriptor.PAGE_TAG.equals(element.getName())) { 285 SearchPageDescriptor desc= new SearchPageDescriptor(element); 286 result.add(desc); 287 } 288 } 289 Collections.sort(result); 290 return result; 291 } 292 293 296 public List getSorterDescriptors() { 297 if (fSorterDescriptors == null) { 298 IConfigurationElement[] elements= Platform.getExtensionRegistry().getConfigurationElementsFor(NewSearchUI.PLUGIN_ID, SORTER_EXTENSION_POINT); 299 fSorterDescriptors= createSorterDescriptors(elements); 300 } 301 return fSorterDescriptors; 302 } 303 304 305 public TextSearchEngineRegistry getTextSearchEngineRegistry() { 306 if (fTextSearchEngineRegistry == null) { 307 fTextSearchEngineRegistry= new TextSearchEngineRegistry(); 308 } 309 return fTextSearchEngineRegistry; 310 } 311 312 public TextSearchQueryProviderRegistry getTextSearchQueryProviderRegistry() { 313 if (fTextSearchQueryProviderRegistry == null) { 314 fTextSearchQueryProviderRegistry= new TextSearchQueryProviderRegistry(); 315 } 316 return fTextSearchQueryProviderRegistry; 317 } 318 319 324 private List createSorterDescriptors(IConfigurationElement[] elements) { 325 List result= new ArrayList (5); 326 for (int i= 0; i < elements.length; i++) { 327 IConfigurationElement element= elements[i]; 328 if (SorterDescriptor.SORTER_TAG.equals(element.getName())) 329 result.add(new SorterDescriptor(element)); 330 } 331 return result; 332 } 333 334 public IDialogSettings getDialogSettingsSection(String name) { 335 IDialogSettings dialogSettings= getDialogSettings(); 336 IDialogSettings section= dialogSettings.getSection(name); 337 if (section == null) { 338 section= dialogSettings.addNewSection(name); 339 } 340 return section; 341 } 342 343 344 348 public static void log(IStatus status) { 349 getDefault().getLog().log(status); 350 } 351 352 public static void log(Throwable e) { 353 log(new Status(IStatus.ERROR, NewSearchUI.PLUGIN_ID, INTERNAL_ERROR, SearchMessages.SearchPlugin_internal_error, e)); 354 } 355 356 public static String getID() { 357 return NewSearchUI.PLUGIN_ID; 358 } 359 360 364 public static void createStandardGroups(IMenuManager menu) { 365 if (!menu.isEmpty()) 366 return; 367 menu.add(new Separator(IContextMenuConstants.GROUP_NEW)); 368 menu.add(new GroupMarker(IContextMenuConstants.GROUP_GOTO)); 369 menu.add(new GroupMarker(IContextMenuConstants.GROUP_OPEN)); 370 menu.add(new Separator(IContextMenuConstants.GROUP_SHOW)); 371 menu.add(new Separator(IContextMenuConstants.GROUP_BUILD)); 372 menu.add(new Separator(IContextMenuConstants.GROUP_REORGANIZE)); 373 menu.add(new Separator(IContextMenuConstants.GROUP_REMOVE_MATCHES)); 374 menu.add(new GroupMarker(IContextMenuConstants.GROUP_GENERATE)); 375 menu.add(new Separator(IContextMenuConstants.GROUP_SEARCH)); 376 menu.add(new Separator(IContextMenuConstants.GROUP_ADDITIONS)); 377 menu.add(new Separator(IContextMenuConstants.GROUP_VIEWER_SETUP)); 378 menu.add(new Separator(IContextMenuConstants.GROUP_PROPERTIES)); 379 } 380 } 381 | Popular Tags |