1 11 package org.eclipse.ui.internal.intro.impl.model; 12 13 import java.io.StringReader ; 14 import java.net.URL ; 15 import java.util.HashSet ; 16 import java.util.Set ; 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 41 42 public class IntroSearchParticipant extends LuceneSearchParticipant { 43 44 private IntroModelRoot model; 45 46 private class TitleAndSummary { 47 String title; 48 String summary; 49 } 50 51 56 public Set getContributingPlugins() { 57 IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor( 58 "org.eclipse.ui.intro.config"); HashSet set = new HashSet (); 60 for (int i = 0; i < elements.length; i++) { 61 IConfigurationElement element = elements[i]; 62 if (!element.getName().equals("config")) continue; 64 set.add(element.getContributor().getName()); 65 } 66 elements = Platform.getExtensionRegistry().getConfigurationElementsFor( 67 "org.eclipse.ui.intro.configExtension"); for (int i = 0; i < elements.length; i++) { 69 IConfigurationElement element = elements[i]; 70 if (!element.getName().equals("configExtension")) continue; 72 set.add(element.getContributor().getName()); 73 } 74 return set; 75 } 76 77 82 public Set getAllDocuments(String locale) { 83 HashSet set = new HashSet (); 84 IProduct product = Platform.getProduct(); 85 if (product == null) { 86 return set; 87 } 88 String productId = product.getId(); 89 IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor( 90 "org.eclipse.ui.intro"); String targetIntroId = null; 92 for (int i = 0; i < elements.length; i++) { 93 IConfigurationElement element = elements[i]; 94 if (element.getName().equals("introProductBinding")) { String pid = element.getAttribute("productId"); String iid = element.getAttribute("introId"); 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"); IConfigurationElement config = null; 107 for (int i = 0; i < elements.length; i++) { 108 IConfigurationElement element = elements[i]; 109 if (element.getName().equals("config")) { String iid = element.getAttribute("introId"); if (targetIntroId.equals(iid)) { 112 config = element; 113 break; 114 } 115 } 116 } 117 if (config == null) 118 return set; 119 String configId = config.getAttribute("id"); 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 set, String 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 bundleId = bundle.getSymbolicName(); 133 String content = page.getRawContent(); 134 String pageId = page.getId(); 135 String href; 136 if (content != null) 137 href = resolveVariables(bundleId, content, locale); 138 else 139 href = pageId; 140 set.add("/" + bundleId + "/" + href + "?id=" + pageId); } 142 } 143 144 151 public IStatus addDocument(ISearchIndex index, String pluginId, String name, URL url, String 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 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 pluginId, String name, URL url, IntroPage page, 171 Document doc) { 172 AbstractIntroElement[] children = page.getChildren(); 173 if (children.length > 0) { 174 StringBuffer buf = new StringBuffer (); 175 TitleAndSummary titleSummary = new TitleAndSummary(); 176 addChildren(children, buf, doc, titleSummary); 177 String 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)); } 184 doc.add(new Field("contents", new StringReader (contents))); doc.add(new Field("exact_contents", new StringReader (contents))); return Status.OK_STATUS; 187 } 188 return index.addDocument(pluginId, name, url, page.getId(), doc); 190 } 191 192 private void addChildren(AbstractIntroElement[] children, StringBuffer 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 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 childId = childIntroText.getId(); 202 String title = null; 203 if ("page-title".equals(childId)) { 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)) { titleSummary.summary = childIntroText.getText(); 213 } 214 } 215 if (child instanceof AbstractIntroContainer) { 216 AbstractIntroContainer container = (AbstractIntroContainer) child; 217 if (!"navigation-links".equals(container.getId())) { AbstractIntroElement[] cc = container.getChildren(); 219 addChildren(cc, buf, doc, titleSummary); 220 } 221 } 222 } 223 } 224 225 private void appendNewText(StringBuffer buf, String text) { 226 if (text == null) return; 227 if (buf.length() > 0) 228 buf.append(" "); buf.append(text); 230 } 231 232 237 public void clear() { 238 model = null; 239 } 240 241 246 public boolean open(String 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); return url.execute(); 254 } 255 } 256 | Popular Tags |