2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.view
.e4
;
13 import javax
.annotation
.PreDestroy
;
14 import javax
.inject
.Inject
;
15 import javax
.inject
.Named
;
17 import org
.eclipse
.e4
.core
.di
.annotations
.Optional
;
18 import org
.eclipse
.e4
.ui
.di
.PersistState
;
19 import org
.eclipse
.e4
.ui
.di
.UISynchronize
;
20 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPart
;
21 import org
.eclipse
.e4
.ui
.services
.IServiceConstants
;
22 import org
.eclipse
.e4
.ui
.workbench
.modeling
.EPartService
;
23 import org
.eclipse
.e4
.ui
.workbench
.modeling
.ESelectionService
;
24 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
25 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
26 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
27 import org
.eclipse
.jface
.viewers
.Viewer
;
29 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
30 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
31 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
32 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
33 import eu
.etaxonomy
.cdm
.model
.description
.Distribution
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationshipType
;
37 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
38 import eu
.etaxonomy
.taxeditor
.editor
.IDistributionEditor
;
39 import eu
.etaxonomy
.taxeditor
.editor
.ITaxonEditor
;
40 import eu
.etaxonomy
.taxeditor
.event
.EventUtility
;
41 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkable
;
42 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
43 import eu
.etaxonomy
.taxeditor
.view
.e4
.details
.DetailsViewerE4
;
44 import eu
.etaxonomy
.taxeditor
.workbench
.WorkbenchUtility
;
45 import eu
.etaxonomy
.taxeditor
.workbench
.part
.ISelectionElementEditingPart
;
52 public abstract class AbstractCdmEditorPartE4
53 implements IConversationEnabled
, IDirtyMarkable
, ISelectionElementEditingPart
, IPostOperationEnabled
{
55 private DelaySelection delaySelection
= null;
57 * This is the monitor for the DelaySelection runnable.
58 * If it is <code>true</code> then it is currently delaying a selection.
60 private boolean isInDelay
;
64 * This class invokes internal_selectionChanged() in a separate thread.
65 * This allows an asynchronous and/or delayed handling of selection changes
67 private class DelaySelection
implements Runnable
{
68 private Object selection
;
69 private MPart activePart
;
70 private MPart thisPart
;
72 public DelaySelection(Object selection
, MPart activePart
, MPart thisPart
) {
74 this.selection
= selection
;
75 this.activePart
= activePart
;
76 this.thisPart
= thisPart
;
82 selectionChanged_internal(selection
, activePart
, thisPart
);
89 public synchronized void setSelection(Object selection
) {
90 this.selection
= selection
;
93 public synchronized void setActivePart(MPart activePart
) {
94 this.activePart
= activePart
;
97 public synchronized void setThisPart(MPart thisPart
) {
98 this.thisPart
= thisPart
;
103 protected Viewer viewer
;
105 protected MPart thisPart
;
107 protected MPart selectionProvidingPart
;
109 protected Object previousSelection
;
111 protected ISelectionChangedListener selectionChangedListener
;
113 public ISelectionChangedListener
getSelectionChangedListener() {
114 return selectionChangedListener
;
118 protected ESelectionService selService
;
120 protected abstract void selectionChanged_internal(Object selection
, MPart activePart
, MPart thisPart
);
123 public void selectionChanged(
124 @Optional@Named(IServiceConstants
.ACTIVE_SELECTION
)Object selection
,
125 @Optional@Named(IServiceConstants
.ACTIVE_PART
)MPart activePart
,
126 MPart thisPart
, UISynchronize sync
, EPartService partService
){
127 if(activePart
==thisPart
&& EventUtility
.getActiveEditorPart(partService
)==null){
131 if (viewer
!= null && viewer
.getControl()!= null && viewer
.getInput() != null && !viewer
.getControl().isDisposed()){
132 viewer
.getControl().setEnabled(true);
134 if(previousSelection
==null ||
135 previousSelection
!=selection
){//skip redundant rendering of details view
136 if(delaySelection
==null){
137 delaySelection
= new DelaySelection(selection
, activePart
, thisPart
);
139 delaySelection
.setSelection(selection
);
140 delaySelection
.setActivePart(activePart
);
141 delaySelection
.setThisPart(thisPart
);
144 sync
.asyncExec(delaySelection
);
145 previousSelection
= selection
;
152 public void changed(Object object
) {
153 if(selectionProvidingPart
!=null){
154 Object part
= selectionProvidingPart
.getObject();
155 if(part
instanceof IDirtyMarkable
){
156 ((IDirtyMarkable
) part
).changed(object
);
161 public Viewer
getViewer() {
165 protected void showViewer(IStructuredSelection selection
, MPart activePart
, Viewer viewer
){
166 if(viewer
!=null && viewer
.getControl()!=null && !viewer
.getControl().isDisposed()){
167 Object element
= selection
.getFirstElement();
168 Object part
= createPartObject(activePart
);
169 viewer
.getControl().setEnabled(true);
170 if(selection
.getFirstElement()!=null){
171 if (element
instanceof Taxon
){
172 Taxon taxon
= HibernateProxyHelper
.deproxy(element
, Taxon
.class);
173 if (taxon
.isMisapplication()){
175 if(part
instanceof ITaxonEditor
){
176 Taxon accepted
= ((ITaxonEditor
) part
).getTaxon();
178 // Taxon accepted= ((ITaxonEditor)activePart).getTaxon();
179 Set
<TaxonRelationship
> rels
= taxon
.getTaxonRelations(accepted
);
181 if (rels
.iterator().hasNext()){
182 TaxonRelationship rel
= rels
.iterator().next();
183 if (rel
.getType().equals(TaxonRelationshipType
.MISAPPLIED_NAME_FOR()) && !rel
.getFromTaxon().equals(((ITaxonEditor
) part
).getTaxon())){
184 viewer
.setInput(rel
);
194 if (element
instanceof Distribution
&& part
instanceof IDistributionEditor
){
195 ((DetailsViewerE4
)viewer
).setInput(element
, part
);
197 viewer
.setInput(element
);
199 selectionProvidingPart
= activePart
;
204 protected Object
createPartObject(MPart activePart
) {
205 Object partObject
= activePart
;
206 Object wrappedPart
= WorkbenchUtility
.getE4WrappedPart(activePart
);
207 if(wrappedPart
!=null){
208 partObject
= wrappedPart
;
213 protected void showEmptyPage() {
214 if(viewer
!=null && viewer
.getControl()!=null && !viewer
.getControl().isDisposed()){
215 viewer
.setInput(null);
216 viewer
.getControl().setEnabled(false);
218 selectionProvidingPart
= null;
220 thisPart
.setLabel(getViewName());
224 protected IStructuredSelection
createSelection(Object selection
) {
228 IStructuredSelection structuredSelection
;
229 if(!(selection
instanceof IStructuredSelection
)){
230 structuredSelection
= new StructuredSelection(selection
);
233 structuredSelection
= (IStructuredSelection
) selection
;
235 return structuredSelection
;
242 public ConversationHolder
getConversationHolder() {
243 if(selectionProvidingPart
!= null && selectionProvidingPart
instanceof IConversationEnabled
) {
244 return ((IConversationEnabled
) selectionProvidingPart
).getConversationHolder();
253 public boolean postOperation(CdmBase objectAffectedByOperation
) {
254 changed(objectAffectedByOperation
);
262 public boolean onComplete() {
271 public Object
getSelectionProvidingPart() {
272 return selectionProvidingPart
;
276 private void dispose() {
280 private void persistState(){
288 public void update(CdmDataChangeMap arg0
) {
295 public void forceDirty() {
298 protected abstract String
getViewName();