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
.container
;
12 import java
.util
.Iterator
;
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
;
47 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
48 import eu
.etaxonomy
.taxeditor
.editor
.ViewerConfiguration
;
49 import eu
.etaxonomy
.taxeditor
.editor
.name
.container
.EditorAnnotation
.EditorAnnotationType
;
50 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
53 * SourceViewer implementation called by NameComposite.
59 public class NameViewer
extends SourceViewer
{
61 /** Constant <code>RULER_WIDTH=16</code> */
62 public static final int RULER_WIDTH
= 16;
64 private final IVerticalRuler ruler
;
65 private final AnnotationModel annotationModel
;
68 * <p>Constructor for NameViewer.</p>
70 * @param parent a {@link org.eclipse.swt.widgets.Composite} object.
72 public NameViewer(Composite parent
) {
73 super(parent
, new RulerWithIcon(RULER_WIDTH
), SWT
.WRAP
| SWT
.MULTI
| SWT
.RESIZE
);
75 this.ruler
= getVerticalRuler();
77 setBackground(EditorUtil
.getColor(Resources
.COLOR_COMPOSITE_BACKGROUND
));
79 Canvas canvas
= (Canvas
) getControl();
80 TableWrapLayout layout
= (TableWrapLayout
) canvas
.getLayout();
82 layout
.rightMargin
= 0;
83 layout
.bottomMargin
= 0;
84 layout
.leftMargin
= 0;
85 layout
.verticalSpacing
= 10;
86 layout
.horizontalSpacing
= 0;
88 canvas
.setLayout(layout
);
90 // Lay out the viewer's widgets
91 TableWrapData twdata
= new TableWrapData(TableWrapData
.FILL_GRAB
);
92 getControl().setLayoutData(twdata
);
94 twdata
= new TableWrapData(TableWrapData
.FILL_GRAB
);
95 getTextWidget().setLayoutData(twdata
);
96 getTextWidget().setLineSpacing(5);
98 // Default implementation adds ruler after the text widget, then
99 // uses a custom layout to display it before the text widget
100 ruler
.getControl().moveAbove(getTextWidget());
102 annotationModel
= new AnnotationModel();
103 this.setDocument(new Document(""), annotationModel
); //$NON-NLS-1$
105 createAnnotationPainter();
107 this.configure(new ViewerConfiguration());
109 // setDecorationSupport();
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));
120 * <p>setBackground</p>
122 * @param color a {@link org.eclipse.swt.graphics.Color} object.
124 public void setBackground(Color color
) {
126 // Set background color of ruler
127 ruler
.getControl().setBackground(color
);
129 // Set background color of text widget
130 getTextWidget().setBackground(color
);
132 // Set background color of strip between ruler and textWidget
133 getTextWidget().getParent().setBackground(color
);
137 * <p>getRulerControl</p>
139 * @return a {@link org.eclipse.swt.widgets.Control} object.
141 public Control
getRulerControl() {
142 return ruler
.getControl();
148 * @param icon a {@link org.eclipse.swt.graphics.Image} object.
150 public void setIcon(Image icon
) {
151 if (ruler
instanceof RulerWithIcon
) {
152 ((RulerWithIcon
) ruler
).setIcon(icon
);
154 throw new IllegalStateException("Viewer's IVerticalRuler is not an instance of RulerWithIcon."); //$NON-NLS-1$
159 * @see org.eclipse.jface.text.source.SourceViewer#createLayout()
162 * <p>createLayout</p>
164 * @return a {@link org.eclipse.swt.widgets.Layout} object.
167 protected Layout
createLayout() {
168 TableWrapLayout layout
= new TableWrapLayout();
169 layout
.numColumns
= 2;
170 layout
.verticalSpacing
= 0;
174 private void createAnnotationPainter(){
175 // Annotations section
176 IAnnotationAccess fAnnotationAccess
= new DefaultMarkerAnnotationAccess();
178 // To paint the annotations
179 AnnotationPainter annotationPainter
= new AnnotationPainter(this, fAnnotationAccess
);
181 // Default SquigglesStrategy doesn't recognize line wraps
182 annotationPainter
.addDrawingStrategy(LineWrapSquigglesStrategy
.ID
, new LineWrapSquigglesStrategy());
184 // Add ability to paint red squigglies
185 annotationPainter
.addAnnotationType(EditorAnnotationType
.ERROR
.name(), LineWrapSquigglesStrategy
.ID
);
186 annotationPainter
.setAnnotationTypeColor(EditorAnnotationType
.ERROR
.image
,
187 new Color(Display
.getDefault(), EditorAnnotationType
.ERROR
.color
));
189 // Add ability to paint yellow squigglies
190 annotationPainter
.addAnnotationType(EditorAnnotationType
.WARNING
.name(), LineWrapSquigglesStrategy
.ID
);
191 annotationPainter
.setAnnotationTypeColor(EditorAnnotationType
.WARNING
.image
,
192 new Color(Display
.getDefault(), EditorAnnotationType
.WARNING
.color
));
194 this.addPainter(annotationPainter
);
199 * <p>clearAnnotations</p>
201 public void clearAnnotations() {
202 Iterator
<Annotation
> annotations
= this.getAnnotationModel().getAnnotationIterator();
203 while (annotations
.hasNext()) {
204 Annotation annotation
= annotations
.next();
205 this.getAnnotationModel().removeAnnotation(annotation
);
210 * If <code>name.hasProblem()</code> is <code>true</code>, underlines section
211 * of text bounded by <code>name.getProblemStarts()</code> and
212 * <code>name.getProblemEnds()</code>.
214 * @param name a {@link eu.etaxonomy.cdm.model.name.TaxonNameBase} object.
216 // public void setShowParsingErrors(TaxonNameBase<?, ?> name) {
218 // String text = this.getTextWidget().getText();
220 // if (name.hasProblem() && text.length() > 0) {
221 // int start = name.getProblemStarts();
222 // int length = name.getProblemEnds() - start;
224 // if (start == -1 || name.getProblemEnds() == -1) {
228 // // Don't let squigglies try to draw beyond the end of the text
229 // if (text.length() < start + length) {
230 // length = text.length() - start;
233 // for (ParserProblem problem : name.getParsingProblems()) {
235 // String type = null;
236 // if (problem.isWarning()) {
237 // type = EditorAnnotation.WARNING_TYPE;
239 // if (problem.isError()) {
240 // type = EditorAnnotation.ERROR_TYPE;
242 // if (type == null) {
245 // this.getAnnotationModel().addAnnotation(
246 // new EditorAnnotation(type, 0, problem.getMessage()),
247 // new Position(start, length));
253 public void addAnnotation(EditorAnnotation annotation
){
254 addAnnotation(annotation
, null);
257 public void addAnnotation(EditorAnnotation annotation
, Position position
){
258 if(position
== null){
259 position
= new Position(0, 0);
262 this.getAnnotationModel().addAnnotation(annotation
, position
);
268 * @param text a {@link java.lang.String} object.
270 public void setText(String text
) {
272 text
= ""; //$NON-NLS-1$
275 Assert
.isNotNull(text
);
276 // TODO figure out why getTextWidget() returns null!
277 if (this.getTextWidget() == null) {
280 Assert
.isNotNull(this.getTextWidget());
281 this.getTextWidget().setText(text
);
282 } catch (RuntimeException e
) {
290 * @param menu a {@link org.eclipse.swt.widgets.Menu} object.
292 public void setMenu(Menu menu
) {
293 getRulerControl().setMenu(menu
);
294 getTextWidget().setMenu(menu
);
298 * <p>setCursorToEOL</p>
300 public void setCursorToEOL() {
301 getTextWidget().setCaretOffset(getTextWidget().getText().length());
302 getTextWidget().setFocus();
307 * <p>getCursorPosition</p>
311 public int getCursorPosition(){
312 return getTextWidget().getCaretOffset();
316 * <p>setCursorPosition</p>
318 * @param offset a int.
320 public void setCursorPosition(int offset
){
322 getTextWidget().setCaretOffset(offset
);
323 }catch(IllegalArgumentException e
){
329 * <p>createUndoSupport</p>
331 * @param editorSite a {@link org.eclipse.ui.IEditorSite} object.
333 public void createUndoSupport(IEditorSite editorSite
) {
335 IUndoManager undoManager
= new TextViewerUndoManager(25);
336 this.setUndoManager(undoManager
);
337 undoManager
.connect(this);
339 // IUndoContext workbenchUndoContext = UiUtil.getWorkbenchUndoContext();
341 IUndoContext workbenchUndoContext
= ((IUndoManagerExtension
)undoManager
).getUndoContext();
343 OperationHistoryActionHandler undoAction
= new UndoActionHandler(editorSite
, workbenchUndoContext
);
344 // undoAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.UNDO);
345 undoAction
.setActionDefinitionId(ActionFactory
.UNDO
.getId());
347 // Create the redo action.
348 OperationHistoryActionHandler redoAction
= new RedoActionHandler(editorSite
, workbenchUndoContext
);
349 // redoAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.REDO);
350 undoAction
.setActionDefinitionId(ActionFactory
.REDO
.getId());
352 IActionBars actionBars
= editorSite
.getActionBars();
353 if (actionBars
!= null) {
354 // actionBars.setGlobalActionHandler(IWorkbenchActionDefinitionIds.UNDO, undoAction);
355 // actionBars.setGlobalActionHandler(IWorkbenchActionDefinitionIds.REDO, redoAction);
356 actionBars
.setGlobalActionHandler(ActionFactory
.UNDO
.getId(), undoAction
);
357 actionBars
.setGlobalActionHandler(ActionFactory
.REDO
.getId(), redoAction
);
361 // actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), undoAction);
362 // actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), redoAction);
366 * <p>createUndoSupport_</p>
368 * @param editorSite a {@link org.eclipse.ui.IEditorSite} object.
370 public void createUndoSupport_(IEditorSite editorSite
) {
371 IUndoManager undoManager
= new TextViewerUndoManager(25);
372 this.setUndoManager(undoManager
);
373 undoManager
.connect(this);
374 IUndoContext undoContext
;
375 if (undoManager
instanceof IUndoManagerExtension
) {
376 undoContext
= ((IUndoManagerExtension
)undoManager
).getUndoContext();
378 OperationHistoryActionHandler undoAction
= new UndoActionHandler(editorSite
, undoContext
);
379 // PlatformUI.getWorkbench().getHelpSystem().setHelp(undoAction, IAbstractTextEditorHelpContextIds.UNDO_ACTION);
380 undoAction
.setActionDefinitionId(IWorkbenchActionDefinitionIds
.UNDO
);
382 // Create the redo action.
383 OperationHistoryActionHandler redoAction
= new RedoActionHandler(editorSite
, undoContext
);
384 // PlatformUI.getWorkbench().getHelpSystem().setHelp(redoAction, IAbstractTextEditorHelpContextIds.REDO_ACTION);
385 redoAction
.setActionDefinitionId(IWorkbenchActionDefinitionIds
.REDO
);
388 IActionBars actionBars
= editorSite
.getActionBars();
389 if (actionBars
!= null) {
390 actionBars
.setGlobalActionHandler(IWorkbenchActionDefinitionIds
.UNDO
, undoAction
);
391 actionBars
.setGlobalActionHandler(IWorkbenchActionDefinitionIds
.REDO
, redoAction
);