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
.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
;
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
.TaxonRelationship
;
40 import eu
.etaxonomy
.cdm
.persistence
.dto
.TermDto
;
41 import eu
.etaxonomy
.cdm
.persistence
.dto
.TermVocabularyDto
;
42 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
43 import eu
.etaxonomy
.taxeditor
.editor
.ITaxonEditor
;
44 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkable
;
45 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
46 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
47 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
48 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
49 import eu
.etaxonomy
.taxeditor
.view
.e4
.details
.DetailsPartE4
;
50 import eu
.etaxonomy
.taxeditor
.view
.e4
.details
.DetailsViewerE4
;
51 import eu
.etaxonomy
.taxeditor
.workbench
.WorkbenchUtility
;
52 import eu
.etaxonomy
.taxeditor
.workbench
.part
.ISelectionElementEditingPart
;
59 public abstract class AbstractCdmEditorPartE4
60 implements IConversationEnabled
, IDirtyMarkable
, ISelectionElementEditingPart
, IPostOperationEnabled
{
62 private DelaySelection delaySelection
= null;
64 * This is the monitor for the DelaySelection runnable.
65 * If it is <code>true</code> then it is currently delaying a selection.
67 private boolean isInDelay
;
68 private static final Logger logger
= Logger
.getLogger(AbstractCdmEditorPartE4
.class);
71 * This class invokes internal_selectionChanged() in a separate thread.
72 * This allows an asynchronous and/or delayed handling of selection changes
74 private class DelaySelection
implements Runnable
{
75 private Object selection
;
76 private MPart activePart
;
77 private MPart thisPart
;
79 public DelaySelection(Object selection
, MPart activePart
, MPart thisPart
) {
81 this.selection
= selection
;
82 this.activePart
= activePart
;
83 this.thisPart
= thisPart
;
89 selectionChanged_internal(selection
, activePart
, thisPart
);
96 public synchronized void setSelection(Object selection
) {
97 this.selection
= selection
;
100 public synchronized void setActivePart(MPart activePart
) {
101 this.activePart
= activePart
;
104 public synchronized void setThisPart(MPart thisPart
) {
105 this.thisPart
= thisPart
;
110 protected Viewer viewer
;
112 protected MPart thisPart
;
114 protected MPart selectionProvidingPart
;
116 protected Object previousSelection
;
118 protected ISelectionChangedListener selectionChangedListener
;
120 public ISelectionChangedListener
getSelectionChangedListener() {
121 return selectionChangedListener
;
125 protected ESelectionService selService
;
128 protected IEclipseContext context
;
130 protected abstract void selectionChanged_internal(Object selection
, MPart activePart
, MPart thisPart
);
133 public void selectionChanged(
134 @Optional@Named(IServiceConstants
.ACTIVE_SELECTION
)Object selection
,
135 @Optional@Named(IServiceConstants
.ACTIVE_PART
)MPart activePart
,
136 MPart thisPart
, UISynchronize sync
, EPartService partService
){
137 //multiple selections are not supported
140 && !activePart
.equals(thisPart
)
141 && selection
instanceof IStructuredSelection
142 && ((IStructuredSelection
) selection
).size()>1){
146 // no active editor found
147 if(activePart
==thisPart
&& WorkbenchUtility
.getActiveEditorPart(partService
)==null){
151 if (viewer
!= null && viewer
.getControl()!= null && viewer
.getInput() != null && !viewer
.getControl().isDisposed()){
153 viewer
.getControl().setEnabled(true);
154 }catch(SWTException e
){
155 logger
.debug("Something went wrong for viewer.getControl().setEnabled(true) in " + this.getClass().getSimpleName(), e
);
160 if((previousSelection
!=null && selection
!=null) && activePart
.equals(selectionProvidingPart
) &&
161 (previousSelection
==selection
162 || previousSelection
.equals(selection
)
163 || new StructuredSelection(selection
).equals(previousSelection
))
167 if(delaySelection
==null){
168 delaySelection
= new DelaySelection(selection
, activePart
, thisPart
);
170 delaySelection
.setSelection(selection
);
171 delaySelection
.setActivePart(activePart
);
172 delaySelection
.setThisPart(thisPart
);
175 sync
.asyncExec(delaySelection
);
176 previousSelection
= selection
;
182 public void changed(Object object
) {
183 if(selectionProvidingPart
!=null){
184 Object part
= selectionProvidingPart
.getObject();
185 if(part
instanceof IDirtyMarkable
){
186 ((IDirtyMarkable
) part
).changed(object
);
191 public Viewer
getViewer() {
195 protected void showViewer(IStructuredSelection selection
, MPart activePart
, Viewer viewer
){
196 if(viewer
!=null && viewer
.getControl()!=null && !viewer
.getControl().isDisposed()){
197 Object element
= selection
.getFirstElement();
198 Object part
= createPartObject(activePart
);
199 viewer
.getControl().setEnabled(true);
200 if(selection
.getFirstElement()!=null){
201 if (element
instanceof Taxon
){
202 Taxon taxon
= HibernateProxyHelper
.deproxy(element
, Taxon
.class);
203 if (taxon
.isMisapplication() || taxon
.isProparteSynonym() || taxon
.isInvalidDesignation()){
205 if(part
instanceof ITaxonEditor
){
206 Taxon accepted
= ((ITaxonEditor
) part
).getTaxon();
207 Set
<TaxonRelationship
> rels
= taxon
.getTaxonRelations(accepted
);
209 if (rels
!= null && rels
.iterator().hasNext() && !taxon
.equals(accepted
)){
210 TaxonRelationship rel
= rels
.iterator().next();
211 if ((rel
.getType().isMisappliedNameOrInvalidDesignation() || rel
.getType().isAnySynonym()) && !rel
.getFromTaxon().equals(((ITaxonEditor
) part
).getTaxon())){
212 viewer
.setInput(rel
);
213 selectionProvidingPart
= activePart
;
223 if(element
instanceof TermDto
){
224 element
= CdmStore
.getService(ITermService
.class).load(((TermDto
) element
).getUuid());
226 else if(element
instanceof TermVocabularyDto
){
227 element
= CdmStore
.getService(IVocabularyService
.class).load(((TermVocabularyDto
) element
).getUuid());
230 selectionProvidingPart
= activePart
;
231 if (viewer
instanceof DetailsViewerE4
){
232 ((DetailsViewerE4
)viewer
).setInput(element
, part
);
233 ((DetailsViewerE4
)viewer
).setDetailsEnabled(true);
238 if (activePart
.getObject() instanceof DetailsPartE4
&& element
instanceof TaxonName
){
239 selectionProvidingPart
= ((DetailsPartE4
)activePart
.getObject()).getSelectionProvidingPart();
241 viewer
.setInput(element
);
247 protected Object
createPartObject(MPart activePart
) {
248 Object partObject
= activePart
;
249 Object wrappedPart
= WorkbenchUtility
.getE4WrappedPart(activePart
);
250 if(wrappedPart
!=null){
251 partObject
= wrappedPart
;
256 protected void showEmptyPage() {
257 if(viewer
!=null && viewer
.getControl()!=null && !viewer
.getControl().isDisposed() ){
258 viewer
.setInput(null);
260 if (!viewer
.getControl().isDisposed()){
261 viewer
.getControl().setEnabled(false);
263 }catch(SWTException e
){
264 if (PreferencesUtil
.isShowUpWidgetIsDisposedMessages() && e
.getMessage().equals("Widget is disposed")){
265 MessagingUtils
.errorDialog("Widget is disposed",
267 MessagingUtils
.WIDGET_IS_DISPOSED_MESSAGE
,
278 thisPart
.setLabel(getViewName());
282 protected IStructuredSelection
createSelection(Object selection
) {
286 IStructuredSelection structuredSelection
;
287 if(!(selection
instanceof IStructuredSelection
)){
288 structuredSelection
= new StructuredSelection(selection
);
291 structuredSelection
= (IStructuredSelection
) selection
;
293 return structuredSelection
;
300 public ConversationHolder
getConversationHolder() {
301 if(selectionProvidingPart
!= null && selectionProvidingPart
instanceof IConversationEnabled
) {
302 return ((IConversationEnabled
) selectionProvidingPart
).getConversationHolder();
311 public boolean postOperation(Object objectAffectedByOperation
) {
312 changed(objectAffectedByOperation
);
320 public boolean onComplete() {
329 public MPart
getSelectionProvidingPart() {
330 return selectionProvidingPart
;
334 private void dispose() {
337 private void reset(){
338 previousSelection
= null;
339 selectionProvidingPart
= null;
340 delaySelection
= null;
341 context
.deactivate();
345 private void persistState(){
353 public void update(CdmDataChangeMap arg0
) {
360 public void forceDirty() {
363 protected abstract String
getViewName();