1 package org.simantics.views.swt.client.impl;
3 import java.io.IOException;
5 import org.eclipse.jface.layout.GridDataFactory;
6 import org.eclipse.jface.layout.GridLayoutFactory;
7 import org.eclipse.mylyn.wikitext.core.parser.MarkupParser;
8 import org.eclipse.swt.browser.Browser;
9 import org.eclipse.swt.widgets.Composite;
10 import org.simantics.Simantics;
11 import org.simantics.browsing.ui.common.ErrorLogger;
12 import org.simantics.db.ReadGraph;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.db.layer0.variable.Variable;
15 import org.simantics.db.request.Read;
16 import org.simantics.scl.runtime.function.Function1;
17 import org.simantics.utils.FileUtils;
18 import org.simantics.utils.datastructures.map.Tuple;
19 import org.simantics.utils.ui.BundleUtils;
20 import org.simantics.views.swt.client.base.SingleSWTViewNode;
21 import org.simantics.wiki.ui.SimanticsDialect;
22 import org.simantics.wiki.ui.language.MediaWikiLanguage;
24 public class SWTWikiBrowser extends SingleSWTViewNode<Browser> {
26 private static final long serialVersionUID = 5826022972081217837L;
28 public String document;
29 public Variable variable;
30 public Function1<String, Boolean> navigate;
31 public Variable edited;
33 private Tuple lastAppliedParametrization;
38 lastAppliedParametrization = null;
42 public void createControls(Composite parent) {
44 GridDataFactory.fillDefaults().grab(true, true).applyTo(parent);
45 GridLayoutFactory.fillDefaults().applyTo(parent);
47 control = new Browser(parent, style);
48 GridDataFactory.fillDefaults().grab(true, true).applyTo(control);
54 public void synchronizeVariable(Variable variable) {
58 public void synchronizeEdited(Variable edited) {
59 propertyCallback.apply("edited", edited);
62 public void synchronizeDocument(final String document) {
64 if(document != null) {
66 final Variable variable = SWTWikiBrowser.this.variable;
67 if (variable == null) {
71 Tuple checkParam = new Tuple(document, variable);
72 if (checkParam.equals(lastAppliedParametrization))
75 lastAppliedParametrization = checkParam;
79 String markup = Simantics.getSession().syncRequest(new Read<String>() {
81 public String perform(ReadGraph graph) throws DatabaseException {
82 return SimanticsDialect.INSTANCE.apply(graph, variable, document);
86 MarkupParser markupParser = new MarkupParser();
87 MediaWikiLanguage language = new MediaWikiLanguage();
88 markupParser.setMarkupLanguage(language);
89 String htmlContent = markupParser.parseToHtml(markup);
90 if (htmlContent == null)
93 final Browser control = this.control;
96 control.setText(htmlContent);
98 } catch (DatabaseException e) {
99 ErrorLogger.defaultLogError(e);