merging in latest changes from trunk
[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.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.model.common.CdmBase;
28 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
29 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
30
31 /**
32 * A list-based editor, where each line in the editor's document is associated with a domain object.
33 * <p>
34 * Extending classes must set:
35 * <ul>
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.
38 * </ul>
39 *
40 * @author p.ciardelli
41 * @created 25.06.2009
42 * @version 1.0
43 */
44 public class AnnotatedLineEditor extends TextEditor implements IConversationEnabled, IPostOperationEnabled {
45
46 private ConversationHolder conversation;
47
48 private IEntityPersistenceService persistenceService;
49 protected ILineDisplayStrategy lineDisplayStrategy;
50
51
52 /**
53 * <p>Constructor for AnnotatedLineEditor.</p>
54 *
55 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
56 */
57 public AnnotatedLineEditor(ConversationHolder conversation) {
58 this.conversation = conversation;
59 }
60
61 /* (non-Javadoc)
62 * @see org.eclipse.ui.editors.text.TextEditor#doSetInput(org.eclipse.ui.IEditorInput)
63 */
64 /** {@inheritDoc} */
65 @Override
66 protected void doSetInput(IEditorInput input) throws CoreException {
67
68 AnnotatedLineDocumentProvider provider = new AnnotatedLineDocumentProvider(input);
69
70 provider.setLineDisplayStrategy(lineDisplayStrategy, input);
71 setDocumentProvider(provider);
72
73 super.doSetInput(input);
74 }
75
76 /**
77 * <p>Setter for the field <code>persistenceService</code>.</p>
78 *
79 * @param persistenceService a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService} object.
80 */
81 protected void setPersistenceService(
82 IEntityPersistenceService persistenceService) {
83 this.persistenceService = persistenceService;
84 }
85
86 /**
87 * <p>Getter for the field <code>persistenceService</code>.</p>
88 *
89 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService} object.
90 */
91 protected IEntityPersistenceService getPersistenceService() {
92 return persistenceService;
93 }
94
95 /**
96 * <p>Setter for the field <code>lineDisplayStrategy</code>.</p>
97 *
98 * @param lineDisplayStrategy a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy} object.
99 */
100 protected void setLineDisplayStrategy(
101 ILineDisplayStrategy lineDisplayStrategy) {
102 this.lineDisplayStrategy = lineDisplayStrategy;
103 }
104
105 /** {@inheritDoc} */
106 @Override
107 protected ISourceViewer createSourceViewer(Composite parent,
108 IVerticalRuler ruler, int styles) {
109
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);
116
117 return viewer;
118 }
119
120 /**
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.
123 *
124 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
125 */
126 public LineAnnotation createAnnotatedLineNewObject() {
127
128 // Create new object
129
130 AnnotatedLineDocumentProvider documentProvider = (AnnotatedLineDocumentProvider) getDocumentProvider();
131 IEntityCreator entityCreator = documentProvider.getEntityCreator(getEditorInput());
132 Object entity = entityCreator.createEntity(null);
133
134 LineAnnotation annotation = createAnnotatedLine(entity);
135 if (annotation != null) {
136 annotation.markAsNew(true);
137 }
138 return annotation;
139 }
140
141 /**
142 * Create an annotated line, first creating an entity of type "key" - this key
143 * must be recognized by the editor's entity creator.
144 *
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.
148 */
149 public LineAnnotation createAnnotatedLineNewObject(Object key, String titleCache) {
150
151 // Create new object
152 Object entity = ((AnnotatedLineDocumentProvider) getDocumentProvider()).
153 getEntityCreator(getEditorInput()).createEntity(key, titleCache);
154
155 LineAnnotation annotation = createAnnotatedLine(entity);
156 if (annotation != null) {
157 annotation.markAsNew(true);
158 }
159 return annotation;
160
161 }
162
163 /**
164 * Creates an annotated line at the end of the document. The annotation contains the entity.
165 *
166 * @param entity a {@link java.lang.Object} object.
167 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
168 */
169 public LineAnnotation createAnnotatedLine(Object entity) {
170
171 IEditorInput input = getEditorInput();
172 AnnotatedLineDocumentProvider provider = (AnnotatedLineDocumentProvider) getDocumentProvider();
173
174 LineAnnotation annotation = null;
175 try {
176 annotation = provider.createAnnotatedLine(input, entity);
177
178 // Jump to new line
179 IAnnotationModel model = provider.getAnnotationModel(input);
180 if(model != null){
181 int start= model.getPosition(annotation).getOffset();
182 selectAndReveal(start, 0);
183 }
184
185 } catch (BadLocationException e) {
186 // TODO Auto-generated catch block
187 e.printStackTrace();
188 }
189 return annotation;
190 }
191
192 /**
193 * <p>removeAnnotatedLine</p>
194 *
195 * @param lineno a int.
196 */
197 public void removeAnnotatedLine(int lineno) {
198 ((AnnotatedLineDocumentProvider) getDocumentProvider()).removeAnnotatedLine(lineno);
199 }
200
201 /**
202 * <p>removeAnnotatedLine</p>
203 *
204 * @param annotation a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
205 */
206 public void removeAnnotatedLine(LineAnnotation annotation) {
207 ((AnnotatedLineDocumentProvider) getDocumentProvider()).removeAnnotatedLine(annotation);
208 }
209
210 /* (non-Javadoc)
211 * @see org.eclipse.ui.texteditor.AbstractTextEditor#doSave(org.eclipse.core.runtime.IProgressMonitor)
212 */
213 /** {@inheritDoc} */
214 @Override
215 public void doSave(IProgressMonitor progressMonitor) {
216 if (getConversationHolder() != null) {
217 if( ! getConversationHolder().isBound()){
218 getConversationHolder().bind();
219 }
220 super.doSave(progressMonitor);
221 getConversationHolder().commit(true);
222 } else {
223 super.doSave(progressMonitor);
224 }
225 firePropertyChange(PROP_DIRTY);
226 }
227
228 /* (non-Javadoc)
229 * @see org.eclipse.ui.texteditor.AbstractTextEditor#setFocus()
230 */
231 /** {@inheritDoc} */
232 @Override
233 public void setFocus() {
234 super.setFocus();
235 if (getConversationHolder() != null) {
236 getConversationHolder().bind();
237 }
238 // TODO pass focus to underlying widgets
239 }
240
241 /* (non-Javadoc)
242 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
243 */
244 /**
245 * <p>getConversationHolder</p>
246 *
247 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
248 */
249 public ConversationHolder getConversationHolder() {
250 return conversation;
251 }
252
253 /* (non-Javadoc)
254 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
255 */
256 /** {@inheritDoc} */
257 public void update(CdmDataChangeMap changeEvents) {}
258
259 /**
260 * Refreshes text in all lines.
261 */
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);
273 }
274 }
275 }
276
277 /* (non-Javadoc)
278 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
279 */
280 /** {@inheritDoc} */
281 public boolean postOperation(CdmBase objectAffectedByOperation) {
282 refreshLineDisplay();
283
284 return true;
285 }
286
287 /* (non-Javadoc)
288 * @see org.eclipse.ui.editors.text.TextEditor#dispose()
289 */
290 /** {@inheritDoc} */
291 @Override
292 public void dispose() {
293 super.dispose();
294 conversation.close();
295 }
296
297 /**
298 * <p>onComplete</p>
299 *
300 * @return a boolean.
301 */
302 public boolean onComplete() {
303 // TODO Auto-generated method stub
304 return false;
305 }
306
307 }