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
.ILineTracker
;
24 import org
.eclipse
.jface
.text
.IUndoManager
;
25 import org
.eclipse
.jface
.text
.IUndoManagerExtension
;
26 import org
.eclipse
.jface
.text
.Position
;
27 import org
.eclipse
.jface
.text
.TextViewerUndoManager
;
28 import org
.eclipse
.jface
.text
.source
.Annotation
;
29 import org
.eclipse
.jface
.text
.source
.AnnotationModel
;
30 import org
.eclipse
.jface
.text
.source
.AnnotationPainter
;
31 import org
.eclipse
.jface
.text
.source
.IAnnotationAccess
;
32 import org
.eclipse
.jface
.text
.source
.SourceViewer
;
33 import org
.eclipse
.swt
.SWT
;
34 import org
.eclipse
.swt
.custom
.StyledText
;
35 import org
.eclipse
.swt
.graphics
.Color
;
36 import org
.eclipse
.swt
.widgets
.Composite
;
37 import org
.eclipse
.swt
.widgets
.Display
;
38 import org
.eclipse
.ui
.IActionBars
;
39 import org
.eclipse
.ui
.IEditorSite
;
40 import org
.eclipse
.ui
.actions
.ActionFactory
;
41 import org
.eclipse
.ui
.forms
.widgets
.TableWrapData
;
42 import org
.eclipse
.ui
.operations
.OperationHistoryActionHandler
;
43 import org
.eclipse
.ui
.operations
.RedoActionHandler
;
44 import org
.eclipse
.ui
.operations
.UndoActionHandler
;
45 import org
.eclipse
.ui
.texteditor
.IWorkbenchActionDefinitionIds
;
47 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
48 import eu
.etaxonomy
.taxeditor
.editor
.AnnotationMarkerAccess
;
49 import eu
.etaxonomy
.taxeditor
.editor
.ErrorAnnotation
;
50 import eu
.etaxonomy
.taxeditor
.editor
.LineBreakListener
;
51 import eu
.etaxonomy
.taxeditor
.editor
.LineWrapSquigglesStrategy
;
52 import eu
.etaxonomy
.taxeditor
.editor
.ParseListener
;
53 import eu
.etaxonomy
.taxeditor
.editor
.WarningAnnotation
;
57 * SourceViewer implementation called by NameComposite.
63 public class NameViewer
extends SourceViewer
{
64 private static final Logger logger
= Logger
65 .getLogger(NameViewer
.class);
67 public NameViewer(Composite parent
) {
68 super(parent
, null, SWT
.WRAP
| SWT
.MULTI
| SWT
.RESIZE
);
70 // Set name viewer's text to name cache
71 AnnotationModel model
= new AnnotationModel();
72 NameViewerDocument document
= new NameViewerDocument("");
73 this.setDocument(document
, model
);
74 // model.connect(document);
77 StyledText textWidget
= this.getTextWidget();
78 textWidget
.setLayoutData(new TableWrapData(TableWrapData
.FILL_GRAB
));
80 // Annotations section
81 IAnnotationAccess fAnnotationAccess
= new AnnotationMarkerAccess();
83 // To paint the annotations
84 AnnotationPainter annotationPainter
= new AnnotationPainter(this, fAnnotationAccess
);
86 // Default SquigglesStrategy doesn't recognize line wraps
87 annotationPainter
.addDrawingStrategy(LineWrapSquigglesStrategy
.ID
, new LineWrapSquigglesStrategy());
89 // Add ability to paint red squigglies
90 annotationPainter
.addAnnotationType(ErrorAnnotation
.ERROR_TYPE
, LineWrapSquigglesStrategy
.ID
);
91 annotationPainter
.setAnnotationTypeColor(ErrorAnnotation
.ERROR_TYPE
,
92 new Color(Display
.getDefault(), ErrorAnnotation
.ERROR_RGB
));
94 // Add ability to paint yellow squigglies
95 annotationPainter
.addAnnotationType(WarningAnnotation
.WARNING_TYPE
, LineWrapSquigglesStrategy
.ID
);
96 annotationPainter
.setAnnotationTypeColor(WarningAnnotation
.WARNING_TYPE
,
97 new Color(Display
.getDefault(), WarningAnnotation
.WARNING_RGB
));
99 this.addPainter(annotationPainter
);
101 // DefaultAnnotationHover hover = new DefaultAnnotationHover();
103 // CompositeRuler fCompositeRuler = new CompositeRuler();
104 // AnnotationRulerColumn annotationCol = new AnnotationRulerColumn(model, 10);
105 // AnnotationBarHoverManager fAnnotationHoverManager = new AnnotationBarHoverManager(annotationCol, this,
106 // new DefaultAnnotationHover(), fHoverControlCreator);
107 // fAnnotationHoverManager.install(annotationRuler.getControl());
110 // DocumentUndoManagerRegistry.connect(this.getDocument());
111 // IDocumentUndoManager docUndoManager = DocumentUndoManagerRegistry.getDocumentUndoManager(this.getDocument());
114 getEditorSite().getActionBars().setGlobalActionHandler(ActionFactory.UNDO.getId(), new UndoActionHandler(getSite(), undoContext));
115 getEditorSite().getActionBars().setGlobalActionHandler(ActionFactory.REDO.getId(), new RedoActionHandler(getSite(), undoContext));
122 * If <code>hasProblem</code> is <code>true</code>, underlines
123 * entire contents of text widget in red squigglies.
127 public void setShowError(boolean hasProblem
) {
129 String text
= this.getTextWidget().getText();
131 Iterator
<Annotation
> annotations
= this.getAnnotationModel().getAnnotationIterator();
132 while (annotations
.hasNext()) {
133 Annotation annotation
= annotations
.next();
134 if (annotation
.getType().equals(ErrorAnnotation
.ERROR_TYPE
))
135 this.getAnnotationModel().removeAnnotation(annotation
);
138 if (hasProblem
&& text
.length() > 0) {
139 this.getAnnotationModel().addAnnotation(
140 new ErrorAnnotation(0, text
),
141 new Position(0, text
.length()));
146 * If <code>name.hasProblem()</code> is <code>true</code>, underlines section
147 * of text bounded by <code>name.getProblemStarts()</code> and
148 * <code>name.getProblemEnds()</code>.
152 public void setShowError(TaxonNameBase name
) {
154 boolean hasProblem
= name
.getHasProblem();
157 logger
.warn(name
.getProblemStarts());
160 String text
= this.getTextWidget().getText();
162 Iterator
<Annotation
> annotations
= this.getAnnotationModel().getAnnotationIterator();
163 while (annotations
.hasNext()) {
164 Annotation annotation
= annotations
.next();
165 if (annotation
.getType().equals(ErrorAnnotation
.ERROR_TYPE
))
166 this.getAnnotationModel().removeAnnotation(annotation
);
169 if (hasProblem
&& text
.length() > 0) {
170 // logger.warn("Name: " + name.getTitleCache() + ", start: " + name.getProblemStarts()+ ", end: " + name.getProblemEnds());
171 int start
= name
.getProblemStarts();
172 int length
= name
.getProblemEnds() - start
;
174 if (start
== -1 || name
.getProblemEnds() == -1) {
175 // logger.warn("Start: " + start + ", End " + name.getProblemEnds());
179 // Don't let squigglies try to draw beyond the end of the text
180 if (text
.length() < start
+ length
) {
181 length
= text
.length() - start
;
184 this.getAnnotationModel().addAnnotation(
185 new ErrorAnnotation(0, text
),
186 new Position(start
, length
));
188 // Successful test of markers which show up in ProblemView
190 // IWorkspaceRunnable editorMarker = new IWorkspaceRunnable() {
191 // public void run(IProgressMonitor monitor) throws CoreException {
192 // IMarker marker = ResourcesPlugin.getWorkspace().getRoot().createMarker(IMarker.PROBLEM);
193 // marker.setAttribute(IMarker.MESSAGE, "Testeroo");
194 // marker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_HIGH);
195 // marker.setAttribute(IMarker.LINE_NUMBER, 1);
196 // marker.setAttribute(IMarker.TRANSIENT, false);
197 // marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
200 // ResourcesPlugin.getWorkspace().run(editorMarker, null);
202 // } catch (CoreException e) {
203 // // TODO Auto-generated catch block
204 // e.printStackTrace();
210 public void addLineBreakListener(LineBreakListener lineBreakListener
) {
211 this.getTextWidget().addVerifyListener(lineBreakListener
);
214 public void removeLineBreakListener(LineBreakListener lineBreakListener
) {
215 this.getTextWidget().removeVerifyListener(lineBreakListener
);
218 public void addParseListener(ParseListener parseListener
) {
219 this.getTextWidget().addModifyListener(parseListener
);
222 public void removeParseListener(ParseListener parseListener
) {
223 this.getTextWidget().removeModifyListener(parseListener
);
226 public void setText(String text
) {
231 Assert
.isNotNull(text
);
232 // TODO figure out why getTextWidget() returns null!
233 if (this.getTextWidget() == null) {
236 Assert
.isNotNull(this.getTextWidget());
237 this.getTextWidget().setText(text
);
238 } catch (RuntimeException e
) {
239 // TODO Auto-generated catch block
244 public void setCursorToEOL() {
245 this.getTextWidget().setCaretOffset(getTextWidget().getText().length());
248 class NameViewerDocument
extends Document
{
249 public NameViewerDocument(String string
) {
253 public ILineTracker
getTracker() {
254 return super.getTracker();
258 public void createUndoSupport(IEditorSite editorSite
) {
260 IUndoManager undoManager
= new TextViewerUndoManager(25);
261 this.setUndoManager(undoManager
);
262 undoManager
.connect(this);
264 // IUndoContext workbenchUndoContext = UiUtil.getWorkbenchUndoContext();
266 IUndoContext workbenchUndoContext
= ((IUndoManagerExtension
)undoManager
).getUndoContext();
268 OperationHistoryActionHandler undoAction
= new UndoActionHandler(editorSite
, workbenchUndoContext
);
269 // undoAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.UNDO);
270 undoAction
.setActionDefinitionId(ActionFactory
.UNDO
.getId());
272 // Create the redo action.
273 OperationHistoryActionHandler redoAction
= new RedoActionHandler(editorSite
, workbenchUndoContext
);
274 // redoAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.REDO);
275 undoAction
.setActionDefinitionId(ActionFactory
.REDO
.getId());
277 IActionBars actionBars
= editorSite
.getActionBars();
278 if (actionBars
!= null) {
279 // actionBars.setGlobalActionHandler(IWorkbenchActionDefinitionIds.UNDO, undoAction);
280 // actionBars.setGlobalActionHandler(IWorkbenchActionDefinitionIds.REDO, redoAction);
281 actionBars
.setGlobalActionHandler(ActionFactory
.UNDO
.getId(), undoAction
);
282 actionBars
.setGlobalActionHandler(ActionFactory
.REDO
.getId(), redoAction
);
286 // actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), undoAction);
287 // actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), redoAction);
290 public void createUndoSupport_(IEditorSite editorSite
) {
291 IUndoManager undoManager
= new TextViewerUndoManager(25);
292 this.setUndoManager(undoManager
);
293 undoManager
.connect(this);
294 IUndoContext undoContext
;
295 if (undoManager
instanceof IUndoManagerExtension
) {
296 undoContext
= ((IUndoManagerExtension
)undoManager
).getUndoContext();
298 OperationHistoryActionHandler undoAction
= new UndoActionHandler(editorSite
, undoContext
);
299 // PlatformUI.getWorkbench().getHelpSystem().setHelp(undoAction, IAbstractTextEditorHelpContextIds.UNDO_ACTION);
300 undoAction
.setActionDefinitionId(IWorkbenchActionDefinitionIds
.UNDO
);
302 // Create the redo action.
303 OperationHistoryActionHandler redoAction
= new RedoActionHandler(editorSite
, undoContext
);
304 // PlatformUI.getWorkbench().getHelpSystem().setHelp(redoAction, IAbstractTextEditorHelpContextIds.REDO_ACTION);
305 redoAction
.setActionDefinitionId(IWorkbenchActionDefinitionIds
.REDO
);
308 IActionBars actionBars
= editorSite
.getActionBars();
309 if (actionBars
!= null) {
310 actionBars
.setGlobalActionHandler(IWorkbenchActionDefinitionIds
.UNDO
, undoAction
);
311 actionBars
.setGlobalActionHandler(IWorkbenchActionDefinitionIds
.REDO
, redoAction
);
317 // public boolean isMultiLine(Annotation annotation) {