84eb5bb1491ffeab1e865b86d1cca533f007c2ff
[taxeditor.git] / taxeditor-bulkeditor / src / main / java / eu / etaxonomy / taxeditor / annotatedlineeditor / AnnotatedLineEditor.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10 package eu.etaxonomy.taxeditor.annotatedlineeditor;
11
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;
20
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;
26
27 /**
28 * A list-based editor, where each line in the editor's document is associated with a domain object.
29 * <p>
30 * Extending classes must set:
31 * <ul>
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.
35 * </ul>
36 *
37 * @author p.ciardelli
38 * @created 25.06.2009
39 * @version 1.0
40 */
41 public class AnnotatedLineEditor extends TextEditor implements IConversationEnabled, IPostOperationEnabled {
42
43 private ConversationHolder conversation;
44
45 private IEntityPersistenceService persistenceService;
46 protected ILineDisplayStrategy lineDisplayStrategy;
47 private IEntityCreatorService entityCreatorService;
48 private IEntityCreator<?> entityCreator;
49
50
51 public AnnotatedLineEditor(ConversationHolder conversation) {
52 this.conversation = conversation;
53 }
54
55 /* (non-Javadoc)
56 * @see org.eclipse.ui.editors.text.TextEditor#doSetInput(org.eclipse.ui.IEditorInput)
57 */
58 @Override
59 protected void doSetInput(IEditorInput input) throws CoreException {
60
61 AnnotatedLineDocumentProvider provider = createAnnotatedLineDocumentProvider();
62 if (entityCreatorService != null) {
63 provider.setEntityCreator(entityCreatorService.getEntityCreator(input), input);
64 } else {
65 provider.setEntityCreator(entityCreator, input);
66 }
67 provider.setLineDisplayStrategy(lineDisplayStrategy, input);
68 setDocumentProvider(provider);
69
70 super.doSetInput(input);
71 }
72
73 /**
74 * @return
75 */
76 protected AnnotatedLineDocumentProvider createAnnotatedLineDocumentProvider() {
77 return new AnnotatedLineDocumentProvider(getEditorInput());
78 }
79
80 /**
81 * @param entityCreatorService
82 */
83 protected void setEntityCreatorService(IEntityCreatorService entityCreatorService) {
84 this.entityCreatorService = entityCreatorService;
85 }
86
87 protected void setEntityCreator(IEntityCreator entityCreator) {
88 this.entityCreator = entityCreator;
89 }
90
91 /**
92 * @param bulkEditorPersistenceService
93 */
94 protected void setPersistenceService(
95 IEntityPersistenceService persistenceService) {
96 this.persistenceService = persistenceService;
97 }
98
99 protected IEntityPersistenceService getPersistenceService() {
100 return persistenceService;
101 }
102
103 /**
104 * @param bulkEditorLineDisplay
105 */
106 protected void setLineDisplayStrategy(
107 ILineDisplayStrategy lineDisplayStrategy) {
108 this.lineDisplayStrategy = lineDisplayStrategy;
109 }
110
111 @Override
112 protected ISourceViewer createSourceViewer(Composite parent,
113 IVerticalRuler ruler, int styles) {
114
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);
121
122 return viewer;
123 }
124
125 /**
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.
128 *
129 * @return
130 */
131 public LineAnnotation createAnnotatedLineNewObject() {
132
133 // Create new object
134 Object entity = ((AnnotatedLineDocumentProvider) getDocumentProvider()).
135 getEntityCreator(getEditorInput()).createEntity(null);
136
137 LineAnnotation annotation = createAnnotatedLine(entity);
138 if (annotation != null) {
139 annotation.markAsNew(true);
140 }
141 return annotation;
142 }
143
144 /**
145 * Create an annotated line, first creating an entity of type "key" - this key
146 * must be recognized by the editor's entity creator.
147 *
148 * @param key
149 * @param value
150 * @return
151 */
152 public LineAnnotation createAnnotatedLineNewObject(Object key, String titleCache) {
153
154 // Create new object
155 Object entity = ((AnnotatedLineDocumentProvider) getDocumentProvider()).
156 getEntityCreator(getEditorInput()).createEntity(key, titleCache);
157
158 LineAnnotation annotation = createAnnotatedLine(entity);
159 if (annotation != null) {
160 annotation.markAsNew(true);
161 }
162 return annotation;
163
164 }
165
166 /**
167 * Creates an annotated line at the end of the document. The annotation contains the entity.
168 *
169 * @param entity
170 * @return
171 */
172 public LineAnnotation createAnnotatedLine(Object entity) {
173
174 IEditorInput input = getEditorInput();
175 AnnotatedLineDocumentProvider provider = (AnnotatedLineDocumentProvider) getDocumentProvider();
176
177 LineAnnotation annotation = null;
178 try {
179 annotation = provider.createAnnotatedLine(input, entity);
180
181 // Jump to new line
182 int start= provider.getAnnotationModel(input).getPosition(annotation).getOffset();
183 selectAndReveal(start, 0);
184
185 } catch (BadLocationException e) {
186 // TODO Auto-generated catch block
187 e.printStackTrace();
188 }
189 return annotation;
190 }
191
192 /**
193 * @param line
194 */
195 public void removeAnnotatedLine(int lineno) {
196 ((AnnotatedLineDocumentProvider) getDocumentProvider()).removeAnnotatedLine(lineno);
197 }
198
199 /**
200 * @param annotation
201 */
202 public void removeAnnotatedLine(LineAnnotation annotation) {
203 ((AnnotatedLineDocumentProvider) getDocumentProvider()).removeAnnotatedLine(annotation);
204 }
205
206 /* (non-Javadoc)
207 * @see org.eclipse.ui.texteditor.AbstractTextEditor#doSave(org.eclipse.core.runtime.IProgressMonitor)
208 */
209 @Override
210 public void doSave(IProgressMonitor progressMonitor) {
211 if (getConversationHolder() != null) {
212 if( ! getConversationHolder().isBound()){
213 getConversationHolder().bind();
214 }
215 super.doSave(progressMonitor);
216 getConversationHolder().commit(true);
217 } else {
218 super.doSave(progressMonitor);
219 }
220 firePropertyChange(PROP_DIRTY);
221 }
222
223 /* (non-Javadoc)
224 * @see org.eclipse.ui.texteditor.AbstractTextEditor#setFocus()
225 */
226 @Override
227 public void setFocus() {
228 super.setFocus();
229 if (getConversationHolder() != null) {
230 getConversationHolder().bind();
231 }
232 // TODO pass focus to underlying widgets
233 }
234
235 /* (non-Javadoc)
236 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
237 */
238 public ConversationHolder getConversationHolder() {
239 return conversation;
240 }
241
242 /* (non-Javadoc)
243 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
244 */
245 public void update(CdmDataChangeMap changeEvents) {}
246
247 /* (non-Javadoc)
248 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
249 */
250 public boolean postOperation(CdmBase objectAffectedByOperation) {
251 return false;
252 }
253
254 /* (non-Javadoc)
255 * @see org.eclipse.ui.editors.text.TextEditor#dispose()
256 */
257 @Override
258 public void dispose() {
259 super.dispose();
260 conversation.close();
261 }
262
263 public boolean onComplete() {
264 // TODO Auto-generated method stub
265 return false;
266 }
267 }