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.eclipse.e4.core.di.annotations.Optional;
|
18
|
import org.eclipse.e4.ui.di.PersistState;
|
19
|
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
20
|
import org.eclipse.e4.ui.services.IServiceConstants;
|
21
|
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
|
22
|
import org.eclipse.jface.viewers.ISelectionChangedListener;
|
23
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
24
|
import org.eclipse.jface.viewers.StructuredSelection;
|
25
|
import org.eclipse.jface.viewers.Viewer;
|
26
|
import org.eclipse.swt.widgets.Display;
|
27
|
|
28
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
29
|
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
|
30
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
31
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
32
|
import eu.etaxonomy.cdm.model.description.Distribution;
|
33
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
34
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
|
35
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
|
36
|
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
|
37
|
import eu.etaxonomy.taxeditor.editor.IDistributionEditor;
|
38
|
import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
|
39
|
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
|
40
|
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
|
41
|
import eu.etaxonomy.taxeditor.view.e4.details.DetailsViewerE4;
|
42
|
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
|
43
|
import eu.etaxonomy.taxeditor.workbench.part.ISelectionElementEditingPart;
|
44
|
|
45
|
/**
|
46
|
* @author pplitzner
|
47
|
* @since Aug 10, 2017
|
48
|
*
|
49
|
*/
|
50
|
public abstract class AbstractCdmEditorPartE4
|
51
|
implements IConversationEnabled, IDirtyMarkable, ISelectionElementEditingPart, IPostOperationEnabled{
|
52
|
|
53
|
private DelaySelection delaySelection = null;
|
54
|
/**
|
55
|
* This is the monitor for the DelaySelection runnable.
|
56
|
* If it is <code>true</code> then it is currently delaying a selection.
|
57
|
*/
|
58
|
private boolean isInDelay;
|
59
|
|
60
|
|
61
|
/**
|
62
|
* This class invokes internal_selectionChanged() in a separate thread.
|
63
|
* This allows an asynchronous and/or delayed handling of selection changes
|
64
|
*/
|
65
|
private class DelaySelection implements Runnable{
|
66
|
private Object selection;
|
67
|
private MPart activePart;
|
68
|
private MPart thisPart;
|
69
|
|
70
|
public DelaySelection(Object selection, MPart activePart, MPart thisPart) {
|
71
|
super();
|
72
|
this.selection = selection;
|
73
|
this.activePart= activePart;
|
74
|
this.thisPart = thisPart;
|
75
|
}
|
76
|
|
77
|
@Override
|
78
|
public void run() {
|
79
|
try{
|
80
|
selectionChanged_internal(selection, activePart, thisPart);
|
81
|
}
|
82
|
finally{
|
83
|
isInDelay = false;
|
84
|
}
|
85
|
}
|
86
|
|
87
|
public synchronized void setSelection(Object selection) {
|
88
|
this.selection = selection;
|
89
|
}
|
90
|
|
91
|
public synchronized void setActivePart(MPart activePart) {
|
92
|
this.activePart = activePart;
|
93
|
}
|
94
|
|
95
|
public synchronized void setThisPart(MPart thisPart) {
|
96
|
this.thisPart = thisPart;
|
97
|
}
|
98
|
|
99
|
}
|
100
|
|
101
|
protected Viewer viewer;
|
102
|
|
103
|
protected MPart thisPart;
|
104
|
|
105
|
protected MPart selectionProvidingPart;
|
106
|
|
107
|
protected Object previousSelection;
|
108
|
|
109
|
protected ISelectionChangedListener selectionChangedListener;
|
110
|
|
111
|
public ISelectionChangedListener getSelectionChangedListener() {
|
112
|
return selectionChangedListener;
|
113
|
}
|
114
|
|
115
|
@Inject
|
116
|
protected ESelectionService selService;
|
117
|
|
118
|
protected abstract void selectionChanged_internal(Object selection, MPart activePart, MPart thisPart);
|
119
|
|
120
|
@Inject
|
121
|
@Optional
|
122
|
public void selectionChanged(
|
123
|
@Named(IServiceConstants.ACTIVE_SELECTION)Object selection,
|
124
|
@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
|
125
|
MPart thisPart){
|
126
|
if(previousSelection==null ||
|
127
|
!previousSelection.equals(selection)){//skip redundant rendering of details view
|
128
|
Object savablePart = WorkbenchUtility.findSavablePart(this);
|
129
|
if(savablePart==null){
|
130
|
showEmptyPage();
|
131
|
}
|
132
|
if(delaySelection==null){
|
133
|
delaySelection = new DelaySelection(selection, activePart, thisPart);
|
134
|
}
|
135
|
delaySelection.setSelection(selection);
|
136
|
delaySelection.setActivePart(activePart);
|
137
|
delaySelection.setThisPart(thisPart);
|
138
|
if(!isInDelay){
|
139
|
isInDelay = true;
|
140
|
Display.getCurrent().asyncExec(delaySelection);
|
141
|
}
|
142
|
}
|
143
|
}
|
144
|
|
145
|
/** {@inheritDoc} */
|
146
|
@Override
|
147
|
public void changed(Object object) {
|
148
|
if(selectionProvidingPart!=null){
|
149
|
Object part = selectionProvidingPart.getObject();
|
150
|
if(part instanceof IDirtyMarkable){
|
151
|
((IDirtyMarkable) part).changed(object);
|
152
|
}
|
153
|
}
|
154
|
}
|
155
|
|
156
|
public Viewer getViewer() {
|
157
|
return viewer;
|
158
|
}
|
159
|
|
160
|
protected void showViewer(IStructuredSelection selection, MPart activePart, Viewer viewer){
|
161
|
if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed()){
|
162
|
Object element = selection.getFirstElement();
|
163
|
Object part = createPartObject(activePart);
|
164
|
if(selection.getFirstElement()!=null){
|
165
|
if (element instanceof Taxon){
|
166
|
Taxon taxon = HibernateProxyHelper.deproxy(element, Taxon.class);
|
167
|
if (taxon.isMisapplication()){
|
168
|
|
169
|
if(part instanceof ITaxonEditor){
|
170
|
Taxon accepted = ((ITaxonEditor) part).getTaxon();
|
171
|
|
172
|
// Taxon accepted= ((ITaxonEditor)activePart).getTaxon();
|
173
|
Set<TaxonRelationship> rels = taxon.getTaxonRelations(accepted);
|
174
|
|
175
|
if (rels.iterator().hasNext()){
|
176
|
TaxonRelationship rel = rels.iterator().next();
|
177
|
if (rel.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR()) && !rel.getFromTaxon().equals(((ITaxonEditor) part).getTaxon())){
|
178
|
viewer.setInput(rel);
|
179
|
|
180
|
return;
|
181
|
}
|
182
|
}
|
183
|
}
|
184
|
|
185
|
|
186
|
}
|
187
|
}
|
188
|
if (element instanceof Distribution && part instanceof IDistributionEditor){
|
189
|
((DetailsViewerE4)viewer).setInput(element, part);
|
190
|
}else{
|
191
|
viewer.setInput(element);
|
192
|
}
|
193
|
|
194
|
}
|
195
|
selectionProvidingPart = activePart;
|
196
|
previousSelection = selection;
|
197
|
}
|
198
|
}
|
199
|
|
200
|
protected Object createPartObject(MPart activePart) {
|
201
|
Object partObject = activePart;
|
202
|
Object wrappedPart = WorkbenchUtility.getE4WrappedPart(activePart);
|
203
|
if(wrappedPart!=null){
|
204
|
partObject = wrappedPart;
|
205
|
}
|
206
|
return partObject;
|
207
|
}
|
208
|
|
209
|
protected void showEmptyPage() {
|
210
|
if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed()){
|
211
|
viewer.setInput(null);
|
212
|
}
|
213
|
selectionProvidingPart = null;
|
214
|
if(thisPart!=null){
|
215
|
thisPart.setLabel(getViewName());
|
216
|
}
|
217
|
}
|
218
|
|
219
|
protected IStructuredSelection createSelection(Object selection) {
|
220
|
if(selection==null){
|
221
|
return null;
|
222
|
}
|
223
|
IStructuredSelection structuredSelection;
|
224
|
if(!(selection instanceof IStructuredSelection)){
|
225
|
structuredSelection = new StructuredSelection(selection);
|
226
|
}
|
227
|
else{
|
228
|
structuredSelection = (IStructuredSelection) selection;
|
229
|
}
|
230
|
return structuredSelection;
|
231
|
}
|
232
|
|
233
|
/**
|
234
|
* {@inheritDoc}
|
235
|
*/
|
236
|
@Override
|
237
|
public ConversationHolder getConversationHolder() {
|
238
|
if(selectionProvidingPart != null && selectionProvidingPart instanceof IConversationEnabled) {
|
239
|
return ((IConversationEnabled) selectionProvidingPart).getConversationHolder();
|
240
|
}
|
241
|
return null;
|
242
|
}
|
243
|
|
244
|
/**
|
245
|
* {@inheritDoc}
|
246
|
*/
|
247
|
@Override
|
248
|
public boolean postOperation(CdmBase objectAffectedByOperation) {
|
249
|
changed(objectAffectedByOperation);
|
250
|
viewer.setInput(objectAffectedByOperation);
|
251
|
return true;
|
252
|
}
|
253
|
|
254
|
/**
|
255
|
* {@inheritDoc}
|
256
|
*/
|
257
|
@Override
|
258
|
public boolean onComplete() {
|
259
|
viewer.refresh();
|
260
|
return true;
|
261
|
}
|
262
|
|
263
|
/**
|
264
|
* {@inheritDoc}
|
265
|
*/
|
266
|
@Override
|
267
|
public Object getSelectionProvidingPart() {
|
268
|
return selectionProvidingPart;
|
269
|
}
|
270
|
|
271
|
@PreDestroy
|
272
|
private void dispose() {
|
273
|
}
|
274
|
|
275
|
@PersistState
|
276
|
private void persistState(){
|
277
|
|
278
|
}
|
279
|
|
280
|
/**
|
281
|
* {@inheritDoc}
|
282
|
*/
|
283
|
@Override
|
284
|
public void update(CdmDataChangeMap arg0) {
|
285
|
}
|
286
|
|
287
|
/**
|
288
|
* {@inheritDoc}
|
289
|
*/
|
290
|
@Override
|
291
|
public void forceDirty() {
|
292
|
}
|
293
|
|
294
|
protected abstract String getViewName();
|
295
|
|
296
|
}
|