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
;
14 import org
.eclipse
.core
.runtime
.CoreException
;
15 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
16 import org
.eclipse
.jface
.text
.BadLocationException
;
17 import org
.eclipse
.jface
.text
.source
.IAnnotationModel
;
18 import org
.eclipse
.jface
.text
.source
.ISourceViewer
;
19 import org
.eclipse
.jface
.text
.source
.IVerticalRuler
;
20 import org
.eclipse
.swt
.widgets
.Composite
;
21 import org
.eclipse
.ui
.IEditorInput
;
22 import org
.eclipse
.ui
.editors
.text
.TextEditor
;
23 import org
.eclipse
.ui
.texteditor
.IDocumentProvider
;
25 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
26 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
27 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
28 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
29 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
30 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
31 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
32 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
35 * A list-based editor, where each line in the editor's document is associated with a domain object.
37 * Extending classes must set:
39 * <li>an {@link IEntityPersistenceService} for interacting with the persistence layer; and
40 * <li>an {@link ILineDisplayStrategy} for various visual manifestations of the domain object.
47 public class AnnotatedLineEditor
extends TextEditor
implements IConversationEnabled
, IPostOperationEnabled
{
49 protected ConversationHolder conversation
;
51 private IEntityPersistenceService persistenceService
;
52 protected ILineDisplayStrategy lineDisplayStrategy
;
56 * <p>Constructor for AnnotatedLineEditor.</p>
58 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
60 public AnnotatedLineEditor(ConversationHolder conversation
) {
61 this.conversation
= conversation
;
65 * @see org.eclipse.ui.editors.text.TextEditor#doSetInput(org.eclipse.ui.IEditorInput)
69 protected void doSetInput(IEditorInput input
) throws CoreException
{
71 AnnotatedLineDocumentProvider provider
= new AnnotatedLineDocumentProvider(input
);
73 provider
.setLineDisplayStrategy(lineDisplayStrategy
, input
);
74 setDocumentProvider(provider
);
76 super.doSetInput(input
);
80 * <p>Setter for the field <code>persistenceService</code>.</p>
82 * @param persistenceService a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService} object.
84 protected void setPersistenceService(
85 IEntityPersistenceService persistenceService
) {
86 this.persistenceService
= persistenceService
;
90 * <p>Getter for the field <code>persistenceService</code>.</p>
92 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService} object.
94 protected IEntityPersistenceService
getPersistenceService() {
95 return persistenceService
;
99 * <p>Setter for the field <code>lineDisplayStrategy</code>.</p>
101 * @param lineDisplayStrategy a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy} object.
103 protected void setLineDisplayStrategy(
104 ILineDisplayStrategy lineDisplayStrategy
) {
105 this.lineDisplayStrategy
= lineDisplayStrategy
;
110 protected ISourceViewer
createSourceViewer(Composite parent
,
111 IVerticalRuler ruler
, int styles
) {
113 fAnnotationAccess
= getAnnotationAccess();
114 fOverviewRuler
= createOverviewRuler(getSharedColors());
115 LineSelectionViewer viewer
= new LineSelectionViewer(parent
, ruler
, getOverviewRuler(),
116 isOverviewRulerVisible(), styles
);
117 // isOverviewRulerVisible(), styles | SWT.WRAP);
118 getSourceViewerDecorationSupport(viewer
);
124 * Create an annotated line with an "empty" entity, i.e. using the editor
125 * input's default entity type and a zero-length title cache.
127 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
129 public LineAnnotation
createAnnotatedLineNewObject() {
133 AnnotatedLineDocumentProvider documentProvider
= (AnnotatedLineDocumentProvider
) getDocumentProvider();
134 IEntityCreator entityCreator
= documentProvider
.getEntityCreator(getEditorInput());
135 Object entity
= entityCreator
.createEntity(null);
137 LineAnnotation annotation
= createAnnotatedLine(entity
);
138 if (annotation
!= null) {
139 annotation
.markAsNew(true);
145 * Create an annotated line, first creating an entity of type "key" - this key
146 * must be recognized by the editor's entity creator.
148 * @param key a {@link java.lang.Object} object.
149 * @param titleCache a {@link java.lang.String} object.
150 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
152 public LineAnnotation
createAnnotatedLineNewObject(Object key
, String titleCache
) {
157 Object entity
= ((AnnotatedLineDocumentProvider
) getDocumentProvider()).
158 getEntityCreator(getEditorInput()).createEntity(key
, titleCache
);
159 // checks if the creator also saves the entity (in a different conversation), in which case
160 // we need to bind back this editors conversation
161 // we also need to reload the entity because the conversation used to
162 // to create / save the entity may have been closed
163 if(entity
!= null && ((AnnotatedLineDocumentProvider
) getDocumentProvider()).getEntityCreator(getEditorInput()).savesEntity()) {
164 getConversationHolder().bind();
165 Object object
= CdmStore
.getService(IOccurrenceService
.class).load(((CdmBase
)entity
).getUuid());
166 entity
= HibernateProxyHelper
.deproxy(object
);
172 LineAnnotation annotation
= createAnnotatedLine(entity
);
173 if (annotation
!= null) {
174 annotation
.markAsNew(true);
182 public boolean isDirty() {
183 return super.isDirty();
187 * Creates an annotated line at the end of the document. The annotation contains the entity.
189 * @param entity a {@link java.lang.Object} object.
190 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
192 public LineAnnotation
createAnnotatedLine(Object entity
) {
194 IEditorInput input
= getEditorInput();
195 AnnotatedLineDocumentProvider provider
= (AnnotatedLineDocumentProvider
) getDocumentProvider();
197 LineAnnotation annotation
= null;
199 annotation
= provider
.createAnnotatedLine(input
, entity
);
202 IAnnotationModel model
= provider
.getAnnotationModel(input
);
204 int start
= model
.getPosition(annotation
).getOffset();
205 selectAndReveal(start
, 0);
208 } catch (BadLocationException e
) {
209 // TODO Auto-generated catch block
216 * <p>removeAnnotatedLine</p>
218 * @param lineno a int.
220 public void removeAnnotatedLine(int lineno
) {
221 ((AnnotatedLineDocumentProvider
) getDocumentProvider()).removeAnnotatedLine(lineno
);
225 * <p>removeAnnotatedLine</p>
227 * @param annotation a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
229 public void removeAnnotatedLine(LineAnnotation annotation
) {
230 ((AnnotatedLineDocumentProvider
) getDocumentProvider()).removeAnnotatedLine(annotation
);
234 * @see org.eclipse.ui.texteditor.AbstractTextEditor#doSave(org.eclipse.core.runtime.IProgressMonitor)
238 public void doSave(IProgressMonitor progressMonitor
) {
239 if (getConversationHolder() != null) {
240 if( ! getConversationHolder().isBound()){
241 getConversationHolder().bind();
243 super.doSave(progressMonitor
);
244 getConversationHolder().commit(true);
246 super.doSave(progressMonitor
);
248 firePropertyChange(PROP_DIRTY
);
252 * @see org.eclipse.ui.texteditor.AbstractTextEditor#setFocus()
256 public void setFocus() {
258 if (getConversationHolder() != null) {
259 getConversationHolder().bind();
261 // TODO pass focus to underlying widgets
265 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
268 * <p>getConversationHolder</p>
270 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
273 public ConversationHolder
getConversationHolder() {
278 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
282 public void update(CdmDataChangeMap changeEvents
) {}
285 * Refreshes text in all lines.
287 protected void refreshLineDisplay() {
288 IDocumentProvider provider
= getDocumentProvider();
289 IEditorInput input
= getEditorInput();
290 IAnnotationModel model
= provider
.getAnnotationModel(input
);
291 Iterator iter
= getDocumentProvider().getAnnotationModel(getEditorInput()).getAnnotationIterator();
292 while (iter
.hasNext()) {
293 Object next
= iter
.next();
294 if (next
instanceof LineAnnotation
) {
295 LineAnnotation annotation
= (LineAnnotation
) next
;
296 ((AnnotatedLineDocumentProvider
) getDocumentProvider()).
297 updateLineFromAnnotation(annotation
);
303 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
307 public boolean postOperation(CdmBase objectAffectedByOperation
) {
308 refreshLineDisplay();
314 * @see org.eclipse.ui.editors.text.TextEditor#dispose()
318 public void dispose() {
320 conversation
.close();
329 public boolean onComplete() {
330 // TODO Auto-generated method stub