KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > impl > model > IntroSearchParticipant


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.intro.impl.model;
12
13 import java.io.StringReader JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.apache.lucene.document.Document;
19 import org.apache.lucene.document.Field;
20 import org.eclipse.core.runtime.IConfigurationElement;
21 import org.eclipse.core.runtime.IProduct;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Platform;
24 import org.eclipse.core.runtime.Status;
25 import org.eclipse.help.search.ISearchIndex;
26 import org.eclipse.help.search.LuceneSearchParticipant;
27 import org.eclipse.ui.PlatformUI;
28 import org.eclipse.ui.internal.intro.impl.IntroPlugin;
29 import org.eclipse.ui.internal.intro.impl.model.loader.ExtensionPointManager;
30 import org.eclipse.ui.intro.IIntroManager;
31 import org.eclipse.ui.intro.IIntroPart;
32 import org.eclipse.ui.intro.config.IIntroURL;
33 import org.eclipse.ui.intro.config.IntroURLFactory;
34 import org.osgi.framework.Bundle;
35
36 /**
37  * An implementation of the Lucene search participant that adds Welcome content into the local help
38  * index so that it can be searched.
39  *
40  */

41
42 public class IntroSearchParticipant extends LuceneSearchParticipant {
43
44     private IntroModelRoot model;
45     
46     private class TitleAndSummary {
47         String JavaDoc title;
48         String JavaDoc summary;
49     }
50
51     /*
52      * (non-Javadoc)
53      *
54      * @see org.eclipse.help.search.LuceneSearchParticipant#getContributingPlugins()
55      */

56     public Set JavaDoc getContributingPlugins() {
57         IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(
58                 "org.eclipse.ui.intro.config"); //$NON-NLS-1$
59
HashSet JavaDoc set = new HashSet JavaDoc();
60         for (int i = 0; i < elements.length; i++) {
61             IConfigurationElement element = elements[i];
62             if (!element.getName().equals("config")) //$NON-NLS-1$
63
continue;
64             set.add(element.getContributor().getName());
65         }
66         elements = Platform.getExtensionRegistry().getConfigurationElementsFor(
67                 "org.eclipse.ui.intro.configExtension"); //$NON-NLS-1$
68
for (int i = 0; i < elements.length; i++) {
69             IConfigurationElement element = elements[i];
70             if (!element.getName().equals("configExtension")) //$NON-NLS-1$
71
continue;
72             set.add(element.getContributor().getName());
73         }
74         return set;
75     }
76
77     /*
78      * (non-Javadoc)
79      *
80      * @see org.eclipse.help.search.LuceneSearchParticipant#getAllDocuments(java.lang.String)
81      */

82     public Set JavaDoc getAllDocuments(String JavaDoc locale) {
83         HashSet JavaDoc set = new HashSet JavaDoc();
84         IProduct product = Platform.getProduct();
85         if (product == null) {
86             return set;
87         }
88         String JavaDoc productId = product.getId();
89         IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(
90                 "org.eclipse.ui.intro"); //$NON-NLS-1$
91
String JavaDoc targetIntroId = null;
92         for (int i = 0; i < elements.length; i++) {
93             IConfigurationElement element = elements[i];
94             if (element.getName().equals("introProductBinding")) { //$NON-NLS-1$
95
String JavaDoc pid = element.getAttribute("productId"); //$NON-NLS-1$
96
String JavaDoc iid = element.getAttribute("introId"); //$NON-NLS-1$
97
if (productId.equals(pid)) {
98                     targetIntroId = iid;
99                     break;
100                 }
101             }
102         }
103         if (targetIntroId == null)
104             return set;
105         elements = Platform.getExtensionRegistry().getConfigurationElementsFor("org.eclipse.ui.intro.config"); //$NON-NLS-1$
106
IConfigurationElement config = null;
107         for (int i = 0; i < elements.length; i++) {
108             IConfigurationElement element = elements[i];
109             if (element.getName().equals("config")) { //$NON-NLS-1$
110
String JavaDoc iid = element.getAttribute("introId"); //$NON-NLS-1$
111
if (targetIntroId.equals(iid)) {
112                     config = element;
113                     break;
114                 }
115             }
116         }
117         if (config == null)
118             return set;
119         String JavaDoc configId = config.getAttribute("id"); //$NON-NLS-1$
120
ExtensionPointManager extensionPointManager = IntroPlugin.getDefault().getExtensionPointManager();
121         model = extensionPointManager.getModel(configId);
122         if (model != null && model.hasValidConfig())
123             loadFromModel(model, set, locale);
124         return set;
125     }
126
127     private void loadFromModel(IntroModelRoot model, HashSet JavaDoc set, String JavaDoc locale) {
128         IntroPage[] pages = model.getPages();
129         for (int i = 0; i < pages.length; i++) {
130             IntroPage page = pages[i];
131             Bundle bundle = page.getBundle();
132             String JavaDoc bundleId = bundle.getSymbolicName();
133             String JavaDoc content = page.getRawContent();
134             String JavaDoc pageId = page.getId();
135             String JavaDoc href;
136             if (content != null)
137                 href = resolveVariables(bundleId, content, locale);
138             else
139                 href = pageId;
140             set.add("/" + bundleId + "/" + href + "?id=" + pageId); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
141
}
142     }
143
144     /*
145      * (non-Javadoc)
146      *
147      * @see org.eclipse.help.search.LuceneSearchParticipant#addDocument(java.lang.String,
148      * java.lang.String, java.net.URL, java.lang.String, java.lang.String,
149      * org.apache.lucene.document.Document)
150      */

151     public IStatus addDocument(ISearchIndex index, String JavaDoc pluginId, String JavaDoc name, URL JavaDoc url, String JavaDoc id,
152             Document doc) {
153         if (model == null)
154             return Status.CANCEL_STATUS;
155         IntroPage page = getPage(id);
156         if (page == null)
157             return Status.CANCEL_STATUS;
158         return addPage(index, pluginId, name, url, page, doc);
159     }
160
161     private IntroPage getPage(String JavaDoc id) {
162         IntroPage[] pages = model.getPages();
163         for (int i = 0; i < pages.length; i++) {
164             if (pages[i].getId().equals(id))
165                 return pages[i];
166         }
167         return null;
168     }
169
170     private IStatus addPage(ISearchIndex index, String JavaDoc pluginId, String JavaDoc name, URL JavaDoc url, IntroPage page,
171             Document doc) {
172         AbstractIntroElement[] children = page.getChildren();
173         if (children.length > 0) {
174             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
175             TitleAndSummary titleSummary = new TitleAndSummary();
176             addChildren(children, buf, doc, titleSummary);
177             String JavaDoc contents = buf.toString();
178             if (titleSummary.title != null) {
179                  addTitle(titleSummary.title, doc);
180             }
181             if (titleSummary.summary != null) {
182                 doc.add(new Field("summary", titleSummary.summary, Field.Store.YES, Field.Index.NO)); //$NON-NLS-1$
183
}
184             doc.add(new Field("contents", new StringReader JavaDoc(contents))); //$NON-NLS-1$
185
doc.add(new Field("exact_contents", new StringReader JavaDoc(contents))); //$NON-NLS-1$
186
return Status.OK_STATUS;
187         }
188         // delegate to the help system
189
return index.addDocument(pluginId, name, url, page.getId(), doc);
190     }
191
192     private void addChildren(AbstractIntroElement[] children, StringBuffer JavaDoc buf, Document doc, TitleAndSummary titleSummary) {
193         for (int i = 0; i < children.length; i++) {
194             AbstractIntroElement child = children[i];
195             if (child instanceof IntroLink) {
196                 String JavaDoc text = ((IntroLink)child).getLabel();
197                 appendNewText(buf, text);
198             } else if (child instanceof IntroText) {
199                 IntroText childIntroText = (IntroText) child;
200                 appendNewText(buf, childIntroText.getText());
201                 String JavaDoc childId = childIntroText.getId();
202                 String JavaDoc title = null;
203                 if ("page-title".equals(childId)) { //$NON-NLS-1$
204
title = childIntroText.getText();
205                 } else if (child instanceof IntroPageTitle) {
206                     title = ((IntroPageTitle) child).getTitle();
207                 }
208                 if (title != null) {
209                     titleSummary.title = title;
210                 }
211                 if ("page-description".equals(childId)) { //$NON-NLS-1$
212
titleSummary.summary = childIntroText.getText();
213                 }
214             }
215             if (child instanceof AbstractIntroContainer) {
216                 AbstractIntroContainer container = (AbstractIntroContainer) child;
217                 if (!"navigation-links".equals(container.getId())) { //$NON-NLS-1$
218
AbstractIntroElement[] cc = container.getChildren();
219                     addChildren(cc, buf, doc, titleSummary);
220                 }
221             }
222         }
223     }
224
225     private void appendNewText(StringBuffer JavaDoc buf, String JavaDoc text) {
226         if (text == null) return;
227         if (buf.length() > 0)
228             buf.append(" "); //$NON-NLS-1$
229
buf.append(text);
230     }
231
232     /*
233      * (non-Javadoc)
234      *
235      * @see org.eclipse.help.search.LuceneSearchParticipant#clear()
236      */

237     public void clear() {
238         model = null;
239     }
240
241     /*
242      * (non-Javadoc)
243      *
244      * @see org.eclipse.help.search.LuceneSearchParticipant#open(java.lang.String)
245      */

246     public boolean open(String JavaDoc id) {
247         IIntroManager introManager = PlatformUI.getWorkbench().getIntroManager();
248         IIntroPart intro = introManager
249                 .showIntro(PlatformUI.getWorkbench().getActiveWorkbenchWindow(), false);
250         if (intro == null)
251             return false;
252         IIntroURL url = IntroURLFactory.createIntroURL("http://org.eclipse.ui.intro/showPage?id=" + id); //$NON-NLS-1$
253
return url.execute();
254     }
255 }
256
Popular Tags