- implemented setFocus() of all EditorParts
[taxeditor.git] / eu.etaxonomy.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 java.util.Iterator;
13
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;
25
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.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 Object object = CdmStore.getService(IOccurrenceService.class).load(((CdmBase)entity).getUuid());
167 entity = HibernateProxyHelper.deproxy(object);
168 }
169
170 if(entity == null) {
171 return null;
172 }
173 LineAnnotation annotation = createAnnotatedLine(entity);
174 if (annotation != null) {
175 annotation.markAsNew(true);
176 }
177
178 return annotation;
179
180 }
181
182 @Override
183 public boolean isDirty() {
184 return super.isDirty();
185 }
186
187 /**
188 * Creates an annotated line at the end of the document. The annotation contains the entity.
189 *
190 * @param entity a {@link java.lang.Object} object.
191 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
192 */
193 public LineAnnotation createAnnotatedLine(Object entity) {
194
195 IEditorInput input = getEditorInput();
196 AnnotatedLineDocumentProvider provider = (AnnotatedLineDocumentProvider) getDocumentProvider();
197
198 LineAnnotation annotation = null;
199 try {
200 annotation = provider.createAnnotatedLine(input, entity);
201
202 // Jump to new line
203 IAnnotationModel model = provider.getAnnotationModel(input);
204 if(model != null){
205 int start= model.getPosition(annotation).getOffset();
206 selectAndReveal(start, 0);
207 }
208
209 } catch (BadLocationException e) {
210 // TODO Auto-generated catch block
211 e.printStackTrace();
212 }
213 return annotation;
214 }
215
216 /**
217 * <p>removeAnnotatedLine</p>
218 *
219 * @param lineno a int.
220 */
221 public void removeAnnotatedLine(int lineno) {
222 ((AnnotatedLineDocumentProvider) getDocumentProvider()).removeAnnotatedLine(lineno);
223 }
224
225 /**
226 * <p>removeAnnotatedLine</p>
227 *
228 * @param annotation a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
229 */
230 public void removeAnnotatedLine(LineAnnotation annotation) {
231 ((AnnotatedLineDocumentProvider) getDocumentProvider()).removeAnnotatedLine(annotation);
232 }
233
234 /* (non-Javadoc)
235 * @see org.eclipse.ui.texteditor.AbstractTextEditor#doSave(org.eclipse.core.runtime.IProgressMonitor)
236 */
237 /** {@inheritDoc} */
238 @Override
239 public void doSave(IProgressMonitor progressMonitor) {
240 if (getConversationHolder() != null) {
241 if( ! getConversationHolder().isBound()){
242 getConversationHolder().bind();
243 }
244 super.doSave(progressMonitor);
245 getConversationHolder().commit(true);
246 } else {
247 super.doSave(progressMonitor);
248 }
249 firePropertyChange(PROP_DIRTY);
250 }
251
252 /* (non-Javadoc)
253 * @see org.eclipse.ui.texteditor.AbstractTextEditor#setFocus()
254 */
255 /** {@inheritDoc} */
256 @Override
257 public void setFocus() {
258 super.setFocus();
259 if (getConversationHolder() != null) {
260 getConversationHolder().bind();
261 }
262 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().setFocus();
263 // TODO pass focus to underlying widgets
264 }
265
266 /* (non-Javadoc)
267 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
268 */
269 /**
270 * <p>getConversationHolder</p>
271 *
272 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
273 */
274 @Override
275 public ConversationHolder getConversationHolder() {
276 return conversation;
277 }
278
279 /* (non-Javadoc)
280 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
281 */
282 /** {@inheritDoc} */
283 @Override
284 public void update(CdmDataChangeMap changeEvents) {}
285
286 /**
287 * Refreshes text in all lines.
288 */
289 protected void refreshLineDisplay() {
290 IDocumentProvider provider = getDocumentProvider();
291 IEditorInput input = getEditorInput();
292 IAnnotationModel model = provider.getAnnotationModel(input);
293 Iterator iter = getDocumentProvider().getAnnotationModel(getEditorInput()).getAnnotationIterator();
294 while (iter.hasNext()) {
295 Object next = iter.next();
296 if (next instanceof LineAnnotation) {
297 LineAnnotation annotation = (LineAnnotation) next;
298 ((AnnotatedLineDocumentProvider) getDocumentProvider()).
299 updateLineFromAnnotation(annotation);
300 }
301 }
302 }
303
304 /* (non-Javadoc)
305 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
306 */
307 /** {@inheritDoc} */
308 @Override
309 public boolean postOperation(CdmBase objectAffectedByOperation) {
310 refreshLineDisplay();
311
312 return true;
313 }
314
315 /* (non-Javadoc)
316 * @see org.eclipse.ui.editors.text.TextEditor#dispose()
317 */
318 /** {@inheritDoc} */
319 @Override
320 public void dispose() {
321 super.dispose();
322 conversation.close();
323 }
324
325 /**
326 * <p>onComplete</p>
327 *
328 * @return a boolean.
329 */
330 @Override
331 public boolean onComplete() {
332 // TODO Auto-generated method stub
333 return false;
334 }
335
336 }