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
.BulkEditorUtil
;
29 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.AbstractBulkEditorInput
;
34 * Using an <code>IEditorPart</code>, creates a document where each line is associated
35 * with an entity. Mapping between document positions and entities is stored in
36 * <code>LineAnnotation</code>s in a <code>LineAnnotationModel</code>.
40 * <li>an <code>IEntityCreator</code> to create entities for new lines;
41 * <li>an <code>IEntityPersistenceService</code> for interacting with the persistence layer; and
42 * <li>an <code>ILineDisplayStrategy</code> for various visual manifestations of the domain object.
50 public class AnnotatedLineDocumentProvider
extends AbstractDocumentProvider
{
52 private IDocument document
;
54 private LineAnnotationModel annotationModel
;
56 private IEntityCreator
<?
> entityCreator
;
58 private ILineDisplayStrategy lineDisplayStrategy
;
60 private IEditorInput input
;
63 * <p>Constructor for AnnotatedLineDocumentProvider.</p>
65 * @param input a {@link org.eclipse.ui.IEditorInput} object.
67 public AnnotatedLineDocumentProvider(IEditorInput input
){
73 public IAnnotationModel
getAnnotationModel(Object element
) {
74 if (element
== input
) {
76 // Create model as necessary
77 if (annotationModel
== null) {
78 annotationModel
= new LineAnnotationModel(getLineDisplayStrategy(element
));
79 annotationModel
.setEntityCreator(getEntityCreator(input
));
81 return annotationModel
;
89 protected IAnnotationModel
createAnnotationModel(Object element
)
90 throws CoreException
{
91 return getAnnotationModel(element
);
96 public IDocument
getDocument(Object element
) {
102 protected IDocument
createDocument(Object element
) throws CoreException
{
104 if (element
instanceof IEditorInput
) {
105 IEditorInput input
= (IEditorInput
) element
;
106 document
= new Document("");
107 // IAnnotationModel model = getAnnotationModel(element);
109 List
<?
> entityList
= getEntityList(element
);
111 if(entityList
!= null){
112 for (Object entity
: entityList
) {
114 createAnnotatedLine(input
, entity
);
115 } catch (BadLocationException e
) {
116 BulkEditorUtil
.error(getClass(), "Problems creating annotated line: ", e
);
127 * Creates an annotated line at the end of the document associated with the element
129 * @throws org.eclipse.jface.text.BadLocationException if any.
130 * @param element a {@link java.lang.Object} object.
131 * @param entity a {@link java.lang.Object} object.
132 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
134 protected LineAnnotation
createAnnotatedLine(Object element
, Object entity
) throws BadLocationException
{
136 Document document
= (Document
) getDocument(element
);
137 ILineDisplayStrategy lineDisplayStrategy
= getLineDisplayStrategy(element
);
139 LineAnnotation annotation
= new LineAnnotation(entity
, lineDisplayStrategy
);
141 // Is document zero length, or is last char in document line delimiter?
142 int docLength
= document
.getLength();
143 boolean useDelimiter
= false;
145 if (docLength
> 1 && !document
.get(docLength
- 2, 2).equals(document
.getDefaultLineDelimiter())) {
150 document
.replace(docLength
, 0, document
.getDefaultLineDelimiter());
154 if (lineDisplayStrategy
.isEntityCacheEmpty(entity
)) {
155 text
= lineDisplayStrategy
.getEmptyCacheMessage(entity
);
157 text
= lineDisplayStrategy
.getText(entity
);
159 text
+= lineDisplayStrategy
.getSupplementalText(entity
);
160 docLength
= document
.getLength();
161 document
.replace(docLength
, 0, text
);
163 Position position
= new Position(docLength
, text
.length());
165 IAnnotationModel model
= getAnnotationModel(element
);
167 model
.addAnnotation(annotation
, position
);
170 // lineDisplayStrategy.addDisplayListener(entity,
171 // new EntityListenerImpl((LineAnnotation) annotation, element));
178 * <p>updateLineFromAnnotation</p>
180 * @param annotation a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
182 public void updateLineFromAnnotation(LineAnnotation annotation
) {
184 IAnnotationModel model
= getAnnotationModel(input
);
185 IDocument document
= getDocument(input
);
186 ILineDisplayStrategy lineDisplay
= getLineDisplayStrategy(input
);
188 Object entity
= annotation
.getEntity();
190 if (getLineDisplayStrategy(input
).isEntityCacheEmpty(entity
)) {
191 text
= lineDisplay
.getEmptyCacheMessage(entity
);
193 text
= lineDisplay
.getText(entity
);
195 text
+= lineDisplay
.getSupplementalText(entity
);
198 if (model
.getPosition(annotation
) == null) {
201 int offset
= model
.getPosition(annotation
).getOffset();
202 int line
= document
.getLineOfOffset(offset
);
203 int lineLength
= document
.getLineLength(document
.getLineOfOffset(offset
));
204 if (document
.getLineDelimiter(line
) != null) {
205 lineLength
-= document
.getLineDelimiter(line
).length();
207 document
.replace(offset
, lineLength
, text
);
208 } catch (BadLocationException e
) {
209 BulkEditorUtil
.error(getClass(), "Problem updating annotated line: " ,e
);
217 private List
<?
> getEntityList(Object element
) {
218 if (element
instanceof AbstractBulkEditorInput
) {
219 return ((AbstractBulkEditorInput
)element
).getModel();
226 protected void doSaveDocument(IProgressMonitor monitor
, Object element
,
227 IDocument document
, boolean overwrite
) throws CoreException
{
228 if (element
instanceof AbstractBulkEditorInput
) {
230 IEntityPersistenceService persistenceService
= (AbstractBulkEditorInput
) element
;
232 // Get new containers from annotation model
233 LineAnnotationModel model
= (LineAnnotationModel
) getAnnotationModel(element
);
234 Iterator iterator
= model
.getAnnotationIterator();
235 while (iterator
.hasNext()) {
236 Annotation annotation
= (Annotation
) iterator
.next();
237 if (annotation
instanceof IEntityContainer
<?
>) {
238 IEntityContainer
<?
> container
= (IEntityContainer
<?
>) annotation
;
239 if (container
.isMarkedAsNew() || container
.isDirty()) {
240 persistenceService
.save(container
.getEntity()); // save
241 container
.setDirty(false);
242 container
.markAsNew(false);
246 for (LineAnnotation annotation
: model
.getDeletedAnnotations()) {
247 if (annotation
.isMarkedAsNew()) {
250 if (annotation
.isMarkedAsMerged()) {
251 persistenceService
.merge(annotation
.getEntity(), annotation
.getMergeTarget()); // merge
253 // TODO clarify w AM whether this needs to be executed on merged objects
254 persistenceService
.delete(annotation
.getEntity()); // delete
257 model
.clearDeletedAnnotations();
263 protected IRunnableContext
getOperationRunner(IProgressMonitor monitor
) {
269 public boolean isModifiable(Object element
) {
275 public boolean isReadOnly(Object element
) {
276 // enables copy & paste
281 * <p>Getter for the field <code>entityCreator</code>.</p>
283 * @param element a {@link java.lang.Object} object.
284 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityCreator} object.
286 public IEntityCreator
<?
> getEntityCreator(IEditorInput input
) {
287 if (input
instanceof AbstractBulkEditorInput
) {
288 entityCreator
= ((AbstractBulkEditorInput
) input
).getEntityCreator();
290 return entityCreator
;
294 * <p>Setter for the field <code>lineDisplayStrategy</code>.</p>
296 * @param lineDisplayStrategy a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy} object.
297 * @param element a {@link java.lang.Object} object.
299 public void setLineDisplayStrategy(
300 ILineDisplayStrategy lineDisplayStrategy
, Object element
) {
301 if (element
instanceof IEditorInput
) {
302 this.lineDisplayStrategy
= lineDisplayStrategy
;
307 * <p>Getter for the field <code>lineDisplayStrategy</code>.</p>
309 * @param element a {@link java.lang.Object} object.
310 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy} object.
312 protected ILineDisplayStrategy
getLineDisplayStrategy(Object element
) {
313 if (element
instanceof IEditorInput
) {
314 Assert
.isNotNull(lineDisplayStrategy
, "No ILineDisplayStrategy set for this element.");
315 return lineDisplayStrategy
;
321 * <p>removeAnnotatedLine</p>
323 * @param entity a {@link java.lang.Object} object.
325 public void removeAnnotatedLine(Object entity
){
326 LineAnnotation annotation
= (LineAnnotation
) annotationModel
.getAnnotation(entity
);
327 removeAnnotatedLine(annotation
);
331 * <p>removeAnnotatedLine</p>
333 * @param lineno a int.
334 * @param element a {@link java.lang.Object} object.
336 public void removeAnnotatedLine(Object element
, int lineno
) {
337 LineAnnotation annotation
= (LineAnnotation
) annotationModel
.getAnnotationAtLine(lineno
, document
);
338 removeAnnotatedLine(annotation
);
342 * <p>removeAnnotatedLine</p>
344 * @param annotation a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
346 public void removeAnnotatedLine(LineAnnotation annotation
) {
347 if (annotation
!= null) {
348 Document document
= (Document
) getDocument(input
);
349 LineAnnotationModel model
= (LineAnnotationModel
) getAnnotationModel(input
);
351 Position position
= model
.getPosition(annotation
);
352 int offset
= position
.getOffset();
353 int length
= position
.getLength();
355 Object entity
= annotation
.getEntity();
356 annotation
.markAsDeleted();
357 model
.removeAnnotation(annotation
);
359 // Immediately followed by a delimiter?
360 int annotationEnd
= offset
+ length
;
362 if (document
.getLength() > annotationEnd
+ 1 && document
.get(annotationEnd
, 2).equals(document
.getDefaultLineDelimiter())) {
365 } catch (BadLocationException e1
) {
366 BulkEditorUtil
.error(getClass(), "Problems removing annotated line", e1
);
370 document
.replace(offset
, length
, "");
371 } catch (BadLocationException e
) {
372 BulkEditorUtil
.error(getClass(), "Problems removing annotated line", e
);
378 * @see org.eclipse.ui.texteditor.AbstractDocumentProvider#changed(java.lang.Object)
382 public void changed(Object entity
) {
383 LineAnnotation annotation
= (LineAnnotation
) annotationModel
.getAnnotation(entity
);
384 if(annotation
!= null){
385 annotation
.setDirty(true);
386 updateLineFromAnnotation(annotation
);