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