3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.annotatedlineeditor
;
12 import java
.util
.Iterator
;
13 import java
.util
.List
;
15 import org
.eclipse
.core
.runtime
.Assert
;
16 import org
.eclipse
.core
.runtime
.CoreException
;
17 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
18 import org
.eclipse
.jface
.operation
.IRunnableContext
;
19 import org
.eclipse
.jface
.text
.BadLocationException
;
20 import org
.eclipse
.jface
.text
.Document
;
21 import org
.eclipse
.jface
.text
.IDocument
;
22 import org
.eclipse
.jface
.text
.Position
;
23 import org
.eclipse
.jface
.text
.source
.Annotation
;
24 import org
.eclipse
.jface
.text
.source
.IAnnotationModel
;
25 import org
.eclipse
.ui
.IEditorInput
;
26 import org
.eclipse
.ui
.texteditor
.AbstractDocumentProvider
;
28 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.AbstractBulkEditorInput
;
29 import eu
.etaxonomy
.taxeditor
.dataimport
.DerivedUnitEditorInput
;
30 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
35 * Using an <code>IEditorPart</code>, creates a document where each line is associated
36 * with an entity. Mapping between document positions and entities is stored in
37 * <code>LineAnnotation</code>s in a <code>LineAnnotationModel</code>.
41 * <li>an <code>IEntityCreator</code> to create entities for new lines;
42 * <li>an <code>IEntityPersistenceService</code> for interacting with the persistence layer; and
43 * <li>an <code>ILineDisplayStrategy</code> for various visual manifestations of the domain object.
51 public class AnnotatedLineDocumentProvider
extends AbstractDocumentProvider
{
53 private IDocument document
;
55 private LineAnnotationModel annotationModel
;
57 private IEntityCreator
<?
> entityCreator
;
59 private ILineDisplayStrategy lineDisplayStrategy
;
61 private IEditorInput input
;
64 * <p>Constructor for AnnotatedLineDocumentProvider.</p>
66 * @param input a {@link org.eclipse.ui.IEditorInput} object.
68 public AnnotatedLineDocumentProvider(IEditorInput input
){
74 public IAnnotationModel
getAnnotationModel(Object element
) {
75 if (element
== input
) {
77 // Create model as necessary
78 if (annotationModel
== null) {
79 annotationModel
= new LineAnnotationModel(getLineDisplayStrategy(element
));
80 annotationModel
.setEntityCreator(getEntityCreator(input
));
82 return annotationModel
;
90 protected IAnnotationModel
createAnnotationModel(Object element
)
91 throws CoreException
{
92 return getAnnotationModel(element
);
97 public IDocument
getDocument(Object element
) {
103 protected IDocument
createDocument(Object element
) throws CoreException
{
105 if (element
instanceof IEditorInput
) {
106 IEditorInput input
= (IEditorInput
) element
;
107 document
= new Document("");
108 // IAnnotationModel model = getAnnotationModel(element);
110 List
<?
> entityList
= getEntityList(element
);
112 if(entityList
!= null){
113 for (Object entity
: entityList
) {
115 createAnnotatedLine(input
, entity
);
116 } catch (BadLocationException e
) {
117 AbstractUtility
.error(getClass(), "Problems creating annotated line: ", e
);
128 * Creates an annotated line at the end of the document associated with the element
130 * @throws org.eclipse.jface.text.BadLocationException if any.
131 * @param element a {@link java.lang.Object} object.
132 * @param entity a {@link java.lang.Object} object.
133 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
135 protected LineAnnotation
createAnnotatedLine(Object element
, Object entity
) throws BadLocationException
{
137 Document document
= (Document
) getDocument(element
);
138 ILineDisplayStrategy lineDisplayStrategy
= getLineDisplayStrategy(element
);
140 LineAnnotation annotation
= new LineAnnotation(entity
, lineDisplayStrategy
);
142 // Is document zero length, or is last char in document line delimiter?
143 int docLength
= document
.getLength();
144 boolean useDelimiter
= false;
146 if (docLength
> 1 && !document
.get(docLength
- 2, 2).equals(document
.getDefaultLineDelimiter())) {
151 document
.replace(docLength
, 0, document
.getDefaultLineDelimiter());
155 if (lineDisplayStrategy
.isEntityCacheEmpty(entity
)) {
156 text
= lineDisplayStrategy
.getEmptyCacheMessage(entity
);
158 text
= lineDisplayStrategy
.getText(entity
);
160 text
+= lineDisplayStrategy
.getSupplementalText(entity
);
161 docLength
= document
.getLength();
162 document
.replace(docLength
, 0, text
);
164 Position position
= new Position(docLength
, text
.length());
166 IAnnotationModel model
= getAnnotationModel(element
);
168 model
.addAnnotation(annotation
, position
);
171 // lineDisplayStrategy.addDisplayListener(entity,
172 // new EntityListenerImpl((LineAnnotation) annotation, element));
179 * <p>updateLineFromAnnotation</p>
181 * @param annotation a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
183 public void updateLineFromAnnotation(LineAnnotation annotation
) {
185 IAnnotationModel model
= getAnnotationModel(input
);
186 IDocument document
= getDocument(input
);
187 ILineDisplayStrategy lineDisplay
= getLineDisplayStrategy(input
);
189 Object entity
= annotation
.getEntity();
191 if (getLineDisplayStrategy(input
).isEntityCacheEmpty(entity
)) {
192 text
= lineDisplay
.getEmptyCacheMessage(entity
);
194 text
= lineDisplay
.getText(entity
);
196 text
+= lineDisplay
.getSupplementalText(entity
);
199 if (model
.getPosition(annotation
) == null) {
202 int offset
= model
.getPosition(annotation
).getOffset();
203 int line
= document
.getLineOfOffset(offset
);
204 int lineLength
= document
.getLineLength(document
.getLineOfOffset(offset
));
205 if (document
.getLineDelimiter(line
) != null) {
206 lineLength
-= document
.getLineDelimiter(line
).length();
208 document
.replace(offset
, lineLength
, text
);
209 } catch (BadLocationException e
) {
210 AbstractUtility
.error(getClass(), "Problem updating annotated line: " ,e
);
218 private List
<?
> getEntityList(Object element
) {
219 if (element
instanceof AbstractBulkEditorInput
) {
220 return ((AbstractBulkEditorInput
)element
).getModel();
222 else if(element
instanceof DerivedUnitEditorInput
){
223 return ((DerivedUnitEditorInput
) element
).getResults();
230 protected void doSaveDocument(IProgressMonitor monitor
, Object element
,
231 IDocument document
, boolean overwrite
) throws CoreException
{
232 if (element
instanceof AbstractBulkEditorInput
) {
234 IEntityPersistenceService persistenceService
= (AbstractBulkEditorInput
) element
;
236 // Get new containers from annotation model
237 LineAnnotationModel model
= (LineAnnotationModel
) getAnnotationModel(element
);
238 Iterator iterator
= model
.getAnnotationIterator();
239 while (iterator
.hasNext()) {
240 Annotation annotation
= (Annotation
) iterator
.next();
241 if (annotation
instanceof IEntityContainer
<?
>) {
242 IEntityContainer
<?
> container
= (IEntityContainer
<?
>) annotation
;
243 if (container
.isMarkedAsNew() || container
.isDirty()) {
244 persistenceService
.save(container
.getEntity()); // save
245 container
.setDirty(false);
246 container
.markAsNew(false);
250 for (LineAnnotation annotation
: model
.getDeletedAnnotations()) {
251 if (annotation
.isMarkedAsNew()) {
254 if (annotation
.isMarkedAsMerged()) {
255 persistenceService
.merge(annotation
.getEntity(), annotation
.getMergeTarget()); // merge
257 // TODO clarify w AM whether this needs to be executed on merged objects
258 //persistenceService.delete(annotation.getEntity()); // delete
261 model
.clearDeletedAnnotations();
267 protected IRunnableContext
getOperationRunner(IProgressMonitor monitor
) {
273 public boolean isModifiable(Object element
) {
279 public boolean isReadOnly(Object element
) {
280 // enables copy & paste
285 * <p>Getter for the field <code>entityCreator</code>.</p>
287 * @param element a {@link java.lang.Object} object.
288 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityCreator} object.
290 public IEntityCreator
<?
> getEntityCreator(IEditorInput input
) {
291 if (input
instanceof AbstractBulkEditorInput
) {
292 entityCreator
= ((AbstractBulkEditorInput
) input
).getEntityCreator();
294 return entityCreator
;
298 * <p>Setter for the field <code>lineDisplayStrategy</code>.</p>
300 * @param lineDisplayStrategy a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy} object.
301 * @param element a {@link java.lang.Object} object.
303 public void setLineDisplayStrategy(
304 ILineDisplayStrategy lineDisplayStrategy
, Object element
) {
305 if (element
instanceof IEditorInput
) {
306 this.lineDisplayStrategy
= lineDisplayStrategy
;
311 * <p>Getter for the field <code>lineDisplayStrategy</code>.</p>
313 * @param element a {@link java.lang.Object} object.
314 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy} object.
316 protected ILineDisplayStrategy
getLineDisplayStrategy(Object element
) {
317 if (element
instanceof IEditorInput
) {
318 Assert
.isNotNull(lineDisplayStrategy
, "No ILineDisplayStrategy set for this element.");
319 return lineDisplayStrategy
;
325 * <p>removeAnnotatedLine</p>
327 * @param entity a {@link java.lang.Object} object.
329 public void removeAnnotatedLine(Object entity
){
330 LineAnnotation annotation
= (LineAnnotation
) annotationModel
.getAnnotation(entity
);
331 removeAnnotatedLine(annotation
);
335 * <p>removeAnnotatedLine</p>
337 * @param lineno a int.
338 * @param element a {@link java.lang.Object} object.
340 public void removeAnnotatedLine(Object element
, int lineno
) {
341 LineAnnotation annotation
= (LineAnnotation
) annotationModel
.getAnnotationAtLine(lineno
, document
);
342 removeAnnotatedLine(annotation
);
346 * <p>removeAnnotatedLine</p>
348 * @param annotation a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
350 public void removeAnnotatedLine(LineAnnotation annotation
) {
351 if (annotation
!= null) {
352 Document document
= (Document
) getDocument(input
);
353 LineAnnotationModel model
= (LineAnnotationModel
) getAnnotationModel(input
);
355 Position position
= model
.getPosition(annotation
);
356 int offset
= position
.getOffset();
357 int length
= position
.getLength();
359 Object entity
= annotation
.getEntity();
360 annotation
.markAsDeleted();
361 model
.removeAnnotation(annotation
);
363 // Immediately followed by a delimiter?
364 int annotationEnd
= offset
+ length
;
366 if (document
.getLength() > annotationEnd
+ 1 && document
.get(annotationEnd
, 2).equals(document
.getDefaultLineDelimiter())) {
369 } catch (BadLocationException e1
) {
370 AbstractUtility
.error(getClass(), "Problems removing annotated line", e1
);
374 document
.replace(offset
, length
, "");
375 } catch (BadLocationException e
) {
376 AbstractUtility
.error(getClass(), "Problems removing annotated line", e
);
382 * @see org.eclipse.ui.texteditor.AbstractDocumentProvider#changed(java.lang.Object)
386 public void changed(Object entity
) {
387 LineAnnotation annotation
= (LineAnnotation
) annotationModel
.getAnnotation(entity
);
388 if(annotation
!= null){
389 annotation
.setDirty(true);
390 updateLineFromAnnotation(annotation
);