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
.model
.common
.CdmBase
;
28 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
29 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
32 * A list-based editor, where each line in the editor's document is associated with a domain object.
34 * Extending classes must set:
36 * <li>an {@link IEntityPersistenceService} for interacting with the persistence layer; and
37 * <li>an {@link ILineDisplayStrategy} for various visual manifestations of the domain object.
44 public class AnnotatedLineEditor
extends TextEditor
implements IConversationEnabled
, IPostOperationEnabled
{
46 private ConversationHolder conversation
;
48 private IEntityPersistenceService persistenceService
;
49 protected ILineDisplayStrategy lineDisplayStrategy
;
53 * <p>Constructor for AnnotatedLineEditor.</p>
55 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
57 public AnnotatedLineEditor(ConversationHolder conversation
) {
58 this.conversation
= conversation
;
62 * @see org.eclipse.ui.editors.text.TextEditor#doSetInput(org.eclipse.ui.IEditorInput)
66 protected void doSetInput(IEditorInput input
) throws CoreException
{
68 AnnotatedLineDocumentProvider provider
= new AnnotatedLineDocumentProvider(input
);
70 provider
.setLineDisplayStrategy(lineDisplayStrategy
, input
);
71 setDocumentProvider(provider
);
73 super.doSetInput(input
);
77 * <p>Setter for the field <code>persistenceService</code>.</p>
79 * @param persistenceService a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService} object.
81 protected void setPersistenceService(
82 IEntityPersistenceService persistenceService
) {
83 this.persistenceService
= persistenceService
;
87 * <p>Getter for the field <code>persistenceService</code>.</p>
89 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService} object.
91 protected IEntityPersistenceService
getPersistenceService() {
92 return persistenceService
;
96 * <p>Setter for the field <code>lineDisplayStrategy</code>.</p>
98 * @param lineDisplayStrategy a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy} object.
100 protected void setLineDisplayStrategy(
101 ILineDisplayStrategy lineDisplayStrategy
) {
102 this.lineDisplayStrategy
= lineDisplayStrategy
;
107 protected ISourceViewer
createSourceViewer(Composite parent
,
108 IVerticalRuler ruler
, int styles
) {
110 fAnnotationAccess
= getAnnotationAccess();
111 fOverviewRuler
= createOverviewRuler(getSharedColors());
112 LineSelectionViewer viewer
= new LineSelectionViewer(parent
, ruler
, getOverviewRuler(),
113 isOverviewRulerVisible(), styles
);
114 // isOverviewRulerVisible(), styles | SWT.WRAP);
115 getSourceViewerDecorationSupport(viewer
);
121 * Create an annotated line with an "empty" entity, i.e. using the editor
122 * input's default entity type and a zero-length title cache.
124 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
126 public LineAnnotation
createAnnotatedLineNewObject() {
130 AnnotatedLineDocumentProvider documentProvider
= (AnnotatedLineDocumentProvider
) getDocumentProvider();
131 IEntityCreator entityCreator
= documentProvider
.getEntityCreator(getEditorInput());
132 Object entity
= entityCreator
.createEntity(null);
134 LineAnnotation annotation
= createAnnotatedLine(entity
);
135 if (annotation
!= null) {
136 annotation
.markAsNew(true);
142 * Create an annotated line, first creating an entity of type "key" - this key
143 * must be recognized by the editor's entity creator.
145 * @param key a {@link java.lang.Object} object.
146 * @param titleCache a {@link java.lang.String} object.
147 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
149 public LineAnnotation
createAnnotatedLineNewObject(Object key
, String titleCache
) {
152 Object entity
= ((AnnotatedLineDocumentProvider
) getDocumentProvider()).
153 getEntityCreator(getEditorInput()).createEntity(key
, titleCache
);
155 LineAnnotation annotation
= createAnnotatedLine(entity
);
156 if (annotation
!= null) {
157 annotation
.markAsNew(true);
164 * Creates an annotated line at the end of the document. The annotation contains the entity.
166 * @param entity a {@link java.lang.Object} object.
167 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
169 public LineAnnotation
createAnnotatedLine(Object entity
) {
171 IEditorInput input
= getEditorInput();
172 AnnotatedLineDocumentProvider provider
= (AnnotatedLineDocumentProvider
) getDocumentProvider();
174 LineAnnotation annotation
= null;
176 annotation
= provider
.createAnnotatedLine(input
, entity
);
179 IAnnotationModel model
= provider
.getAnnotationModel(input
);
181 int start
= model
.getPosition(annotation
).getOffset();
182 selectAndReveal(start
, 0);
185 } catch (BadLocationException e
) {
186 // TODO Auto-generated catch block
193 * <p>removeAnnotatedLine</p>
195 * @param lineno a int.
197 public void removeAnnotatedLine(int lineno
) {
198 ((AnnotatedLineDocumentProvider
) getDocumentProvider()).removeAnnotatedLine(lineno
);
202 * <p>removeAnnotatedLine</p>
204 * @param annotation a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
206 public void removeAnnotatedLine(LineAnnotation annotation
) {
207 ((AnnotatedLineDocumentProvider
) getDocumentProvider()).removeAnnotatedLine(annotation
);
211 * @see org.eclipse.ui.texteditor.AbstractTextEditor#doSave(org.eclipse.core.runtime.IProgressMonitor)
215 public void doSave(IProgressMonitor progressMonitor
) {
216 if (getConversationHolder() != null) {
217 if( ! getConversationHolder().isBound()){
218 getConversationHolder().bind();
220 super.doSave(progressMonitor
);
221 getConversationHolder().commit(true);
223 super.doSave(progressMonitor
);
225 firePropertyChange(PROP_DIRTY
);
229 * @see org.eclipse.ui.texteditor.AbstractTextEditor#setFocus()
233 public void setFocus() {
235 if (getConversationHolder() != null) {
236 getConversationHolder().bind();
238 // TODO pass focus to underlying widgets
242 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
245 * <p>getConversationHolder</p>
247 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
249 public ConversationHolder
getConversationHolder() {
254 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
257 public void update(CdmDataChangeMap changeEvents
) {}
260 * Refreshes text in all lines.
262 protected void refreshLineDisplay() {
263 IDocumentProvider provider
= getDocumentProvider();
264 IEditorInput input
= getEditorInput();
265 IAnnotationModel model
= provider
.getAnnotationModel(input
);
266 Iterator iter
= getDocumentProvider().getAnnotationModel(getEditorInput()).getAnnotationIterator();
267 while (iter
.hasNext()) {
268 Object next
= iter
.next();
269 if (next
instanceof LineAnnotation
) {
270 LineAnnotation annotation
= (LineAnnotation
) next
;
271 ((AnnotatedLineDocumentProvider
) getDocumentProvider()).
272 updateLineFromAnnotation(annotation
);
278 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
281 public boolean postOperation(CdmBase objectAffectedByOperation
) {
282 refreshLineDisplay();
288 * @see org.eclipse.ui.editors.text.TextEditor#dispose()
292 public void dispose() {
294 conversation
.close();
302 public boolean onComplete() {
303 // TODO Auto-generated method stub