]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/WikiBrowser.java b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/WikiBrowser.java
new file mode 100644 (file)
index 0000000..a15a69a
--- /dev/null
@@ -0,0 +1,172 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2012 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.browsing.ui.swt.widgets;\r
+\r
+import org.eclipse.mylyn.wikitext.core.parser.MarkupParser;\r
+import org.eclipse.swt.browser.Browser;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Control;\r
+import org.eclipse.swt.widgets.Display;\r
+import org.simantics.Simantics;\r
+import org.simantics.browsing.ui.common.ErrorLogger;\r
+import org.simantics.browsing.ui.swt.widgets.impl.ReadFactory;\r
+import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.db.procedure.Listener;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.utils.datastructures.map.Tuple;\r
+import org.simantics.wiki.ui.SimanticsDialect;\r
+import org.simantics.wiki.ui.language.MediaWikiLanguage;\r
+\r
+public class WikiBrowser extends WidgetImpl {\r
+\r
+    private ReadFactory<?, String>   textFactory;\r
+    private ReadFactory<?, Variable> variableFactory;\r
+\r
+    private String                   text;\r
+    private Variable                 variable;\r
+\r
+    private final Display            display;\r
+    private final Browser   browser;\r
+\r
+    private Tuple                    lastAppliedParametrization;\r
+\r
+    public WikiBrowser(Composite parent, WidgetSupport support, int style) {\r
+       super(support);\r
+        display = parent.getDisplay();\r
+        browser = new Browser(parent, style);\r
+        support.register(this);\r
+    }\r
+\r
+    public void setTextFactory(ReadFactory<?, String> textFactory) {\r
+        this.textFactory = textFactory;\r
+    }\r
+\r
+    public void setVariableFactory(ReadFactory<?, Variable> variableFactory) {\r
+        this.variableFactory = variableFactory;\r
+    }\r
+\r
+    public Browser getWidget() {\r
+        return browser;\r
+    }\r
+\r
+    @Override\r
+    public Control getControl() {\r
+        return browser;\r
+    }\r
+\r
+    private void update() {\r
+        // Prevent synchronization problems since multiple threads and context\r
+        // switches are involved in this process\r
+        String text = this.text;\r
+        Variable variable = this.variable;\r
+        System.out.println("update(" + text + ", " + variable + ")");\r
+        if (text == null || variable == null || browser.isDisposed())\r
+            return;\r
+\r
+        display.asyncExec(new Runnable() {\r
+\r
+            @Override\r
+            public void run() {\r
+                final String text = WikiBrowser.this.text;\r
+                final Variable variable = WikiBrowser.this.variable;\r
+                if (text == null || variable == null || browser.isDisposed()) {\r
+                    return;\r
+                }\r
+\r
+                Tuple checkParam = new Tuple(text, variable);\r
+                if (checkParam.equals(lastAppliedParametrization))\r
+                    return;\r
+                lastAppliedParametrization = checkParam;\r
+\r
+                try {\r
+                    String markup = Simantics.getSession().syncRequest(new Read<String>() {\r
+                        @Override\r
+                        public String perform(ReadGraph graph) throws DatabaseException {\r
+                            return SimanticsDialect.INSTANCE.apply(graph, variable, text);\r
+                        }\r
+                    });\r
+\r
+                    MarkupParser markupParser = new MarkupParser();\r
+                    MediaWikiLanguage language = new MediaWikiLanguage();\r
+                    markupParser.setMarkupLanguage(language);\r
+                    final String htmlContent = markupParser.parseToHtml(markup);\r
+                    if (htmlContent == null)\r
+                        return;\r
+\r
+                    browser.setText(htmlContent);\r
+\r
+                } catch (DatabaseException e) {\r
+                    ErrorLogger.defaultLogError(e);\r
+                }\r
+            }\r
+        });\r
+    }\r
+\r
+    @Override\r
+    public void setInput(ISessionContext context, Object input) {\r
+\r
+        if(textFactory != null) {\r
+            textFactory.listen(context, input, new Listener<String>() {\r
+\r
+                @Override\r
+                public void exception(Throwable t) {\r
+                    ErrorLogger.defaultLogError(t);\r
+                }\r
+\r
+                @Override\r
+                public void execute(final String text) {\r
+                    WikiBrowser.this.text = text;\r
+                    update();\r
+                }\r
+\r
+                @Override\r
+                public boolean isDisposed() {\r
+                    return browser.isDisposed();\r
+                }\r
+\r
+            });\r
+        }\r
+\r
+        if(variableFactory != null) {\r
+            variableFactory.listen(context, input, new Listener<Variable>() {\r
+\r
+                @Override\r
+                public void exception(Throwable t) {\r
+                    ErrorLogger.defaultLogError(t);\r
+                }\r
+\r
+                @Override\r
+                public void execute(final Variable variable) {\r
+                    WikiBrowser.this.variable = variable;\r
+                    update();\r
+                }\r
+\r
+                @Override\r
+                public boolean isDisposed() {\r
+                    return browser.isDisposed();\r
+                }\r
+\r
+            });\r
+\r
+        }\r
+\r
+    }\r
+\r
+    public boolean isDisposed() {\r
+        return browser.isDisposed();\r
+    }\r
+\r
+}\r