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