Project

General

Profile

Download (12.3 KB) Statistics
| Branch: | Tag: | Revision:
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.e4;
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.annotatedlineeditor.AnnotatedLineDocumentProvider;
32
import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityCreator;
33
import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService;
34
import eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy;
35
import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation;
36
import eu.etaxonomy.taxeditor.annotatedlineeditor.LineSelectionViewer;
37
import eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput;
38
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
39
import eu.etaxonomy.taxeditor.store.CdmStore;
40

    
41
/**
42
 * A list-based editor, where each line in the editor's document is associated with a domain object.
43
 * <p>
44
 * Extending classes must set:
45
 * <ul>
46
 * <li>an {@link IEntityPersistenceService} for interacting with the persistence layer; and
47
 * <li>an {@link ILineDisplayStrategy} for various visual manifestations of the domain object.
48
 * </ul>
49
 *
50
 * @author p.ciardelli
51
 * @created 25.06.2009
52
 * @version 1.0
53
 */
54
public class AnnotatedLineEditorE4 extends TextEditor implements IConversationEnabled, IPostOperationEnabled {
55

    
56
    protected ConversationHolder conversation;
57

    
58
    private IEntityPersistenceService persistenceService;
59
    protected ILineDisplayStrategy lineDisplayStrategy;
60

    
61

    
62
    /**
63
     * <p>Constructor for AnnotatedLineEditor.</p>
64
     *
65
     * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
66
     */
67
    public AnnotatedLineEditorE4(ConversationHolder conversation) {
68
        this.conversation = conversation;
69
    }
70

    
71
    /* (non-Javadoc)
72
     * @see org.eclipse.ui.editors.text.TextEditor#doSetInput(org.eclipse.ui.IEditorInput)
73
     */
74
    /** {@inheritDoc} */
75
    @Override
76
    protected void doSetInput(IEditorInput input) throws CoreException {
77

    
78
        AnnotatedLineDocumentProvider provider = new AnnotatedLineDocumentProvider(input);
79

    
80
        provider.setLineDisplayStrategy(lineDisplayStrategy, input);
81
        setDocumentProvider(provider);
82

    
83
        super.doSetInput(input);
84
    }
85

    
86
    /**
87
     * <p>Setter for the field <code>persistenceService</code>.</p>
88
     *
89
     * @param persistenceService a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService} object.
90
     */
91
    protected void setPersistenceService(
92
            IEntityPersistenceService persistenceService) {
93
        this.persistenceService = persistenceService;
94
    }
95

    
96
    /**
97
     * <p>Getter for the field <code>persistenceService</code>.</p>
98
     *
99
     * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService} object.
100
     */
101
    protected IEntityPersistenceService getPersistenceService() {
102
        return persistenceService;
103
    }
104

    
105
    /**
106
     * <p>Setter for the field <code>lineDisplayStrategy</code>.</p>
107
     *
108
     * @param lineDisplayStrategy a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy} object.
109
     */
110
    protected void setLineDisplayStrategy(
111
            ILineDisplayStrategy lineDisplayStrategy) {
112
        this.lineDisplayStrategy = lineDisplayStrategy;
113
    }
114

    
115
    /** {@inheritDoc} */
116
    @Override
117
    protected ISourceViewer createSourceViewer(Composite parent,
118
            IVerticalRuler ruler, int styles) {
119

    
120
        fAnnotationAccess= getAnnotationAccess();
121
        fOverviewRuler= createOverviewRuler(getSharedColors());
122
        LineSelectionViewer viewer = new LineSelectionViewer(parent, ruler, getOverviewRuler(),
123
                isOverviewRulerVisible(), styles);
124
        //						isOverviewRulerVisible(), styles | SWT.WRAP);
125
        getSourceViewerDecorationSupport(viewer);
126

    
127
        return viewer;
128
    }
129

    
130
    /**
131
     * Create an annotated line with an "empty" entity, i.e. using the editor
132
     * input's default entity type and a zero-length title cache.
133
     *
134
     * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
135
     */
136
    public LineAnnotation createAnnotatedLineNewObject() {
137

    
138
        // Create new object
139

    
140
        AnnotatedLineDocumentProvider documentProvider = (AnnotatedLineDocumentProvider) getDocumentProvider();
141
        IEntityCreator entityCreator = documentProvider.getEntityCreator(getEditorInput());
142
        Object entity = entityCreator.createEntity(null);
143

    
144
        LineAnnotation annotation = createAnnotatedLine(entity);
145
        if (annotation != null) {
146
            annotation.markAsNew(true);
147
        }
148
        return annotation;
149
    }
150

    
151
    /**
152
     * Create an annotated line, first creating an entity of type "key" - this key
153
     * must be recognized by the editor's entity creator.
154
     *
155
     * @param key a {@link java.lang.Object} object.
156
     * @param titleCache a {@link java.lang.String} object.
157
     * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
158
     */
159
    public LineAnnotation createAnnotatedLineNewObject(Object key, String titleCache) {
160

    
161

    
162

    
163
        // Create new object
164
        Object entity = ((AnnotatedLineDocumentProvider) getDocumentProvider()).
165
                getEntityCreator(getEditorInput()).createEntity(key, titleCache);
166
        // checks if the creator also saves the entity (in a different conversation), in which case
167
        // we need to bind back this editors conversation
168
        // we also need to reload the entity because the conversation used to
169
        // to create / save the entity may have been closed
170
        if(entity != null && ((AnnotatedLineDocumentProvider) getDocumentProvider()).getEntityCreator(getEditorInput()).savesEntity()) {
171
            getConversationHolder().bind();
172
            //FIXME: why do we use IOccurrenceService here? is this generic?
173
            //because this method is only invoked by the handler which is used in the
174
            //specimen bulk editor. This is unsafe and should be refactored!
175
            Object object = CdmStore.getService(IOccurrenceService.class).load(((CdmBase)entity).getUuid());
176
            entity = HibernateProxyHelper.deproxy(object);
177
        }
178

    
179
        if(entity == null) {
180
            return null;
181
        }
182
        LineAnnotation annotation = createAnnotatedLine(entity);
183
        if (annotation != null) {
184
            annotation.markAsNew(true);
185
        }
186

    
187
        return annotation;
188

    
189
    }
190

    
191
    @Override
192
    public boolean isDirty() {
193
        return super.isDirty();
194
    }
195

    
196
    /**
197
     * Creates an annotated line at the end of the document. The annotation contains the entity.
198
     *
199
     * @param entity a {@link java.lang.Object} object.
200
     * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
201
     */
202
    public LineAnnotation createAnnotatedLine(Object entity) {
203

    
204
        IEditorInput input = getEditorInput();
205
        this.conversation.close();
206
        this.conversation = ((AbstractBulkEditorInput)input).getConversation();
207
        AnnotatedLineDocumentProvider provider = (AnnotatedLineDocumentProvider) getDocumentProvider();
208

    
209
        LineAnnotation annotation = null;
210
        try {
211
            annotation = provider.createAnnotatedLine(input, entity);
212

    
213
            // Jump to new line
214
            IAnnotationModel model = provider.getAnnotationModel(input);
215
            if(model != null){
216
                int start= model.getPosition(annotation).getOffset();
217
                selectAndReveal(start, 0);
218
            }
219

    
220
        } catch (BadLocationException e) {
221
            // TODO Auto-generated catch block
222
            e.printStackTrace();
223
        }
224
        return annotation;
225
    }
226

    
227
    /**
228
     * <p>removeAnnotatedLine</p>
229
     *
230
     * @param lineno a int.
231
     */
232
    public void removeAnnotatedLine(int lineno) {
233
        ((AnnotatedLineDocumentProvider) getDocumentProvider()).removeAnnotatedLine(lineno);
234
    }
235

    
236
    /**
237
     * <p>removeAnnotatedLine</p>
238
     *
239
     * @param annotation a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
240
     */
241
    public void removeAnnotatedLine(LineAnnotation annotation) {
242
        ((AnnotatedLineDocumentProvider) getDocumentProvider()).removeAnnotatedLine(annotation);
243

    
244
    }
245

    
246
    /* (non-Javadoc)
247
     * @see org.eclipse.ui.texteditor.AbstractTextEditor#doSave(org.eclipse.core.runtime.IProgressMonitor)
248
     */
249
    /** {@inheritDoc} */
250
    @Override
251
    public void doSave(IProgressMonitor progressMonitor) {
252
        if (getConversationHolder() != null) {
253
            if( ! getConversationHolder().isBound()){
254
                getConversationHolder().bind();
255
            }
256
            super.doSave(progressMonitor);
257
            getConversationHolder().commit(true);
258
        } else {
259
            super.doSave(progressMonitor);
260
        }
261
        firePropertyChange(PROP_DIRTY);
262
    }
263

    
264
    /* (non-Javadoc)
265
     * @see org.eclipse.ui.texteditor.AbstractTextEditor#setFocus()
266
     */
267
    /** {@inheritDoc} */
268
    @Override
269
    public void setFocus() {
270
        super.setFocus();
271
        if (getConversationHolder() != null) {
272
            getConversationHolder().bind();
273
        }
274
        ((AbstractBulkEditorInput)getEditorInput()).bind();
275
        PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().setFocus();
276
        // TODO pass focus to underlying widgets
277
    }
278

    
279
    /* (non-Javadoc)
280
     * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
281
     */
282
    /**
283
     * <p>getConversationHolder</p>
284
     *
285
     * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
286
     */
287
    @Override
288
    public ConversationHolder getConversationHolder() {
289
        return conversation;
290
    }
291

    
292
    /* (non-Javadoc)
293
     * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
294
     */
295
    /** {@inheritDoc} */
296
    @Override
297
    public void update(CdmDataChangeMap changeEvents) {}
298

    
299
    /**
300
     * Refreshes text in all lines.
301
     */
302
    protected void refreshLineDisplay() {
303
        IDocumentProvider provider = getDocumentProvider();
304
        IEditorInput input = getEditorInput();
305
        IAnnotationModel model = provider.getAnnotationModel(input);
306
        Iterator iter = getDocumentProvider().getAnnotationModel(getEditorInput()).getAnnotationIterator();
307
        while (iter.hasNext()) {
308
            Object next = iter.next();
309
            if (next instanceof LineAnnotation) {
310
                LineAnnotation annotation = (LineAnnotation) next;
311
                ((AnnotatedLineDocumentProvider) getDocumentProvider()).
312
                updateLineFromAnnotation(annotation);
313
            }
314
        }
315
    }
316

    
317
    /* (non-Javadoc)
318
     * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
319
     */
320
    /** {@inheritDoc} */
321
    @Override
322
    public boolean postOperation(CdmBase objectAffectedByOperation) {
323
        refreshLineDisplay();
324

    
325
        return true;
326
    }
327

    
328
    /* (non-Javadoc)
329
     * @see org.eclipse.ui.editors.text.TextEditor#dispose()
330
     */
331
    /** {@inheritDoc} */
332
    @Override
333
    public void dispose() {
334
        super.dispose();
335
        conversation.close();
336
        ((AbstractBulkEditorInput)getEditorInput()).dispose();
337
    }
338

    
339
    /**
340
     * <p>onComplete</p>
341
     *
342
     * @return a boolean.
343
     */
344
    @Override
345
    public boolean onComplete() {
346
        // TODO Auto-generated method stub
347
        return false;
348
    }
349

    
350

    
351
}
    (1-1/1)