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
.ArrayList
;
13 import java
.util
.Collection
;
14 import java
.util
.Iterator
;
15 import java
.util
.List
;
17 import org
.eclipse
.core
.runtime
.Assert
;
18 import org
.eclipse
.core
.runtime
.CoreException
;
19 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
20 import org
.eclipse
.jface
.operation
.IRunnableContext
;
21 import org
.eclipse
.jface
.text
.BadLocationException
;
22 import org
.eclipse
.jface
.text
.Document
;
23 import org
.eclipse
.jface
.text
.IDocument
;
24 import org
.eclipse
.jface
.text
.Position
;
25 import org
.eclipse
.jface
.text
.source
.Annotation
;
26 import org
.eclipse
.jface
.text
.source
.IAnnotationModel
;
27 import org
.eclipse
.ui
.IEditorInput
;
28 import org
.eclipse
.ui
.texteditor
.AbstractDocumentProvider
;
30 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
31 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.AbstractBulkEditorInput
;
32 import eu
.etaxonomy
.taxeditor
.dataimport
.DerivedUnitEditorInput
;
33 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
38 * Using an <code>IEditorPart</code>, creates a document where each line is associated
39 * with an entity. Mapping between document positions and entities is stored in
40 * <code>LineAnnotation</code>s in a <code>LineAnnotationModel</code>.
44 * <li>an <code>IEntityCreator</code> to create entities for new lines;
45 * <li>an <code>IEntityPersistenceService</code> for interacting with the persistence layer; and
46 * <li>an <code>ILineDisplayStrategy</code> for various visual manifestations of the domain object.
54 public class AnnotatedLineDocumentProvider
extends AbstractDocumentProvider
{
56 private IDocument document
;
58 private LineAnnotationModel annotationModel
;
60 private IEntityCreator
<?
> entityCreator
;
62 private ILineDisplayStrategy lineDisplayStrategy
;
64 private final IEditorInput input
;
67 * <p>Constructor for AnnotatedLineDocumentProvider.</p>
69 * @param input a {@link org.eclipse.ui.IEditorInput} object.
71 public AnnotatedLineDocumentProvider(IEditorInput input
){
77 public IAnnotationModel
getAnnotationModel(Object element
) {
78 if (element
== input
) {
80 // Create model as necessary
81 if (annotationModel
== null) {
82 annotationModel
= new LineAnnotationModel(getLineDisplayStrategy(element
));
83 annotationModel
.setEntityCreator(getEntityCreator(input
));
85 return annotationModel
;
93 protected IAnnotationModel
createAnnotationModel(Object element
)
94 throws CoreException
{
95 return getAnnotationModel(element
);
100 public IDocument
getDocument(Object element
) {
106 protected IDocument
createDocument(Object element
) throws CoreException
{
108 if (element
instanceof IEditorInput
) {
109 IEditorInput input
= (IEditorInput
) element
;
110 document
= new Document("");
111 // IAnnotationModel model = getAnnotationModel(element);
113 List
<?
> entityList
= getEntityList(element
);
115 if(entityList
!= null){
116 for (Object entity
: entityList
) {
118 createAnnotatedLine(input
, entity
);
119 } catch (BadLocationException e
) {
120 AbstractUtility
.error(getClass(), "Problems creating annotated line: ", e
);
131 * Creates an annotated line at the end of the document associated with the element
133 * @throws org.eclipse.jface.text.BadLocationException if any.
134 * @param element a {@link java.lang.Object} object.
135 * @param entity a {@link java.lang.Object} object.
136 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
138 protected LineAnnotation
createAnnotatedLine(Object element
, Object entity
) throws BadLocationException
{
140 Document document
= (Document
) getDocument(element
);
141 ILineDisplayStrategy lineDisplayStrategy
= getLineDisplayStrategy(element
);
143 LineAnnotation annotation
= new LineAnnotation(entity
, lineDisplayStrategy
);
145 // Is document zero length, or is last char in document line delimiter?
146 int docLength
= document
.getLength();
147 boolean useDelimiter
= false;
149 if (docLength
> 1 && !document
.get(docLength
- 2, 2).equals(document
.getDefaultLineDelimiter())) {
154 document
.replace(docLength
, 0, document
.getDefaultLineDelimiter());
158 if (lineDisplayStrategy
.isEntityCacheEmpty(entity
)) {
159 text
= lineDisplayStrategy
.getEmptyCacheMessage(entity
);
161 text
= lineDisplayStrategy
.getText(entity
);
163 text
+= lineDisplayStrategy
.getSupplementalText(entity
);
164 docLength
= document
.getLength();
165 document
.replace(docLength
, 0, text
);
167 Position position
= new Position(docLength
, text
.length());
169 IAnnotationModel model
= getAnnotationModel(element
);
171 model
.addAnnotation(annotation
, position
);
174 // lineDisplayStrategy.addDisplayListener(entity,
175 // new EntityListenerImpl((LineAnnotation) annotation, element));
182 * <p>updateLineFromAnnotation</p>
184 * @param annotation a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
186 public void updateLineFromAnnotation(LineAnnotation annotation
) {
188 IAnnotationModel model
= getAnnotationModel(input
);
189 IDocument document
= getDocument(input
);
190 ILineDisplayStrategy lineDisplay
= getLineDisplayStrategy(input
);
192 Object entity
= annotation
.getEntity();
194 if (getLineDisplayStrategy(input
).isEntityCacheEmpty(entity
)) {
195 text
= lineDisplay
.getEmptyCacheMessage(entity
);
197 text
= lineDisplay
.getText(entity
);
199 text
+= lineDisplay
.getSupplementalText(entity
);
202 if (model
.getPosition(annotation
) == null) {
205 int offset
= model
.getPosition(annotation
).getOffset();
206 int line
= document
.getLineOfOffset(offset
);
207 int lineLength
= document
.getLineLength(document
.getLineOfOffset(offset
));
208 if (document
.getLineDelimiter(line
) != null) {
209 lineLength
-= document
.getLineDelimiter(line
).length();
211 document
.replace(offset
, lineLength
, text
);
212 } catch (BadLocationException e
) {
213 AbstractUtility
.error(getClass(), "Problem updating annotated line: " ,e
);
221 private List
<?
> getEntityList(Object element
) {
222 if (element
instanceof AbstractBulkEditorInput
) {
223 return ((AbstractBulkEditorInput
)element
).getModel();
225 else if(element
instanceof DerivedUnitEditorInput
){
226 Collection
<SpecimenOrObservationBase
> results
= ((DerivedUnitEditorInput
) element
).getResults();
228 return new ArrayList
<SpecimenOrObservationBase
>(results
);
236 protected void doSaveDocument(IProgressMonitor monitor
, Object element
,
237 IDocument document
, boolean overwrite
) throws CoreException
{
238 if (element
instanceof AbstractBulkEditorInput
) {
240 IEntityPersistenceService persistenceService
= (AbstractBulkEditorInput
) element
;
242 // Get new containers from annotation model
243 LineAnnotationModel model
= (LineAnnotationModel
) getAnnotationModel(element
);
244 Iterator iterator
= model
.getAnnotationIterator();
245 while (iterator
.hasNext()) {
246 Annotation annotation
= (Annotation
) iterator
.next();
247 if (annotation
instanceof IEntityContainer
<?
>) {
248 IEntityContainer
<?
> container
= (IEntityContainer
<?
>) annotation
;
249 if (container
.isMarkedAsNew() || container
.isDirty()) {
250 persistenceService
.save(container
.getEntity()); // save
251 container
.setDirty(false);
252 container
.markAsNew(false);
256 for (LineAnnotation annotation
: model
.getDeletedAnnotations()) {
257 if (annotation
.isMarkedAsNew()) {
260 if (annotation
.isMarkedAsMerged()) {
261 persistenceService
.merge(annotation
.getEntity(), annotation
.getMergeTarget()); // merge
263 // TODO clarify w AM whether this needs to be executed on merged objects
264 //persistenceService.delete(annotation.getEntity()); // delete
267 model
.clearDeletedAnnotations();
273 protected IRunnableContext
getOperationRunner(IProgressMonitor monitor
) {
279 public boolean isModifiable(Object element
) {
285 public boolean isReadOnly(Object element
) {
286 // enables copy & paste
291 * <p>Getter for the field <code>entityCreator</code>.</p>
293 * @param element a {@link java.lang.Object} object.
294 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityCreator} object.
296 public IEntityCreator
<?
> getEntityCreator(IEditorInput input
) {
297 if (input
instanceof AbstractBulkEditorInput
) {
298 entityCreator
= ((AbstractBulkEditorInput
) input
).getEntityCreator();
300 return entityCreator
;
304 * <p>Setter for the field <code>lineDisplayStrategy</code>.</p>
306 * @param lineDisplayStrategy a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy} object.
307 * @param element a {@link java.lang.Object} object.
309 public void setLineDisplayStrategy(
310 ILineDisplayStrategy lineDisplayStrategy
, Object element
) {
311 if (element
instanceof IEditorInput
) {
312 this.lineDisplayStrategy
= lineDisplayStrategy
;
317 * <p>Getter for the field <code>lineDisplayStrategy</code>.</p>
319 * @param element a {@link java.lang.Object} object.
320 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy} object.
322 protected ILineDisplayStrategy
getLineDisplayStrategy(Object element
) {
323 if (element
instanceof IEditorInput
) {
324 Assert
.isNotNull(lineDisplayStrategy
, "No ILineDisplayStrategy set for this element.");
325 return lineDisplayStrategy
;
331 * <p>removeAnnotatedLine</p>
333 * @param entity a {@link java.lang.Object} object.
335 public void removeAnnotatedLine(Object entity
){
336 LineAnnotation annotation
= (LineAnnotation
) annotationModel
.getAnnotation(entity
);
337 removeAnnotatedLine(annotation
);
341 * <p>removeAnnotatedLine</p>
343 * @param lineno a int.
344 * @param element a {@link java.lang.Object} object.
346 public void removeAnnotatedLine(Object element
, int lineno
) {
347 LineAnnotation annotation
= (LineAnnotation
) annotationModel
.getAnnotationAtLine(lineno
, document
);
348 removeAnnotatedLine(annotation
);
352 * <p>removeAnnotatedLine</p>
354 * @param annotation a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
356 public void removeAnnotatedLine(LineAnnotation annotation
) {
357 if (annotation
!= null) {
358 Document document
= (Document
) getDocument(input
);
359 LineAnnotationModel model
= (LineAnnotationModel
) getAnnotationModel(input
);
361 Position position
= model
.getPosition(annotation
);
362 int offset
= position
.getOffset();
363 int length
= position
.getLength();
365 Object entity
= annotation
.getEntity();
366 annotation
.markAsDeleted();
367 model
.removeAnnotation(annotation
);
369 // Immediately followed by a delimiter?
370 int annotationEnd
= offset
+ length
;
372 if (document
.getLength() > annotationEnd
+ 1 && document
.get(annotationEnd
, 2).equals(document
.getDefaultLineDelimiter())) {
375 } catch (BadLocationException e1
) {
376 AbstractUtility
.error(getClass(), "Problems removing annotated line", e1
);
380 document
.replace(offset
, length
, "");
381 } catch (BadLocationException e
) {
382 AbstractUtility
.error(getClass(), "Problems removing annotated line", e
);
388 * @see org.eclipse.ui.texteditor.AbstractDocumentProvider#changed(java.lang.Object)
392 public void changed(Object entity
) {
393 LineAnnotation annotation
= (LineAnnotation
) annotationModel
.getAnnotation(entity
);
394 if(annotation
!= null){
395 annotation
.setDirty(true);
396 updateLineFromAnnotation(annotation
);