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.hibernate.HibernateProxyHelper;
|
35
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
36
|
import eu.etaxonomy.cdm.model.description.Distribution;
|
37
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
38
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
|
39
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
|
40
|
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
|
41
|
import eu.etaxonomy.taxeditor.editor.IDistributionEditor;
|
42
|
import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
|
43
|
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
|
44
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
45
|
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
|
46
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
47
|
import eu.etaxonomy.taxeditor.view.e4.details.DetailsViewerE4;
|
48
|
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
|
49
|
import eu.etaxonomy.taxeditor.workbench.part.ISelectionElementEditingPart;
|
50
|
|
51
|
/**
|
52
|
* @author pplitzner
|
53
|
* @since Aug 10, 2017
|
54
|
*
|
55
|
*/
|
56
|
public abstract class AbstractCdmEditorPartE4
|
57
|
implements IConversationEnabled, IDirtyMarkable, ISelectionElementEditingPart, IPostOperationEnabled{
|
58
|
|
59
|
private DelaySelection delaySelection = null;
|
60
|
/**
|
61
|
* This is the monitor for the DelaySelection runnable.
|
62
|
* If it is <code>true</code> then it is currently delaying a selection.
|
63
|
*/
|
64
|
private boolean isInDelay;
|
65
|
private static final Logger logger = Logger.getLogger(AbstractCdmEditorPartE4.class);
|
66
|
|
67
|
/**
|
68
|
* This class invokes internal_selectionChanged() in a separate thread.
|
69
|
* This allows an asynchronous and/or delayed handling of selection changes
|
70
|
*/
|
71
|
private class DelaySelection implements Runnable{
|
72
|
private Object selection;
|
73
|
private MPart activePart;
|
74
|
private MPart thisPart;
|
75
|
|
76
|
public DelaySelection(Object selection, MPart activePart, MPart thisPart) {
|
77
|
super();
|
78
|
this.selection = selection;
|
79
|
this.activePart= activePart;
|
80
|
this.thisPart = thisPart;
|
81
|
}
|
82
|
|
83
|
@Override
|
84
|
public void run() {
|
85
|
try{
|
86
|
selectionChanged_internal(selection, activePart, thisPart);
|
87
|
}
|
88
|
finally{
|
89
|
isInDelay = false;
|
90
|
}
|
91
|
}
|
92
|
|
93
|
public synchronized void setSelection(Object selection) {
|
94
|
this.selection = selection;
|
95
|
}
|
96
|
|
97
|
public synchronized void setActivePart(MPart activePart) {
|
98
|
this.activePart = activePart;
|
99
|
}
|
100
|
|
101
|
public synchronized void setThisPart(MPart thisPart) {
|
102
|
this.thisPart = thisPart;
|
103
|
}
|
104
|
|
105
|
}
|
106
|
|
107
|
protected Viewer viewer;
|
108
|
|
109
|
protected MPart thisPart;
|
110
|
|
111
|
protected MPart selectionProvidingPart;
|
112
|
|
113
|
protected Object previousSelection;
|
114
|
|
115
|
public Object getPreviousSelection() {
|
116
|
return previousSelection;
|
117
|
}
|
118
|
|
119
|
public void setPreviousSelection(Object previousSelection) {
|
120
|
this.previousSelection = previousSelection;
|
121
|
}
|
122
|
|
123
|
protected ISelectionChangedListener selectionChangedListener;
|
124
|
|
125
|
public ISelectionChangedListener getSelectionChangedListener() {
|
126
|
return selectionChangedListener;
|
127
|
}
|
128
|
|
129
|
@Inject
|
130
|
protected ESelectionService selService;
|
131
|
|
132
|
@Inject
|
133
|
protected IEclipseContext context;
|
134
|
|
135
|
protected abstract void selectionChanged_internal(Object selection, MPart activePart, MPart thisPart);
|
136
|
|
137
|
@Inject
|
138
|
public void selectionChanged(
|
139
|
@Optional@Named(IServiceConstants.ACTIVE_SELECTION)Object selection,
|
140
|
@Optional@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
|
141
|
MPart thisPart, UISynchronize sync, EPartService partService){
|
142
|
if(activePart==thisPart && WorkbenchUtility.getActiveEditorPart(partService)==null){
|
143
|
showEmptyPage();
|
144
|
return;
|
145
|
}
|
146
|
if (viewer != null && viewer.getControl()!= null && viewer.getInput() != null && !viewer.getControl().isDisposed()){
|
147
|
try{
|
148
|
viewer.getControl().setEnabled(true);
|
149
|
}catch(SWTException e){
|
150
|
logger.debug("Something went wrong for viewer.getControl().setEnabled(true) in " + this.getClass().getSimpleName(), e);
|
151
|
}
|
152
|
|
153
|
}
|
154
|
|
155
|
|
156
|
if(previousSelection==null ||
|
157
|
previousSelection!=selection){//skip redundant rendering of details view
|
158
|
if(delaySelection==null){
|
159
|
delaySelection = new DelaySelection(selection, activePart, thisPart);
|
160
|
}
|
161
|
delaySelection.setSelection(selection);
|
162
|
delaySelection.setActivePart(activePart);
|
163
|
delaySelection.setThisPart(thisPart);
|
164
|
if(!isInDelay){
|
165
|
isInDelay = true;
|
166
|
sync.asyncExec(delaySelection);
|
167
|
previousSelection = selection;
|
168
|
}
|
169
|
}
|
170
|
}
|
171
|
|
172
|
/** {@inheritDoc} */
|
173
|
@Override
|
174
|
public void changed(Object object) {
|
175
|
if(selectionProvidingPart!=null){
|
176
|
Object part = selectionProvidingPart.getObject();
|
177
|
if(part instanceof IDirtyMarkable){
|
178
|
((IDirtyMarkable) part).changed(object);
|
179
|
}
|
180
|
}
|
181
|
}
|
182
|
|
183
|
public Viewer getViewer() {
|
184
|
return viewer;
|
185
|
}
|
186
|
|
187
|
protected void showViewer(IStructuredSelection selection, MPart activePart, Viewer viewer){
|
188
|
if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed()){
|
189
|
Object element = selection.getFirstElement();
|
190
|
Object part = createPartObject(activePart);
|
191
|
viewer.getControl().setEnabled(true);
|
192
|
if(selection.getFirstElement()!=null){
|
193
|
if (element instanceof Taxon){
|
194
|
Taxon taxon = HibernateProxyHelper.deproxy(element, Taxon.class);
|
195
|
if (taxon.isMisapplication() || taxon.isProparteSynonym()){
|
196
|
|
197
|
if(part instanceof ITaxonEditor){
|
198
|
Taxon accepted = ((ITaxonEditor) part).getTaxon();
|
199
|
Set<TaxonRelationship> rels = taxon.getTaxonRelations(accepted);
|
200
|
|
201
|
if (rels != null && rels.iterator().hasNext()){
|
202
|
TaxonRelationship rel = rels.iterator().next();
|
203
|
if ((rel.getType().isAnyMisappliedName() || rel.getType().equals(TaxonRelationshipType.PRO_PARTE_SYNONYM_FOR()))&& !rel.getFromTaxon().equals(((ITaxonEditor) part).getTaxon())){
|
204
|
viewer.setInput(rel);
|
205
|
|
206
|
return;
|
207
|
}
|
208
|
}
|
209
|
}
|
210
|
|
211
|
|
212
|
}
|
213
|
}
|
214
|
if (element instanceof Distribution && part instanceof IDistributionEditor){
|
215
|
((DetailsViewerE4)viewer).setInput(element, part);
|
216
|
}else{
|
217
|
viewer.setInput(element);
|
218
|
if (viewer instanceof DetailsViewerE4){
|
219
|
((DetailsViewerE4)viewer).setDetailsEnabled(true);
|
220
|
}
|
221
|
}
|
222
|
selectionProvidingPart = activePart;
|
223
|
}
|
224
|
}
|
225
|
}
|
226
|
|
227
|
protected Object createPartObject(MPart activePart) {
|
228
|
Object partObject = activePart;
|
229
|
Object wrappedPart = WorkbenchUtility.getE4WrappedPart(activePart);
|
230
|
if(wrappedPart!=null){
|
231
|
partObject = wrappedPart;
|
232
|
}
|
233
|
return partObject;
|
234
|
}
|
235
|
|
236
|
protected void showEmptyPage() {
|
237
|
if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed() ){
|
238
|
viewer.setInput(null);
|
239
|
try{
|
240
|
if (!viewer.getControl().isDisposed()){
|
241
|
viewer.getControl().setEnabled(false);
|
242
|
}
|
243
|
}catch(SWTException e){
|
244
|
if (PreferencesUtil.isShowUpWidgetIsDisposedMessages() && e.getMessage().equals("Widget is disposed")){
|
245
|
MessagingUtils.errorDialog("Widget is disposed",
|
246
|
null,
|
247
|
MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
|
248
|
null,
|
249
|
e,
|
250
|
true);
|
251
|
|
252
|
}
|
253
|
}
|
254
|
|
255
|
}
|
256
|
reset();
|
257
|
if(thisPart!=null){
|
258
|
thisPart.setLabel(getViewName());
|
259
|
}
|
260
|
}
|
261
|
|
262
|
protected IStructuredSelection createSelection(Object selection) {
|
263
|
if(selection==null){
|
264
|
return null;
|
265
|
}
|
266
|
IStructuredSelection structuredSelection;
|
267
|
if(!(selection instanceof IStructuredSelection)){
|
268
|
structuredSelection = new StructuredSelection(selection);
|
269
|
}
|
270
|
else{
|
271
|
structuredSelection = (IStructuredSelection) selection;
|
272
|
}
|
273
|
return structuredSelection;
|
274
|
}
|
275
|
|
276
|
/**
|
277
|
* {@inheritDoc}
|
278
|
*/
|
279
|
@Override
|
280
|
public ConversationHolder getConversationHolder() {
|
281
|
if(selectionProvidingPart != null && selectionProvidingPart instanceof IConversationEnabled) {
|
282
|
return ((IConversationEnabled) selectionProvidingPart).getConversationHolder();
|
283
|
}
|
284
|
return null;
|
285
|
}
|
286
|
|
287
|
/**
|
288
|
* {@inheritDoc}
|
289
|
*/
|
290
|
@Override
|
291
|
public boolean postOperation(CdmBase objectAffectedByOperation) {
|
292
|
changed(objectAffectedByOperation);
|
293
|
return true;
|
294
|
}
|
295
|
|
296
|
/**
|
297
|
* {@inheritDoc}
|
298
|
*/
|
299
|
@Override
|
300
|
public boolean onComplete() {
|
301
|
viewer.refresh();
|
302
|
return true;
|
303
|
}
|
304
|
|
305
|
/**
|
306
|
* {@inheritDoc}
|
307
|
*/
|
308
|
@Override
|
309
|
public MPart getSelectionProvidingPart() {
|
310
|
return selectionProvidingPart;
|
311
|
}
|
312
|
|
313
|
@PreDestroy
|
314
|
private void dispose() {
|
315
|
}
|
316
|
|
317
|
private void reset(){
|
318
|
previousSelection = null;
|
319
|
selectionProvidingPart = null;
|
320
|
delaySelection = null;
|
321
|
context.deactivate();
|
322
|
}
|
323
|
|
324
|
@PersistState
|
325
|
private void persistState(){
|
326
|
|
327
|
}
|
328
|
|
329
|
/**
|
330
|
* {@inheritDoc}
|
331
|
*/
|
332
|
@Override
|
333
|
public void update(CdmDataChangeMap arg0) {
|
334
|
}
|
335
|
|
336
|
/**
|
337
|
* {@inheritDoc}
|
338
|
*/
|
339
|
@Override
|
340
|
public void forceDirty() {
|
341
|
}
|
342
|
|
343
|
protected abstract String getViewName();
|
344
|
|
345
|
}
|