Project

General

Profile

Download (12.7 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

    
10
package eu.etaxonomy.taxeditor.bulkeditor.e4;
11

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

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

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

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

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

    
79
    @Inject
80
	private MDirtyable dirty;
81

    
82
    private AbstractBulkEditorInput input;
83

    
84
    private ConversationHolder conversation;
85

    
86
    @Inject
87
    private IEventBroker eventBroker;
88

    
89
    @Inject
90
    IEclipseContext context;
91

    
92
    @Inject
93
    private MPart thisPart;
94

    
95
    private BulkEditorQuery lastQuery = null;
96

    
97
    private BulkEditorE4Composite bulkEditorComposite;
98

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

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

    
108
	    bulkEditorComposite.init(input);
109
	}
110

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

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

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

    
129
        IStructuredSelection selection = getSelection();
130

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

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

    
141

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

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

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

    
177
    @Optional
178
    @Inject
179
    private void updateAfterSearch(@UIEventTopic(WorkbenchEventConstants.BULK_EDITOR_SEARCH_FINISHED)IStructuredSelection selection){
180

    
181

    
182
        if(selection!=null){
183
            setSelection(selection);
184
        }
185
    }
186

    
187
    public void refresh(){
188
        bulkEditorComposite.refresh();
189
    }
190

    
191
    public IStructuredSelection getSelection(){
192
        return bulkEditorComposite.getSelection();
193
    }
194

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

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

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

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

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

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

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

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

    
229
    @Override
230
    public void changed(Object element) {
231
        if(element instanceof DerivedUnitFacade){
232
            DerivedUnit derivedUnit = ((DerivedUnitFacade) element).innerDerivedUnit();
233
            if(derivedUnit!=null){
234
                getEditorInput().addSaveCandidate(derivedUnit);
235
            }
236
            FieldUnit fieldUnit = ((DerivedUnitFacade) element).innerFieldUnit();
237
            if(fieldUnit!=null){
238
                getEditorInput().addSaveCandidate(fieldUnit);
239
            }
240
        }
241
        else if (element instanceof CdmBase) {
242
            if (element instanceof DescriptionBase){
243
                if (element instanceof TaxonNameDescription){
244
                    TaxonName changedName = ((TaxonNameDescription)element).getTaxonName();
245
                    getEditorInput().addSaveCandidate(changedName);
246
                    input.replaceInModel(changedName);
247
                }else if (element instanceof TaxonDescription){
248
                    Taxon changedTaxon = ((TaxonDescription)element).getTaxon();
249
                    getEditorInput().addSaveCandidate(changedTaxon);
250
                    input.replaceInModel(changedTaxon);
251
                }
252
            }else if (element instanceof DescriptionElementBase){
253
                if (((DescriptionElementBase)element).getInDescription() instanceof TaxonNameDescription){
254
                    TaxonName changedName = ((TaxonNameDescription)((DescriptionElementBase)element).getInDescription()).getTaxonName();
255
                    getEditorInput().addSaveCandidate(changedName);
256
                    input.replaceInModel(changedName);
257
                }else if (((DescriptionElementBase)element).getInDescription() instanceof TaxonDescription){
258
                    Taxon changedTaxon = ((TaxonDescription)((DescriptionElementBase)element).getInDescription()).getTaxon();
259
                    getEditorInput().addSaveCandidate(changedTaxon);
260
                    input.replaceInModel(changedTaxon);
261
                }else if (((DescriptionElementBase)element).getInDescription() instanceof SpecimenDescription){
262
                    SpecimenOrObservationBase changedSpecimen = ((SpecimenDescription)((DescriptionElementBase)element).getInDescription()).getDescribedSpecimenOrObservation();
263
                    getEditorInput().addSaveCandidate(changedSpecimen);
264
                    input.replaceInModel(changedSpecimen);
265
                }
266
            }else if (element instanceof Media){
267
                IStructuredSelection sel = getSelection();
268
                Object firstElement = sel.getFirstElement();
269
                if (firstElement instanceof TaxonBase){
270
                    TaxonBase changedTaxon = (TaxonBase)sel.getFirstElement();
271
                    getEditorInput().addSaveCandidate(changedTaxon);
272
                    input.replaceInModel(changedTaxon);
273
                }else if (firstElement instanceof SpecimenOrObservationBase){
274
                    SpecimenOrObservationBase changedSpecimen = (SpecimenOrObservationBase)sel.getFirstElement();
275
                    getEditorInput().addSaveCandidate(changedSpecimen);
276
                    input.replaceInModel(changedSpecimen);
277
                }
278
            }else if(element instanceof Group){
279
                 Group oldGroup = ((GroupEditorInput)input).getEntityFromModel((Group)element);
280
                 ((GroupEditorInput)input).getSaveUserCandidates().addAll(oldGroup.getMembers());
281
                 getEditorInput().addSaveCandidate((Group)element);
282
                 input.replaceInModel((CdmBase) element);
283
            }else{
284
                getEditorInput().addSaveCandidate((CdmBase)element);
285
                input.replaceInModel((CdmBase) element);
286
            }
287
        }
288
        dirty.setDirty(true);
289
        setSelection(new StructuredSelection(element));
290
    }
291

    
292
    @Override
293
    public void forceDirty() {
294
        dirty.setDirty(true);
295
    }
296

    
297
    @Override
298
    public boolean postOperation(Object objectAffectedByOperation) {
299
        return false;
300
    }
301

    
302
    @Override
303
    public boolean onComplete() {
304
        return false;
305
    }
306

    
307
    @Override
308
    public ConversationHolder getConversationHolder() {
309
        return conversation;
310
    }
311

    
312
    public BulkEditorQuery getLastQuery() {
313
       return lastQuery;
314
    }
315

    
316
    public void setLastQuery(BulkEditorQuery lastQuery) {
317
       this.lastQuery = lastQuery;
318

    
319
    }
320

    
321
    @Override
322
    public Taxon getTaxon() {
323
        IStructuredSelection selection = getSelection();
324
        if(selection.size()==1) {
325
            Object object = selection.iterator().next();
326
            if(object instanceof Taxon){
327
                return (Taxon) object;
328
            }
329
        }
330
        return null;
331
    }
332

    
333
    @Override
334
    public void update() {
335
        input.performSearch(lastQuery, getSelection());
336
    }
337

    
338
    @Override
339
    public void addOperation(AbstractPostOperation operation) {
340
        // operations not yet used for bulk editor
341
    }
342

    
343
    @Inject
344
    @Optional
345
    private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_NAME_EDITOR) List<CdmBase> cdmBases) {
346
        if (getEditorInput() instanceof TaxonEditorInput){
347
            for (CdmBase cdmBase: cdmBases){
348
                if (getEditorInput().getModel().contains(cdmBase)){
349
                    input.performSearch(lastQuery, getSelection());
350
                    break;
351
                }
352
            }
353
        }
354
    }
355

    
356
    @Inject
357
    @Optional
358
    private void updatefromDelete(@UIEventTopic(WorkbenchEventConstants.REMOVE_USER) User user) {
359
        if (input instanceof GroupEditorInput){
360
            ((GroupEditorInput)input).getSaveUserCandidates().add(user);
361
        }
362
    }
363

    
364
    @Override
365
    public TaxonNode getTaxonNode() {
366
        return null;
367
    }
368

    
369
}
(3-3/9)