Project

General

Profile

Download (10.1 KB) Statistics
| Branch: | Tag: | Revision:
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.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.operation.IPostOperationEnabled;
32
import eu.etaxonomy.taxeditor.store.CdmStore;
33

    
34
/**
35
 * A list-based editor, where each line in the editor's document is associated with a domain object.
36
 * <p>
37
 * Extending classes must set:
38
 * <ul>
39
 * <li>an {@link IEntityPersistenceService} for interacting with the persistence layer; and
40
 * <li>an {@link ILineDisplayStrategy} for various visual manifestations of the domain object.
41
 * </ul>
42
 *
43
 * @author p.ciardelli
44
 * @created 25.06.2009
45
 * @version 1.0
46
 */
47
public class AnnotatedLineEditor extends TextEditor implements IConversationEnabled, IPostOperationEnabled {
48
	
49
	private ConversationHolder conversation;
50
	
51
	private IEntityPersistenceService persistenceService;
52
	protected ILineDisplayStrategy lineDisplayStrategy;
53
	
54
	
55
	/**
56
	 * <p>Constructor for AnnotatedLineEditor.</p>
57
	 *
58
	 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
59
	 */
60
	public AnnotatedLineEditor(ConversationHolder conversation) {
61
		this.conversation = conversation;
62
	}
63
	
64
	/* (non-Javadoc)
65
	 * @see org.eclipse.ui.editors.text.TextEditor#doSetInput(org.eclipse.ui.IEditorInput)
66
	 */
67
	/** {@inheritDoc} */
68
	@Override
69
	protected void doSetInput(IEditorInput input) throws CoreException {
70

    
71
		AnnotatedLineDocumentProvider provider = new AnnotatedLineDocumentProvider(input);
72
		
73
		provider.setLineDisplayStrategy(lineDisplayStrategy, input);
74
		setDocumentProvider(provider);
75
		
76
		super.doSetInput(input);	
77
	}
78
		
79
	/**
80
	 * <p>Setter for the field <code>persistenceService</code>.</p>
81
	 *
82
	 * @param persistenceService a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService} object.
83
	 */
84
	protected void setPersistenceService(
85
			IEntityPersistenceService persistenceService) {
86
		this.persistenceService = persistenceService;	
87
	}
88
	
89
	/**
90
	 * <p>Getter for the field <code>persistenceService</code>.</p>
91
	 *
92
	 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService} object.
93
	 */
94
	protected IEntityPersistenceService getPersistenceService() {
95
		return persistenceService;	
96
	}
97

    
98
	/**
99
	 * <p>Setter for the field <code>lineDisplayStrategy</code>.</p>
100
	 *
101
	 * @param lineDisplayStrategy a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy} object.
102
	 */
103
	protected void setLineDisplayStrategy(
104
			ILineDisplayStrategy lineDisplayStrategy) {
105
		this.lineDisplayStrategy = lineDisplayStrategy;		
106
	}
107
	
108
	/** {@inheritDoc} */
109
	@Override
110
	protected ISourceViewer createSourceViewer(Composite parent,
111
			IVerticalRuler ruler, int styles) {
112
		
113
		fAnnotationAccess= getAnnotationAccess();
114
		fOverviewRuler= createOverviewRuler(getSharedColors());	
115
		LineSelectionViewer viewer = new LineSelectionViewer(parent, ruler, getOverviewRuler(),  
116
						isOverviewRulerVisible(), styles);
117
//						isOverviewRulerVisible(), styles | SWT.WRAP);
118
		getSourceViewerDecorationSupport(viewer);
119
				
120
		return viewer;
121
	}
122
	
123
	/**
124
	 * Create an annotated line with an "empty" entity, i.e. using the editor
125
	 * input's default entity type and a zero-length title cache.
126
	 *
127
	 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
128
	 */
129
	public LineAnnotation createAnnotatedLineNewObject() {
130
		
131
		// Create new object
132
		
133
		AnnotatedLineDocumentProvider documentProvider = (AnnotatedLineDocumentProvider) getDocumentProvider();
134
		IEntityCreator entityCreator = documentProvider.getEntityCreator(getEditorInput());
135
		Object entity = entityCreator.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 a {@link java.lang.Object} object.
149
	 * @param titleCache a {@link java.lang.String} object.
150
	 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
151
	 */
152
	public LineAnnotation createAnnotatedLineNewObject(Object key, String titleCache) {
153
		
154
		
155

    
156
		// Create new object
157
		Object entity = ((AnnotatedLineDocumentProvider) getDocumentProvider()).
158
							getEntityCreator(getEditorInput()).createEntity(key, titleCache);
159
		// checks if the creator also saves the entity (in a different conversation), in which case 
160
		// we need to bind back this editors conversation
161
		// we also need to reload the entity because the conversation used to
162
		// to create / save the entity may have been closed
163
		if(entity != null && ((AnnotatedLineDocumentProvider) getDocumentProvider()).getEntityCreator(getEditorInput()).savesEntity()) {		
164
			getConversationHolder().bind();
165
			Object object = CdmStore.getService(IOccurrenceService.class).load(((CdmBase)entity).getUuid());			
166
			entity = HibernateProxyHelper.deproxy(object);			
167
		}
168
		
169
		if(entity == null) {
170
			return null;
171
		}
172
		LineAnnotation annotation = createAnnotatedLine(entity);
173
		if (annotation != null) {
174
			annotation.markAsNew(true);
175
		}
176

    
177
		return annotation;
178
		
179
	}
180
	
181
	  @Override
182
	  public boolean isDirty() {
183
		return super.isDirty();		  
184
	  }
185
	
186
	/**
187
	 * Creates an annotated line at the end of the document. The annotation contains the entity.
188
	 *
189
	 * @param entity a {@link java.lang.Object} object.
190
	 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
191
	 */
192
	public LineAnnotation createAnnotatedLine(Object entity) {
193
		
194
		IEditorInput input = getEditorInput();
195
		AnnotatedLineDocumentProvider provider = (AnnotatedLineDocumentProvider) getDocumentProvider();
196
		
197
		LineAnnotation annotation = null;
198
		try {
199
			annotation = provider.createAnnotatedLine(input, entity);
200

    
201
			// Jump to new line
202
			IAnnotationModel model = provider.getAnnotationModel(input);
203
			if(model != null){
204
				int start= model.getPosition(annotation).getOffset();
205
				selectAndReveal(start, 0);
206
			}
207
			
208
		} catch (BadLocationException e) {
209
			// TODO Auto-generated catch block
210
			e.printStackTrace();
211
		}		
212
		return annotation;
213
	}
214
	
215
	/**
216
	 * <p>removeAnnotatedLine</p>
217
	 *
218
	 * @param lineno a int.
219
	 */
220
	public void removeAnnotatedLine(int lineno) {
221
		((AnnotatedLineDocumentProvider) getDocumentProvider()).removeAnnotatedLine(lineno);
222
	}
223

    
224
	/**
225
	 * <p>removeAnnotatedLine</p>
226
	 *
227
	 * @param annotation a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
228
	 */
229
	public void removeAnnotatedLine(LineAnnotation annotation) {
230
		((AnnotatedLineDocumentProvider) getDocumentProvider()).removeAnnotatedLine(annotation);
231
	}
232
	
233
	/* (non-Javadoc)
234
	 * @see org.eclipse.ui.texteditor.AbstractTextEditor#doSave(org.eclipse.core.runtime.IProgressMonitor)
235
	 */
236
	/** {@inheritDoc} */
237
	@Override
238
	public void doSave(IProgressMonitor progressMonitor) {
239
		if (getConversationHolder() != null) {
240
			if( ! getConversationHolder().isBound()){
241
				getConversationHolder().bind();
242
			}
243
			super.doSave(progressMonitor);
244
			getConversationHolder().commit(true);
245
		} else {
246
			super.doSave(progressMonitor);	
247
		}
248
		firePropertyChange(PROP_DIRTY);
249
	}
250
	
251
	/* (non-Javadoc)
252
	 * @see org.eclipse.ui.texteditor.AbstractTextEditor#setFocus()
253
	 */
254
	/** {@inheritDoc} */
255
	@Override
256
	public void setFocus() {
257
		super.setFocus();
258
		if (getConversationHolder() != null) {
259
			getConversationHolder().bind();
260
		}
261
		// TODO pass focus to underlying widgets
262
	}
263

    
264
	/* (non-Javadoc)
265
	 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
266
	 */
267
	/**
268
	 * <p>getConversationHolder</p>
269
	 *
270
	 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
271
	 */
272
	public ConversationHolder getConversationHolder() {
273
		return conversation;
274
	}
275

    
276
	/* (non-Javadoc)
277
	 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
278
	 */
279
	/** {@inheritDoc} */
280
	public void update(CdmDataChangeMap changeEvents) {}
281

    
282
	/**
283
	 * Refreshes text in all lines.
284
	 */
285
	protected void refreshLineDisplay() {
286
		IDocumentProvider provider = getDocumentProvider();
287
		IEditorInput input = getEditorInput();
288
		IAnnotationModel model = provider.getAnnotationModel(input);
289
		Iterator iter = getDocumentProvider().getAnnotationModel(getEditorInput()).getAnnotationIterator();
290
		while (iter.hasNext()) {
291
			Object next = iter.next();
292
			if (next instanceof LineAnnotation) {
293
				LineAnnotation annotation = (LineAnnotation) next;
294
				((AnnotatedLineDocumentProvider) getDocumentProvider()).
295
						updateLineFromAnnotation(annotation);
296
			}
297
		}
298
	}
299
	
300
	/* (non-Javadoc)
301
	 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
302
	 */
303
	/** {@inheritDoc} */
304
	public boolean postOperation(CdmBase objectAffectedByOperation) {
305
		refreshLineDisplay();
306
		
307
		return true;
308
	}
309
	
310
	/* (non-Javadoc)
311
	 * @see org.eclipse.ui.editors.text.TextEditor#dispose()
312
	 */
313
	/** {@inheritDoc} */
314
	@Override
315
	public void dispose() {
316
		super.dispose();
317
		conversation.close();
318
	}
319

    
320
	/**
321
	 * <p>onComplete</p>
322
	 *
323
	 * @return a boolean.
324
	 */
325
	public boolean onComplete() {
326
		// TODO Auto-generated method stub
327
		return false;
328
	}
329

    
330
}
(2-2/16)