]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.views.text/src/org/simantics/views/text/internal/SWTMarkupSourceViewer.java
(refs #7358) Initial 4.7 update commit
[simantics/platform.git] / bundles / org.simantics.views.text / src / org / simantics / views / text / internal / SWTMarkupSourceViewer.java
1 /*******************************************************************************
2  * Copyright (c) 2017 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     Semantum Oy - (#7066) initial API and implementation
11  *******************************************************************************/
12 package org.simantics.views.text.internal;
13
14 import org.eclipse.jface.text.Document;
15 import org.eclipse.mylyn.wikitext.mediawiki.MediaWikiLanguage;
16 import org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage;
17 import org.eclipse.mylyn.wikitext.ui.editor.MarkupSourceViewer;
18 import org.eclipse.mylyn.wikitext.ui.editor.MarkupSourceViewerConfiguration;
19 import org.eclipse.mylyn.wikitext.ui.editor.ShowInTargetBridge;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.custom.StyledText;
22 import org.eclipse.swt.events.SelectionEvent;
23 import org.eclipse.swt.events.SelectionListener;
24 import org.eclipse.swt.graphics.Point;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.ui.services.IServiceLocator;
27 import org.eclipse.ui.swt.IFocusService;
28 import org.simantics.views.ViewUtils;
29 import org.simantics.views.swt.client.base.SingleSWTViewNode;
30
31 /**
32  * @author Tuukka Lehtonen
33  * @since 1.28.0
34  */
35 public class SWTMarkupSourceViewer extends SingleSWTViewNode<StyledText> {
36
37         private static final long serialVersionUID = 3034624586096417826L;
38
39         private MarkupSourceViewer viewer;
40         private boolean textInitialized = false;
41
42         @Override
43         public void createControls(Composite parent) {
44                 MarkupLanguage language = new MediaWikiLanguage();
45                 viewer = new MarkupSourceViewer(parent, null, style | SWT.WRAP, language);
46                 viewer.setEditable(false);
47                 MarkupSourceViewerConfiguration configuration = new MarkupSourceViewerConfiguration(Activator.getDefault().getPreferenceStore());
48                 configuration.setMarkupLanguage(language);
49                 configuration.setShowInTarget(new ShowInTargetBridge(viewer));
50                 viewer.configure(configuration);
51                 viewer.setDocument(new Document(text != null ? text : ""));
52                 control = viewer.getTextWidget();
53                 control.setData(TextViewerConstants.KEY_UNDO_MANAGER, viewer.getUndoManager());
54                 control.setEnabled(false);
55
56                 setProperties();
57
58                 // Allow undo/redo handler to be bound to this text editor's focus
59                 IServiceLocator locator = getSite();
60                 if (locator != null) {
61                         IFocusService focusService = locator.getService(IFocusService.class);
62                         if (focusService != null) {
63                                 focusService.addFocusTracker(control, TextViewerConstants.CONTEXT_IN_TEXT_VIEWER);
64                         }
65                 }
66
67                 control.addSelectionListener(new SelectionListener() {
68                         @Override
69                         public void widgetSelected(SelectionEvent e) {
70                                 ViewUtils.setWorkbenchSelection(viewer.getSelection());
71                         }
72                         @Override
73                         public void widgetDefaultSelected(SelectionEvent e) {
74                                 widgetSelected(e);
75                         }
76                 });
77         }
78
79         @Override
80         public void synchronizeText(String text) {
81                 this.text = text;
82                 if (text != null) {
83                         // Try to keep the vertical scroll position of the text widget
84                         int caretOffset = control.getCaretOffset();
85                         int charCount = control.getCharCount();
86                         int topIndex = viewer.getTopIndex();
87                         int diff = text.length() - charCount;
88                         int newCaretOffset = Math.max(0, Math.min(caretOffset + diff, text.length()));
89
90                         viewer.getDocument().set(text);
91                         viewer.setTopIndex(topIndex);
92                         control.setCaretOffset(newCaretOffset);
93                         viewer.setEditable(true);
94                         control.setEnabled(true);
95
96                         // Prevent text viewer undo from allowing the
97                         // user to undo the text viewer back to empty.
98                         if (!textInitialized) {
99                                 viewer.getUndoManager().reset();
100                                 textInitialized = true;
101                         }
102                 } else {
103                         textInitialized = false;
104                         viewer.setEditable(false);
105                         control.setEnabled(false);
106                         viewer.getDocument().set("");
107                         viewer.getUndoManager().reset();
108                 }
109         }
110
111         public String readText() {
112                 return viewer.getDocument().get();
113         }
114
115         public Point readSelection() {
116                 return control.getSelection();
117         }
118
119 }