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
|
|
180
|
|
181
|
if(selection!=null){
|
182
|
setSelection(selection);
|
183
|
}
|
184
|
}
|
185
|
|
186
|
public void refresh(){
|
187
|
bulkEditorComposite.refresh();
|
188
|
}
|
189
|
|
190
|
public IStructuredSelection getSelection(){
|
191
|
return bulkEditorComposite.getSelection();
|
192
|
}
|
193
|
|
194
|
public void setSelection(IStructuredSelection selection){
|
195
|
bulkEditorComposite.setSelection(selection);
|
196
|
}
|
197
|
|
198
|
public void setDirty(boolean isDirty){
|
199
|
dirty.setDirty(isDirty);
|
200
|
}
|
201
|
|
202
|
public void setDirty(){
|
203
|
setDirty(true);
|
204
|
}
|
205
|
|
206
|
@Override
|
207
|
public boolean isDirty() {
|
208
|
return dirty.isDirty();
|
209
|
}
|
210
|
|
211
|
public AbstractBulkEditorInput getEditorInput() {
|
212
|
return input;
|
213
|
}
|
214
|
|
215
|
public void copyDataToClipboard() {
|
216
|
bulkEditorComposite.copyDataToClipboard();
|
217
|
}
|
218
|
|
219
|
@Override
|
220
|
public void update(CdmDataChangeMap arg0) {
|
221
|
}
|
222
|
|
223
|
@Override
|
224
|
public boolean canAttachMedia() {
|
225
|
return true;
|
226
|
}
|
227
|
|
228
|
@Override
|
229
|
public void changed(Object element) {
|
230
|
if(element instanceof DerivedUnitFacade){
|
231
|
DerivedUnit derivedUnit = ((DerivedUnitFacade) element).innerDerivedUnit();
|
232
|
if(derivedUnit!=null){
|
233
|
getEditorInput().addSaveCandidate(derivedUnit);
|
234
|
}
|
235
|
FieldUnit fieldUnit = ((DerivedUnitFacade) element).innerFieldUnit();
|
236
|
if(fieldUnit!=null){
|
237
|
getEditorInput().addSaveCandidate(fieldUnit);
|
238
|
}
|
239
|
}
|
240
|
else if (element instanceof CdmBase) {
|
241
|
if (element instanceof DescriptionBase){
|
242
|
if (element instanceof TaxonNameDescription){
|
243
|
TaxonName changedName = ((TaxonNameDescription)element).getTaxonName();
|
244
|
if (getEditorInput() instanceof TaxonEditorInput){
|
245
|
IStructuredSelection sel = getSelection();
|
246
|
Object firstElement = sel.getFirstElement();
|
247
|
if (firstElement instanceof TaxonBase){
|
248
|
getEditorInput().addSaveCandidate((TaxonBase)firstElement);
|
249
|
}
|
250
|
}else{
|
251
|
getEditorInput().addSaveCandidate(changedName);
|
252
|
}
|
253
|
|
254
|
input.replaceInModel(changedName);
|
255
|
}else if (element instanceof TaxonDescription){
|
256
|
Taxon changedTaxon = ((TaxonDescription)element).getTaxon();
|
257
|
getEditorInput().addSaveCandidate(changedTaxon);
|
258
|
input.replaceInModel(changedTaxon);
|
259
|
}
|
260
|
}else if (element instanceof DescriptionElementBase){
|
261
|
if (((DescriptionElementBase)element).getInDescription() instanceof TaxonNameDescription){
|
262
|
TaxonName changedName = ((TaxonNameDescription)((DescriptionElementBase)element).getInDescription()).getTaxonName();
|
263
|
//check whether the bulk editor is taxon or name bulk editor
|
264
|
if (getEditorInput() instanceof TaxonEditorInput){
|
265
|
IStructuredSelection sel = getSelection();
|
266
|
Object firstElement = sel.getFirstElement();
|
267
|
if (firstElement instanceof TaxonBase){
|
268
|
getEditorInput().addSaveCandidate((TaxonBase)firstElement);
|
269
|
}
|
270
|
}else{
|
271
|
getEditorInput().addSaveCandidate(changedName);
|
272
|
}
|
273
|
input.replaceInModel(changedName);
|
274
|
}else if (((DescriptionElementBase)element).getInDescription() instanceof TaxonDescription){
|
275
|
Taxon changedTaxon = ((TaxonDescription)((DescriptionElementBase)element).getInDescription()).getTaxon();
|
276
|
getEditorInput().addSaveCandidate(changedTaxon);
|
277
|
input.replaceInModel(changedTaxon);
|
278
|
}else if (((DescriptionElementBase)element).getInDescription() instanceof SpecimenDescription){
|
279
|
SpecimenOrObservationBase changedSpecimen = ((SpecimenDescription)((DescriptionElementBase)element).getInDescription()).getDescribedSpecimenOrObservation();
|
280
|
getEditorInput().addSaveCandidate(changedSpecimen);
|
281
|
input.replaceInModel(changedSpecimen);
|
282
|
}
|
283
|
}else if (element instanceof Media){
|
284
|
IStructuredSelection sel = getSelection();
|
285
|
Object firstElement = sel.getFirstElement();
|
286
|
if (firstElement instanceof TaxonBase){
|
287
|
TaxonBase changedTaxon = (TaxonBase)sel.getFirstElement();
|
288
|
getEditorInput().addSaveCandidate(changedTaxon);
|
289
|
input.replaceInModel(changedTaxon);
|
290
|
}else if (firstElement instanceof SpecimenOrObservationBase){
|
291
|
SpecimenOrObservationBase changedSpecimen = (SpecimenOrObservationBase)sel.getFirstElement();
|
292
|
getEditorInput().addSaveCandidate(changedSpecimen);
|
293
|
input.replaceInModel(changedSpecimen);
|
294
|
} else if (firstElement instanceof Media){
|
295
|
getEditorInput().addSaveCandidate((Media)element);
|
296
|
input.replaceInModel((Media)element);
|
297
|
}
|
298
|
}else if(element instanceof Group){
|
299
|
Group oldGroup = ((GroupEditorInput)input).getEntityFromModel((Group)element);
|
300
|
((GroupEditorInput)input).getSaveUserCandidates().addAll(oldGroup.getMembers());
|
301
|
getEditorInput().addSaveCandidate((Group)element);
|
302
|
input.replaceInModel((CdmBase) element);
|
303
|
}else{
|
304
|
getEditorInput().addSaveCandidate((CdmBase)element);
|
305
|
input.replaceInModel((CdmBase) element);
|
306
|
}
|
307
|
}
|
308
|
dirty.setDirty(true);
|
309
|
setSelection(new StructuredSelection(element));
|
310
|
}
|
311
|
|
312
|
@Override
|
313
|
public void forceDirty() {
|
314
|
dirty.setDirty(true);
|
315
|
}
|
316
|
|
317
|
@Override
|
318
|
public boolean postOperation(Object objectAffectedByOperation) {
|
319
|
return false;
|
320
|
}
|
321
|
|
322
|
@Override
|
323
|
public boolean onComplete() {
|
324
|
return false;
|
325
|
}
|
326
|
|
327
|
@Override
|
328
|
public ConversationHolder getConversationHolder() {
|
329
|
return conversation;
|
330
|
}
|
331
|
|
332
|
public BulkEditorQuery getLastQuery() {
|
333
|
return lastQuery;
|
334
|
}
|
335
|
|
336
|
public void setLastQuery(BulkEditorQuery lastQuery) {
|
337
|
this.lastQuery = lastQuery;
|
338
|
|
339
|
}
|
340
|
|
341
|
@Override
|
342
|
public Taxon getTaxon() {
|
343
|
IStructuredSelection selection = getSelection();
|
344
|
if(selection.size()==1) {
|
345
|
Object object = selection.iterator().next();
|
346
|
if(object instanceof Taxon){
|
347
|
return (Taxon) object;
|
348
|
}
|
349
|
}
|
350
|
return null;
|
351
|
}
|
352
|
|
353
|
@Override
|
354
|
public void update() {
|
355
|
input.performSearch(lastQuery, getSelection());
|
356
|
}
|
357
|
|
358
|
@Override
|
359
|
public void addOperation(AbstractPostOperation operation) {
|
360
|
// operations not yet used for bulk editor
|
361
|
}
|
362
|
|
363
|
@Inject
|
364
|
@Optional
|
365
|
private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_NAME_EDITOR) List<CdmBase> cdmBases) {
|
366
|
if (getEditorInput() instanceof TaxonEditorInput){
|
367
|
for (CdmBase cdmBase: cdmBases){
|
368
|
if (getEditorInput().getModel().contains(cdmBase)){
|
369
|
input.performSearch(lastQuery, getSelection());
|
370
|
break;
|
371
|
}
|
372
|
}
|
373
|
}
|
374
|
}
|
375
|
|
376
|
@Inject
|
377
|
@Optional
|
378
|
private void updatefromDelete(@UIEventTopic(WorkbenchEventConstants.REMOVE_USER) User user) {
|
379
|
if (input instanceof GroupEditorInput){
|
380
|
((GroupEditorInput)input).getSaveUserCandidates().add(user);
|
381
|
}
|
382
|
}
|
383
|
|
384
|
@Override
|
385
|
public TaxonNode getTaxonNode() {
|
386
|
return null;
|
387
|
}
|
388
|
|
389
|
}
|