Doubtless successful attempt to sync with Niels.
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / editor / name / NameViewer.java
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;
11
12 import java.util.Iterator;
13
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;
46
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;
54
55
56 /**
57 * SourceViewer implementation called by NameComposite.
58 *
59 * @author p.ciardelli
60 * @created 27.05.2008
61 * @version 1.0
62 */
63 public class NameViewer extends SourceViewer {
64 private static final Logger logger = Logger
65 .getLogger(NameViewer.class);
66
67 public NameViewer(Composite parent) {
68 super(parent, null, SWT.WRAP | SWT.MULTI | SWT.RESIZE);
69
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);
75
76 // Lay out StyledText
77 StyledText textWidget = this.getTextWidget();
78 textWidget.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB));
79
80 // Annotations section
81 IAnnotationAccess fAnnotationAccess = new AnnotationMarkerAccess();
82
83 // To paint the annotations
84 AnnotationPainter annotationPainter = new AnnotationPainter(this, fAnnotationAccess);
85
86 // Default SquigglesStrategy doesn't recognize line wraps
87 annotationPainter.addDrawingStrategy(LineWrapSquigglesStrategy.ID, new LineWrapSquigglesStrategy());
88
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));
93
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));
98
99 this.addPainter(annotationPainter);
100
101 // DefaultAnnotationHover hover = new DefaultAnnotationHover();
102 //
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());
108
109
110 // DocumentUndoManagerRegistry.connect(this.getDocument());
111 // IDocumentUndoManager docUndoManager = DocumentUndoManagerRegistry.getDocumentUndoManager(this.getDocument());
112
113 /**
114 getEditorSite().getActionBars().setGlobalActionHandler(ActionFactory.UNDO.getId(), new UndoActionHandler(getSite(), undoContext));
115 getEditorSite().getActionBars().setGlobalActionHandler(ActionFactory.REDO.getId(), new RedoActionHandler(getSite(), undoContext));
116 **/
117
118
119 }
120
121 /**
122 * If <code>hasProblem</code> is <code>true</code>, underlines
123 * entire contents of text widget in red squigglies.
124 *
125 * @param hasProblem
126 */
127 public void setShowError(boolean hasProblem) {
128
129 String text = this.getTextWidget().getText();
130
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);
136 }
137
138 if (hasProblem && text.length() > 0) {
139 this.getAnnotationModel().addAnnotation(
140 new ErrorAnnotation(0, text),
141 new Position(0, text.length()));
142 }
143 }
144
145 /**
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>.
149 *
150 * @param name
151 */
152 public void setShowError(TaxonNameBase name) {
153
154 boolean hasProblem = name.getHasProblem();
155
156 if (!hasProblem) {
157 logger.warn(name.getProblemStarts());
158 }
159
160 String text = this.getTextWidget().getText();
161
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);
167 }
168
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;
173
174 if (start == -1 || name.getProblemEnds() == -1) {
175 // logger.warn("Start: " + start + ", End " + name.getProblemEnds());
176 return;
177 }
178
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;
182 }
183
184 this.getAnnotationModel().addAnnotation(
185 new ErrorAnnotation(0, text),
186 new Position(start, length));
187
188 // Successful test of markers which show up in ProblemView
189 // try {
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);
198 // }
199 // };
200 // ResourcesPlugin.getWorkspace().run(editorMarker, null);
201 //
202 // } catch (CoreException e) {
203 // // TODO Auto-generated catch block
204 // e.printStackTrace();
205 // }
206
207 }
208 }
209
210 public void addLineBreakListener(LineBreakListener lineBreakListener) {
211 this.getTextWidget().addVerifyListener(lineBreakListener);
212 }
213
214 public void removeLineBreakListener(LineBreakListener lineBreakListener) {
215 this.getTextWidget().removeVerifyListener(lineBreakListener);
216 }
217
218 public void addParseListener(ParseListener parseListener) {
219 this.getTextWidget().addModifyListener(parseListener);
220 }
221
222 public void removeParseListener(ParseListener parseListener) {
223 this.getTextWidget().removeModifyListener(parseListener);
224 }
225
226 public void setText(String text) {
227 if (text == null) {
228 text = "";
229 }
230 try {
231 Assert.isNotNull(text);
232 // TODO figure out why getTextWidget() returns null!
233 if (this.getTextWidget() == null) {
234 return;
235 }
236 Assert.isNotNull(this.getTextWidget());
237 this.getTextWidget().setText(text);
238 } catch (RuntimeException e) {
239 // TODO Auto-generated catch block
240 e.printStackTrace();
241 }
242 }
243
244 public void setCursorToEOL() {
245 this.getTextWidget().setCaretOffset(getTextWidget().getText().length());
246 }
247
248 class NameViewerDocument extends Document {
249 public NameViewerDocument(String string) {
250 super(string);
251 }
252
253 public ILineTracker getTracker() {
254 return super.getTracker();
255 }
256 }
257
258 public void createUndoSupport(IEditorSite editorSite) {
259
260 IUndoManager undoManager = new TextViewerUndoManager(25);
261 this.setUndoManager(undoManager);
262 undoManager.connect(this);
263
264 // IUndoContext workbenchUndoContext = UiUtil.getWorkbenchUndoContext();
265
266 IUndoContext workbenchUndoContext = ((IUndoManagerExtension)undoManager).getUndoContext();
267
268 OperationHistoryActionHandler undoAction = new UndoActionHandler(editorSite, workbenchUndoContext);
269 // undoAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.UNDO);
270 undoAction.setActionDefinitionId(ActionFactory.UNDO.getId());
271
272 // Create the redo action.
273 OperationHistoryActionHandler redoAction = new RedoActionHandler(editorSite, workbenchUndoContext);
274 // redoAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.REDO);
275 undoAction.setActionDefinitionId(ActionFactory.REDO.getId());
276
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);
283
284 }
285
286 // actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), undoAction);
287 // actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), redoAction);
288 }
289
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();
297
298 OperationHistoryActionHandler undoAction = new UndoActionHandler(editorSite, undoContext);
299 // PlatformUI.getWorkbench().getHelpSystem().setHelp(undoAction, IAbstractTextEditorHelpContextIds.UNDO_ACTION);
300 undoAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.UNDO);
301
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);
306
307
308 IActionBars actionBars = editorSite.getActionBars();
309 if (actionBars != null) {
310 actionBars.setGlobalActionHandler(IWorkbenchActionDefinitionIds.UNDO, undoAction);
311 actionBars.setGlobalActionHandler(IWorkbenchActionDefinitionIds.REDO, redoAction);
312
313 }
314 }
315 }
316
317 // public boolean isMultiLine(Annotation annotation) {
318 // return true;
319 // }
320 }