Skip to main content
summaryrefslogtreecommitdiffstats
blob: 656714c7c5d07029a243044cdcabe42cfcadb423 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*******************************************************************************
 * Copyright (c) 2005, 2010 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.internal.intro.impl.model;

import java.net.URL;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IProduct;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.help.search.IHelpSearchIndex;
import org.eclipse.help.search.ISearchDocument;
import org.eclipse.help.search.SearchParticipant;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.intro.impl.IntroPlugin;
import org.eclipse.ui.internal.intro.impl.model.loader.ExtensionPointManager;
import org.eclipse.ui.intro.IIntroManager;
import org.eclipse.ui.intro.IIntroPart;
import org.eclipse.ui.intro.config.IIntroURL;
import org.eclipse.ui.intro.config.IntroURLFactory;
import org.osgi.framework.Bundle;

/**
 * An implementation of SearchParticipant that adds Welcome content into the local help
 * index so that it can be searched.
 * 
 */

public class IntroSearchParticipant extends SearchParticipant {

	private IntroModelRoot model;
	
	private class TitleAndSummary {
		String title;
		String summary;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.help.search.LuceneSearchParticipant#getContributingPlugins()
	 */
	public Set getContributingPlugins() {
		IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(
				"org.eclipse.ui.intro.config"); //$NON-NLS-1$
		HashSet set = new HashSet();
		for (int i = 0; i < elements.length; i++) {
			IConfigurationElement element = elements[i];
			if (!element.getName().equals("config")) //$NON-NLS-1$
				continue;
			set.add(element.getContributor().getName());
		}
		elements = Platform.getExtensionRegistry().getConfigurationElementsFor(
				"org.eclipse.ui.intro.configExtension"); //$NON-NLS-1$
		for (int i = 0; i < elements.length; i++) {
			IConfigurationElement element = elements[i];
			if (!element.getName().equals("configExtension")) //$NON-NLS-1$
				continue;
			set.add(element.getContributor().getName());
		}
		return set;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.help.search.LuceneSearchParticipant#getAllDocuments(java.lang.String)
	 */
	public Set getAllDocuments(String locale) {
		HashSet set = new HashSet();
		IProduct product = Platform.getProduct();
		if (product == null) {
			return set;
		}
		String productId = product.getId();
		IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(
				"org.eclipse.ui.intro"); //$NON-NLS-1$
		String targetIntroId = null;
		for (int i = 0; i < elements.length; i++) {
			IConfigurationElement element = elements[i];
			if (element.getName().equals("introProductBinding")) { //$NON-NLS-1$
				String pid = element.getAttribute("productId"); //$NON-NLS-1$
				String iid = element.getAttribute("introId"); //$NON-NLS-1$
				if (productId.equals(pid)) {
					targetIntroId = iid;
					break;
				}
			}
		}
		if (targetIntroId == null)
			return set;
		elements = Platform.getExtensionRegistry().getConfigurationElementsFor("org.eclipse.ui.intro.config"); //$NON-NLS-1$
		IConfigurationElement config = null;
		for (int i = 0; i < elements.length; i++) {
			IConfigurationElement element = elements[i];
			if (element.getName().equals("config")) { //$NON-NLS-1$
				String iid = element.getAttribute("introId"); //$NON-NLS-1$
				if (targetIntroId.equals(iid)) {
					config = element;
					break;
				}
			}
		}
		if (config == null)
			return set;
		String configId = config.getAttribute("id"); //$NON-NLS-1$
		ExtensionPointManager extensionPointManager = IntroPlugin.getDefault().getExtensionPointManager();
		model = extensionPointManager.getModel(configId);
		if (model != null && model.hasValidConfig())
			loadFromModel(model, set, locale);
		return set;
	}

	private void loadFromModel(IntroModelRoot model, HashSet set, String locale) {
		IntroPage[] pages = model.getPages();
		for (int i = 0; i < pages.length; i++) {
			IntroPage page = pages[i];
			Bundle bundle = page.getBundle();
			String bundleId = bundle.getSymbolicName();
			String content = page.getRawContent();
			String pageId = page.getId();
			String href;
			if (content != null)
				href = resolveVariables(bundleId, content, locale);
			else
				href = pageId;
			set.add("/" + bundleId + "/" + href + "?id=" + pageId); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.help.search.LuceneSearchParticipant#addDocument(java.lang.String,
	 *      java.lang.String, java.net.URL, java.lang.String, java.lang.String,
	 *      org.apache.lucene.document.Document)
	 */

	public IStatus addDocument(IHelpSearchIndex index, String pluginId, String name, URL url, String id,
			ISearchDocument doc) {
		if (model == null)
			return Status.CANCEL_STATUS;
		IntroPage page = getPage(id);
		if (page == null)
			return Status.CANCEL_STATUS;
		return addPage(index, pluginId, name, url, page, doc);
	}

	private IntroPage getPage(String id) {
		IntroPage[] pages = model.getPages();
		for (int i = 0; i < pages.length; i++) {
			if (pages[i].getId().equals(id))
				return pages[i];
		}
		return null;
	}

	private IStatus addPage(IHelpSearchIndex index, String pluginId, String name, URL url, IntroPage page,
			ISearchDocument doc) {
		AbstractIntroElement[] children = page.getChildren();
		if (children.length > 0) {
			StringBuffer buf = new StringBuffer();
			TitleAndSummary titleSummary = new TitleAndSummary();
			addChildren(children, buf, doc, titleSummary);
			String contents = buf.toString();
            if (titleSummary.title != null) {
			     addTitle(titleSummary.title, doc);
            }
            if (titleSummary.summary != null) {
            	doc.setSummary(titleSummary.summary); 
            }
            doc.addContents(contents);
			return Status.OK_STATUS;
		}
		// delegate to the help system
		return index.addSearchableDocument(pluginId, name, url, page.getId(), doc);
	}

	private void addChildren(AbstractIntroElement[] children, StringBuffer buf, ISearchDocument doc, TitleAndSummary titleSummary) {
		for (int i = 0; i < children.length; i++) {
			AbstractIntroElement child = children[i];
			if (child instanceof IntroLink) {
				String text = ((IntroLink)child).getLabel();
				appendNewText(buf, text);
			} else if (child instanceof IntroGroup) {
				String text = ((IntroGroup)child).getLabel();
				appendNewText(buf, text);
			} else if (child instanceof IntroText) {
				IntroText childIntroText = (IntroText) child;
				appendNewText(buf, childIntroText.getText());
				String childId = childIntroText.getId();
				String title = null;
				if ("page-title".equals(childId)) { //$NON-NLS-1$
					title = childIntroText.getText();
				} else if (child instanceof IntroPageTitle) {
					title = ((IntroPageTitle) child).getTitle();
				}
				if (title != null) {
					titleSummary.title = title;
				}				
				if  ("page-description".equals(childId)) { //$NON-NLS-1$
					titleSummary.summary = childIntroText.getText(); 
				}
			} 
            if (child instanceof AbstractIntroContainer) {
				AbstractIntroContainer container = (AbstractIntroContainer) child;
				if (!"navigation-links".equals(container.getId())) { //$NON-NLS-1$
				    AbstractIntroElement[] cc = container.getChildren();
				    addChildren(cc, buf, doc, titleSummary);
                }
			}
		}
	}

	private void appendNewText(StringBuffer buf, String text) {
		if (text == null) return;
		if (buf.length() > 0)
			buf.append(" "); //$NON-NLS-1$
		buf.append(text);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.help.search.LuceneSearchParticipant#clear()
	 */
	public void clear() {
		model = null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.help.search.LuceneSearchParticipant#open(java.lang.String)
	 */
	public boolean open(String id) {
		IIntroManager introManager = PlatformUI.getWorkbench().getIntroManager();
		IIntroPart intro = introManager
				.showIntro(PlatformUI.getWorkbench().getActiveWorkbenchWindow(), false);
		if (intro == null)
			return false;
		IIntroURL url = IntroURLFactory.createIntroURL("http://org.eclipse.ui.intro/showPage?id=" + id); //$NON-NLS-1$
		return url.execute();
	}

}

Back to the top