1
|
/**
|
2
|
* Copyright (C) 2017 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.view.e4;
|
10
|
|
11
|
import java.util.Set;
|
12
|
|
13
|
import javax.annotation.PreDestroy;
|
14
|
import javax.inject.Inject;
|
15
|
import javax.inject.Named;
|
16
|
|
17
|
import org.apache.log4j.Logger;
|
18
|
import org.eclipse.e4.core.contexts.IEclipseContext;
|
19
|
import org.eclipse.e4.core.di.annotations.Optional;
|
20
|
import org.eclipse.e4.ui.di.PersistState;
|
21
|
import org.eclipse.e4.ui.di.UISynchronize;
|
22
|
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
23
|
import org.eclipse.e4.ui.services.IServiceConstants;
|
24
|
import org.eclipse.e4.ui.workbench.modeling.EPartService;
|
25
|
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
|
26
|
import org.eclipse.jface.viewers.ISelectionChangedListener;
|
27
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
28
|
import org.eclipse.jface.viewers.StructuredSelection;
|
29
|
import org.eclipse.jface.viewers.Viewer;
|
30
|
import org.eclipse.swt.SWTException;
|
31
|
|
32
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
33
|
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
|
34
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
35
|
import eu.etaxonomy.cdm.api.service.IVocabularyService;
|
36
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
37
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
38
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
39
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
40
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
|
41
|
import eu.etaxonomy.cdm.persistence.dto.TermDto;
|
42
|
import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
|
43
|
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
|
44
|
import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
|
45
|
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
|
46
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
47
|
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
|
48
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
49
|
import eu.etaxonomy.taxeditor.security.RequiredPermissions;
|
50
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
51
|
import eu.etaxonomy.taxeditor.view.e4.details.DetailsPartE4;
|
52
|
import eu.etaxonomy.taxeditor.view.e4.details.DetailsViewerE4;
|
53
|
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
|
54
|
import eu.etaxonomy.taxeditor.workbench.part.ISelectionElementEditingPart;
|
55
|
|
56
|
/**
|
57
|
* @author pplitzner
|
58
|
* @since Aug 10, 2017
|
59
|
*
|
60
|
*/
|
61
|
public abstract class AbstractCdmEditorPartE4
|
62
|
implements IConversationEnabled, IDirtyMarkable, ISelectionElementEditingPart, IPostOperationEnabled{
|
63
|
|
64
|
private DelaySelection delaySelection = null;
|
65
|
/**
|
66
|
* This is the monitor for the DelaySelection runnable.
|
67
|
* If it is <code>true</code> then it is currently delaying a selection.
|
68
|
*/
|
69
|
private boolean isInDelay;
|
70
|
private boolean isEnabled = true;
|
71
|
private static final Logger logger = Logger.getLogger(AbstractCdmEditorPartE4.class);
|
72
|
|
73
|
/**
|
74
|
* This class invokes internal_selectionChanged() in a separate thread.
|
75
|
* This allows an asynchronous and/or delayed handling of selection changes
|
76
|
*/
|
77
|
private class DelaySelection implements Runnable{
|
78
|
private Object selection;
|
79
|
private MPart activePart;
|
80
|
private MPart thisPart;
|
81
|
|
82
|
public DelaySelection(Object selection, MPart activePart, MPart thisPart) {
|
83
|
super();
|
84
|
this.selection = selection;
|
85
|
this.activePart= activePart;
|
86
|
this.thisPart = thisPart;
|
87
|
}
|
88
|
|
89
|
@Override
|
90
|
public void run() {
|
91
|
try{
|
92
|
selectionChanged_internal(selection, activePart, thisPart);
|
93
|
}
|
94
|
finally{
|
95
|
isInDelay = false;
|
96
|
}
|
97
|
}
|
98
|
|
99
|
public synchronized void setSelection(Object selection) {
|
100
|
this.selection = selection;
|
101
|
}
|
102
|
|
103
|
public synchronized void setActivePart(MPart activePart) {
|
104
|
this.activePart = activePart;
|
105
|
}
|
106
|
|
107
|
public synchronized void setThisPart(MPart thisPart) {
|
108
|
this.thisPart = thisPart;
|
109
|
}
|
110
|
|
111
|
}
|
112
|
|
113
|
protected Viewer viewer;
|
114
|
|
115
|
protected MPart thisPart;
|
116
|
|
117
|
protected MPart selectionProvidingPart;
|
118
|
|
119
|
protected Object previousSelection;
|
120
|
|
121
|
protected ISelectionChangedListener selectionChangedListener;
|
122
|
|
123
|
public ISelectionChangedListener getSelectionChangedListener() {
|
124
|
return selectionChangedListener;
|
125
|
}
|
126
|
|
127
|
@Inject
|
128
|
protected ESelectionService selService;
|
129
|
|
130
|
@Inject
|
131
|
protected IEclipseContext context;
|
132
|
|
133
|
protected abstract void selectionChanged_internal(Object selection, MPart activePart, MPart thisPart);
|
134
|
|
135
|
@Inject
|
136
|
public void selectionChanged(
|
137
|
@Optional@Named(IServiceConstants.ACTIVE_SELECTION)Object selection,
|
138
|
@Optional@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
|
139
|
MPart thisPart, UISynchronize sync, EPartService partService){
|
140
|
//multiple selections are not supported
|
141
|
if(activePart!=null
|
142
|
&& thisPart!=null
|
143
|
&& !activePart.equals(thisPart)
|
144
|
&& selection instanceof IStructuredSelection
|
145
|
&& ((IStructuredSelection) selection).size()>1){
|
146
|
showEmptyPage();
|
147
|
return;
|
148
|
}
|
149
|
// no active editor found
|
150
|
if(activePart==thisPart && WorkbenchUtility.getActiveEditorPart(partService)==null){
|
151
|
showEmptyPage();
|
152
|
return;
|
153
|
}
|
154
|
if (viewer != null && viewer.getControl()!= null && viewer.getInput() != null && !viewer.getControl().isDisposed()){
|
155
|
try{
|
156
|
viewer.getControl().setEnabled(isEnabled);
|
157
|
}catch(SWTException e){
|
158
|
logger.debug("Something went wrong for viewer.getControl().setEnabled(true) in " + this.getClass().getSimpleName(), e);
|
159
|
}
|
160
|
|
161
|
}
|
162
|
|
163
|
if((previousSelection!=null && selection!=null) && (activePart != null && selectionProvidingPart != null && activePart.equals(selectionProvidingPart)) &&
|
164
|
(previousSelection==selection
|
165
|
|| previousSelection.equals(selection)
|
166
|
|| new StructuredSelection(selection).equals(previousSelection))
|
167
|
) {
|
168
|
return;
|
169
|
}
|
170
|
if(delaySelection==null){
|
171
|
delaySelection = new DelaySelection(selection, activePart, thisPart);
|
172
|
}
|
173
|
delaySelection.setSelection(selection);
|
174
|
delaySelection.setActivePart(activePart);
|
175
|
delaySelection.setThisPart(thisPart);
|
176
|
if(!isInDelay){
|
177
|
isInDelay = true;
|
178
|
sync.asyncExec(delaySelection);
|
179
|
previousSelection = selection;
|
180
|
}
|
181
|
}
|
182
|
|
183
|
/** {@inheritDoc} */
|
184
|
@Override
|
185
|
public void changed(Object object) {
|
186
|
if(selectionProvidingPart!=null){
|
187
|
Object part = selectionProvidingPart.getObject();
|
188
|
if(part instanceof IDirtyMarkable){
|
189
|
((IDirtyMarkable) part).changed(object);
|
190
|
}
|
191
|
}
|
192
|
}
|
193
|
|
194
|
public Viewer getViewer() {
|
195
|
return viewer;
|
196
|
}
|
197
|
|
198
|
public boolean isEnabled() {
|
199
|
return isEnabled;
|
200
|
}
|
201
|
|
202
|
public void setEnabled(boolean isEnabled) {
|
203
|
this.isEnabled = isEnabled;
|
204
|
}
|
205
|
|
206
|
protected void showViewer(IStructuredSelection selection, MPart activePart, Viewer viewer){
|
207
|
if(CdmStore.getCurrentSessionManager().getActiveSession()==null){
|
208
|
return;
|
209
|
}
|
210
|
if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed()){
|
211
|
Object element = selection.getFirstElement();
|
212
|
Object part = createPartObject(activePart);
|
213
|
if (viewer.getControl().isDisposed()){
|
214
|
return;
|
215
|
}
|
216
|
viewer.getControl().setEnabled(true);
|
217
|
if(selection.getFirstElement()!=null){
|
218
|
if (element instanceof Taxon){
|
219
|
|
220
|
Taxon taxon = HibernateProxyHelper.deproxy(element, Taxon.class);
|
221
|
if (part instanceof ITaxonEditor){
|
222
|
TaxonNode node = ((ITaxonEditor) part).getTaxonNode();
|
223
|
if (node != null){
|
224
|
boolean doEnable = CdmStore.currentAuthentiationHasPermission(node,
|
225
|
RequiredPermissions.TAXON_EDIT);
|
226
|
|
227
|
//TODO: differ between the views
|
228
|
this.isEnabled = doEnable;
|
229
|
}
|
230
|
|
231
|
|
232
|
}
|
233
|
if (taxon.isMisapplication() || taxon.isProparteSynonym() || taxon.isInvalidDesignation()){
|
234
|
|
235
|
if(part instanceof ITaxonEditor){
|
236
|
Taxon accepted = ((ITaxonEditor) part).getTaxon();
|
237
|
|
238
|
Set<TaxonRelationship> rels = taxon.getTaxonRelations(accepted);
|
239
|
|
240
|
if (rels != null && rels.iterator().hasNext() && !taxon.equals(accepted)){
|
241
|
TaxonRelationship rel = rels.iterator().next();
|
242
|
if ((rel.getType().isMisappliedNameOrInvalidDesignation() || rel.getType().isAnySynonym()) && !rel.getFromTaxon().equals(((ITaxonEditor) part).getTaxon())){
|
243
|
viewer.setInput(rel);
|
244
|
selectionProvidingPart = activePart;
|
245
|
return;
|
246
|
}
|
247
|
}
|
248
|
}
|
249
|
|
250
|
|
251
|
}
|
252
|
}
|
253
|
//unwrap term DTOs
|
254
|
if(element instanceof TermDto){
|
255
|
element = CdmStore.getService(ITermService.class).load(((TermDto) element).getUuid());
|
256
|
}
|
257
|
else if(element instanceof TermVocabularyDto){
|
258
|
element = CdmStore.getService(IVocabularyService.class).load(((TermVocabularyDto) element).getUuid());
|
259
|
}
|
260
|
|
261
|
selectionProvidingPart = activePart;
|
262
|
if (viewer instanceof DetailsViewerE4){
|
263
|
|
264
|
if (!selectionProvidingPart.getElementId().equals("eu.etaxonomy.taxeditor.editor.view.checklist.e4.DistributionEditorPart")){
|
265
|
((DetailsViewerE4)viewer).setDetailsEnabled(true);
|
266
|
}
|
267
|
((DetailsViewerE4)viewer).setDetailsEnabled(isEnabled);
|
268
|
((DetailsViewerE4)viewer).setInput(element, part);
|
269
|
|
270
|
}
|
271
|
|
272
|
else{
|
273
|
if (activePart.getObject() instanceof DetailsPartE4 && element instanceof TaxonName){
|
274
|
selectionProvidingPart = ((DetailsPartE4)activePart.getObject()).getSelectionProvidingPart();
|
275
|
}
|
276
|
|
277
|
viewer.setInput(element);
|
278
|
viewer.getControl().setEnabled(isEnabled);
|
279
|
|
280
|
|
281
|
}
|
282
|
}
|
283
|
}
|
284
|
}
|
285
|
|
286
|
protected Object createPartObject(MPart activePart) {
|
287
|
Object partObject = activePart;
|
288
|
Object wrappedPart = WorkbenchUtility.getE4WrappedPart(activePart);
|
289
|
if(wrappedPart!=null){
|
290
|
partObject = wrappedPart;
|
291
|
}
|
292
|
return partObject;
|
293
|
}
|
294
|
|
295
|
protected void showEmptyPage() {
|
296
|
if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed() ){
|
297
|
viewer.setInput(null);
|
298
|
try{
|
299
|
if (!viewer.getControl().isDisposed()){
|
300
|
viewer.getControl().setEnabled(false);
|
301
|
}
|
302
|
}catch(SWTException e){
|
303
|
if (PreferencesUtil.isShowUpWidgetIsDisposedMessages() && e.getMessage().equals("Widget is disposed")){
|
304
|
MessagingUtils.errorDialog("Widget is disposed",
|
305
|
null,
|
306
|
MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
|
307
|
null,
|
308
|
e,
|
309
|
true);
|
310
|
|
311
|
}
|
312
|
}
|
313
|
|
314
|
}
|
315
|
reset();
|
316
|
if(thisPart!=null){
|
317
|
thisPart.setLabel(getViewName());
|
318
|
}
|
319
|
}
|
320
|
|
321
|
protected IStructuredSelection createSelection(Object selection) {
|
322
|
if(selection==null){
|
323
|
return null;
|
324
|
}
|
325
|
IStructuredSelection structuredSelection;
|
326
|
if(!(selection instanceof IStructuredSelection)){
|
327
|
structuredSelection = new StructuredSelection(selection);
|
328
|
}
|
329
|
else{
|
330
|
structuredSelection = (IStructuredSelection) selection;
|
331
|
}
|
332
|
return structuredSelection;
|
333
|
}
|
334
|
|
335
|
/**
|
336
|
* {@inheritDoc}
|
337
|
*/
|
338
|
@Override
|
339
|
public ConversationHolder getConversationHolder() {
|
340
|
if(selectionProvidingPart != null && selectionProvidingPart instanceof IConversationEnabled) {
|
341
|
return ((IConversationEnabled) selectionProvidingPart).getConversationHolder();
|
342
|
}
|
343
|
return null;
|
344
|
}
|
345
|
|
346
|
/**
|
347
|
* {@inheritDoc}
|
348
|
*/
|
349
|
@Override
|
350
|
public boolean postOperation(Object objectAffectedByOperation) {
|
351
|
changed(objectAffectedByOperation);
|
352
|
return true;
|
353
|
}
|
354
|
|
355
|
/**
|
356
|
* {@inheritDoc}
|
357
|
*/
|
358
|
@Override
|
359
|
public boolean onComplete() {
|
360
|
viewer.refresh();
|
361
|
return true;
|
362
|
}
|
363
|
|
364
|
/**
|
365
|
* {@inheritDoc}
|
366
|
*/
|
367
|
@Override
|
368
|
public MPart getSelectionProvidingPart() {
|
369
|
return selectionProvidingPart;
|
370
|
}
|
371
|
|
372
|
@PreDestroy
|
373
|
private void dispose() {
|
374
|
}
|
375
|
|
376
|
private void reset(){
|
377
|
previousSelection = null;
|
378
|
selectionProvidingPart = null;
|
379
|
delaySelection = null;
|
380
|
context.deactivate();
|
381
|
}
|
382
|
|
383
|
@PersistState
|
384
|
private void persistState(){
|
385
|
|
386
|
}
|
387
|
|
388
|
/**
|
389
|
* {@inheritDoc}
|
390
|
*/
|
391
|
@Override
|
392
|
public void update(CdmDataChangeMap arg0) {
|
393
|
}
|
394
|
|
395
|
/**
|
396
|
* {@inheritDoc}
|
397
|
*/
|
398
|
@Override
|
399
|
public void forceDirty() {
|
400
|
}
|
401
|
|
402
|
protected abstract String getViewName();
|
403
|
|
404
|
}
|