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
.cdm
.api
.conversation
.ConversationHolder
;
29 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.AbstractBulkEditorInput
;
30 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
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 final IEditorInput input
;
62 private ConversationHolder conversation
;
65 * @return the conversation
67 public ConversationHolder
getConversation() {
72 * @param conversation the conversation to set
74 public void setConversation(ConversationHolder conversation
) {
75 this.conversation
= conversation
;
79 * <p>Constructor for AnnotatedLineDocumentProvider.</p>
81 * @param input a {@link org.eclipse.ui.IEditorInput} object.
83 public AnnotatedLineDocumentProvider(IEditorInput input
){
85 this.conversation
= ((AbstractBulkEditorInput
)input
).getConversation();
90 public IAnnotationModel
getAnnotationModel(Object element
) {
91 if (element
== input
) {
93 // Create model as necessary
94 if (annotationModel
== null) {
95 annotationModel
= new LineAnnotationModel(getLineDisplayStrategy(element
));
96 annotationModel
.setEntityCreator(getEntityCreator(input
));
98 return annotationModel
;
106 protected IAnnotationModel
createAnnotationModel(Object element
)
107 throws CoreException
{
108 return getAnnotationModel(element
);
113 public IDocument
getDocument(Object element
) {
119 protected IDocument
createDocument(Object element
) throws CoreException
{
121 if (element
instanceof IEditorInput
) {
122 IEditorInput input
= (IEditorInput
) element
;
123 document
= new Document("");
124 // IAnnotationModel model = getAnnotationModel(element);
126 List
<?
> entityList
= getEntityList(element
);
128 if(entityList
!= null){
129 for (Object entity
: entityList
) {
131 createAnnotatedLine(input
, entity
);
132 } catch (BadLocationException e
) {
133 MessagingUtils
.error(getClass(), "Problems creating annotated line: ", e
);
144 * Creates an annotated line at the end of the document associated with the element
146 * @throws org.eclipse.jface.text.BadLocationException if any.
147 * @param element a {@link java.lang.Object} object.
148 * @param entity a {@link java.lang.Object} object.
149 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
151 protected LineAnnotation
createAnnotatedLine(Object element
, Object entity
) throws BadLocationException
{
153 Document document
= (Document
) getDocument(element
);
154 ILineDisplayStrategy lineDisplayStrategy
= getLineDisplayStrategy(element
);
156 LineAnnotation annotation
= new LineAnnotation(entity
, lineDisplayStrategy
);
158 // Is document zero length, or is last char in document line delimiter?
159 int docLength
= document
.getLength();
160 boolean useDelimiter
= false;
162 if (docLength
> 1 && !document
.get(docLength
- 2, 2).equals(document
.getDefaultLineDelimiter())) {
167 document
.replace(docLength
, 0, document
.getDefaultLineDelimiter());
171 if (lineDisplayStrategy
.isEntityCacheEmpty(entity
)) {
172 text
= lineDisplayStrategy
.getEmptyCacheMessage(entity
);
174 text
= lineDisplayStrategy
.getText(entity
);
176 text
+= lineDisplayStrategy
.getSupplementalText(entity
);
177 docLength
= document
.getLength();
178 document
.replace(docLength
, 0, text
);
180 Position position
= new Position(docLength
, text
.length());
182 IAnnotationModel model
= getAnnotationModel(element
);
184 model
.addAnnotation(annotation
, position
);
187 // lineDisplayStrategy.addDisplayListener(entity,
188 // new EntityListenerImpl((LineAnnotation) annotation, element));
195 * <p>updateLineFromAnnotation</p>
197 * @param annotation a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
199 public void updateLineFromAnnotation(LineAnnotation annotation
) {
201 IAnnotationModel model
= getAnnotationModel(input
);
202 IDocument document
= getDocument(input
);
203 ILineDisplayStrategy lineDisplay
= getLineDisplayStrategy(input
);
205 Object entity
= annotation
.getEntity();
207 if (getLineDisplayStrategy(input
).isEntityCacheEmpty(entity
)) {
208 text
= lineDisplay
.getEmptyCacheMessage(entity
);
210 text
= lineDisplay
.getText(entity
);
212 text
+= lineDisplay
.getSupplementalText(entity
);
215 if (model
.getPosition(annotation
) == null) {
218 int offset
= model
.getPosition(annotation
).getOffset();
219 int line
= document
.getLineOfOffset(offset
);
220 int lineLength
= document
.getLineLength(document
.getLineOfOffset(offset
));
221 if (document
.getLineDelimiter(line
) != null) {
222 lineLength
-= document
.getLineDelimiter(line
).length();
224 document
.replace(offset
, lineLength
, text
);
225 } catch (BadLocationException e
) {
226 MessagingUtils
.error(getClass(), "Problem updating annotated line: " ,e
);
234 private List
<?
> getEntityList(Object element
) {
235 if (element
instanceof AbstractBulkEditorInput
) {
236 return ((AbstractBulkEditorInput
)element
).getModel();
243 protected void doSaveDocument(IProgressMonitor monitor
, Object element
,
244 IDocument document
, boolean overwrite
) throws CoreException
{
245 if (element
instanceof AbstractBulkEditorInput
) {
247 IEntityPersistenceService persistenceService
= (AbstractBulkEditorInput
) element
;
249 // Get new containers from annotation model
250 LineAnnotationModel model
= (LineAnnotationModel
) getAnnotationModel(element
);
251 Iterator iterator
= model
.getAnnotationIterator();
252 if (!this.conversation
.isBound()){
253 this.conversation
.bind();
256 while (iterator
.hasNext()) {
257 Annotation annotation
= (Annotation
) iterator
.next();
258 if (annotation
instanceof IEntityContainer
<?
>) {
259 IEntityContainer
<?
> container
= (IEntityContainer
<?
>) annotation
;
261 if (container
.isMarkedAsNew() || container
.isDirty()) {
263 Object entity
= persistenceService
.save(container
.getEntity()); // save
264 container
.setEntity(entity
);
265 container
.setDirty(false);
266 container
.markAsNew(false);
270 this.conversation
.commit(true);
271 for (LineAnnotation annotation
: model
.getDeletedAnnotations()) {
272 if (annotation
.isMarkedAsNew()) {
275 if (annotation
.isMarkedAsMerged()) {
276 persistenceService
.merge(annotation
.getEntity(), annotation
.getMergeTarget()); // merge
278 // TODO clarify w AM whether this needs to be executed on merged objects
279 //persistenceService.delete(annotation.getEntity()); // delete
282 model
.clearDeletedAnnotations();
288 protected IRunnableContext
getOperationRunner(IProgressMonitor monitor
) {
294 public boolean isModifiable(Object element
) {
300 public boolean isReadOnly(Object element
) {
301 // enables copy & paste
306 * <p>Getter for the field <code>entityCreator</code>.</p>
308 * @param element a {@link java.lang.Object} object.
309 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityCreator} object.
311 public IEntityCreator
<?
> getEntityCreator(IEditorInput input
) {
312 if (input
instanceof AbstractBulkEditorInput
) {
313 entityCreator
= ((AbstractBulkEditorInput
) input
).getEntityCreator();
315 return entityCreator
;
319 * <p>Setter for the field <code>lineDisplayStrategy</code>.</p>
321 * @param lineDisplayStrategy a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy} object.
322 * @param element a {@link java.lang.Object} object.
324 public void setLineDisplayStrategy(
325 ILineDisplayStrategy lineDisplayStrategy
, Object element
) {
326 if (element
instanceof IEditorInput
) {
327 this.lineDisplayStrategy
= lineDisplayStrategy
;
332 * <p>Getter for the field <code>lineDisplayStrategy</code>.</p>
334 * @param element a {@link java.lang.Object} object.
335 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy} object.
337 protected ILineDisplayStrategy
getLineDisplayStrategy(Object element
) {
338 if (element
instanceof IEditorInput
) {
339 Assert
.isNotNull(lineDisplayStrategy
, "No ILineDisplayStrategy set for this element.");
340 return lineDisplayStrategy
;
346 * <p>removeAnnotatedLine</p>
348 * @param entity a {@link java.lang.Object} object.
350 public void removeAnnotatedLine(Object entity
){
351 LineAnnotation annotation
= (LineAnnotation
) annotationModel
.getAnnotation(entity
);
352 removeAnnotatedLine(annotation
);
356 * <p>removeAnnotatedLine</p>
358 * @param lineno a int.
359 * @param element a {@link java.lang.Object} object.
361 public void removeAnnotatedLine(Object element
, int lineno
) {
362 LineAnnotation annotation
= (LineAnnotation
) annotationModel
.getAnnotationAtLine(lineno
, document
);
363 removeAnnotatedLine(annotation
);
367 * <p>removeAnnotatedLine</p>
369 * @param annotation a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
371 public void removeAnnotatedLine(LineAnnotation annotation
) {
372 if (annotation
!= null) {
373 Document document
= (Document
) getDocument(input
);
374 LineAnnotationModel model
= (LineAnnotationModel
) getAnnotationModel(input
);
376 Position position
= model
.getPosition(annotation
);
377 int offset
= position
.getOffset();
378 int length
= position
.getLength();
380 Object entity
= annotation
.getEntity();
381 annotation
.markAsDeleted();
382 model
.removeAnnotation(annotation
);
384 // Immediately followed by a delimiter?
385 int annotationEnd
= offset
+ length
;
387 if (document
.getLength() > annotationEnd
+ 1 && document
.get(annotationEnd
, 2).equals(document
.getDefaultLineDelimiter())) {
390 } catch (BadLocationException e1
) {
391 MessagingUtils
.error(getClass(), "Problems removing annotated line", e1
);
395 document
.replace(offset
, length
, "");
396 } catch (BadLocationException e
) {
397 MessagingUtils
.error(getClass(), "Problems removing annotated line", e
);
403 * @see org.eclipse.ui.texteditor.AbstractDocumentProvider#changed(java.lang.Object)
407 public void changed(Object entity
) {
408 LineAnnotation annotation
= (LineAnnotation
) annotationModel
.getAnnotation(entity
);
409 if(annotation
!= null){
410 annotation
.setDirty(true);
411 updateLineFromAnnotation(annotation
);