1
|
/**
|
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.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.cdm.model.name.NonViralName;
|
48
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
49
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
50
|
import eu.etaxonomy.cdm.strategy.parser.ParserProblem;
|
51
|
import eu.etaxonomy.taxeditor.editor.EditorUtil;
|
52
|
import eu.etaxonomy.taxeditor.editor.ViewerConfiguration;
|
53
|
import eu.etaxonomy.taxeditor.preference.Resources;
|
54
|
|
55
|
/**
|
56
|
* SourceViewer implementation called by NameComposite.
|
57
|
*
|
58
|
* @author p.ciardelli
|
59
|
* @created 27.05.2008
|
60
|
* @version 1.0
|
61
|
*/
|
62
|
public class NameViewer extends SourceViewer {
|
63
|
|
64
|
/** Constant <code>RULER_WIDTH=16</code> */
|
65
|
public static final int RULER_WIDTH = 16;
|
66
|
|
67
|
private IVerticalRuler ruler;
|
68
|
private AnnotationModel annotationModel;
|
69
|
|
70
|
/**
|
71
|
* <p>Constructor for NameViewer.</p>
|
72
|
*
|
73
|
* @param parent a {@link org.eclipse.swt.widgets.Composite} object.
|
74
|
*/
|
75
|
public NameViewer(Composite parent) {
|
76
|
super(parent, new RulerWithIcon(RULER_WIDTH), SWT.WRAP | SWT.MULTI | SWT.RESIZE);
|
77
|
|
78
|
this.ruler = getVerticalRuler();
|
79
|
|
80
|
setBackground(EditorUtil.getColor(Resources.COLOR_COMPOSITE_BACKGROUND));
|
81
|
|
82
|
Canvas canvas = (Canvas) getControl();
|
83
|
TableWrapLayout layout = (TableWrapLayout) canvas.getLayout();
|
84
|
layout.topMargin = 0;
|
85
|
layout.rightMargin = 0;
|
86
|
layout.bottomMargin = 0;
|
87
|
layout.leftMargin = 0;
|
88
|
layout.verticalSpacing = 10;
|
89
|
layout.horizontalSpacing = 0;
|
90
|
|
91
|
canvas.setLayout(layout);
|
92
|
|
93
|
// Lay out the viewer's widgets
|
94
|
TableWrapData twdata = new TableWrapData(TableWrapData.FILL_GRAB);
|
95
|
getControl().setLayoutData(twdata);
|
96
|
|
97
|
twdata = new TableWrapData(TableWrapData.FILL_GRAB);
|
98
|
getTextWidget().setLayoutData(twdata);
|
99
|
getTextWidget().setLineSpacing(5);
|
100
|
|
101
|
// Default implementation adds ruler after the text widget, then
|
102
|
// uses a custom layout to display it before the text widget
|
103
|
ruler.getControl().moveAbove(getTextWidget());
|
104
|
|
105
|
annotationModel = new AnnotationModel();
|
106
|
this.setDocument(new Document(""), annotationModel);
|
107
|
|
108
|
createAnnotationPainter();
|
109
|
|
110
|
this.configure(new ViewerConfiguration());
|
111
|
|
112
|
// setDecorationSupport();
|
113
|
// DocumentUndoManagerRegistry.connect(this.getDocument());
|
114
|
// IDocumentUndoManager docUndoManager = DocumentUndoManagerRegistry.getDocumentUndoManager(this.getDocument());
|
115
|
|
116
|
/**
|
117
|
getEditorSite().getActionBars().setGlobalActionHandler(ActionFactory.UNDO.getId(), new UndoActionHandler(getSite(), undoContext));
|
118
|
getEditorSite().getActionBars().setGlobalActionHandler(ActionFactory.REDO.getId(), new RedoActionHandler(getSite(), undoContext));
|
119
|
**/
|
120
|
}
|
121
|
|
122
|
/**
|
123
|
* <p>setBackground</p>
|
124
|
*
|
125
|
* @param color a {@link org.eclipse.swt.graphics.Color} object.
|
126
|
*/
|
127
|
public void setBackground(Color color) {
|
128
|
|
129
|
// Set background color of ruler
|
130
|
ruler.getControl().setBackground(color);
|
131
|
|
132
|
// Set background color of text widget
|
133
|
getTextWidget().setBackground(color);
|
134
|
|
135
|
// Set background color of strip between ruler and textWidget
|
136
|
getTextWidget().getParent().setBackground(color);
|
137
|
}
|
138
|
|
139
|
/**
|
140
|
* <p>getRulerControl</p>
|
141
|
*
|
142
|
* @return a {@link org.eclipse.swt.widgets.Control} object.
|
143
|
*/
|
144
|
public Control getRulerControl() {
|
145
|
return ruler.getControl();
|
146
|
}
|
147
|
|
148
|
/**
|
149
|
* <p>setIcon</p>
|
150
|
*
|
151
|
* @param icon a {@link org.eclipse.swt.graphics.Image} object.
|
152
|
*/
|
153
|
public void setIcon(Image icon) {
|
154
|
if (ruler instanceof RulerWithIcon) {
|
155
|
((RulerWithIcon) ruler).setIcon(icon);
|
156
|
} else {
|
157
|
throw new IllegalStateException("Viewer's IVerticalRuler is not an instance of RulerWithIcon.");
|
158
|
}
|
159
|
}
|
160
|
|
161
|
/* (non-Javadoc)
|
162
|
* @see org.eclipse.jface.text.source.SourceViewer#createLayout()
|
163
|
*/
|
164
|
/**
|
165
|
* <p>createLayout</p>
|
166
|
*
|
167
|
* @return a {@link org.eclipse.swt.widgets.Layout} object.
|
168
|
*/
|
169
|
protected Layout createLayout() {
|
170
|
TableWrapLayout layout = new TableWrapLayout();
|
171
|
layout.numColumns = 2;
|
172
|
layout.verticalSpacing = 0;
|
173
|
return layout;
|
174
|
}
|
175
|
|
176
|
private void createAnnotationPainter(){
|
177
|
// Annotations section
|
178
|
IAnnotationAccess fAnnotationAccess = new DefaultMarkerAnnotationAccess();
|
179
|
|
180
|
// To paint the annotations
|
181
|
AnnotationPainter annotationPainter = new AnnotationPainter(this, fAnnotationAccess);
|
182
|
|
183
|
// Default SquigglesStrategy doesn't recognize line wraps
|
184
|
annotationPainter.addDrawingStrategy(LineWrapSquigglesStrategy.ID, new LineWrapSquigglesStrategy());
|
185
|
|
186
|
// Add ability to paint red squigglies
|
187
|
annotationPainter.addAnnotationType(EditorAnnotation.ERROR_TYPE, LineWrapSquigglesStrategy.ID);
|
188
|
annotationPainter.setAnnotationTypeColor(EditorAnnotation.ERROR_TYPE,
|
189
|
new Color(Display.getDefault(), EditorAnnotation.ERROR_RGB));
|
190
|
|
191
|
// Add ability to paint yellow squigglies
|
192
|
annotationPainter.addAnnotationType(EditorAnnotation.WARNING_TYPE, LineWrapSquigglesStrategy.ID);
|
193
|
annotationPainter.setAnnotationTypeColor(EditorAnnotation.WARNING_TYPE,
|
194
|
new Color(Display.getDefault(), EditorAnnotation.WARNING_RGB));
|
195
|
|
196
|
this.addPainter(annotationPainter);
|
197
|
}
|
198
|
|
199
|
|
200
|
/**
|
201
|
* <p>clearErrors</p>
|
202
|
*/
|
203
|
public void clearErrors() {
|
204
|
Iterator<Annotation> annotations = this.getAnnotationModel().getAnnotationIterator();
|
205
|
while (annotations.hasNext()) {
|
206
|
Annotation annotation = annotations.next();
|
207
|
if (annotation.getType().equals(EditorAnnotation.ERROR_TYPE) || annotation.getType().equals(EditorAnnotation.WARNING_TYPE)) {
|
208
|
this.getAnnotationModel().removeAnnotation(annotation);
|
209
|
}
|
210
|
}
|
211
|
}
|
212
|
|
213
|
/**
|
214
|
* If <code>name.hasProblem()</code> is <code>true</code>, underlines section
|
215
|
* of text bounded by <code>name.getProblemStarts()</code> and
|
216
|
* <code>name.getProblemEnds()</code>.
|
217
|
*
|
218
|
* @param name a {@link eu.etaxonomy.cdm.model.name.TaxonNameBase} object.
|
219
|
*/
|
220
|
public void setShowParsingErrors(TaxonNameBase<?, ?> name) {
|
221
|
|
222
|
String text = this.getTextWidget().getText();
|
223
|
|
224
|
if (name.hasProblem() && text.length() > 0) {
|
225
|
int start = name.getProblemStarts();
|
226
|
int length = name.getProblemEnds() - start;
|
227
|
|
228
|
if (start == -1 || name.getProblemEnds() == -1) {
|
229
|
return;
|
230
|
}
|
231
|
|
232
|
// Don't let squigglies try to draw beyond the end of the text
|
233
|
if (text.length() < start + length) {
|
234
|
length = text.length() - start;
|
235
|
}
|
236
|
|
237
|
for (ParserProblem problem : name.getParsingProblems()) {
|
238
|
|
239
|
String type = null;
|
240
|
if (problem.isWarning()) {
|
241
|
type = EditorAnnotation.WARNING_TYPE;
|
242
|
}
|
243
|
if (problem.isError()) {
|
244
|
type = EditorAnnotation.ERROR_TYPE;
|
245
|
}
|
246
|
if (type == null) {
|
247
|
continue;
|
248
|
}
|
249
|
this.getAnnotationModel().addAnnotation(
|
250
|
new EditorAnnotation(type, 0, problem.getMessage()),
|
251
|
new Position(start, length));
|
252
|
}
|
253
|
}
|
254
|
}
|
255
|
|
256
|
|
257
|
/**
|
258
|
* <p>setShowSecError</p>
|
259
|
*
|
260
|
* @param taxon a {@link eu.etaxonomy.cdm.model.taxon.Taxon} object.
|
261
|
*/
|
262
|
public void setShowSecError(Taxon taxon) {
|
263
|
|
264
|
// If taxon has no sec, show an annotation
|
265
|
if (taxon.getSec() == null) {
|
266
|
|
267
|
String text = "This taxon requires a sec. reference.";
|
268
|
|
269
|
this.getAnnotationModel().addAnnotation(
|
270
|
new EditorAnnotation(0, text),
|
271
|
new Position(0, 0));
|
272
|
}
|
273
|
}
|
274
|
|
275
|
public void setShowMultipleNameUsageWarning(TaxonNameBase name){
|
276
|
if(name.getTaxonBases().size() > 1){
|
277
|
String text = "This taxons name is used multiple times.";
|
278
|
|
279
|
this.getAnnotationModel().addAnnotation(
|
280
|
new EditorAnnotation(EditorAnnotation.WARNING_TYPE, 0, text),
|
281
|
new Position(0, 0));
|
282
|
}
|
283
|
}
|
284
|
|
285
|
/**
|
286
|
* @param name
|
287
|
*/
|
288
|
public void setShowNameNotParsableWarning(NonViralName name) {
|
289
|
String text = "This name may only be edited in the details view.";
|
290
|
|
291
|
this.getAnnotationModel().addAnnotation(
|
292
|
new EditorAnnotation(EditorAnnotation.WARNING_TYPE, 0, text),
|
293
|
new Position(0, 0));
|
294
|
|
295
|
}
|
296
|
|
297
|
/**
|
298
|
* <p>setText</p>
|
299
|
*
|
300
|
* @param text a {@link java.lang.String} object.
|
301
|
*/
|
302
|
public void setText(String text) {
|
303
|
if (text == null) {
|
304
|
text = "";
|
305
|
}
|
306
|
try {
|
307
|
Assert.isNotNull(text);
|
308
|
// TODO figure out why getTextWidget() returns null!
|
309
|
if (this.getTextWidget() == null) {
|
310
|
return;
|
311
|
}
|
312
|
Assert.isNotNull(this.getTextWidget());
|
313
|
this.getTextWidget().setText(text);
|
314
|
} catch (RuntimeException e) {
|
315
|
throw e;
|
316
|
}
|
317
|
}
|
318
|
|
319
|
/**
|
320
|
* <p>setMenu</p>
|
321
|
*
|
322
|
* @param menu a {@link org.eclipse.swt.widgets.Menu} object.
|
323
|
*/
|
324
|
public void setMenu(Menu menu) {
|
325
|
getRulerControl().setMenu(menu);
|
326
|
getTextWidget().setMenu(menu);
|
327
|
}
|
328
|
|
329
|
/**
|
330
|
* <p>setCursorToEOL</p>
|
331
|
*/
|
332
|
public void setCursorToEOL() {
|
333
|
getTextWidget().setCaretOffset(getTextWidget().getText().length());
|
334
|
getTextWidget().setFocus();
|
335
|
}
|
336
|
|
337
|
|
338
|
/**
|
339
|
* <p>getCursorPosition</p>
|
340
|
*
|
341
|
* @return a int.
|
342
|
*/
|
343
|
public int getCursorPosition(){
|
344
|
return getTextWidget().getCaretOffset();
|
345
|
}
|
346
|
|
347
|
/**
|
348
|
* <p>setCursorPosition</p>
|
349
|
*
|
350
|
* @param offset a int.
|
351
|
*/
|
352
|
public void setCursorPosition(int offset){
|
353
|
try{
|
354
|
getTextWidget().setCaretOffset(offset);
|
355
|
}catch(IllegalArgumentException e){
|
356
|
// do nothing
|
357
|
}
|
358
|
}
|
359
|
|
360
|
/**
|
361
|
* <p>createUndoSupport</p>
|
362
|
*
|
363
|
* @param editorSite a {@link org.eclipse.ui.IEditorSite} object.
|
364
|
*/
|
365
|
public void createUndoSupport(IEditorSite editorSite) {
|
366
|
|
367
|
IUndoManager undoManager = new TextViewerUndoManager(25);
|
368
|
this.setUndoManager(undoManager);
|
369
|
undoManager.connect(this);
|
370
|
|
371
|
// IUndoContext workbenchUndoContext = UiUtil.getWorkbenchUndoContext();
|
372
|
|
373
|
IUndoContext workbenchUndoContext = ((IUndoManagerExtension)undoManager).getUndoContext();
|
374
|
|
375
|
OperationHistoryActionHandler undoAction = new UndoActionHandler(editorSite, workbenchUndoContext);
|
376
|
// undoAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.UNDO);
|
377
|
undoAction.setActionDefinitionId(ActionFactory.UNDO.getId());
|
378
|
|
379
|
// Create the redo action.
|
380
|
OperationHistoryActionHandler redoAction = new RedoActionHandler(editorSite, workbenchUndoContext);
|
381
|
// redoAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.REDO);
|
382
|
undoAction.setActionDefinitionId(ActionFactory.REDO.getId());
|
383
|
|
384
|
IActionBars actionBars = editorSite.getActionBars();
|
385
|
if (actionBars != null) {
|
386
|
// actionBars.setGlobalActionHandler(IWorkbenchActionDefinitionIds.UNDO, undoAction);
|
387
|
// actionBars.setGlobalActionHandler(IWorkbenchActionDefinitionIds.REDO, redoAction);
|
388
|
actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), undoAction);
|
389
|
actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), redoAction);
|
390
|
|
391
|
}
|
392
|
|
393
|
// actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), undoAction);
|
394
|
// actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), redoAction);
|
395
|
}
|
396
|
|
397
|
/**
|
398
|
* <p>createUndoSupport_</p>
|
399
|
*
|
400
|
* @param editorSite a {@link org.eclipse.ui.IEditorSite} object.
|
401
|
*/
|
402
|
public void createUndoSupport_(IEditorSite editorSite) {
|
403
|
IUndoManager undoManager = new TextViewerUndoManager(25);
|
404
|
this.setUndoManager(undoManager);
|
405
|
undoManager.connect(this);
|
406
|
IUndoContext undoContext;
|
407
|
if (undoManager instanceof IUndoManagerExtension) {
|
408
|
undoContext = ((IUndoManagerExtension)undoManager).getUndoContext();
|
409
|
|
410
|
OperationHistoryActionHandler undoAction = new UndoActionHandler(editorSite, undoContext);
|
411
|
// PlatformUI.getWorkbench().getHelpSystem().setHelp(undoAction, IAbstractTextEditorHelpContextIds.UNDO_ACTION);
|
412
|
undoAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.UNDO);
|
413
|
|
414
|
// Create the redo action.
|
415
|
OperationHistoryActionHandler redoAction = new RedoActionHandler(editorSite, undoContext);
|
416
|
// PlatformUI.getWorkbench().getHelpSystem().setHelp(redoAction, IAbstractTextEditorHelpContextIds.REDO_ACTION);
|
417
|
redoAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.REDO);
|
418
|
|
419
|
|
420
|
IActionBars actionBars = editorSite.getActionBars();
|
421
|
if (actionBars != null) {
|
422
|
actionBars.setGlobalActionHandler(IWorkbenchActionDefinitionIds.UNDO, undoAction);
|
423
|
actionBars.setGlobalActionHandler(IWorkbenchActionDefinitionIds.REDO, redoAction);
|
424
|
|
425
|
}
|
426
|
}
|
427
|
}
|
428
|
|
429
|
}
|
430
|
|