Project

General

Profile

Download (13.9 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.bulkeditor.e4;
10

    
11
import java.io.FileOutputStream;
12
import java.io.IOException;
13
import java.util.List;
14

    
15
import javax.annotation.PostConstruct;
16
import javax.annotation.PreDestroy;
17
import javax.inject.Inject;
18

    
19
import org.eclipse.core.runtime.IProgressMonitor;
20
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
21
import org.eclipse.e4.core.contexts.IEclipseContext;
22
import org.eclipse.e4.core.di.annotations.Optional;
23
import org.eclipse.e4.core.services.events.IEventBroker;
24
import org.eclipse.e4.ui.di.Focus;
25
import org.eclipse.e4.ui.di.Persist;
26
import org.eclipse.e4.ui.di.UIEventTopic;
27
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
28
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
29
import org.eclipse.jface.viewers.IStructuredSelection;
30
import org.eclipse.jface.viewers.StructuredSelection;
31
import org.eclipse.swt.SWT;
32
import org.eclipse.swt.widgets.Composite;
33

    
34
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
35
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
36
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
37
import eu.etaxonomy.cdm.model.common.CdmBase;
38
import eu.etaxonomy.cdm.model.description.DescriptionBase;
39
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
40
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
41
import eu.etaxonomy.cdm.model.description.TaxonDescription;
42
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
43
import eu.etaxonomy.cdm.model.media.Media;
44
import eu.etaxonomy.cdm.model.name.TaxonName;
45
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
46
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
47
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
48
import eu.etaxonomy.cdm.model.permission.Group;
49
import eu.etaxonomy.cdm.model.permission.User;
50
import eu.etaxonomy.cdm.model.taxon.Taxon;
51
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
52
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
53
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
54
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorQuery;
55
import eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput;
56
import eu.etaxonomy.taxeditor.bulkeditor.input.GroupEditorInput;
57
import eu.etaxonomy.taxeditor.bulkeditor.input.TaxonEditorInput;
58
import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
59
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
60
import eu.etaxonomy.taxeditor.model.IDerivedUnitFacadePart;
61
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
62
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
63
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
64
import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
65
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
66
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
67
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
68
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
69

    
70
/**
71
 * @author pplitzner
72
 * @since Sep 8, 2017
73
 */
74
public class BulkEditorE4 implements IPartContentHasDetails, IConversationEnabled, IPostOperationEnabled,
75
        IDirtyMarkable, IDerivedUnitFacadePart, IPartContentHasFactualData,
76
        IPartContentHasSupplementalData, IPartContentHasMedia, IE4SavablePart, ITaxonEditor {
77

    
78
    @Inject
79
	private MDirtyable dirty;
80

    
81
    private AbstractBulkEditorInput input;
82

    
83
    private ConversationHolder conversation;
84

    
85
    @Inject
86
    private IEventBroker eventBroker;
87

    
88
    @Inject
89
    IEclipseContext context;
90

    
91
    @Inject
92
    private MPart thisPart;
93

    
94
    private BulkEditorQuery lastQuery = null;
95

    
96
    private BulkEditorE4Composite bulkEditorComposite;
97

    
98
    @Inject
99
    public BulkEditorE4() {
100
	}
101

    
102
    public void init(AbstractBulkEditorInput<?> input){
103
	    this.input = input;
104
	    this.conversation = input.getConversation();
105
	    thisPart.setLabel(input.getEditorName());
106

    
107
	    bulkEditorComposite.init(input);
108
	}
109

    
110
	@PostConstruct
111
	public void createPartControl(Composite parent, IEclipseContext context) {
112
	    bulkEditorComposite = new BulkEditorE4Composite(this, parent, SWT.NONE);
113
	    ContextInjectionFactory.inject(bulkEditorComposite, context);
114
	}
115

    
116
	@Override
117
	@Persist
118
	public void save(IProgressMonitor monitor) {
119
	    save(monitor, true);
120
	}
121

    
122
    public void save(IProgressMonitor monitor, boolean resetMerge) {
123
        if (!input.getCdmEntitySession().isActive()){
124
            input.getCdmEntitySession().bind();
125
        }
126
        input.saveModel(resetMerge);
127

    
128
        IStructuredSelection selection = getSelection();
129

    
130
        dirty.setDirty(false);
131
        input.dispose();
132
        input.bind();
133
        conversation.commit(true);
134

    
135
        if (lastQuery != null){
136
            bulkEditorComposite.performSearch(lastQuery, selection);
137
        }
138
    }
139

    
140

    
141
	@Focus
142
	public void setFocus() {
143
        //make sure to bind again if maybe in another view the conversation was unbound
144
        if(conversation!=null && !conversation.isBound()){
145
            conversation.bind();
146
        }
147
        if(input!=null && input.getCdmEntitySession()!= null) {
148
            input.getCdmEntitySession().bind();
149
        }
150

    
151
	    //make sure to bind again if maybe in another view the conversation was unbound
152
	    eventBroker.post(WorkbenchEventConstants.CURRENT_ACTIVE_EDITOR, this);
153
	}
154

    
155
	@PreDestroy
156
	public void dispose() {
157
	    if(conversation!=null){
158
	        conversation.unregisterForDataStoreChanges(this);
159
	        conversation.close();
160
	    }
161
	    if(input!=null){
162
	        input.dispose();
163
	    }
164
	    dirty.setDirty(false);
165
	    //save table settings
166
	    if(bulkEditorComposite.getNatTableState()!=null){
167
            try (FileOutputStream tableStateStream =
168
                    new FileOutputStream(bulkEditorComposite.getStatePropertiesFile())) {
169
                bulkEditorComposite.getNatTableState().store(tableStateStream, null);
170
            } catch (IOException ioe) {
171
                ioe.printStackTrace();
172
            }
173
        }
174
	}
175

    
176
    @Optional
177
    @Inject
178
    private void updateAfterSearch(@UIEventTopic(WorkbenchEventConstants.BULK_EDITOR_SEARCH_FINISHED)IStructuredSelection selection){
179
        if(selection!=null){
180
            setSelection(selection);
181
        }
182
    }
183

    
184
    public void refresh(){
185
        bulkEditorComposite.refresh();
186
    }
187

    
188
    public IStructuredSelection getSelection(){
189
        return bulkEditorComposite.getSelection();
190
    }
191

    
192
    public IStructuredSelection getCellSelection(){
193
        return bulkEditorComposite.getCellSelection();
194
    }
195

    
196
    public void setSelection(IStructuredSelection selection){
197
        bulkEditorComposite.setSelection(selection);
198
    }
199

    
200
    public void setDirty(boolean isDirty){
201
        dirty.setDirty(isDirty);
202
    }
203

    
204
    public void setDirty(){
205
        setDirty(true);
206
    }
207

    
208
    @Override
209
    public boolean isDirty() {
210
        return dirty.isDirty();
211
    }
212

    
213
    public AbstractBulkEditorInput getEditorInput() {
214
        return input;
215
    }
216

    
217
    public void copyDataToClipboard() {
218
        bulkEditorComposite.copyDataToClipboard();
219
    }
220

    
221
    @Override
222
    public void update(CdmDataChangeMap arg0) {
223
    }
224

    
225
    @Override
226
    public boolean canAttachMedia() {
227
        return true;
228
    }
229

    
230
    @Override
231
    public void changed(Object element) {
232
        if(element instanceof DerivedUnitFacade){
233
            DerivedUnit derivedUnit = ((DerivedUnitFacade) element).innerDerivedUnit();
234
            if(derivedUnit!=null){
235
                getEditorInput().addSaveCandidate(derivedUnit);
236
            }
237
            FieldUnit fieldUnit = ((DerivedUnitFacade) element).innerFieldUnit();
238
            if(fieldUnit!=null){
239
                getEditorInput().addSaveCandidate(fieldUnit);
240
            }
241
        }
242
        else if (element instanceof CdmBase) {
243
            if (element instanceof DescriptionBase){
244
                if (element instanceof TaxonNameDescription){
245
                    TaxonName changedName = ((TaxonNameDescription)element).getTaxonName();
246
                    if (getEditorInput() instanceof TaxonEditorInput){
247
                        IStructuredSelection sel = getSelection();
248
                        Object firstElement = sel.getFirstElement();
249
                        if (firstElement instanceof TaxonBase){
250
                            getEditorInput().addSaveCandidate((TaxonBase)firstElement);
251
                        }
252
                    }else{
253
                        getEditorInput().addSaveCandidate(changedName);
254
                    }
255

    
256
                    input.replaceInModel(changedName);
257
                }else if (element instanceof TaxonDescription){
258
                    Taxon changedTaxon = ((TaxonDescription)element).getTaxon();
259
                    getEditorInput().addSaveCandidate(changedTaxon);
260
                    input.replaceInModel(changedTaxon);
261
                }
262
            }else if (element instanceof DescriptionElementBase){
263
                if (((DescriptionElementBase)element).getInDescription() instanceof TaxonNameDescription){
264
                    TaxonName changedName = ((TaxonNameDescription)((DescriptionElementBase)element).getInDescription()).getTaxonName();
265
                    //check whether the bulk editor is taxon or name bulk editor
266
                    if (getEditorInput() instanceof TaxonEditorInput){
267
                        IStructuredSelection sel = getSelection();
268
                        Object firstElement = sel.getFirstElement();
269
                        if (firstElement instanceof TaxonBase){
270
                            getEditorInput().addSaveCandidate((TaxonBase)firstElement);
271
                        }
272
                    }else{
273
                        getEditorInput().addSaveCandidate(changedName);
274
                    }
275
                    input.replaceInModel(changedName);
276
                }else if (((DescriptionElementBase)element).getInDescription() instanceof TaxonDescription){
277
                    Taxon changedTaxon = ((TaxonDescription)((DescriptionElementBase)element).getInDescription()).getTaxon();
278
                    getEditorInput().addSaveCandidate(changedTaxon);
279
                    input.replaceInModel(changedTaxon);
280
                }else if (((DescriptionElementBase)element).getInDescription() instanceof SpecimenDescription){
281
                    SpecimenOrObservationBase changedSpecimen = ((SpecimenDescription)((DescriptionElementBase)element).getInDescription()).getDescribedSpecimenOrObservation();
282
                    getEditorInput().addSaveCandidate(changedSpecimen);
283
                    input.replaceInModel(changedSpecimen);
284
                }
285
            }else if (element instanceof Media){
286
                IStructuredSelection sel = getSelection();
287
                Object firstElement = sel.getFirstElement();
288
                if (firstElement instanceof TaxonBase){
289
                    TaxonBase changedTaxon = (TaxonBase)sel.getFirstElement();
290
                    getEditorInput().addSaveCandidate(changedTaxon);
291
                    input.replaceInModel(changedTaxon);
292
                }else if (firstElement instanceof SpecimenOrObservationBase){
293
                    SpecimenOrObservationBase changedSpecimen = (SpecimenOrObservationBase)sel.getFirstElement();
294
                    getEditorInput().addSaveCandidate(changedSpecimen);
295
                    input.replaceInModel(changedSpecimen);
296
                } else if (firstElement instanceof Media){
297
                    getEditorInput().addSaveCandidate((Media)element);
298
                    input.replaceInModel((Media)element);
299
                }
300
            }else if(element instanceof Group){
301
                 Group oldGroup = ((GroupEditorInput)input).getEntityFromModel((Group)element);
302
                 ((GroupEditorInput)input).getSaveUserCandidates().addAll(oldGroup.getMembers());
303
                 getEditorInput().addSaveCandidate((Group)element);
304
                 input.replaceInModel((CdmBase) element);
305
            }else{
306
                getEditorInput().addSaveCandidate((CdmBase)element);
307
                input.replaceInModel((CdmBase) element);
308
            }
309
        }
310
        dirty.setDirty(true);
311
        setSelection(new StructuredSelection(element));
312
    }
313

    
314
    @Override
315
    public void forceDirty() {
316
        dirty.setDirty(true);
317
    }
318

    
319
    @Override
320
    public boolean postOperation(Object objectAffectedByOperation) {
321
        return false;
322
    }
323

    
324
    @Override
325
    public boolean onComplete() {
326
        return false;
327
    }
328

    
329
    @Override
330
    public ConversationHolder getConversationHolder() {
331
        return conversation;
332
    }
333

    
334
    public BulkEditorQuery getLastQuery() {
335
       return lastQuery;
336
    }
337

    
338
    public void setLastQuery(BulkEditorQuery lastQuery) {
339
       this.lastQuery = lastQuery;
340

    
341
    }
342

    
343
    @Override
344
    public Taxon getTaxon() {
345
        IStructuredSelection selection = getSelection();
346
        if(selection.size()==1) {
347
            Object object = selection.iterator().next();
348
            if(object instanceof Taxon){
349
                return (Taxon) object;
350
            }
351
        }
352
        return null;
353
    }
354

    
355
    @Override
356
    public void update() {
357
        input.performSearch(lastQuery, getSelection());
358
    }
359

    
360
    @Override
361
    public void addOperation(AbstractPostOperation operation) {
362
        // operations not yet used for bulk editor
363
    }
364

    
365
    @Inject
366
    @Optional
367
    private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_NAME_EDITOR) List<CdmBase> cdmBases) {
368
        if (getEditorInput() instanceof TaxonEditorInput){
369
            for (CdmBase cdmBase: cdmBases){
370
                if (getEditorInput().getModel().contains(cdmBase)){
371
                    input.performSearch(lastQuery, getSelection());
372
                    break;
373
                }
374
            }
375
        }
376
    }
377

    
378
    @Inject
379
    @Optional
380
    private void updatefromDelete(@UIEventTopic(WorkbenchEventConstants.REMOVE_USER) User user) {
381
        if (input instanceof GroupEditorInput){
382
            ((GroupEditorInput)input).getSaveUserCandidates().add(user);
383
        }
384
    }
385

    
386
    @Override
387
    public TaxonNode getTaxonNode() {
388
        return null;
389
    }
390

    
391
}
(3-3/9)