]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.views.swt.client/src/org/simantics/views/swt/client/impl/SWTWikiBrowser.java
(refs #7358) Initial 4.7 update commit
[simantics/platform.git] / bundles / org.simantics.views.swt.client / src / org / simantics / views / swt / client / impl / SWTWikiBrowser.java
1 package org.simantics.views.swt.client.impl;
2
3 import org.eclipse.jface.layout.GridDataFactory;
4 import org.eclipse.jface.layout.GridLayoutFactory;
5 import org.eclipse.mylyn.wikitext.parser.MarkupParser;
6 import org.eclipse.swt.browser.Browser;
7 import org.eclipse.swt.widgets.Composite;
8 import org.simantics.Simantics;
9 import org.simantics.browsing.ui.common.ErrorLogger;
10 import org.simantics.db.ReadGraph;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.variable.Variable;
13 import org.simantics.db.request.Read;
14 import org.simantics.scl.runtime.function.Function1;
15 import org.simantics.utils.datastructures.map.Tuple;
16 import org.simantics.views.swt.client.base.SingleSWTViewNode;
17 import org.simantics.wiki.ui.SimanticsDialect;
18 import org.simantics.wiki.ui.language.MediaWikiLanguage;
19
20 public class SWTWikiBrowser extends SingleSWTViewNode<Browser> {
21
22         private static final long serialVersionUID = 5826022972081217837L;
23         
24         public String                   document;
25         public Variable                 variable;
26         public Function1<String, Boolean> navigate;
27         public Variable                                 edited;
28
29         private Tuple                    lastAppliedParametrization;
30
31         @Override
32         public void reset() {
33                 super.reset();
34                 lastAppliedParametrization = null;
35         }
36         
37         @Override
38         public void createControls(Composite parent) {
39
40                 GridDataFactory.fillDefaults().grab(true, true).applyTo(parent);
41                 GridLayoutFactory.fillDefaults().applyTo(parent);
42
43                 control = new Browser(parent, style);
44                 GridDataFactory.fillDefaults().grab(true, true).applyTo(control);
45
46                 setProperties();
47
48         }
49
50         public void synchronizeVariable(Variable variable) {
51                 
52         }
53         
54         public void synchronizeEdited(Variable edited) {
55                 propertyCallback.apply("edited", edited);
56         }
57
58         public void synchronizeDocument(final String document) {
59                 
60                 if(document != null) {
61                         
62                         final Variable variable = SWTWikiBrowser.this.variable;
63                         if (variable == null) {
64                                 return;
65                         }
66
67                         Tuple checkParam = new Tuple(document, variable);
68                         if (checkParam.equals(lastAppliedParametrization))
69                                 return;
70                         
71                         lastAppliedParametrization = checkParam;
72
73                         try {
74                                 
75                                 String markup = Simantics.getSession().syncRequest(new Read<String>() {
76                                         @Override
77                                         public String perform(ReadGraph graph) throws DatabaseException {
78                                                 return SimanticsDialect.INSTANCE.apply(graph, variable, document);
79                                         }
80                                 });
81
82                                 MarkupParser markupParser = new MarkupParser();
83                                 MediaWikiLanguage language = new MediaWikiLanguage();
84                                 markupParser.setMarkupLanguage(language);
85                                 String htmlContent = markupParser.parseToHtml(markup);
86                                 if (htmlContent == null)
87                                         return;
88                         
89                                 final Browser control = this.control;
90                                 if (control == null)
91                                         return;
92                                 control.setText(htmlContent);
93                                 
94                         } catch (DatabaseException e) {
95                                 ErrorLogger.defaultLogError(e);
96                         }                       
97                         
98                 }
99         }
100
101 }