KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
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 /**
54  * The plug-in runtime class for Search plug-in
55  */

56 public class SearchPlugin extends AbstractUIPlugin {
57     
58     public static final String JavaDoc SEARCH_PAGE_EXTENSION_POINT= "searchPages"; //$NON-NLS-1$
59
public static final String JavaDoc SORTER_EXTENSION_POINT= "searchResultSorters"; //$NON-NLS-1$
60

61     /**
62      * Filtered search marker type (value <code>"org.eclipse.search.filteredsearchmarker"</code>).
63      *
64      * @see org.eclipse.core.resources.IMarker
65      */

66     public static final String JavaDoc FILTERED_SEARCH_MARKER= NewSearchUI.PLUGIN_ID + ".filteredsearchmarker"; //$NON-NLS-1$
67

68     /**
69      * Search annotation type (value <code>"org.eclipse.search.results"</code>).
70      *
71      * @since 3.2
72      */

73     public static final String JavaDoc SEARCH_ANNOTATION_TYPE= NewSearchUI.PLUGIN_ID + ".results"; //$NON-NLS-1$
74

75     /**
76      * Filtered search annotation type (value <code>"org.eclipse.search.filteredResults"</code>).
77      *
78      * @since 3.2
79      */

80     public static final String JavaDoc FILTERED_SEARCH_ANNOTATION_TYPE= NewSearchUI.PLUGIN_ID + ".filteredResults"; //$NON-NLS-1$
81

82     /** Status code describing an internal error */
83     public static final int INTERNAL_ERROR= 1;
84     
85     private static SearchPlugin fgSearchPlugin;
86
87             
88     private List JavaDoc fPageDescriptors;
89     private List JavaDoc 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     /**
102      * @return Returns the search plugin instance.
103      */

104     public static SearchPlugin getDefault() {
105         return fgSearchPlugin;
106     }
107     
108     /**
109      * Returns the active workbench window.
110      * @return returns <code>null</code> if the active window is not a workbench window
111      */

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 JavaDoc() {
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 JavaDoc 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 JavaDoc data= shells[i].getData();
147             if (data instanceof IWorkbenchWindow) {
148                 windowRef.window= (IWorkbenchWindow)data;
149                 return;
150             }
151         }
152     }
153
154     /**
155      * @return Returns the shell of the active workbench window.
156      */

157     public static Shell getActiveWorkbenchShell() {
158         IWorkbenchWindow window= getActiveWorkbenchWindow();
159         if (window != null)
160             return window.getShell();
161         return null;
162     }
163
164     /**
165      * Beeps using the display of the active workbench window.
166      */

167     public static void beep() {
168         getActiveWorkbenchShell().getDisplay().beep();
169     }
170
171     /**
172      * @return Returns the active workbench window's currrent page.
173      */

174     public static IWorkbenchPage getActivePage() {
175         return getActiveWorkbenchWindow().getActivePage();
176     }
177
178     /**
179      * @return Returns the workbench from which this plugin has been loaded.
180      */

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     /**
204      * This method is called upon plug-in activation
205      * @param context
206      * @throws Exception
207      */

208     public void start(BundleContext context) throws Exception JavaDoc {
209         super.start(context);
210     }
211
212     /**
213      * This method is called when the plug-in is stopped
214      * @param context
215      * @throws Exception
216      */

217     public void stop(BundleContext context) throws Exception JavaDoc {
218         InternalSearchUI.shutdown();
219         getWorkspace().removeResourceChangeListener(SearchManager.getDefault());
220         super.stop(context);
221         fgSearchPlugin= null;
222     }
223
224     /**
225      * @return Returns all search pages contributed to the workbench.
226      */

227     public List JavaDoc 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     /**
236      * @param pageId the page id
237      * @return Returns all search pages contributed to the workbench.
238      */

239     public List JavaDoc getEnabledSearchPageDescriptors(String JavaDoc pageId) {
240         Iterator JavaDoc iter= getSearchPageDescriptors().iterator();
241         List JavaDoc enabledDescriptors= new ArrayList JavaDoc(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     /**
251      * @return Returns the help context ID for the Search view
252      * as provided by the current search page extension.
253      *
254      * @since 3.0
255      * @deprecated old search
256      */

257     public String JavaDoc getSearchViewHelpContextId() {
258         Search currentSearch= SearchManager.getDefault().getCurrentSearch();
259         if (currentSearch != null) {
260             String JavaDoc pageId= currentSearch.getPageId();
261             Iterator JavaDoc iter= getSearchPageDescriptors().iterator();
262             while (iter.hasNext()) {
263                 SearchPageDescriptor desc= (SearchPageDescriptor)iter.next();
264                 if (desc.getId().equals(pageId)) {
265                     String JavaDoc 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     /**
276      * Creates all necessary search page nodes.
277      * @param elements the configuration elements
278      * @return the created SearchPageDescriptor
279      */

280     private List JavaDoc createSearchPageDescriptors(IConfigurationElement[] elements) {
281         List JavaDoc result= new ArrayList JavaDoc(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     /**
294      * @return Returns all sorters contributed to the workbench.
295      */

296     public List JavaDoc 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     /**
320      * Creates all necessary sorter description nodes.
321      * @param elements the configuration elements
322      * @return the created SorterDescriptor
323      */

324     private List JavaDoc createSorterDescriptors(IConfigurationElement[] elements) {
325         List JavaDoc result= new ArrayList JavaDoc(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 JavaDoc 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     /**
345      * Log status to platform log
346      * @param status the status to log
347      */

348     public static void log(IStatus status) {
349         getDefault().getLog().log(status);
350     }
351
352     public static void log(Throwable JavaDoc e) {
353         log(new Status(IStatus.ERROR, NewSearchUI.PLUGIN_ID, INTERNAL_ERROR, SearchMessages.SearchPlugin_internal_error, e));
354     }
355     
356     public static String JavaDoc getID() {
357         return NewSearchUI.PLUGIN_ID;
358     }
359
360     /**
361      * Creates the Search plugin standard groups in a context menu.
362      * @param menu the menu to create in
363      */

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