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 org
.eclipse
.core
.runtime
.CoreException
;
13 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
14 import org
.eclipse
.jface
.text
.BadLocationException
;
15 import org
.eclipse
.jface
.text
.source
.ISourceViewer
;
16 import org
.eclipse
.jface
.text
.source
.IVerticalRuler
;
17 import org
.eclipse
.swt
.widgets
.Composite
;
18 import org
.eclipse
.ui
.IEditorInput
;
19 import org
.eclipse
.ui
.editors
.text
.TextEditor
;
21 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
22 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
23 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
24 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
25 import eu
.etaxonomy
.taxeditor
.operations
.IPostOperationEnabled
;
28 * A list-based editor, where each line in the editor's document is associated with a domain object.
30 * Extending classes must set:
32 * <li>an <code>IEntityCreator</code> to create a new domain object when the user creates a new line;
33 * <li>an <code>IEntityPersistenceService</code> for interacting with the persistence layer; and
34 * <li>an <code>ILineDisplayStrategy</code> for various visual manifestations of the domain object.
41 public class AnnotatedLineEditor
extends TextEditor
implements IConversationEnabled
, IPostOperationEnabled
{
43 private ConversationHolder conversation
;
45 private IEntityPersistenceService persistenceService
;
46 protected ILineDisplayStrategy lineDisplayStrategy
;
47 private IEntityCreatorService entityCreatorService
;
48 private IEntityCreator
<?
> entityCreator
;
51 public AnnotatedLineEditor(ConversationHolder conversation
) {
52 this.conversation
= conversation
;
56 * @see org.eclipse.ui.editors.text.TextEditor#doSetInput(org.eclipse.ui.IEditorInput)
59 protected void doSetInput(IEditorInput input
) throws CoreException
{
61 AnnotatedLineDocumentProvider provider
= createAnnotatedLineDocumentProvider();
62 if (entityCreatorService
!= null) {
63 provider
.setEntityCreator(entityCreatorService
.getEntityCreator(input
), input
);
65 provider
.setEntityCreator(entityCreator
, input
);
67 provider
.setLineDisplayStrategy(lineDisplayStrategy
, input
);
68 setDocumentProvider(provider
);
70 super.doSetInput(input
);
76 protected AnnotatedLineDocumentProvider
createAnnotatedLineDocumentProvider() {
77 return new AnnotatedLineDocumentProvider(getEditorInput());
81 * @param entityCreatorService
83 protected void setEntityCreatorService(IEntityCreatorService entityCreatorService
) {
84 this.entityCreatorService
= entityCreatorService
;
87 protected void setEntityCreator(IEntityCreator entityCreator
) {
88 this.entityCreator
= entityCreator
;
92 * @param bulkEditorPersistenceService
94 protected void setPersistenceService(
95 IEntityPersistenceService persistenceService
) {
96 this.persistenceService
= persistenceService
;
99 protected IEntityPersistenceService
getPersistenceService() {
100 return persistenceService
;
104 * @param bulkEditorLineDisplay
106 protected void setLineDisplayStrategy(
107 ILineDisplayStrategy lineDisplayStrategy
) {
108 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.
131 public LineAnnotation
createAnnotatedLineNewObject() {
134 Object entity
= ((AnnotatedLineDocumentProvider
) getDocumentProvider()).
135 getEntityCreator(getEditorInput()).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.
152 public LineAnnotation
createAnnotatedLineNewObject(Object key
, String titleCache
) {
155 Object entity
= ((AnnotatedLineDocumentProvider
) getDocumentProvider()).
156 getEntityCreator(getEditorInput()).createEntity(key
, titleCache
);
158 LineAnnotation annotation
= createAnnotatedLine(entity
);
159 if (annotation
!= null) {
160 annotation
.markAsNew(true);
167 * Creates an annotated line at the end of the document. The annotation contains the entity.
172 public LineAnnotation
createAnnotatedLine(Object entity
) {
174 IEditorInput input
= getEditorInput();
175 AnnotatedLineDocumentProvider provider
= (AnnotatedLineDocumentProvider
) getDocumentProvider();
177 LineAnnotation annotation
= null;
179 annotation
= provider
.createAnnotatedLine(input
, entity
);
182 int start
= provider
.getAnnotationModel(input
).getPosition(annotation
).getOffset();
183 selectAndReveal(start
, 0);
185 } catch (BadLocationException e
) {
186 // TODO Auto-generated catch block
195 public void removeAnnotatedLine(int lineno
) {
196 ((AnnotatedLineDocumentProvider
) getDocumentProvider()).removeAnnotatedLine(lineno
);
202 public void removeAnnotatedLine(LineAnnotation annotation
) {
203 ((AnnotatedLineDocumentProvider
) getDocumentProvider()).removeAnnotatedLine(annotation
);
207 * @see org.eclipse.ui.texteditor.AbstractTextEditor#doSave(org.eclipse.core.runtime.IProgressMonitor)
210 public void doSave(IProgressMonitor progressMonitor
) {
211 if (getConversationHolder() != null) {
212 if( ! getConversationHolder().isBound()){
213 getConversationHolder().bind();
215 super.doSave(progressMonitor
);
216 getConversationHolder().commit(true);
218 super.doSave(progressMonitor
);
220 firePropertyChange(PROP_DIRTY
);
224 * @see org.eclipse.ui.texteditor.AbstractTextEditor#setFocus()
227 public void setFocus() {
229 if (getConversationHolder() != null) {
230 getConversationHolder().bind();
232 // TODO pass focus to underlying widgets
236 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
238 public ConversationHolder
getConversationHolder() {
243 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
245 public void update(CdmDataChangeMap changeEvents
) {}
248 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
250 public boolean postOperation(CdmBase objectAffectedByOperation
) {
255 * @see org.eclipse.ui.editors.text.TextEditor#dispose()
258 public void dispose() {
260 conversation
.close();
263 public boolean onComplete() {
264 // TODO Auto-generated method stub