]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/WikiBrowser.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / widgets / WikiBrowser.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.browsing.ui.swt.widgets;\r
13 \r
14 import org.eclipse.mylyn.wikitext.core.parser.MarkupParser;\r
15 import org.eclipse.swt.browser.Browser;\r
16 import org.eclipse.swt.widgets.Composite;\r
17 import org.eclipse.swt.widgets.Control;\r
18 import org.eclipse.swt.widgets.Display;\r
19 import org.simantics.Simantics;\r
20 import org.simantics.browsing.ui.common.ErrorLogger;\r
21 import org.simantics.browsing.ui.swt.widgets.impl.ReadFactory;\r
22 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
23 import org.simantics.db.ReadGraph;\r
24 import org.simantics.db.exception.DatabaseException;\r
25 import org.simantics.db.layer0.variable.Variable;\r
26 import org.simantics.db.management.ISessionContext;\r
27 import org.simantics.db.procedure.Listener;\r
28 import org.simantics.db.request.Read;\r
29 import org.simantics.utils.datastructures.map.Tuple;\r
30 import org.simantics.wiki.ui.SimanticsDialect;\r
31 import org.simantics.wiki.ui.language.MediaWikiLanguage;\r
32 \r
33 public class WikiBrowser extends WidgetImpl {\r
34 \r
35     private ReadFactory<?, String>   textFactory;\r
36     private ReadFactory<?, Variable> variableFactory;\r
37 \r
38     private String                   text;\r
39     private Variable                 variable;\r
40 \r
41     private final Display            display;\r
42     private final Browser   browser;\r
43 \r
44     private Tuple                    lastAppliedParametrization;\r
45 \r
46     public WikiBrowser(Composite parent, WidgetSupport support, int style) {\r
47         super(support);\r
48         display = parent.getDisplay();\r
49         browser = new Browser(parent, style);\r
50         support.register(this);\r
51     }\r
52 \r
53     public void setTextFactory(ReadFactory<?, String> textFactory) {\r
54         this.textFactory = textFactory;\r
55     }\r
56 \r
57     public void setVariableFactory(ReadFactory<?, Variable> variableFactory) {\r
58         this.variableFactory = variableFactory;\r
59     }\r
60 \r
61     public Browser getWidget() {\r
62         return browser;\r
63     }\r
64 \r
65     @Override\r
66     public Control getControl() {\r
67         return browser;\r
68     }\r
69 \r
70     private void update() {\r
71         // Prevent synchronization problems since multiple threads and context\r
72         // switches are involved in this process\r
73         String text = this.text;\r
74         Variable variable = this.variable;\r
75         System.out.println("update(" + text + ", " + variable + ")");\r
76         if (text == null || variable == null || browser.isDisposed())\r
77             return;\r
78 \r
79         display.asyncExec(new Runnable() {\r
80 \r
81             @Override\r
82             public void run() {\r
83                 final String text = WikiBrowser.this.text;\r
84                 final Variable variable = WikiBrowser.this.variable;\r
85                 if (text == null || variable == null || browser.isDisposed()) {\r
86                     return;\r
87                 }\r
88 \r
89                 Tuple checkParam = new Tuple(text, variable);\r
90                 if (checkParam.equals(lastAppliedParametrization))\r
91                     return;\r
92                 lastAppliedParametrization = checkParam;\r
93 \r
94                 try {\r
95                     String markup = Simantics.getSession().syncRequest(new Read<String>() {\r
96                         @Override\r
97                         public String perform(ReadGraph graph) throws DatabaseException {\r
98                             return SimanticsDialect.INSTANCE.apply(graph, variable, text);\r
99                         }\r
100                     });\r
101 \r
102                     MarkupParser markupParser = new MarkupParser();\r
103                     MediaWikiLanguage language = new MediaWikiLanguage();\r
104                     markupParser.setMarkupLanguage(language);\r
105                     final String htmlContent = markupParser.parseToHtml(markup);\r
106                     if (htmlContent == null)\r
107                         return;\r
108 \r
109                     browser.setText(htmlContent);\r
110 \r
111                 } catch (DatabaseException e) {\r
112                     ErrorLogger.defaultLogError(e);\r
113                 }\r
114             }\r
115         });\r
116     }\r
117 \r
118     @Override\r
119     public void setInput(ISessionContext context, Object input) {\r
120 \r
121         if(textFactory != null) {\r
122             textFactory.listen(context, input, new Listener<String>() {\r
123 \r
124                 @Override\r
125                 public void exception(Throwable t) {\r
126                     ErrorLogger.defaultLogError(t);\r
127                 }\r
128 \r
129                 @Override\r
130                 public void execute(final String text) {\r
131                     WikiBrowser.this.text = text;\r
132                     update();\r
133                 }\r
134 \r
135                 @Override\r
136                 public boolean isDisposed() {\r
137                     return browser.isDisposed();\r
138                 }\r
139 \r
140             });\r
141         }\r
142 \r
143         if(variableFactory != null) {\r
144             variableFactory.listen(context, input, new Listener<Variable>() {\r
145 \r
146                 @Override\r
147                 public void exception(Throwable t) {\r
148                     ErrorLogger.defaultLogError(t);\r
149                 }\r
150 \r
151                 @Override\r
152                 public void execute(final Variable variable) {\r
153                     WikiBrowser.this.variable = variable;\r
154                     update();\r
155                 }\r
156 \r
157                 @Override\r
158                 public boolean isDisposed() {\r
159                     return browser.isDisposed();\r
160                 }\r
161 \r
162             });\r
163 \r
164         }\r
165 \r
166     }\r
167 \r
168     public boolean isDisposed() {\r
169         return browser.isDisposed();\r
170     }\r
171 \r
172 }\r