ref #6190 removing svn property place holder in first line of code - java files
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / annotatedlineeditor / AnnotatedLineEditor.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.annotatedlineeditor;
10
11 import java.util.Iterator;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.jface.text.BadLocationException;
16 import org.eclipse.jface.text.source.IAnnotationModel;
17 import org.eclipse.jface.text.source.ISourceViewer;
18 import org.eclipse.jface.text.source.IVerticalRuler;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.ui.IEditorInput;
21 import org.eclipse.ui.PlatformUI;
22 import org.eclipse.ui.editors.text.TextEditor;
23 import org.eclipse.ui.texteditor.IDocumentProvider;
24
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.bulkeditor.input.AbstractBulkEditorInput;
32 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
33 import eu.etaxonomy.taxeditor.store.CdmStore;
34
35 /**
36 * A list-based editor, where each line in the editor's document is associated with a domain object.
37 * <p>
38 * Extending classes must set:
39 * <ul>
40 * <li>an {@link IEntityPersistenceService} for interacting with the persistence layer; and
41 * <li>an {@link ILineDisplayStrategy} for various visual manifestations of the domain object.
42 * </ul>
43 *
44 * @author p.ciardelli
45 * @created 25.06.2009
46 * @version 1.0
47 */
48 public class AnnotatedLineEditor extends TextEditor implements IConversationEnabled, IPostOperationEnabled {
49
50 protected ConversationHolder conversation;
51
52 private IEntityPersistenceService persistenceService;
53 protected ILineDisplayStrategy lineDisplayStrategy;
54
55
56 /**
57 * <p>Constructor for AnnotatedLineEditor.</p>
58 *
59 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
60 */
61 public AnnotatedLineEditor(ConversationHolder conversation) {
62 this.conversation = conversation;
63 }
64
65 /* (non-Javadoc)
66 * @see org.eclipse.ui.editors.text.TextEditor#doSetInput(org.eclipse.ui.IEditorInput)
67 */
68 /** {@inheritDoc} */
69 @Override
70 protected void doSetInput(IEditorInput input) throws CoreException {
71
72 AnnotatedLineDocumentProvider provider = new AnnotatedLineDocumentProvider(input);
73
74 provider.setLineDisplayStrategy(lineDisplayStrategy, input);
75 setDocumentProvider(provider);
76
77 super.doSetInput(input);
78 }
79
80 /**
81 * <p>Setter for the field <code>persistenceService</code>.</p>
82 *
83 * @param persistenceService a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService} object.
84 */
85 protected void setPersistenceService(
86 IEntityPersistenceService persistenceService) {
87 this.persistenceService = persistenceService;
88 }
89
90 /**
91 * <p>Getter for the field <code>persistenceService</code>.</p>
92 *
93 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService} object.
94 */
95 protected IEntityPersistenceService getPersistenceService() {
96 return persistenceService;
97 }
98
99 /**
100 * <p>Setter for the field <code>lineDisplayStrategy</code>.</p>
101 *
102 * @param lineDisplayStrategy a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy} object.
103 */
104 protected void setLineDisplayStrategy(
105 ILineDisplayStrategy lineDisplayStrategy) {
106 this.lineDisplayStrategy = lineDisplayStrategy;
107 }
108
109 /** {@inheritDoc} */
110 @Override
111 protected ISourceViewer createSourceViewer(Composite parent,
112 IVerticalRuler ruler, int styles) {
113
114 fAnnotationAccess= getAnnotationAccess();
115 fOverviewRuler= createOverviewRuler(getSharedColors());
116 LineSelectionViewer viewer = new LineSelectionViewer(parent, ruler, getOverviewRuler(),
117 isOverviewRulerVisible(), styles);
118 // isOverviewRulerVisible(), styles | SWT.WRAP);
119 getSourceViewerDecorationSupport(viewer);
120
121 return viewer;
122 }
123
124 /**
125 * Create an annotated line with an "empty" entity, i.e. using the editor
126 * input's default entity type and a zero-length title cache.
127 *
128 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
129 */
130 public LineAnnotation createAnnotatedLineNewObject() {
131
132 // Create new object
133
134 AnnotatedLineDocumentProvider documentProvider = (AnnotatedLineDocumentProvider) getDocumentProvider();
135 IEntityCreator entityCreator = documentProvider.getEntityCreator(getEditorInput());
136 Object entity = entityCreator.createEntity(null);
137
138 LineAnnotation annotation = createAnnotatedLine(entity);
139 if (annotation != null) {
140 annotation.markAsNew(true);
141 }
142 return annotation;
143 }
144
145 /**
146 * Create an annotated line, first creating an entity of type "key" - this key
147 * must be recognized by the editor's entity creator.
148 *
149 * @param key a {@link java.lang.Object} object.
150 * @param titleCache a {@link java.lang.String} object.
151 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
152 */
153 public LineAnnotation createAnnotatedLineNewObject(Object key, String titleCache) {
154
155
156
157 // Create new object
158 Object entity = ((AnnotatedLineDocumentProvider) getDocumentProvider()).
159 getEntityCreator(getEditorInput()).createEntity(key, titleCache);
160 // checks if the creator also saves the entity (in a different conversation), in which case
161 // we need to bind back this editors conversation
162 // we also need to reload the entity because the conversation used to
163 // to create / save the entity may have been closed
164 if(entity != null && ((AnnotatedLineDocumentProvider) getDocumentProvider()).getEntityCreator(getEditorInput()).savesEntity()) {
165 getConversationHolder().bind();
166 //FIXME: why do we use IOccurrenceService here? is this generic?
167 //because this method is only invoked by the handler which is used in the
168 //specimen bulk editor. This is unsafe and should be refactored!
169 Object object = CdmStore.getService(IOccurrenceService.class).load(((CdmBase)entity).getUuid());
170 entity = HibernateProxyHelper.deproxy(object);
171 }
172
173 if(entity == null) {
174 return null;
175 }
176 LineAnnotation annotation = createAnnotatedLine(entity);
177 if (annotation != null) {
178 annotation.markAsNew(true);
179 }
180
181 return annotation;
182
183 }
184
185 @Override
186 public boolean isDirty() {
187 return super.isDirty();
188 }
189
190 /**
191 * Creates an annotated line at the end of the document. The annotation contains the entity.
192 *
193 * @param entity a {@link java.lang.Object} object.
194 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
195 */
196 public LineAnnotation createAnnotatedLine(Object entity) {
197
198 IEditorInput input = getEditorInput();
199 this.conversation.close();
200 this.conversation = ((AbstractBulkEditorInput)input).getConversation();
201 AnnotatedLineDocumentProvider provider = (AnnotatedLineDocumentProvider) getDocumentProvider();
202
203 LineAnnotation annotation = null;
204 try {
205 annotation = provider.createAnnotatedLine(input, entity);
206
207 // Jump to new line
208 IAnnotationModel model = provider.getAnnotationModel(input);
209 if(model != null){
210 int start= model.getPosition(annotation).getOffset();
211 selectAndReveal(start, 0);
212 }
213
214 } catch (BadLocationException e) {
215 // TODO Auto-generated catch block
216 e.printStackTrace();
217 }
218 return annotation;
219 }
220
221 /**
222 * <p>removeAnnotatedLine</p>
223 *
224 * @param lineno a int.
225 */
226 public void removeAnnotatedLine(int lineno) {
227 ((AnnotatedLineDocumentProvider) getDocumentProvider()).removeAnnotatedLine(lineno);
228 }
229
230 /**
231 * <p>removeAnnotatedLine</p>
232 *
233 * @param annotation a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
234 */
235 public void removeAnnotatedLine(LineAnnotation annotation) {
236 ((AnnotatedLineDocumentProvider) getDocumentProvider()).removeAnnotatedLine(annotation);
237
238 }
239
240 /* (non-Javadoc)
241 * @see org.eclipse.ui.texteditor.AbstractTextEditor#doSave(org.eclipse.core.runtime.IProgressMonitor)
242 */
243 /** {@inheritDoc} */
244 @Override
245 public void doSave(IProgressMonitor progressMonitor) {
246 if (getConversationHolder() != null) {
247 if( ! getConversationHolder().isBound()){
248 getConversationHolder().bind();
249 }
250 super.doSave(progressMonitor);
251 getConversationHolder().commit(true);
252 } else {
253 super.doSave(progressMonitor);
254 }
255 firePropertyChange(PROP_DIRTY);
256 }
257
258 /* (non-Javadoc)
259 * @see org.eclipse.ui.texteditor.AbstractTextEditor#setFocus()
260 */
261 /** {@inheritDoc} */
262 @Override
263 public void setFocus() {
264 super.setFocus();
265 if (getConversationHolder() != null) {
266 getConversationHolder().bind();
267 }
268 ((AbstractBulkEditorInput)getEditorInput()).bind();
269 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().setFocus();
270 // TODO pass focus to underlying widgets
271 }
272
273 /* (non-Javadoc)
274 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
275 */
276 /**
277 * <p>getConversationHolder</p>
278 *
279 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
280 */
281 @Override
282 public ConversationHolder getConversationHolder() {
283 return conversation;
284 }
285
286 /* (non-Javadoc)
287 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
288 */
289 /** {@inheritDoc} */
290 @Override
291 public void update(CdmDataChangeMap changeEvents) {}
292
293 /**
294 * Refreshes text in all lines.
295 */
296 protected void refreshLineDisplay() {
297 IDocumentProvider provider = getDocumentProvider();
298 IEditorInput input = getEditorInput();
299 IAnnotationModel model = provider.getAnnotationModel(input);
300 Iterator iter = getDocumentProvider().getAnnotationModel(getEditorInput()).getAnnotationIterator();
301 while (iter.hasNext()) {
302 Object next = iter.next();
303 if (next instanceof LineAnnotation) {
304 LineAnnotation annotation = (LineAnnotation) next;
305 ((AnnotatedLineDocumentProvider) getDocumentProvider()).
306 updateLineFromAnnotation(annotation);
307 }
308 }
309 }
310
311 /* (non-Javadoc)
312 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
313 */
314 /** {@inheritDoc} */
315 @Override
316 public boolean postOperation(CdmBase objectAffectedByOperation) {
317 refreshLineDisplay();
318
319 return true;
320 }
321
322 /* (non-Javadoc)
323 * @see org.eclipse.ui.editors.text.TextEditor#dispose()
324 */
325 /** {@inheritDoc} */
326 @Override
327 public void dispose() {
328 super.dispose();
329 conversation.close();
330 ((AbstractBulkEditorInput)getEditorInput()).dispose();
331 }
332
333 /**
334 * <p>onComplete</p>
335 *
336 * @return a boolean.
337 */
338 @Override
339 public boolean onComplete() {
340 // TODO Auto-generated method stub
341 return false;
342 }
343
344
345 }