2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.editor
.name
;
12 import java
.util
.Iterator
;
14 import org
.apache
.log4j
.Logger
;
15 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
16 import org
.eclipse
.core
.resources
.IMarker
;
17 import org
.eclipse
.core
.resources
.IWorkspaceRunnable
;
18 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
19 import org
.eclipse
.core
.runtime
.Assert
;
20 import org
.eclipse
.core
.runtime
.CoreException
;
21 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
22 import org
.eclipse
.jface
.text
.Document
;
23 import org
.eclipse
.jface
.text
.IUndoManager
;
24 import org
.eclipse
.jface
.text
.IUndoManagerExtension
;
25 import org
.eclipse
.jface
.text
.Position
;
26 import org
.eclipse
.jface
.text
.TextViewerUndoManager
;
27 import org
.eclipse
.jface
.text
.source
.Annotation
;
28 import org
.eclipse
.jface
.text
.source
.AnnotationModel
;
29 import org
.eclipse
.jface
.text
.source
.AnnotationPainter
;
30 import org
.eclipse
.jface
.text
.source
.IAnnotationAccess
;
31 import org
.eclipse
.jface
.text
.source
.IAnnotationModel
;
32 import org
.eclipse
.jface
.text
.source
.IVerticalRuler
;
33 import org
.eclipse
.jface
.text
.source
.SourceViewer
;
34 import org
.eclipse
.swt
.SWT
;
35 import org
.eclipse
.swt
.graphics
.Color
;
36 import org
.eclipse
.swt
.graphics
.Image
;
37 import org
.eclipse
.swt
.widgets
.Composite
;
38 import org
.eclipse
.swt
.widgets
.Control
;
39 import org
.eclipse
.swt
.widgets
.Display
;
40 import org
.eclipse
.swt
.widgets
.Layout
;
41 import org
.eclipse
.ui
.IActionBars
;
42 import org
.eclipse
.ui
.IEditorSite
;
43 import org
.eclipse
.ui
.actions
.ActionFactory
;
44 import org
.eclipse
.ui
.editors
.text
.EditorsUI
;
45 import org
.eclipse
.ui
.forms
.widgets
.TableWrapData
;
46 import org
.eclipse
.ui
.forms
.widgets
.TableWrapLayout
;
47 import org
.eclipse
.ui
.operations
.OperationHistoryActionHandler
;
48 import org
.eclipse
.ui
.operations
.RedoActionHandler
;
49 import org
.eclipse
.ui
.operations
.UndoActionHandler
;
50 import org
.eclipse
.ui
.texteditor
.DefaultMarkerAnnotationAccess
;
51 import org
.eclipse
.ui
.texteditor
.IWorkbenchActionDefinitionIds
;
52 import org
.eclipse
.ui
.texteditor
.SourceViewerDecorationSupport
;
54 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
55 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
56 import eu
.etaxonomy
.taxeditor
.editor
.ErrorAnnotation
;
57 import eu
.etaxonomy
.taxeditor
.editor
.LineWrapSquigglesStrategy
;
58 import eu
.etaxonomy
.taxeditor
.editor
.ViewerConfiguration
;
59 import eu
.etaxonomy
.taxeditor
.editor
.WarningAnnotation
;
60 import eu
.etaxonomy
.taxeditor
.store
.model
.Resources
;
63 * SourceViewer implementation called by NameComposite.
69 public class NameViewer
extends SourceViewer
{
70 private static final Logger logger
= Logger
71 .getLogger(NameViewer
.class);
73 public static final int RULER_WIDTH
= 16;
75 private IVerticalRuler ruler
;
76 private AnnotationModel annotationModel
;
78 private SourceViewerDecorationSupport decorationSupport
;
80 public NameViewer(Composite parent
) {
81 super(parent
, new RulerWithIcon(RULER_WIDTH
), SWT
.WRAP
| SWT
.MULTI
| SWT
.RESIZE
);
83 this.ruler
= getVerticalRuler();
85 setBackground(Resources
.getColor(Resources
.COLOR_COMPOSITE_BACKGROUND
));
87 // Lay out the viewer's widgets
88 getControl().setLayoutData(new TableWrapData(TableWrapData
.FILL_GRAB
));
89 getTextWidget().setLayoutData(new TableWrapData(TableWrapData
.FILL_GRAB
));
91 // Default implentation adds ruler after the text widget, then
92 // uses a custom layout to display it before the text widget
93 ruler
.getControl().moveAbove(getTextWidget());
95 annotationModel
= new AnnotationModel();
96 this.setDocument(new Document(""), annotationModel
);
98 createAnnotationPainter();
100 this.configure(new ViewerConfiguration());
102 // setDecorationSupport();
103 // DocumentUndoManagerRegistry.connect(this.getDocument());
104 // IDocumentUndoManager docUndoManager = DocumentUndoManagerRegistry.getDocumentUndoManager(this.getDocument());
107 getEditorSite().getActionBars().setGlobalActionHandler(ActionFactory.UNDO.getId(), new UndoActionHandler(getSite(), undoContext));
108 getEditorSite().getActionBars().setGlobalActionHandler(ActionFactory.REDO.getId(), new RedoActionHandler(getSite(), undoContext));
115 private void setDecorationSupport() {
116 decorationSupport
= new SourceViewerDecorationSupport(this, null, null, EditorsUI
.getSharedTextColors());
117 decorationSupport
.setCursorLinePainterPreferenceKeys("currentLine", "currentLineColor");
118 decorationSupport
.install(EditorsUI
.getPreferenceStore());
121 public void setBackground(Color color
) {
122 // Set background color of ruler
123 ruler
.getControl().setBackground(color
);
125 // Set background color of text widget
126 getTextWidget().setBackground(color
);
128 // Set background color of strip between ruler and textWidget
129 getTextWidget().getParent().setBackground(color
);
132 public Control
getRulerControl() {
133 return ruler
.getControl();
136 public void setIcon(Image icon
) {
137 if (ruler
instanceof RulerWithIcon
) {
138 ((RulerWithIcon
) ruler
).setIcon(icon
);
140 logger
.warn("Viewer's IVerticalRuler is not an instance of RulerWithIcon.");
145 * @see org.eclipse.jface.text.source.SourceViewer#createLayout()
147 protected Layout
createLayout() {
148 TableWrapLayout layout
= new TableWrapLayout();
149 layout
.numColumns
= 2;
150 layout
.verticalSpacing
= 0;
154 private void createAnnotationPainter(){
155 // Annotations section
156 IAnnotationAccess fAnnotationAccess
= new DefaultMarkerAnnotationAccess();
158 // To paint the annotations
159 AnnotationPainter annotationPainter
= new AnnotationPainter(this, fAnnotationAccess
);
161 // Default SquigglesStrategy doesn't recognize line wraps
162 annotationPainter
.addDrawingStrategy(LineWrapSquigglesStrategy
.ID
, new LineWrapSquigglesStrategy());
164 // Add ability to paint red squigglies
165 annotationPainter
.addAnnotationType(ErrorAnnotation
.ERROR_TYPE
, LineWrapSquigglesStrategy
.ID
);
166 annotationPainter
.setAnnotationTypeColor(ErrorAnnotation
.ERROR_TYPE
,
167 new Color(Display
.getDefault(), ErrorAnnotation
.ERROR_RGB
));
169 // Add ability to paint yellow squigglies
170 annotationPainter
.addAnnotationType(WarningAnnotation
.WARNING_TYPE
, LineWrapSquigglesStrategy
.ID
);
171 annotationPainter
.setAnnotationTypeColor(WarningAnnotation
.WARNING_TYPE
,
172 new Color(Display
.getDefault(), WarningAnnotation
.WARNING_RGB
));
174 this.addPainter(annotationPainter
);
178 public void clearErrors() {
179 Iterator
<Annotation
> annotations
= this.getAnnotationModel().getAnnotationIterator();
180 while (annotations
.hasNext()) {
181 Annotation annotation
= annotations
.next();
182 if (annotation
.getType().equals(ErrorAnnotation
.ERROR_TYPE
))
183 this.getAnnotationModel().removeAnnotation(annotation
);
188 * If <code>name.hasProblem()</code> is <code>true</code>, underlines section
189 * of text bounded by <code>name.getProblemStarts()</code> and
190 * <code>name.getProblemEnds()</code>.
194 public void setShowParsingError(TaxonNameBase name
) {
198 boolean hasProblem
= name
.getHasProblem();
201 logger
.debug(name
.getProblemStarts());
204 String text
= this.getTextWidget().getText();
206 if (hasProblem
&& text
.length() > 0) {
207 // logger.warn("Name: " + name.getTitleCache() + ", start: " + name.getProblemStarts()+ ", end: " + name.getProblemEnds());
208 int start
= name
.getProblemStarts();
209 int length
= name
.getProblemEnds() - start
;
211 if (start
== -1 || name
.getProblemEnds() == -1) {
212 // logger.warn("Start: " + start + ", End " + name.getProblemEnds());
216 // Don't let squigglies try to draw beyond the end of the text
217 if (text
.length() < start
+ length
) {
218 length
= text
.length() - start
;
221 this.getAnnotationModel().addAnnotation(
222 new ErrorAnnotation(0, "Error parsing string '" + text
+ "'"),
223 new Position(start
, length
));
227 public void setShowSecError(Taxon taxon
) {
229 // If taxon has no sec, show an annotation
230 if (taxon
.getSec() == null) {
232 String text
= "This taxon requires a sec. reference.";
234 IAnnotationModel model
= this.getAnnotationModel();
235 this.getAnnotationModel().addAnnotation(
236 new ErrorAnnotation(0, text
),
238 // new Position(0, getTextWidget().getText().length()));
242 private void testMarkers() {
243 // Successful test of markers which show up in ProblemView
245 IWorkspaceRunnable editorMarker
= new IWorkspaceRunnable() {
246 public void run(IProgressMonitor monitor
) throws CoreException
{
247 IMarker marker
= ResourcesPlugin
.getWorkspace().getRoot().createMarker(IMarker
.PROBLEM
);
248 marker
.setAttribute(IMarker
.MESSAGE
, "Testeroo");
249 marker
.setAttribute(IMarker
.PRIORITY
, IMarker
.PRIORITY_HIGH
);
250 marker
.setAttribute(IMarker
.LINE_NUMBER
, 1);
251 marker
.setAttribute(IMarker
.TRANSIENT
, false);
252 marker
.setAttribute(IMarker
.SEVERITY
, IMarker
.SEVERITY_ERROR
);
255 ResourcesPlugin
.getWorkspace().run(editorMarker
, null);
257 } catch (CoreException e
) {
262 public void setText(String text
) {
267 Assert
.isNotNull(text
);
268 // TODO figure out why getTextWidget() returns null!
269 if (this.getTextWidget() == null) {
272 Assert
.isNotNull(this.getTextWidget());
273 this.getTextWidget().setText(text
);
274 } catch (RuntimeException e
) {
279 public void setCursorToEOL() {
280 getTextWidget().setCaretOffset(getTextWidget().getText().length());
281 getTextWidget().setFocus();
284 public void createUndoSupport(IEditorSite editorSite
) {
286 IUndoManager undoManager
= new TextViewerUndoManager(25);
287 this.setUndoManager(undoManager
);
288 undoManager
.connect(this);
290 // IUndoContext workbenchUndoContext = UiUtil.getWorkbenchUndoContext();
292 IUndoContext workbenchUndoContext
= ((IUndoManagerExtension
)undoManager
).getUndoContext();
294 OperationHistoryActionHandler undoAction
= new UndoActionHandler(editorSite
, workbenchUndoContext
);
295 // undoAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.UNDO);
296 undoAction
.setActionDefinitionId(ActionFactory
.UNDO
.getId());
298 // Create the redo action.
299 OperationHistoryActionHandler redoAction
= new RedoActionHandler(editorSite
, workbenchUndoContext
);
300 // redoAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.REDO);
301 undoAction
.setActionDefinitionId(ActionFactory
.REDO
.getId());
303 IActionBars actionBars
= editorSite
.getActionBars();
304 if (actionBars
!= null) {
305 // actionBars.setGlobalActionHandler(IWorkbenchActionDefinitionIds.UNDO, undoAction);
306 // actionBars.setGlobalActionHandler(IWorkbenchActionDefinitionIds.REDO, redoAction);
307 actionBars
.setGlobalActionHandler(ActionFactory
.UNDO
.getId(), undoAction
);
308 actionBars
.setGlobalActionHandler(ActionFactory
.REDO
.getId(), redoAction
);
312 // actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), undoAction);
313 // actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), redoAction);
316 public void createUndoSupport_(IEditorSite editorSite
) {
317 IUndoManager undoManager
= new TextViewerUndoManager(25);
318 this.setUndoManager(undoManager
);
319 undoManager
.connect(this);
320 IUndoContext undoContext
;
321 if (undoManager
instanceof IUndoManagerExtension
) {
322 undoContext
= ((IUndoManagerExtension
)undoManager
).getUndoContext();
324 OperationHistoryActionHandler undoAction
= new UndoActionHandler(editorSite
, undoContext
);
325 // PlatformUI.getWorkbench().getHelpSystem().setHelp(undoAction, IAbstractTextEditorHelpContextIds.UNDO_ACTION);
326 undoAction
.setActionDefinitionId(IWorkbenchActionDefinitionIds
.UNDO
);
328 // Create the redo action.
329 OperationHistoryActionHandler redoAction
= new RedoActionHandler(editorSite
, undoContext
);
330 // PlatformUI.getWorkbench().getHelpSystem().setHelp(redoAction, IAbstractTextEditorHelpContextIds.REDO_ACTION);
331 redoAction
.setActionDefinitionId(IWorkbenchActionDefinitionIds
.REDO
);
334 IActionBars actionBars
= editorSite
.getActionBars();
335 if (actionBars
!= null) {
336 actionBars
.setGlobalActionHandler(IWorkbenchActionDefinitionIds
.UNDO
, undoAction
);
337 actionBars
.setGlobalActionHandler(IWorkbenchActionDefinitionIds
.REDO
, redoAction
);