]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/winterwell.markdown/src/winterwell/markdown/views/MarkdownPreview.java
Tycho compilation changes for SVN version also.
[simantics/platform.git] / bundles / winterwell.markdown / src / winterwell / markdown / views / MarkdownPreview.java
1 package winterwell.markdown.views;
2
3
4 import java.io.File;
5 import java.net.URI;
6
7 import org.eclipse.core.runtime.IPath;
8 import org.eclipse.swt.SWT;
9 import org.eclipse.swt.browser.Browser;
10 import org.eclipse.swt.widgets.Composite;
11 import org.eclipse.ui.IEditorPart;
12 import org.eclipse.ui.IPathEditorInput;
13 import org.eclipse.ui.part.ViewPart;
14
15 import winterwell.markdown.editors.ActionBarContributor;
16 import winterwell.markdown.editors.MarkdownEditor;
17 import winterwell.markdown.pagemodel.MarkdownPage;
18
19
20
21
22 public class MarkdownPreview extends ViewPart {
23         
24         public static MarkdownPreview preview = null;
25         
26         private Browser viewer = null;
27
28         /**
29          * The constructor.
30          */
31         public MarkdownPreview() {
32                 preview = this;
33         }
34
35         /**
36          * This is a callback that will allow us
37          * to create the viewer and initialize it.
38          */
39         @Override
40         public void createPartControl(Composite parent) {
41                 viewer = new Browser(parent, SWT.MULTI); // | SWT.H_SCROLL | SWT.V_SCROLL
42         }
43
44
45
46
47         /**
48          * Passing the focus request to the viewer's control.
49          */
50         @Override
51         public void setFocus() {
52                 if (viewer==null) return;
53                 viewer.setFocus();
54                 update();
55         }
56
57         public void update() {
58                 if (viewer==null) return;
59                 try {
60                         IEditorPart editor = ActionBarContributor.getActiveEditor();
61                         if (!(editor instanceof MarkdownEditor)) {
62                                 viewer.setText("");
63                                 return;
64                         }
65                         MarkdownEditor ed = (MarkdownEditor) editor;
66                         MarkdownPage page = ed.getMarkdownPage();
67                         String html = page.html();
68                         html = addBaseURL(editor, html);
69                         if (page != null) viewer.setText(html);
70                         else viewer.setText("");
71                 } catch (Exception ex) {
72                         // Smother
73                         System.out.println(ex);
74                         
75                         if (viewer != null && !viewer.isDisposed())
76                                 viewer.setText(ex.getMessage());
77                 }
78         }
79
80         /**
81          * Adjust the URL base to be the file's directory.
82          * @param editor
83          * @param html
84          * @return
85          */
86         private String addBaseURL(IEditorPart editor, String html) {
87         try {
88                 IPathEditorInput input = (IPathEditorInput) editor.getEditorInput();
89                 IPath path = input.getPath();
90                 path = path.removeLastSegments(1);
91                 File f = path.toFile();
92                 URI fileURI = f.toURI();
93                 String html2 = "<html><head><base href='"+fileURI+"' /></head><body>\r\n"+html
94                 +"\r\n</body></html>";
95                 return html2;
96         } catch (Exception ex) {
97                 return html;
98         }
99         }
100 }