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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.browsing.ui.swt.widgets;
\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
33 public class WikiBrowser extends WidgetImpl {
\r
35 private ReadFactory<?, String> textFactory;
\r
36 private ReadFactory<?, Variable> variableFactory;
\r
38 private String text;
\r
39 private Variable variable;
\r
41 private final Display display;
\r
42 private final Browser browser;
\r
44 private Tuple lastAppliedParametrization;
\r
46 public WikiBrowser(Composite parent, WidgetSupport support, int style) {
\r
48 display = parent.getDisplay();
\r
49 browser = new Browser(parent, style);
\r
50 support.register(this);
\r
53 public void setTextFactory(ReadFactory<?, String> textFactory) {
\r
54 this.textFactory = textFactory;
\r
57 public void setVariableFactory(ReadFactory<?, Variable> variableFactory) {
\r
58 this.variableFactory = variableFactory;
\r
61 public Browser getWidget() {
\r
66 public Control getControl() {
\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
79 display.asyncExec(new Runnable() {
\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
89 Tuple checkParam = new Tuple(text, variable);
\r
90 if (checkParam.equals(lastAppliedParametrization))
\r
92 lastAppliedParametrization = checkParam;
\r
95 String markup = Simantics.getSession().syncRequest(new Read<String>() {
\r
97 public String perform(ReadGraph graph) throws DatabaseException {
\r
98 return SimanticsDialect.INSTANCE.apply(graph, variable, text);
\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
109 browser.setText(htmlContent);
\r
111 } catch (DatabaseException e) {
\r
112 ErrorLogger.defaultLogError(e);
\r
119 public void setInput(ISessionContext context, Object input) {
\r
121 if(textFactory != null) {
\r
122 textFactory.listen(context, input, new Listener<String>() {
\r
125 public void exception(Throwable t) {
\r
126 ErrorLogger.defaultLogError(t);
\r
130 public void execute(final String text) {
\r
131 WikiBrowser.this.text = text;
\r
136 public boolean isDisposed() {
\r
137 return browser.isDisposed();
\r
143 if(variableFactory != null) {
\r
144 variableFactory.listen(context, input, new Listener<Variable>() {
\r
147 public void exception(Throwable t) {
\r
148 ErrorLogger.defaultLogError(t);
\r
152 public void execute(final Variable variable) {
\r
153 WikiBrowser.this.variable = variable;
\r
158 public boolean isDisposed() {
\r
159 return browser.isDisposed();
\r
168 public boolean isDisposed() {
\r
169 return browser.isDisposed();
\r