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
.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 import org
.eclipse
.ui
.IEditorPart
;
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
.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
.ITaxonEditor
;
38 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
39 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkable
;
40 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
41 import eu
.etaxonomy
.taxeditor
.workbench
.WorkbenchUtility
;
42 import eu
.etaxonomy
.taxeditor
.workbench
.part
.ISelectionElementEditingPart
;
49 public abstract class AbstractCdmEditorPartE4
50 implements IConversationEnabled
, IDirtyMarkable
, ISelectionElementEditingPart
, IPostOperationEnabled
{
52 private DelaySelection delaySelection
= null;
54 * This is the monitor for the DelaySelection runnable.
55 * If it is <code>true</code> then it is currently delaying a selection.
57 private boolean isInDelay
;
61 * This class invokes internal_selectionChanged() in a separate thread.
62 * This allows an asynchronous and/or delayed handling of selection changes
64 private class DelaySelection
implements Runnable
{
65 private Object selection
;
66 private MPart activePart
;
67 private MPart thisPart
;
69 public DelaySelection(Object selection
, MPart activePart
, MPart thisPart
) {
71 this.selection
= selection
;
72 this.activePart
= activePart
;
73 this.thisPart
= thisPart
;
79 selectionChanged_internal(selection
, activePart
, thisPart
);
86 public synchronized void setSelection(Object selection
) {
87 this.selection
= selection
;
90 public synchronized void setActivePart(MPart activePart
) {
91 this.activePart
= activePart
;
94 public synchronized void setThisPart(MPart thisPart
) {
95 this.thisPart
= thisPart
;
100 protected abstract void selectionChanged_internal(Object selection
, MPart activePart
, MPart thisPart
);
104 public void selectionChanged(
105 @Named(IServiceConstants
.ACTIVE_SELECTION
)Object selection
,
106 @Named(IServiceConstants
.ACTIVE_PART
)MPart activePart
,
108 Object savablePart
= WorkbenchUtility
.findSavablePart(this);
109 if(savablePart
==null){
112 if(delaySelection
==null){
113 delaySelection
= new DelaySelection(selection
, activePart
, thisPart
);
115 delaySelection
.setSelection(selection
);
116 delaySelection
.setActivePart(activePart
);
117 delaySelection
.setThisPart(thisPart
);
120 Display
.getCurrent().asyncExec(delaySelection
);
124 protected Viewer viewer
;
126 protected MPart thisPart
;
128 protected MPart selectionProvidingPart
;
130 protected ISelectionChangedListener selectionChangedListener
;
133 protected ESelectionService selService
;
137 public void changed(Object object
) {
138 if(selectionProvidingPart
!=null){
139 Object part
= selectionProvidingPart
.getObject();
140 if(part
instanceof IDirtyMarkable
){
141 ((IDirtyMarkable
) part
).changed(object
);
144 IEditorPart editor
= AbstractUtility
.getActiveEditor();
145 if (editor
!= null && editor
instanceof IDirtyMarkable
) {
146 ((IDirtyMarkable
) editor
).changed(object
);
151 createPartTitle(object
);
155 protected void createPartTitle(Object selectedObject
){
157 if(selectedObject
!=null){
158 thisPart
.setLabel(getViewName()+": "+selectedObject
); //$NON-NLS-1$
161 thisPart
.setLabel(getViewName());
166 public Viewer
getViewer() {
170 protected void showViewer(IStructuredSelection selection
, MPart activePart
, Viewer viewer
){
171 if(viewer
!=null && viewer
.getControl()!=null && !viewer
.getControl().isDisposed()){
172 Object element
= selection
.getFirstElement();
173 if(selection
.getFirstElement()!=null){
174 if (element
instanceof Taxon
){
175 Taxon taxon
= HibernateProxyHelper
.deproxy(element
, Taxon
.class);
176 if (taxon
.isMisapplication()){
177 Object part
= createPartObject(activePart
);
178 if(part
instanceof ITaxonEditor
){
179 Taxon accepted
= ((ITaxonEditor
) part
).getTaxon();
181 // Taxon accepted= ((ITaxonEditor)activePart).getTaxon();
182 Set
<TaxonRelationship
> rels
= taxon
.getTaxonRelations(accepted
);
184 if (rels
.iterator().hasNext()){
185 TaxonRelationship rel
= rels
.iterator().next();
186 if (rel
.getType().equals(TaxonRelationshipType
.MISAPPLIED_NAME_FOR()) && !rel
.getFromTaxon().equals(((ITaxonEditor
) part
).getTaxon())){
187 viewer
.setInput(rel
);
197 viewer
.setInput(element
);
199 createPartTitle(element
);
202 selectionProvidingPart
= activePart
;
206 protected Object
createPartObject(MPart activePart
) {
207 Object partObject
= activePart
;
208 Object wrappedPart
= WorkbenchUtility
.getE4WrappedPart(activePart
);
209 if(wrappedPart
!=null){
210 partObject
= wrappedPart
;
215 protected void showEmptyPage() {
216 if(viewer
!=null && viewer
.getControl()!=null && viewer
.getControl().isDisposed()){
217 viewer
.setInput(null);
219 selectionProvidingPart
= null;
222 protected IStructuredSelection
createSelection(Object selection
) {
226 IStructuredSelection structuredSelection
;
227 if(!(selection
instanceof IStructuredSelection
)){
228 structuredSelection
= new StructuredSelection(selection
);
231 structuredSelection
= (IStructuredSelection
) selection
;
233 return structuredSelection
;
240 public ConversationHolder
getConversationHolder() {
241 if(selectionProvidingPart
!= null && selectionProvidingPart
instanceof IConversationEnabled
) {
242 return ((IConversationEnabled
) selectionProvidingPart
).getConversationHolder();
251 public boolean postOperation(CdmBase objectAffectedByOperation
) {
252 changed(objectAffectedByOperation
);
260 public boolean onComplete() {
269 public Object
getSelectionProvidingPart() {
270 return selectionProvidingPart
;
274 private void dispose() {
278 private void persistState(){
286 public void update(CdmDataChangeMap arg0
) {
293 public void forceDirty() {
296 protected abstract String
getViewName();