1 /*******************************************************************************
2 * Copyright (c) 2017 Association for Decentralized Information Management
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
10 * Semantum Oy - (#7066) initial API and implementation
11 *******************************************************************************/
12 package org.simantics.views.text.internal;
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;
32 * @author Tuukka Lehtonen
35 public class SWTMarkupSourceViewer extends SingleSWTViewNode<StyledText> {
37 private static final long serialVersionUID = 3034624586096417826L;
39 private MarkupSourceViewer viewer;
40 private boolean textInitialized = false;
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);
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);
67 control.addSelectionListener(new SelectionListener() {
69 public void widgetSelected(SelectionEvent e) {
70 ViewUtils.setWorkbenchSelection(viewer.getSelection());
73 public void widgetDefaultSelected(SelectionEvent e) {
80 public void synchronizeText(String text) {
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()));
90 viewer.getDocument().set(text);
91 viewer.setTopIndex(topIndex);
92 control.setCaretOffset(newCaretOffset);
93 viewer.setEditable(true);
94 control.setEnabled(true);
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;
103 textInitialized = false;
104 viewer.setEditable(false);
105 control.setEnabled(false);
106 viewer.getDocument().set("");
107 viewer.getUndoManager().reset();
111 public String readText() {
112 return viewer.getDocument().get();
115 public Point readSelection() {
116 return control.getSelection();