KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > cheatsheets > CheatsheetSearchParticipant


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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.ui.internal.cheatsheets;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.core.runtime.IConfigurationElement;
17 import org.eclipse.core.runtime.Platform;
18 import org.eclipse.help.search.XMLSearchParticipant;
19 import org.eclipse.jface.action.Action;
20 import org.eclipse.ui.cheatsheets.OpenCheatSheetAction;
21 import org.eclipse.ui.internal.cheatsheets.composite.parser.ICompositeCheatsheetTags;
22 import org.eclipse.ui.internal.cheatsheets.data.IParserTags;
23 import org.eclipse.ui.internal.cheatsheets.registry.CheatSheetRegistryReader;
24 import org.xml.sax.Attributes JavaDoc;
25
26 public class CheatsheetSearchParticipant extends XMLSearchParticipant {
27     private static final String JavaDoc INTRO_DESC = "cheatsheet/intro/description"; //$NON-NLS-1$
28

29     private static final String JavaDoc ITEM_DESC = "cheatsheet/item/description"; //$NON-NLS-1$
30

31     private static final String JavaDoc CCS_DESC = "compositeCheatsheet/taskGroup/intro"; //$NON-NLS-1$
32

33     /**
34      * Returns all the documents that this participant knows about. This method
35      * is only used for participants that handle documents outside of the help
36      * system's TOC.
37      *
38      * @return a set of hrefs for documents managed by this participant.
39      */

40     public Set JavaDoc getAllDocuments(String JavaDoc locale) {
41         HashSet JavaDoc set = new HashSet JavaDoc();
42         IConfigurationElement[] elements = Platform.getExtensionRegistry()
43                 .getConfigurationElementsFor(
44                         ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID + '.'
45                         + CheatSheetRegistryReader.CHEAT_SHEET_CONTENT);
46         for (int i = 0; i < elements.length; i++) {
47             IConfigurationElement element = elements[i];
48             if (!element.getName().equals(CheatSheetRegistryReader.TAG_CHEATSHEET))
49                 continue;
50             String JavaDoc fileName = element.getAttribute(CheatSheetRegistryReader.ATT_CONTENTFILE);
51             String JavaDoc id = element.getAttribute("id"); //$NON-NLS-1$
52
String JavaDoc pluginId = element.getContributor().getName();
53             if (isExtensionValid(fileName, id, pluginId)) {
54                 try {
55                     fileName = resolveVariables(pluginId, fileName, locale);
56                     set.add("/" + pluginId + "/" + fileName + "?id=" + id); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
57
}
58                 catch (Throwable JavaDoc t) {
59                     // log and skip
60
CheatSheetPlugin.logError("Error parsing cheat sheet extension from plug-in " + pluginId + ", id " + id + ", file " + fileName, t); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
61
}
62             }
63         }
64         return set;
65     }
66
67     public Set JavaDoc getContributingPlugins() {
68         IConfigurationElement[] elements = Platform.getExtensionRegistry()
69                 .getConfigurationElementsFor(
70                         ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID + '.'
71                         + CheatSheetRegistryReader.CHEAT_SHEET_CONTENT);
72         HashSet JavaDoc set = new HashSet JavaDoc();
73         for (int i = 0; i < elements.length; i++) {
74             IConfigurationElement element = elements[i];
75             if (element.getName().equals(CheatSheetRegistryReader.TAG_CHEATSHEET)) {
76                 set.add(element.getContributor().getName());
77             }
78         }
79         return set;
80     }
81
82     protected void handleStartElement(String JavaDoc name, Attributes JavaDoc attributes,
83             IParsedXMLContent data) {
84         if (name.equals(IParserTags.CHEATSHEET)) {
85             data.setTitle(attributes.getValue(IParserTags.TITLE));
86             data.addText(attributes.getValue(IParserTags.TITLE));
87         } else if (name.equals(ICompositeCheatsheetTags.COMPOSITE_CHEATSHEET)) {
88             data.addText(attributes.getValue(ICompositeCheatsheetTags.NAME));
89             data.setTitle(attributes.getValue(ICompositeCheatsheetTags.NAME));
90         } else if (name.equals(IParserTags.ITEM)) {
91             data.addText(attributes.getValue(IParserTags.TITLE));
92         } else if (name.equals(IParserTags.SUBITEM)) {
93             data.addText(attributes.getValue(IParserTags.LABEL));
94         } else if (name.equals(ICompositeCheatsheetTags.TASK )
95                 || name.equals(ICompositeCheatsheetTags.TASK_GROUP)) {
96             data.addText(attributes.getValue(ICompositeCheatsheetTags.NAME));
97         }
98     }
99
100     protected void handleEndElement(String JavaDoc name, IParsedXMLContent data) {
101     }
102
103     protected void handleText(String JavaDoc text, IParsedXMLContent data) {
104         String JavaDoc stackPath = getElementStackPath();
105         String JavaDoc top = getTopElement();
106         if (IParserTags.INTRO.equals(top)) {
107             data.addText(text);
108             if (stackPath.equalsIgnoreCase(CCS_DESC)) {
109                 data.addToSummary(text);
110             }
111         } else if (IParserTags.ON_COMPLETION.equals(top)) {
112             data.addText(text);
113         } else if (stackPath.equalsIgnoreCase(INTRO_DESC)) {
114             data.addText(text);
115             data.addToSummary(text);
116             return;
117         } else if (stackPath.equalsIgnoreCase(ITEM_DESC)) {
118             data.addText(text);
119             return;
120         }
121     }
122
123     public boolean open(String JavaDoc id) {
124         Action openAction = new OpenCheatSheetAction(id);
125         openAction.run();
126         return true;
127     }
128     
129     private static boolean isExtensionValid(String JavaDoc fileName, String JavaDoc id, String JavaDoc pluginId) {
130         if (fileName.indexOf('\\') != -1) {
131             CheatSheetPlugin.logError("Error in cheat sheet extension id " + id + " from plug-in " + pluginId + ": path should not contain back-slashes (\\): " + fileName + ". This cheat sheet will not be indexed for searching.", null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
132
return false;
133         }
134         return true;
135     }
136 }
137
Popular Tags