Project

General

Profile

« Previous | Next » 

Revision 3be6ef3e

Added by Niels Hoffmann over 13 years ago

performed javacscript:fix and worked on documentation

View differences:

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
}
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
	/**
52
	 * <p>Constructor for AnnotatedLineEditor.</p>
53
	 *
54
	 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
55
	 */
56
	public AnnotatedLineEditor(ConversationHolder conversation) {
57
		this.conversation = conversation;
58
	}
59
	
60
	/* (non-Javadoc)
61
	 * @see org.eclipse.ui.editors.text.TextEditor#doSetInput(org.eclipse.ui.IEditorInput)
62
	 */
63
	/** {@inheritDoc} */
64
	@Override
65
	protected void doSetInput(IEditorInput input) throws CoreException {
66

  
67
		AnnotatedLineDocumentProvider provider = createAnnotatedLineDocumentProvider();
68
		if (entityCreatorService != null) {
69
			provider.setEntityCreator(entityCreatorService.getEntityCreator(input), input);
70
		} else {
71
			provider.setEntityCreator(entityCreator, input);
72
		}
73
		provider.setLineDisplayStrategy(lineDisplayStrategy, input);
74
		setDocumentProvider(provider);
75
		
76
		super.doSetInput(input);	
77
	}
78
		
79
	/**
80
	 * <p>createAnnotatedLineDocumentProvider</p>
81
	 *
82
	 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.AnnotatedLineDocumentProvider} object.
83
	 */
84
	protected AnnotatedLineDocumentProvider createAnnotatedLineDocumentProvider() {
85
		return new AnnotatedLineDocumentProvider(getEditorInput());
86
	}
87

  
88
	/**
89
	 * <p>Setter for the field <code>entityCreatorService</code>.</p>
90
	 *
91
	 * @param entityCreatorService a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityCreatorService} object.
92
	 */
93
	protected void setEntityCreatorService(IEntityCreatorService entityCreatorService) {
94
		this.entityCreatorService = entityCreatorService;
95
	}
96
	
97
	/**
98
	 * <p>Setter for the field <code>entityCreator</code>.</p>
99
	 *
100
	 * @param entityCreator a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityCreator} object.
101
	 */
102
	protected void setEntityCreator(IEntityCreator entityCreator) {
103
		this.entityCreator = entityCreator;
104
	}
105
		
106
	/**
107
	 * <p>Setter for the field <code>persistenceService</code>.</p>
108
	 *
109
	 * @param persistenceService a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService} object.
110
	 */
111
	protected void setPersistenceService(
112
			IEntityPersistenceService persistenceService) {
113
		this.persistenceService = persistenceService;	
114
	}
115
	
116
	/**
117
	 * <p>Getter for the field <code>persistenceService</code>.</p>
118
	 *
119
	 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService} object.
120
	 */
121
	protected IEntityPersistenceService getPersistenceService() {
122
		return persistenceService;	
123
	}
124

  
125
	/**
126
	 * <p>Setter for the field <code>lineDisplayStrategy</code>.</p>
127
	 *
128
	 * @param lineDisplayStrategy a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy} object.
129
	 */
130
	protected void setLineDisplayStrategy(
131
			ILineDisplayStrategy lineDisplayStrategy) {
132
		this.lineDisplayStrategy = lineDisplayStrategy;		
133
	}
134
	
135
	/** {@inheritDoc} */
136
	@Override
137
	protected ISourceViewer createSourceViewer(Composite parent,
138
			IVerticalRuler ruler, int styles) {
139
		
140
		fAnnotationAccess= getAnnotationAccess();
141
		fOverviewRuler= createOverviewRuler(getSharedColors());	
142
		LineSelectionViewer viewer = new LineSelectionViewer(parent, ruler, getOverviewRuler(),  
143
						isOverviewRulerVisible(), styles);
144
//						isOverviewRulerVisible(), styles | SWT.WRAP);
145
		getSourceViewerDecorationSupport(viewer);
146
				
147
		return viewer;
148
	}
149
	
150
	/**
151
	 * Create an annotated line with an "empty" entity, i.e. using the editor
152
	 * input's default entity type and a zero-length title cache.
153
	 *
154
	 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
155
	 */
156
	public LineAnnotation createAnnotatedLineNewObject() {
157
		
158
		// Create new object
159
		Object entity = ((AnnotatedLineDocumentProvider) getDocumentProvider()).
160
							getEntityCreator(getEditorInput()).createEntity(null);
161

  
162
		LineAnnotation annotation = createAnnotatedLine(entity);
163
		if (annotation != null) {
164
			annotation.markAsNew(true);
165
		}
166
		return annotation;
167
	}
168
	
169
	/**
170
	 * Create an annotated line, first creating an entity of type "key" - this key
171
	 * must be recognized by the editor's entity creator.
172
	 *
173
	 * @param key a {@link java.lang.Object} object.
174
	 * @param titleCache a {@link java.lang.String} object.
175
	 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
176
	 */
177
	public LineAnnotation createAnnotatedLineNewObject(Object key, String titleCache) {
178
		
179
		// Create new object
180
		Object entity = ((AnnotatedLineDocumentProvider) getDocumentProvider()).
181
							getEntityCreator(getEditorInput()).createEntity(key, titleCache);
182

  
183
		LineAnnotation annotation = createAnnotatedLine(entity);
184
		if (annotation != null) {
185
			annotation.markAsNew(true);
186
		}
187
		return annotation;
188
		
189
	}
190
	
191
	/**
192
	 * Creates an annotated line at the end of the document. The annotation contains the entity.
193
	 *
194
	 * @param entity a {@link java.lang.Object} object.
195
	 * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
196
	 */
197
	public LineAnnotation createAnnotatedLine(Object entity) {
198
		
199
		IEditorInput input = getEditorInput();
200
		AnnotatedLineDocumentProvider provider = (AnnotatedLineDocumentProvider) getDocumentProvider();
201
		
202
		LineAnnotation annotation = null;
203
		try {
204
			annotation = provider.createAnnotatedLine(input, entity);
205

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

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

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

  
284
	/* (non-Javadoc)
285
	 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
286
	 */
287
	/** {@inheritDoc} */
288
	public boolean postOperation(CdmBase objectAffectedByOperation) {
289
		return false;
290
	}
291
	
292
	/* (non-Javadoc)
293
	 * @see org.eclipse.ui.editors.text.TextEditor#dispose()
294
	 */
295
	/** {@inheritDoc} */
296
	@Override
297
	public void dispose() {
298
		super.dispose();
299
		conversation.close();
300
	}
301

  
302
	/**
303
	 * <p>onComplete</p>
304
	 *
305
	 * @return a boolean.
306
	 */
307
	public boolean onComplete() {
308
		// TODO Auto-generated method stub
309
		return false;
310
	}
311
}

Also available in: Unified diff