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
|
|
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.TaxonDescription;
|
41
|
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
|
42
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
43
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
|
44
|
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
|
45
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
46
|
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
|
47
|
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorQuery;
|
48
|
import eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput;
|
49
|
import eu.etaxonomy.taxeditor.bulkeditor.input.TaxonEditorInput;
|
50
|
import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
|
51
|
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
|
52
|
import eu.etaxonomy.taxeditor.model.IDerivedUnitFacadePart;
|
53
|
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
|
54
|
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
|
55
|
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
|
56
|
import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
|
57
|
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
|
58
|
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
|
59
|
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
|
60
|
|
61
|
/**
|
62
|
*
|
63
|
* @author pplitzner
|
64
|
* @since Sep 8, 2017
|
65
|
*
|
66
|
*/
|
67
|
public class BulkEditorE4 implements IPartContentHasDetails, IConversationEnabled, IPostOperationEnabled,
|
68
|
IDirtyMarkable, IDerivedUnitFacadePart, IPartContentHasFactualData,
|
69
|
IPartContentHasSupplementalData, IPartContentHasMedia, IE4SavablePart, ITaxonEditor {
|
70
|
|
71
|
@Inject
|
72
|
private MDirtyable dirty;
|
73
|
|
74
|
private AbstractBulkEditorInput input;
|
75
|
|
76
|
private ConversationHolder conversation;
|
77
|
|
78
|
@Inject
|
79
|
private IEventBroker eventBroker;
|
80
|
|
81
|
@Inject
|
82
|
IEclipseContext context;
|
83
|
|
84
|
@Inject
|
85
|
private MPart thisPart;
|
86
|
|
87
|
private BulkEditorQuery lastQuery = null;
|
88
|
|
89
|
private BulkEditorE4Composite bulkEditorComposite;
|
90
|
|
91
|
@Inject
|
92
|
public BulkEditorE4() {
|
93
|
}
|
94
|
|
95
|
@SuppressWarnings("unused")
|
96
|
public void init(AbstractBulkEditorInput<?> input){
|
97
|
this.input = input;
|
98
|
this.conversation = input.getConversation();
|
99
|
thisPart.setLabel(input.getEditorName());
|
100
|
|
101
|
bulkEditorComposite.init(input);
|
102
|
}
|
103
|
|
104
|
/** {@inheritDoc} */
|
105
|
@PostConstruct
|
106
|
public void createPartControl(Composite parent, IEclipseContext context) {
|
107
|
bulkEditorComposite = new BulkEditorE4Composite(this, parent, SWT.NONE);
|
108
|
ContextInjectionFactory.inject(bulkEditorComposite, context);
|
109
|
}
|
110
|
|
111
|
@Override
|
112
|
@Persist
|
113
|
public void save(IProgressMonitor monitor) {
|
114
|
save(monitor, true);
|
115
|
}
|
116
|
|
117
|
public void save(IProgressMonitor monitor, boolean resetMerge) {
|
118
|
if (!input.getCdmEntitySession().isActive()){
|
119
|
input.getCdmEntitySession().bind();
|
120
|
}
|
121
|
input.saveModel(resetMerge);
|
122
|
|
123
|
IStructuredSelection selection = getSelection();
|
124
|
|
125
|
dirty.setDirty(false);
|
126
|
input.dispose();
|
127
|
input.bind();
|
128
|
conversation.commit(true);
|
129
|
|
130
|
if (lastQuery != null){
|
131
|
bulkEditorComposite.performSearch(lastQuery, selection);
|
132
|
}
|
133
|
}
|
134
|
|
135
|
|
136
|
@Focus
|
137
|
public void setFocus() {
|
138
|
//make sure to bind again if maybe in another view the conversation was unbound
|
139
|
if(conversation!=null && !conversation.isBound()){
|
140
|
conversation.bind();
|
141
|
}
|
142
|
if(input!=null && input.getCdmEntitySession()!= null) {
|
143
|
input.getCdmEntitySession().bind();
|
144
|
}
|
145
|
|
146
|
//make sure to bind again if maybe in another view the conversation was unbound
|
147
|
eventBroker.post(WorkbenchEventConstants.CURRENT_ACTIVE_EDITOR, this);
|
148
|
}
|
149
|
|
150
|
@PreDestroy
|
151
|
public void dispose() {
|
152
|
if(conversation!=null){
|
153
|
conversation.unregisterForDataStoreChanges(this);
|
154
|
conversation.close();
|
155
|
}
|
156
|
if(input!=null){
|
157
|
input.dispose();
|
158
|
}
|
159
|
dirty.setDirty(false);
|
160
|
//save table settings
|
161
|
if(bulkEditorComposite.getNatTableState()!=null){
|
162
|
try (FileOutputStream tableStateStream =
|
163
|
new FileOutputStream(bulkEditorComposite.getStatePropertiesFile())) {
|
164
|
bulkEditorComposite.getNatTableState().store(tableStateStream, null);
|
165
|
} catch (IOException ioe) {
|
166
|
ioe.printStackTrace();
|
167
|
}
|
168
|
}
|
169
|
}
|
170
|
|
171
|
@Optional
|
172
|
@Inject
|
173
|
private void updateAfterSearch(@UIEventTopic(WorkbenchEventConstants.BULK_EDITOR_SEARCH_FINISHED)IStructuredSelection selection){
|
174
|
if(selection!=null){
|
175
|
setSelection(selection);
|
176
|
}
|
177
|
}
|
178
|
|
179
|
public void refresh(){
|
180
|
bulkEditorComposite.refresh();
|
181
|
}
|
182
|
|
183
|
public IStructuredSelection getSelection(){
|
184
|
return bulkEditorComposite.getSelection();
|
185
|
}
|
186
|
|
187
|
public void setSelection(IStructuredSelection selection){
|
188
|
bulkEditorComposite.setSelection(selection);
|
189
|
}
|
190
|
|
191
|
public void setDirty(boolean isDirty){
|
192
|
dirty.setDirty(isDirty);
|
193
|
}
|
194
|
|
195
|
public void setDirty(){
|
196
|
setDirty(true);
|
197
|
}
|
198
|
|
199
|
@Override
|
200
|
public boolean isDirty() {
|
201
|
return dirty.isDirty();
|
202
|
}
|
203
|
|
204
|
public AbstractBulkEditorInput getEditorInput() {
|
205
|
return input;
|
206
|
}
|
207
|
|
208
|
public void copyDataToClipboard() {
|
209
|
bulkEditorComposite.copyDataToClipboard();
|
210
|
}
|
211
|
|
212
|
@Override
|
213
|
public void update(CdmDataChangeMap arg0) {
|
214
|
}
|
215
|
|
216
|
@Override
|
217
|
public boolean canAttachMedia() {
|
218
|
return true;
|
219
|
}
|
220
|
|
221
|
@Override
|
222
|
public void changed(Object element) {
|
223
|
if(element instanceof DerivedUnitFacade){
|
224
|
DerivedUnit derivedUnit = ((DerivedUnitFacade) element).innerDerivedUnit();
|
225
|
if(derivedUnit!=null){
|
226
|
getEditorInput().addSaveCandidate(derivedUnit);
|
227
|
}
|
228
|
FieldUnit fieldUnit = ((DerivedUnitFacade) element).innerFieldUnit();
|
229
|
if(fieldUnit!=null){
|
230
|
getEditorInput().addSaveCandidate(fieldUnit);
|
231
|
}
|
232
|
}
|
233
|
else if (element instanceof CdmBase) {
|
234
|
if (element instanceof DescriptionBase){
|
235
|
if (element instanceof TaxonNameDescription){
|
236
|
TaxonName changedName = ((TaxonNameDescription)element).getTaxonName();
|
237
|
getEditorInput().addSaveCandidate(changedName);
|
238
|
input.replaceInModel(changedName);
|
239
|
}else if (element instanceof TaxonDescription){
|
240
|
Taxon changedTaxon = ((TaxonDescription)element).getTaxon();
|
241
|
getEditorInput().addSaveCandidate(changedTaxon);
|
242
|
input.replaceInModel(changedTaxon);
|
243
|
}
|
244
|
}else if (element instanceof DescriptionElementBase){
|
245
|
if (((DescriptionElementBase)element).getInDescription() instanceof TaxonNameDescription){
|
246
|
TaxonName changedName = ((TaxonNameDescription)((DescriptionElementBase)element).getInDescription()).getTaxonName();
|
247
|
getEditorInput().addSaveCandidate(changedName);
|
248
|
input.replaceInModel(changedName);
|
249
|
}else if (((DescriptionElementBase)element).getInDescription() instanceof TaxonDescription){
|
250
|
Taxon changedTaxon = ((TaxonDescription)((DescriptionElementBase)element).getInDescription()).getTaxon();
|
251
|
getEditorInput().addSaveCandidate(changedTaxon);
|
252
|
input.replaceInModel(changedTaxon);
|
253
|
}
|
254
|
}else{
|
255
|
getEditorInput().addSaveCandidate((CdmBase)element);
|
256
|
input.replaceInModel((CdmBase) element);
|
257
|
}
|
258
|
}
|
259
|
dirty.setDirty(true);
|
260
|
setSelection(new StructuredSelection(element));
|
261
|
}
|
262
|
|
263
|
@Override
|
264
|
public void forceDirty() {
|
265
|
dirty.setDirty(true);
|
266
|
}
|
267
|
|
268
|
@Override
|
269
|
public boolean postOperation(Object objectAffectedByOperation) {
|
270
|
return false;
|
271
|
}
|
272
|
|
273
|
@Override
|
274
|
public boolean onComplete() {
|
275
|
return false;
|
276
|
}
|
277
|
|
278
|
@Override
|
279
|
public ConversationHolder getConversationHolder() {
|
280
|
return conversation;
|
281
|
}
|
282
|
|
283
|
public BulkEditorQuery getLastQuery() {
|
284
|
return lastQuery;
|
285
|
}
|
286
|
|
287
|
public void setLastQuery(BulkEditorQuery lastQuery) {
|
288
|
this.lastQuery = lastQuery;
|
289
|
|
290
|
}
|
291
|
|
292
|
@Override
|
293
|
public Taxon getTaxon() {
|
294
|
IStructuredSelection selection = getSelection();
|
295
|
if(selection.size()==1) {
|
296
|
Object object = selection.iterator().next();
|
297
|
if(object instanceof Taxon){
|
298
|
return (Taxon) object;
|
299
|
}
|
300
|
}
|
301
|
return null;
|
302
|
}
|
303
|
|
304
|
/**
|
305
|
* {@inheritDoc}
|
306
|
*/
|
307
|
@Override
|
308
|
public void update() {
|
309
|
input.performSearch(lastQuery, getSelection());
|
310
|
|
311
|
}
|
312
|
|
313
|
@Inject
|
314
|
@Optional
|
315
|
private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_NAME_EDITOR) CdmBase cdmbase) {
|
316
|
|
317
|
if (getEditorInput() instanceof TaxonEditorInput && getEditorInput().getModel().contains(cdmbase)){
|
318
|
input.performSearch(lastQuery, getSelection());
|
319
|
}
|
320
|
}
|
321
|
|
322
|
}
|