KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Michael Fraenkel (fraenkel@us.ibm.com) - contributed a fix for:
11  * o Search dialog not respecting activity enablement
12  * (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=45729)
13  *******************************************************************************/

14 package org.eclipse.search.internal.ui;
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.Arrays JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.StringTokenizer JavaDoc;
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 /**
49  * Proxy that represents a search page.
50  */

51 class SearchPageDescriptor implements IPluginContribution, Comparable JavaDoc {
52
53     public final static String JavaDoc PAGE_TAG= "page"; //$NON-NLS-1$
54
private final static String JavaDoc ID_ATTRIBUTE= "id"; //$NON-NLS-1$
55
private final static String JavaDoc ICON_ATTRIBUTE= "icon"; //$NON-NLS-1$
56
private final static String JavaDoc CLASS_ATTRIBUTE= "class"; //$NON-NLS-1$
57
private final static String JavaDoc LABEL_ATTRIBUTE= "label"; //$NON-NLS-1$
58
private final static String JavaDoc SIZE_ATTRIBUTE= "sizeHint"; //$NON-NLS-1$
59
private final static String JavaDoc TAB_POSITION_ATTRIBUTE= "tabPosition"; //$NON-NLS-1$
60
private final static String JavaDoc EXTENSIONS_ATTRIBUTE= "extensions"; //$NON-NLS-1$
61
private final static String JavaDoc SHOW_SCOPE_SECTION_ATTRIBUTE= "showScopeSection"; //$NON-NLS-1$
62
private final static String JavaDoc CAN_SEARCH_ENCLOSING_PROJECTS= "canSearchEnclosingProjects"; //$NON-NLS-1$
63
private final static String JavaDoc ENABLED_ATTRIBUTE= "enabled"; //$NON-NLS-1$
64
private final static String JavaDoc SEARCH_VIEW_HELP_CONTEXT_ID_ATTRIBUTE= "searchViewHelpContextId"; //$NON-NLS-1$
65

66     public final static Point UNKNOWN_SIZE= new Point(SWT.DEFAULT, SWT.DEFAULT);
67
68     // dialog store id constants
69
private final static String JavaDoc SECTION_ID= "Search"; //$NON-NLS-1$
70
private final static String JavaDoc STORE_ENABLED_PAGE_IDS= SECTION_ID + ".enabledPageIds"; //$NON-NLS-1$
71
private final static String JavaDoc STORE_PROCESSED_PAGE_IDS= SECTION_ID + ".processedPageIds"; //$NON-NLS-1$
72

73     private static List JavaDoc fgEnabledPageIds;
74     
75     private static class ExtensionScorePair {
76         public String JavaDoc extension;
77         public int score;
78         public ExtensionScorePair(String JavaDoc extension, int score) {
79             this.extension= extension;
80             this.score= score;
81         }
82     }
83
84     private IConfigurationElement fElement;
85     private List JavaDoc fExtensionScorePairs;
86     private int fWildcardScore= ISearchPageScoreComputer.UNKNOWN;
87     private ISearchPage fCreatedPage;
88     
89     /**
90      * Creates a new search page node with the given configuration element.
91      * @param element The configuration element
92      */

93     public SearchPageDescriptor(IConfigurationElement element) {
94         fElement= element;
95     }
96
97     /**
98      * Creates a new search page from this node.
99      * @param container The parent container
100      * @return the created page or null if the creation failed
101      * @throws CoreException Page creation failed
102      */

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     //---- XML Attribute accessors ---------------------------------------------
125

126     /**
127      * Returns the page's id.
128      * @return The id of the page
129      */

130     public String JavaDoc getId() {
131         return fElement.getAttribute(ID_ATTRIBUTE);
132     }
133      
134     /**
135      * Returns the page's image
136      * @return ImageDescriptor of the image or null if creating failed
137      */

138     public ImageDescriptor getImage() {
139         String JavaDoc 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     /**
147      * @return Returns the page's label.
148      */

149     public String JavaDoc getLabel() {
150         return fElement.getAttribute(LABEL_ATTRIBUTE);
151     }
152
153     /**
154      * @return Returns <code>true</code> if the scope section needs
155      * to be shown in the dialog.
156      */

157     public boolean showScopeSection() {
158         return Boolean.valueOf(fElement.getAttribute(SHOW_SCOPE_SECTION_ATTRIBUTE)).booleanValue();
159     }
160
161     /**
162      * Returns <code>true</code> if the page is initially
163      * shown in the Search dialog.
164      *
165      * This attribute is optional and defaults to <code>true</code>.
166      * @return Returns if the page should be initially shown
167      */

168     public boolean isInitiallyEnabled() {
169         String JavaDoc strVal= fElement.getAttribute(ENABLED_ATTRIBUTE);
170         return strVal == null || Boolean.valueOf(strVal).booleanValue();
171     }
172
173     /**
174      * Returns <code>true</code> if the page can handle
175      * searches in enclosing projects. The value should be ignored if <code>showScopeSection()</code>
176      * returns <code>false</code>.
177      *
178      * This attribute is optional and defaults to <code>false</code>.
179      * @return Returns if the page can handle searches in enclosing projects
180      */

181     public boolean canSearchInProjects() {
182         return Boolean.valueOf(fElement.getAttribute(CAN_SEARCH_ENCLOSING_PROJECTS)).booleanValue();
183     }
184
185     /**
186      * @return Returns the page's preferred size
187      */

188     public Point getPreferredSize() {
189         return StringConverter.asPoint(
190             fElement.getAttribute(SIZE_ATTRIBUTE), UNKNOWN_SIZE);
191     }
192     
193     /**
194      * Returns the page's tab position relative to the other tabs.
195      * @return the tab position or <code>Integer.MAX_VALUE</code> if not defined in
196      * the plugins.xml file
197      */

198     public int getTabPosition() {
199         int position= Integer.MAX_VALUE / 2;
200         String JavaDoc str= fElement.getAttribute(TAB_POSITION_ATTRIBUTE);
201         if (str != null)
202             try {
203                 position= Integer.parseInt(str);
204         } catch (NumberFormatException JavaDoc ex) {
205             ExceptionHandler.log(ex, SearchMessages.Search_Error_createSearchPage_message);
206             // position is Integer.MAX_VALUE;
207
}
208         return position;
209     }
210
211     boolean isEnabled() {
212         return getEnabledPageIds().contains(getId());
213     }
214
215     /**
216      * Returns the help context for help shown in search view.
217      *
218      * @return the help context id or <code>null</code> if not defined
219      */

220     public String JavaDoc getSearchViewHelpContextId() {
221         return fElement.getAttribute(SEARCH_VIEW_HELP_CONTEXT_ID_ATTRIBUTE);
222     }
223
224     static void setEnabled(Object JavaDoc[] enabledDescriptors) {
225         fgEnabledPageIds= new ArrayList JavaDoc(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 JavaDoc getEnabledPageIds() {
234         if (fgEnabledPageIds == null) {
235             List JavaDoc descriptors= SearchPlugin.getDefault().getSearchPageDescriptors();
236             
237             String JavaDoc[] enabledPageIds= getDialogSettings().getArray(STORE_ENABLED_PAGE_IDS);
238             if (enabledPageIds == null)
239                 fgEnabledPageIds= new ArrayList JavaDoc(descriptors.size());
240             else
241                 fgEnabledPageIds= new ArrayList JavaDoc(Arrays.asList(enabledPageIds));
242             
243
244             List JavaDoc processedPageIds;
245             String JavaDoc[] processedPageIdsArr= getDialogSettings().getArray(STORE_PROCESSED_PAGE_IDS);
246             if (processedPageIdsArr == null)
247                 processedPageIds= new ArrayList JavaDoc(descriptors.size());
248             else
249                 processedPageIds= new ArrayList JavaDoc(Arrays.asList(processedPageIdsArr));
250             
251             // Enable pages based on contribution
252
Iterator JavaDoc 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 JavaDoc[])processedPageIds.toArray(new String JavaDoc[processedPageIds.size()]));
264             storeEnabledPageIds();
265         }
266         return fgEnabledPageIds;
267     }
268
269     private static void storeEnabledPageIds() {
270         getDialogSettings().put(STORE_ENABLED_PAGE_IDS, (String JavaDoc[])fgEnabledPageIds.toArray(new String JavaDoc[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             // create new section
279
section= settings.addNewSection(SECTION_ID);
280         return section;
281     }
282
283     /*
284      * Implements a method from IComparable
285      */

286     public int compareTo(Object JavaDoc 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     //---- Suitability tests ---------------------------------------------------
296

297     /**
298      * Returns the score for this page with the given input element.
299      * @param element The input element
300      * @return The scope for the page
301      */

302     public int computeScore(Object JavaDoc 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 JavaDoc 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         } /* can be removed as ISearchResultViewEntry adapts to IResource
316             else if (element instanceof ISearchResultViewEntry) {
317             ISearchResultViewEntry entry= (ISearchResultViewEntry)element;
318             return computeScore(entry.getSelectedMarker());
319         }*/

320         if (fWildcardScore != ISearchPageScoreComputer.UNKNOWN)
321             return fWildcardScore;
322             
323         return ISearchPageScoreComputer.LOWEST;
324     }
325     
326     private int getScoreForFileExtension(String JavaDoc 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 JavaDoc(3);
344         String JavaDoc content= fElement.getAttribute(EXTENSIONS_ATTRIBUTE);
345         if (content == null)
346             return;
347         StringTokenizer JavaDoc tokenizer= new StringTokenizer JavaDoc(content, ","); //$NON-NLS-1$
348
while (tokenizer.hasMoreElements()) {
349             String JavaDoc token= tokenizer.nextToken().trim();
350             int pos= token.indexOf(':');
351             if (pos != -1) {
352                 String JavaDoc extension= token.substring(0, pos);
353                 int score= StringConverter.asInt(token.substring(pos+1), ISearchPageScoreComputer.UNKNOWN);
354                 if (extension.equals("*")) { //$NON-NLS-1$
355
fWildcardScore= score;
356                 } else {
357                     fExtensionScorePairs.add(new ExtensionScorePair(extension, score));
358                 }
359             }
360         }
361     }
362
363     /* (non-Javadoc)
364      * @see org.eclipse.ui.IPluginContribution#getLocalId()
365      */

366     public String JavaDoc getLocalId() {
367         return getId();
368     }
369
370     /* (non-Javadoc)
371      * @see org.eclipse.ui.IPluginContribution#getPluginId()
372      */

373     public String JavaDoc getPluginId() {
374         return fElement.getContributor().getName();
375     }
376 }
377
Popular Tags