]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.help.core/src/org/simantics/help/core/SimanticsTocProvider.java
Search-field in Help->Contents Fix.
[simantics/platform.git] / bundles / org.simantics.help.core / src / org / simantics / help / core / SimanticsTocProvider.java
1 package org.simantics.help.core;\r
2 \r
3 import java.nio.file.Path;\r
4 import java.nio.file.Paths;\r
5 import java.util.ArrayList;\r
6 import java.util.HashMap;\r
7 import java.util.List;\r
8 import java.util.Map;\r
9 \r
10 import javax.xml.parsers.DocumentBuilder;\r
11 import javax.xml.parsers.DocumentBuilderFactory;\r
12 import javax.xml.parsers.ParserConfigurationException;\r
13 \r
14 import org.eclipse.help.AbstractTocProvider;\r
15 import org.eclipse.help.ITocContribution;\r
16 import org.eclipse.help.internal.HelpPlugin;\r
17 import org.eclipse.help.internal.Topic;\r
18 import org.eclipse.help.internal.entityresolver.LocalEntityResolver;\r
19 import org.eclipse.help.internal.toc.Toc;\r
20 import org.eclipse.help.internal.toc.TocContribution;\r
21 import org.simantics.Simantics;\r
22 import org.simantics.db.ReadGraph;\r
23 import org.simantics.db.common.request.ReadRequest;\r
24 import org.simantics.db.exception.DatabaseException;\r
25 import org.w3c.dom.Document;\r
26 import org.w3c.dom.Element;\r
27 \r
28 @SuppressWarnings("restriction")\r
29 public class SimanticsTocProvider extends AbstractTocProvider {\r
30 \r
31     private static DocumentBuilder builder;\r
32     private static Document document;\r
33     \r
34     private static Map<String, Path> widgetReferences; \r
35     private static Map<String, List<Path>> tutorials;\r
36     \r
37     @Override\r
38     public ITocContribution[] getTocContributions(String locale) {\r
39         List<ITocContribution> contributions = new ArrayList<>();\r
40         \r
41         if (widgetReferences == null || tutorials == null) {\r
42             try {\r
43                 Simantics.getSession().syncRequest(new ReadRequest() {\r
44                     @Override\r
45                     public void run(ReadGraph graph) throws DatabaseException {\r
46                         widgetReferences = HelpUtils.collectWidgetReferencesFromSharedLibraries(graph);\r
47                         tutorials = HelpUtils.collectHelpsFromSharedLibraries(graph);\r
48                         \r
49                     }\r
50                 });\r
51             } catch (DatabaseException e) {\r
52                 e.printStackTrace();\r
53             }\r
54         }\r
55 \r
56         Map<String, Toc> tocs = new HashMap<>();\r
57         \r
58         if (widgetReferences != null) {\r
59             for (Map.Entry<String, Path> widgetReference : widgetReferences.entrySet()) {\r
60                 String libName = widgetReference.getKey();\r
61                 Path htmlFile = widgetReference.getValue();\r
62                 Toc toc = tocs.get(libName);\r
63                 if (toc == null) {\r
64                     Element element = getDocument().createElement("toc");\r
65                     toc = new Toc(element); \r
66                     toc.setLabel(libName);\r
67 //                    toc.setTopic(libName);\r
68 //                    toc.setHref(libName);\r
69                     tocs.put(libName, toc);\r
70                 }\r
71                 \r
72                 Topic topic = new Topic();\r
73                 //topic.setLabel(htmlFile.getFileName().toString());\r
74                 topic.setLabel("Widget Reference");\r
75 //                String href = htmlFile.toUri().toString();\r
76                 String href = htmlFile.toString().split(Activator.PLUGIN_ID)[1].substring(1);\r
77                 href = "PLUGINS_ROOT/platform:/meta/" + Activator.PLUGIN_ID + "/" + href;\r
78                 topic.setHref(href.replace("\\", "/").replace(" ", "%20"));\r
79                 \r
80                 toc.appendChild(topic);\r
81             }\r
82         }\r
83 \r
84         if (tutorials != null) {\r
85             for (Map.Entry<String, List<Path>> tutorial : tutorials.entrySet()) {\r
86                 String libName = tutorial.getKey();\r
87                 Toc toc = tocs.get(libName);\r
88                 if (toc == null) {\r
89                     Element element = getDocument().createElement("toc");\r
90                     toc = new Toc(element); \r
91                     toc.setLabel(libName);\r
92                     toc.setTopic(libName);\r
93                     toc.setHref(libName);\r
94                     tocs.put(libName, toc);\r
95                 }\r
96                 \r
97                 Map<String, Topic> topics = new HashMap<>();\r
98                 \r
99                 for (Path htmlFile : tutorial.getValue()) {\r
100                     Path path = Paths.get(htmlFile.toString().split(libName)[1]);\r
101                     Topic topic = getOrCreateTopic(topics, toc, path);\r
102                     String href = htmlFile.toString().split(Activator.PLUGIN_ID)[1].substring(1);\r
103                     href = "PLUGINS_ROOT/platform:/meta/" + Activator.PLUGIN_ID + "/" + href;\r
104 //                    String href = htmlFile.toUri().toString();\r
105                     topic.setHref(href.replace("\\", "/").replace(" ", "%20"));\r
106                 }\r
107             }\r
108         }\r
109         \r
110         for (Toc toc : tocs.values()) {\r
111             TocContribution contribution = new TocContribution();\r
112             contribution.setLocale(locale);\r
113             contribution.setId(toc.getLabel());\r
114             contribution.setCategoryId("category_" + toc.getLabel());\r
115             contribution.setPrimary(true);\r
116             contribution.setContributorId(Activator.PLUGIN_ID);\r
117             contribution.setExtraDocuments(new String[0]);\r
118             contribution.setToc(toc);\r
119             contributions.add(contribution);\r
120         }\r
121 \r
122         \r
123         return (ITocContribution[])contributions.toArray(new ITocContribution[contributions.size()]);\r
124     }\r
125     \r
126     private static Topic getOrCreateTopic(Map<String, Topic> topics, Toc toc, Path topicPath) {\r
127         String topicName = topicPath.getFileName().toString();\r
128         Topic topic = topics.get(topicName);\r
129         if (topic == null) {\r
130             topic = new Topic();\r
131             topic.setLabel(topicName);\r
132             Path parentPath = topicPath.getParent();\r
133             if (parentPath != null && parentPath.getFileName() != null) {\r
134                 Topic parentTopic = getOrCreateTopic(topics, toc, parentPath);\r
135                 parentTopic.appendChild(topic);\r
136             } else {\r
137                 toc.appendChild(topic);\r
138             }\r
139             topics.put(topicName, topic);\r
140         }\r
141         return topic;\r
142     }\r
143     \r
144     private static Document getDocument() {\r
145         if (document == null) {\r
146             if (builder == null) {\r
147                 try {\r
148                     builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();\r
149                     builder.setEntityResolver(new LocalEntityResolver());\r
150                 }\r
151                 catch (ParserConfigurationException e) {\r
152                     String msg = "Error creating document builder"; //$NON-NLS-1$\r
153                     HelpPlugin.logError(msg, e);\r
154                 }\r
155             }\r
156             document = builder.newDocument();\r
157         }\r
158         return document;\r
159     }\r
160     \r
161     public static void clearTocCache() {\r
162         widgetReferences.clear();\r
163         widgetReferences = null;\r
164         tutorials.clear();\r
165         tutorials = null;\r
166         HelpPlugin.getTocManager().clearCache();\r
167     }\r
168 }\r