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
.PlatformUI
;
23 import org
.eclipse
.ui
.editors
.text
.TextEditor
;
24 import org
.eclipse
.ui
.texteditor
.IDocumentProvider
;
26 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
27 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
28 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
29 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
30 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
31 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
32 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.AbstractBulkEditorInput
;
33 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
34 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
37 * A list-based editor, where each line in the editor's document is associated with a domain object.
39 * Extending classes must set:
41 * <li>an {@link IEntityPersistenceService} for interacting with the persistence layer; and
42 * <li>an {@link ILineDisplayStrategy} for various visual manifestations of the domain object.
49 public class AnnotatedLineEditor
extends TextEditor
implements IConversationEnabled
, IPostOperationEnabled
{
51 protected ConversationHolder conversation
;
53 private IEntityPersistenceService persistenceService
;
54 protected ILineDisplayStrategy lineDisplayStrategy
;
58 * <p>Constructor for AnnotatedLineEditor.</p>
60 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
62 public AnnotatedLineEditor(ConversationHolder conversation
) {
63 this.conversation
= conversation
;
67 * @see org.eclipse.ui.editors.text.TextEditor#doSetInput(org.eclipse.ui.IEditorInput)
71 protected void doSetInput(IEditorInput input
) throws CoreException
{
73 AnnotatedLineDocumentProvider provider
= new AnnotatedLineDocumentProvider(input
);
75 provider
.setLineDisplayStrategy(lineDisplayStrategy
, input
);
76 setDocumentProvider(provider
);
78 super.doSetInput(input
);
82 * <p>Setter for the field <code>persistenceService</code>.</p>
84 * @param persistenceService a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService} object.
86 protected void setPersistenceService(
87 IEntityPersistenceService persistenceService
) {
88 this.persistenceService
= persistenceService
;
92 * <p>Getter for the field <code>persistenceService</code>.</p>
94 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService} object.
96 protected IEntityPersistenceService
getPersistenceService() {
97 return persistenceService
;
101 * <p>Setter for the field <code>lineDisplayStrategy</code>.</p>
103 * @param lineDisplayStrategy a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy} object.
105 protected void setLineDisplayStrategy(
106 ILineDisplayStrategy lineDisplayStrategy
) {
107 this.lineDisplayStrategy
= lineDisplayStrategy
;
112 protected ISourceViewer
createSourceViewer(Composite parent
,
113 IVerticalRuler ruler
, int styles
) {
115 fAnnotationAccess
= getAnnotationAccess();
116 fOverviewRuler
= createOverviewRuler(getSharedColors());
117 LineSelectionViewer viewer
= new LineSelectionViewer(parent
, ruler
, getOverviewRuler(),
118 isOverviewRulerVisible(), styles
);
119 // isOverviewRulerVisible(), styles | SWT.WRAP);
120 getSourceViewerDecorationSupport(viewer
);
126 * Create an annotated line with an "empty" entity, i.e. using the editor
127 * input's default entity type and a zero-length title cache.
129 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
131 public LineAnnotation
createAnnotatedLineNewObject() {
135 AnnotatedLineDocumentProvider documentProvider
= (AnnotatedLineDocumentProvider
) getDocumentProvider();
136 IEntityCreator entityCreator
= documentProvider
.getEntityCreator(getEditorInput());
137 Object entity
= entityCreator
.createEntity(null);
139 LineAnnotation annotation
= createAnnotatedLine(entity
);
140 if (annotation
!= null) {
141 annotation
.markAsNew(true);
147 * Create an annotated line, first creating an entity of type "key" - this key
148 * must be recognized by the editor's entity creator.
150 * @param key a {@link java.lang.Object} object.
151 * @param titleCache a {@link java.lang.String} object.
152 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
154 public LineAnnotation
createAnnotatedLineNewObject(Object key
, String titleCache
) {
159 Object entity
= ((AnnotatedLineDocumentProvider
) getDocumentProvider()).
160 getEntityCreator(getEditorInput()).createEntity(key
, titleCache
);
161 // checks if the creator also saves the entity (in a different conversation), in which case
162 // we need to bind back this editors conversation
163 // we also need to reload the entity because the conversation used to
164 // to create / save the entity may have been closed
165 if(entity
!= null && ((AnnotatedLineDocumentProvider
) getDocumentProvider()).getEntityCreator(getEditorInput()).savesEntity()) {
166 getConversationHolder().bind();
167 Object object
= CdmStore
.getService(IOccurrenceService
.class).load(((CdmBase
)entity
).getUuid());
168 entity
= HibernateProxyHelper
.deproxy(object
);
174 LineAnnotation annotation
= createAnnotatedLine(entity
);
175 if (annotation
!= null) {
176 annotation
.markAsNew(true);
184 public boolean isDirty() {
185 return super.isDirty();
189 * Creates an annotated line at the end of the document. The annotation contains the entity.
191 * @param entity a {@link java.lang.Object} object.
192 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
194 public LineAnnotation
createAnnotatedLine(Object entity
) {
196 IEditorInput input
= getEditorInput();
197 AnnotatedLineDocumentProvider provider
= (AnnotatedLineDocumentProvider
) getDocumentProvider();
199 LineAnnotation annotation
= null;
201 annotation
= provider
.createAnnotatedLine(input
, entity
);
204 IAnnotationModel model
= provider
.getAnnotationModel(input
);
206 int start
= model
.getPosition(annotation
).getOffset();
207 selectAndReveal(start
, 0);
210 } catch (BadLocationException e
) {
211 // TODO Auto-generated catch block
218 * <p>removeAnnotatedLine</p>
220 * @param lineno a int.
222 public void removeAnnotatedLine(int lineno
) {
223 ((AnnotatedLineDocumentProvider
) getDocumentProvider()).removeAnnotatedLine(lineno
);
227 * <p>removeAnnotatedLine</p>
229 * @param annotation a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
231 public void removeAnnotatedLine(LineAnnotation annotation
) {
232 ((AnnotatedLineDocumentProvider
) getDocumentProvider()).removeAnnotatedLine(annotation
);
236 * @see org.eclipse.ui.texteditor.AbstractTextEditor#doSave(org.eclipse.core.runtime.IProgressMonitor)
240 public void doSave(IProgressMonitor progressMonitor
) {
241 if (getConversationHolder() != null) {
242 if( ! getConversationHolder().isBound()){
243 getConversationHolder().bind();
245 super.doSave(progressMonitor
);
246 getConversationHolder().commit(true);
248 super.doSave(progressMonitor
);
250 firePropertyChange(PROP_DIRTY
);
254 * @see org.eclipse.ui.texteditor.AbstractTextEditor#setFocus()
258 public void setFocus() {
260 if (getConversationHolder() != null) {
261 getConversationHolder().bind();
263 ((AbstractBulkEditorInput
)getEditorInput()).bind();
264 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell().setFocus();
265 // TODO pass focus to underlying widgets
269 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
272 * <p>getConversationHolder</p>
274 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
277 public ConversationHolder
getConversationHolder() {
282 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
286 public void update(CdmDataChangeMap changeEvents
) {}
289 * Refreshes text in all lines.
291 protected void refreshLineDisplay() {
292 IDocumentProvider provider
= getDocumentProvider();
293 IEditorInput input
= getEditorInput();
294 IAnnotationModel model
= provider
.getAnnotationModel(input
);
295 Iterator iter
= getDocumentProvider().getAnnotationModel(getEditorInput()).getAnnotationIterator();
296 while (iter
.hasNext()) {
297 Object next
= iter
.next();
298 if (next
instanceof LineAnnotation
) {
299 LineAnnotation annotation
= (LineAnnotation
) next
;
300 ((AnnotatedLineDocumentProvider
) getDocumentProvider()).
301 updateLineFromAnnotation(annotation
);
307 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
311 public boolean postOperation(CdmBase objectAffectedByOperation
) {
312 refreshLineDisplay();
318 * @see org.eclipse.ui.editors.text.TextEditor#dispose()
322 public void dispose() {
324 conversation
.close();
325 ((AbstractBulkEditorInput
)getEditorInput()).dispose();
334 public boolean onComplete() {
335 // TODO Auto-generated method stub