1 |
65514c8c
|
Patrick Plitzner
|
/**
|
2 |
|
|
* Copyright (C) 2007 EDIT
|
3 |
|
|
* European Distributed Institute of Taxonomy
|
4 |
|
|
* http://www.e-taxonomy.eu
|
5 |
|
|
*
|
6 |
|
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7 |
|
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8 |
|
|
*/
|
9 |
|
|
|
10 |
|
|
package eu.etaxonomy.taxeditor.editor.name.e4.container;
|
11 |
|
|
|
12 |
|
|
import java.util.Iterator;
|
13 |
|
|
|
14 |
|
|
import org.eclipse.core.commands.operations.IUndoContext;
|
15 |
|
|
import org.eclipse.core.runtime.Assert;
|
16 |
|
|
import org.eclipse.jface.text.Document;
|
17 |
|
|
import org.eclipse.jface.text.IUndoManager;
|
18 |
|
|
import org.eclipse.jface.text.IUndoManagerExtension;
|
19 |
|
|
import org.eclipse.jface.text.Position;
|
20 |
|
|
import org.eclipse.jface.text.TextViewerUndoManager;
|
21 |
|
|
import org.eclipse.jface.text.source.Annotation;
|
22 |
|
|
import org.eclipse.jface.text.source.AnnotationModel;
|
23 |
|
|
import org.eclipse.jface.text.source.AnnotationPainter;
|
24 |
|
|
import org.eclipse.jface.text.source.IAnnotationAccess;
|
25 |
|
|
import org.eclipse.jface.text.source.IVerticalRuler;
|
26 |
|
|
import org.eclipse.jface.text.source.SourceViewer;
|
27 |
|
|
import org.eclipse.swt.SWT;
|
28 |
|
|
import org.eclipse.swt.graphics.Color;
|
29 |
|
|
import org.eclipse.swt.graphics.Image;
|
30 |
|
|
import org.eclipse.swt.widgets.Canvas;
|
31 |
|
|
import org.eclipse.swt.widgets.Composite;
|
32 |
|
|
import org.eclipse.swt.widgets.Control;
|
33 |
|
|
import org.eclipse.swt.widgets.Display;
|
34 |
|
|
import org.eclipse.swt.widgets.Layout;
|
35 |
|
|
import org.eclipse.swt.widgets.Menu;
|
36 |
|
|
import org.eclipse.ui.IActionBars;
|
37 |
|
|
import org.eclipse.ui.IEditorSite;
|
38 |
|
|
import org.eclipse.ui.actions.ActionFactory;
|
39 |
|
|
import org.eclipse.ui.forms.widgets.TableWrapData;
|
40 |
|
|
import org.eclipse.ui.forms.widgets.TableWrapLayout;
|
41 |
|
|
import org.eclipse.ui.operations.OperationHistoryActionHandler;
|
42 |
|
|
import org.eclipse.ui.operations.RedoActionHandler;
|
43 |
|
|
import org.eclipse.ui.operations.UndoActionHandler;
|
44 |
|
|
import org.eclipse.ui.texteditor.DefaultMarkerAnnotationAccess;
|
45 |
|
|
import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds;
|
46 |
|
|
|
47 |
|
|
import eu.etaxonomy.taxeditor.editor.ViewerConfiguration;
|
48 |
|
|
import eu.etaxonomy.taxeditor.editor.name.container.EditorAnnotation;
|
49 |
|
|
import eu.etaxonomy.taxeditor.editor.name.container.EditorAnnotation.EditorAnnotationType;
|
50 |
|
|
import eu.etaxonomy.taxeditor.editor.name.container.LineWrapSquigglesStrategy;
|
51 |
|
|
import eu.etaxonomy.taxeditor.editor.name.container.RulerWithIcon;
|
52 |
|
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
53 |
|
|
import eu.etaxonomy.taxeditor.preference.Resources;
|
54 |
|
|
|
55 |
|
|
/**
|
56 |
|
|
*
|
57 |
|
|
* @author pplitzner
|
58 |
|
|
* @since Aug 25, 2017
|
59 |
|
|
*
|
60 |
|
|
*/
|
61 |
|
|
public class NameViewerE4 extends SourceViewer {
|
62 |
|
|
|
63 |
|
|
public static final int RULER_WIDTH = 16;
|
64 |
|
|
|
65 |
|
|
private final IVerticalRuler ruler;
|
66 |
|
|
private final AnnotationModel annotationModel;
|
67 |
|
|
|
68 |
|
|
public NameViewerE4(Composite parent) {
|
69 |
|
|
super(parent, new RulerWithIcon(RULER_WIDTH), SWT.WRAP | SWT.MULTI | SWT.RESIZE);
|
70 |
|
|
|
71 |
|
|
this.ruler = getVerticalRuler();
|
72 |
|
|
|
73 |
|
|
setBackground(AbstractUtility.getColor(Resources.COLOR_COMPOSITE_BACKGROUND));
|
74 |
|
|
|
75 |
|
|
Canvas canvas = (Canvas) getControl();
|
76 |
|
|
TableWrapLayout layout = (TableWrapLayout) canvas.getLayout();
|
77 |
|
|
layout.topMargin = 0;
|
78 |
|
|
layout.rightMargin = 0;
|
79 |
|
|
layout.bottomMargin = 0;
|
80 |
|
|
layout.leftMargin = 0;
|
81 |
|
|
layout.verticalSpacing = 10;
|
82 |
|
|
layout.horizontalSpacing = 0;
|
83 |
|
|
|
84 |
|
|
canvas.setLayout(layout);
|
85 |
|
|
|
86 |
|
|
// Lay out the viewer's widgets
|
87 |
|
|
TableWrapData twdata = new TableWrapData(TableWrapData.FILL_GRAB);
|
88 |
|
|
getControl().setLayoutData(twdata);
|
89 |
|
|
|
90 |
|
|
twdata = new TableWrapData(TableWrapData.FILL_GRAB);
|
91 |
|
|
getTextWidget().setLayoutData(twdata);
|
92 |
|
|
getTextWidget().setLineSpacing(5);
|
93 |
|
|
|
94 |
|
|
// Default implementation adds ruler after the text widget, then
|
95 |
|
|
// uses a custom layout to display it before the text widget
|
96 |
|
|
ruler.getControl().moveAbove(getTextWidget());
|
97 |
|
|
|
98 |
|
|
annotationModel = new AnnotationModel();
|
99 |
|
|
this.setDocument(new Document(""), annotationModel); //$NON-NLS-1$
|
100 |
|
|
|
101 |
|
|
createAnnotationPainter();
|
102 |
|
|
|
103 |
|
|
this.configure(new ViewerConfiguration());
|
104 |
|
|
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
public void setBackground(Color color) {
|
108 |
|
|
|
109 |
|
|
// Set background color of ruler
|
110 |
|
|
ruler.getControl().setBackground(color);
|
111 |
|
|
|
112 |
|
|
// Set background color of text widget
|
113 |
|
|
getTextWidget().setBackground(color);
|
114 |
|
|
|
115 |
|
|
// Set background color of strip between ruler and textWidget
|
116 |
|
|
getTextWidget().getParent().setBackground(color);
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
public Control getRulerControl() {
|
120 |
|
|
return ruler.getControl();
|
121 |
|
|
}
|
122 |
|
|
|
123 |
|
|
public void setIcon(Image icon) {
|
124 |
|
|
if (ruler instanceof RulerWithIcon) {
|
125 |
|
|
((RulerWithIcon) ruler).setIcon(icon);
|
126 |
|
|
} else {
|
127 |
|
|
throw new IllegalStateException("Viewer's IVerticalRuler is not an instance of RulerWithIcon."); //$NON-NLS-1$
|
128 |
|
|
}
|
129 |
|
|
}
|
130 |
|
|
|
131 |
|
|
@Override
|
132 |
|
|
protected Layout createLayout() {
|
133 |
|
|
TableWrapLayout layout = new TableWrapLayout();
|
134 |
|
|
layout.numColumns = 2;
|
135 |
|
|
layout.verticalSpacing = 0;
|
136 |
|
|
return layout;
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
private void createAnnotationPainter(){
|
140 |
|
|
// Annotations section
|
141 |
|
|
IAnnotationAccess fAnnotationAccess = new DefaultMarkerAnnotationAccess();
|
142 |
|
|
|
143 |
|
|
// To paint the annotations
|
144 |
|
|
AnnotationPainter annotationPainter = new AnnotationPainter(this, fAnnotationAccess);
|
145 |
|
|
|
146 |
|
|
// Default SquigglesStrategy doesn't recognize line wraps
|
147 |
|
|
annotationPainter.addDrawingStrategy(LineWrapSquigglesStrategy.ID, new LineWrapSquigglesStrategy());
|
148 |
|
|
|
149 |
|
|
// Add ability to paint red squigglies
|
150 |
|
|
annotationPainter.addAnnotationType(EditorAnnotationType.ERROR.name(), LineWrapSquigglesStrategy.ID);
|
151 |
|
|
annotationPainter.setAnnotationTypeColor(EditorAnnotationType.ERROR.image,
|
152 |
|
|
new Color(Display.getDefault(), EditorAnnotationType.ERROR.color));
|
153 |
|
|
|
154 |
|
|
// Add ability to paint yellow squigglies
|
155 |
|
|
annotationPainter.addAnnotationType(EditorAnnotationType.WARNING.name(), LineWrapSquigglesStrategy.ID);
|
156 |
|
|
annotationPainter.setAnnotationTypeColor(EditorAnnotationType.WARNING.image,
|
157 |
|
|
new Color(Display.getDefault(), EditorAnnotationType.WARNING.color));
|
158 |
|
|
|
159 |
|
|
this.addPainter(annotationPainter);
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
|
163 |
|
|
public void clearAnnotations() {
|
164 |
|
|
Iterator<Annotation> annotations = this.getAnnotationModel().getAnnotationIterator();
|
165 |
|
|
while (annotations.hasNext()) {
|
166 |
|
|
Annotation annotation = annotations.next();
|
167 |
|
|
this.getAnnotationModel().removeAnnotation(annotation);
|
168 |
|
|
}
|
169 |
|
|
}
|
170 |
|
|
|
171 |
|
|
public void addAnnotation(EditorAnnotation annotation){
|
172 |
|
|
addAnnotation(annotation, null);
|
173 |
|
|
}
|
174 |
|
|
|
175 |
|
|
public void addAnnotation(EditorAnnotation annotation, Position position){
|
176 |
|
|
if(position == null){
|
177 |
|
|
position = new Position(0, 0);
|
178 |
|
|
}
|
179 |
|
|
|
180 |
|
|
this.getAnnotationModel().addAnnotation(annotation, position);
|
181 |
|
|
}
|
182 |
|
|
|
183 |
|
|
public void setText(String text) {
|
184 |
|
|
if (text == null) {
|
185 |
|
|
text = ""; //$NON-NLS-1$
|
186 |
|
|
}
|
187 |
|
|
try {
|
188 |
|
|
Assert.isNotNull(text);
|
189 |
|
|
// TODO figure out why getTextWidget() returns null!
|
190 |
|
|
if (this.getTextWidget() == null) {
|
191 |
|
|
return;
|
192 |
|
|
}
|
193 |
|
|
Assert.isNotNull(this.getTextWidget());
|
194 |
|
|
this.getTextWidget().setText(text);
|
195 |
|
|
} catch (RuntimeException e) {
|
196 |
|
|
throw e;
|
197 |
|
|
}
|
198 |
|
|
}
|
199 |
|
|
|
200 |
|
|
public void setMenu(Menu menu) {
|
201 |
|
|
getRulerControl().setMenu(menu);
|
202 |
|
|
getTextWidget().setMenu(menu);
|
203 |
|
|
}
|
204 |
|
|
|
205 |
|
|
public void setCursorToEOL() {
|
206 |
|
|
getTextWidget().setCaretOffset(getTextWidget().getText().length());
|
207 |
|
|
getTextWidget().setFocus();
|
208 |
|
|
}
|
209 |
|
|
|
210 |
|
|
public int getCursorPosition(){
|
211 |
|
|
return getTextWidget().getCaretOffset();
|
212 |
|
|
}
|
213 |
|
|
|
214 |
|
|
public void setCursorPosition(int offset){
|
215 |
|
|
try{
|
216 |
|
|
getTextWidget().setCaretOffset(offset);
|
217 |
|
|
}catch(IllegalArgumentException e){
|
218 |
|
|
// do nothing
|
219 |
|
|
}
|
220 |
|
|
}
|
221 |
|
|
|
222 |
|
|
public void createUndoSupport(IEditorSite editorSite) {
|
223 |
|
|
|
224 |
|
|
IUndoManager undoManager = new TextViewerUndoManager(25);
|
225 |
|
|
this.setUndoManager(undoManager);
|
226 |
|
|
undoManager.connect(this);
|
227 |
|
|
|
228 |
|
|
IUndoContext workbenchUndoContext = ((IUndoManagerExtension)undoManager).getUndoContext();
|
229 |
|
|
|
230 |
|
|
OperationHistoryActionHandler undoAction = new UndoActionHandler(editorSite, workbenchUndoContext);
|
231 |
|
|
undoAction.setActionDefinitionId(ActionFactory.UNDO.getId());
|
232 |
|
|
|
233 |
|
|
// Create the redo action.
|
234 |
|
|
OperationHistoryActionHandler redoAction = new RedoActionHandler(editorSite, workbenchUndoContext);
|
235 |
|
|
undoAction.setActionDefinitionId(ActionFactory.REDO.getId());
|
236 |
|
|
|
237 |
|
|
IActionBars actionBars = editorSite.getActionBars();
|
238 |
|
|
if (actionBars != null) {
|
239 |
|
|
actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), undoAction);
|
240 |
|
|
actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), redoAction);
|
241 |
|
|
|
242 |
|
|
}
|
243 |
|
|
|
244 |
|
|
}
|
245 |
|
|
|
246 |
|
|
public void createUndoSupport_(IEditorSite editorSite) {
|
247 |
|
|
IUndoManager undoManager = new TextViewerUndoManager(25);
|
248 |
|
|
this.setUndoManager(undoManager);
|
249 |
|
|
undoManager.connect(this);
|
250 |
|
|
IUndoContext undoContext;
|
251 |
|
|
if (undoManager instanceof IUndoManagerExtension) {
|
252 |
|
|
undoContext = ((IUndoManagerExtension)undoManager).getUndoContext();
|
253 |
|
|
|
254 |
|
|
OperationHistoryActionHandler undoAction = new UndoActionHandler(editorSite, undoContext);
|
255 |
|
|
undoAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.UNDO);
|
256 |
|
|
|
257 |
|
|
// Create the redo action.
|
258 |
|
|
OperationHistoryActionHandler redoAction = new RedoActionHandler(editorSite, undoContext);
|
259 |
|
|
redoAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.REDO);
|
260 |
|
|
|
261 |
|
|
IActionBars actionBars = editorSite.getActionBars();
|
262 |
|
|
if (actionBars != null) {
|
263 |
|
|
actionBars.setGlobalActionHandler(IWorkbenchActionDefinitionIds.UNDO, undoAction);
|
264 |
|
|
actionBars.setGlobalActionHandler(IWorkbenchActionDefinitionIds.REDO, redoAction);
|
265 |
|
|
|
266 |
|
|
}
|
267 |
|
|
}
|
268 |
|
|
}
|
269 |
|
|
|
270 |
|
|
}
|