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
;
11 import java
.util
.Collection
;
14 import javax
.annotation
.PreDestroy
;
15 import javax
.inject
.Inject
;
16 import javax
.inject
.Named
;
18 import org
.apache
.log4j
.Logger
;
19 import org
.eclipse
.e4
.core
.contexts
.IEclipseContext
;
20 import org
.eclipse
.e4
.core
.di
.annotations
.Optional
;
21 import org
.eclipse
.e4
.ui
.di
.PersistState
;
22 import org
.eclipse
.e4
.ui
.di
.UISynchronize
;
23 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPart
;
24 import org
.eclipse
.e4
.ui
.services
.IServiceConstants
;
25 import org
.eclipse
.e4
.ui
.workbench
.modeling
.EPartService
;
26 import org
.eclipse
.e4
.ui
.workbench
.modeling
.ESelectionService
;
27 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
28 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
29 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
30 import org
.eclipse
.jface
.viewers
.Viewer
;
31 import org
.eclipse
.swt
.SWTException
;
32 import org
.springframework
.security
.core
.GrantedAuthority
;
34 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
35 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
36 import eu
.etaxonomy
.cdm
.api
.service
.ITermNodeService
;
37 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
38 import eu
.etaxonomy
.cdm
.api
.service
.IVocabularyService
;
39 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
40 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
41 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
42 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
43 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
44 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
45 import eu
.etaxonomy
.cdm
.persistence
.dto
.TermDto
;
46 import eu
.etaxonomy
.cdm
.persistence
.dto
.TermNodeDto
;
47 import eu
.etaxonomy
.cdm
.persistence
.dto
.TermVocabularyDto
;
48 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
49 import eu
.etaxonomy
.taxeditor
.editor
.IBulkEditor
;
50 import eu
.etaxonomy
.taxeditor
.editor
.ITaxonEditor
;
51 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkable
;
52 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
53 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
54 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
55 import eu
.etaxonomy
.taxeditor
.security
.RequiredPermissions
;
56 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
57 import eu
.etaxonomy
.taxeditor
.view
.e4
.details
.DetailsPartE4
;
58 import eu
.etaxonomy
.taxeditor
.view
.e4
.details
.DetailsViewerE4
;
59 import eu
.etaxonomy
.taxeditor
.workbench
.WorkbenchUtility
;
60 import eu
.etaxonomy
.taxeditor
.workbench
.part
.ISelectionElementEditingPart
;
66 public abstract class AbstractCdmEditorPartE4
67 implements IConversationEnabled
, IDirtyMarkable
, ISelectionElementEditingPart
, IPostOperationEnabled
{
69 private static final Logger logger
= Logger
.getLogger(AbstractCdmEditorPartE4
.class);
71 private DelaySelection delaySelection
= null;
73 * This is the monitor for the DelaySelection runnable.
74 * If it is <code>true</code> then it is currently delaying a selection.
76 private boolean isInDelay
;
77 private boolean isEnabled
= true;
80 * This class invokes internal_selectionChanged() in a separate thread.
81 * This allows an asynchronous and/or delayed handling of selection changes
83 private class DelaySelection
implements Runnable
{
84 private Object selection
;
85 private MPart activePart
;
86 private MPart thisPart
;
88 public DelaySelection(Object selection
, MPart activePart
, MPart thisPart
) {
90 this.selection
= selection
;
91 this.activePart
= activePart
;
92 this.thisPart
= thisPart
;
98 selectionChanged_internal(selection
, activePart
, thisPart
);
105 public synchronized void setSelection(Object selection
) {
106 this.selection
= selection
;
109 public synchronized void setActivePart(MPart activePart
) {
110 this.activePart
= activePart
;
113 public synchronized void setThisPart(MPart thisPart
) {
114 this.thisPart
= thisPart
;
118 protected Viewer viewer
;
120 protected MPart thisPart
;
122 protected MPart selectionProvidingPart
;
124 protected Object previousSelection
;
126 protected ISelectionChangedListener selectionChangedListener
;
128 public ISelectionChangedListener
getSelectionChangedListener() {
129 return selectionChangedListener
;
133 protected ESelectionService selService
;
136 protected IEclipseContext context
;
138 protected abstract void selectionChanged_internal(Object selection
, MPart activePart
, MPart thisPart
);
140 protected boolean showEmptyIfNoActiveEditor(){
145 public void selectionChanged(
146 @Optional@Named(IServiceConstants
.ACTIVE_SELECTION
)Object selection
,
147 @Optional@Named(IServiceConstants
.ACTIVE_PART
)MPart activePart
,
148 MPart thisPart
, UISynchronize sync
, EPartService partService
){
149 //multiple selections are not supported
152 && !activePart
.equals(thisPart
)
153 && selection
instanceof IStructuredSelection
154 && ((IStructuredSelection
) selection
).size()>1){
158 // no active editor found
159 if(activePart
==thisPart
&& WorkbenchUtility
.getActiveEditorPart(partService
)==null && showEmptyIfNoActiveEditor()){
163 if (viewer
!= null && viewer
.getControl()!= null && viewer
.getInput() != null && !viewer
.getControl().isDisposed()){
165 viewer
.getControl().setEnabled(isEnabled
);
166 }catch(SWTException e
){
167 logger
.debug("Something went wrong for viewer.getControl().setEnabled(true) in " + this.getClass().getSimpleName(), e
);
171 if((previousSelection
!=null && selection
!=null)
172 && (activePart
!= null && selectionProvidingPart
!= null && activePart
.equals(selectionProvidingPart
))
173 && (previousSelection
==selection
174 || previousSelection
.equals(selection
)
175 || new StructuredSelection(selection
).equals(previousSelection
))
179 if(delaySelection
==null){
180 delaySelection
= new DelaySelection(selection
, activePart
, thisPart
);
182 delaySelection
.setSelection(selection
);
183 delaySelection
.setActivePart(activePart
);
184 delaySelection
.setThisPart(thisPart
);
188 sync
.asyncExec(delaySelection
);
189 previousSelection
= selection
;
194 public void changed(Object object
) {
195 if(selectionProvidingPart
!=null){
196 Object part
= selectionProvidingPart
.getObject();
197 if(part
instanceof IDirtyMarkable
){
198 ((IDirtyMarkable
) part
).changed(object
);
200 if (part
instanceof IBulkEditor
){
201 ((IBulkEditor
)part
).setSelection(createSelection(object
));
206 public Viewer
getViewer() {
210 public boolean isEnabled() {
214 public void setEnabled(boolean isEnabled
) {
215 this.isEnabled
= isEnabled
;
218 protected void showViewer(IStructuredSelection selection
, MPart activePart
, Viewer viewer
){
219 if(CdmStore
.getCurrentSessionManager().getActiveSession()==null){
222 if(viewer
!=null && viewer
.getControl()!=null && !viewer
.getControl().isDisposed()){
223 Object element
= selection
.getFirstElement();
224 Object part
= createPartObject(activePart
);
225 if (viewer
.getControl().isDisposed()){
228 viewer
.getControl().setEnabled(true);
230 if (element
instanceof Taxon
){
232 Taxon taxon
= HibernateProxyHelper
.deproxy(element
, Taxon
.class);
233 if (part
instanceof ITaxonEditor
){
234 TaxonNode node
= ((ITaxonEditor
) part
).getTaxonNode();
236 boolean doEnable
= CdmStore
.currentAuthentiationHasPermission(node
,
237 RequiredPermissions
.TAXON_EDIT
);
239 //check whether there are explicit TaxonNode rights
240 boolean taxonnodePermissionExists
= false;
241 Collection
<?
extends GrantedAuthority
> authorities
= CdmStore
.getCurrentAuthentiation().getAuthorities();
242 for (GrantedAuthority grantedAuthority
: authorities
){
243 if (grantedAuthority
.getAuthority().startsWith("TAXONNODE")){
244 taxonnodePermissionExists
= true;
247 if (!taxonnodePermissionExists
){
252 //TODO: differ between the views
253 this.isEnabled
= doEnable
;
256 if (taxon
.isMisapplication() || taxon
.isProparteSynonym() ){
258 if(part
instanceof ITaxonEditor
){
259 Taxon accepted
= ((ITaxonEditor
) part
).getTaxon();
261 Set
<TaxonRelationship
> rels
= taxon
.getTaxonRelations(accepted
);
263 if (rels
!= null && rels
.iterator().hasNext() && !taxon
.equals(accepted
)){
264 TaxonRelationship rel
= rels
.iterator().next();
265 if ((rel
.getType().isMisappliedName() || rel
.getType().isAnySynonym()) && !rel
.getFromTaxon().equals(((ITaxonEditor
) part
).getTaxon())){
266 viewer
.setInput(rel
);
267 selectionProvidingPart
= activePart
;
276 if(element
instanceof TermDto
){
277 element
= CdmStore
.getService(ITermService
.class).load(((TermDto
) element
).getUuid());
279 else if(element
instanceof TermVocabularyDto
){
280 element
= CdmStore
.getService(IVocabularyService
.class).load(((TermVocabularyDto
) element
).getUuid());
284 selectionProvidingPart
= activePart
;
285 if (viewer
instanceof DetailsViewerE4
){
287 if (selectionProvidingPart
.getElementId().equals("eu.etaxonomy.taxeditor.editor.view.checklist.e4.DistributionEditorPart")){
288 ((DetailsViewerE4
)viewer
).setDetailsEnabled(false);
290 ((DetailsViewerE4
)viewer
).setDetailsEnabled(isEnabled
);
292 ((DetailsViewerE4
)viewer
).setInput(element
, part
);
297 if(element
instanceof TermNodeDto
){
298 element
= CdmStore
.getService(ITermNodeService
.class).load(((TermNodeDto
) element
).getUuid());
300 if (activePart
.getObject() instanceof DetailsPartE4
&& element
instanceof TaxonName
){
301 selectionProvidingPart
= ((DetailsPartE4
)activePart
.getObject()).getSelectionProvidingPart();
304 viewer
.setInput(element
);
305 viewer
.getControl().setEnabled(isEnabled
);
312 protected Object
createPartObject(MPart activePart
) {
313 Object partObject
= activePart
;
314 Object wrappedPart
= WorkbenchUtility
.getE4WrappedPart(activePart
);
315 if(wrappedPart
!=null){
316 partObject
= wrappedPart
;
321 protected void showEmptyPage() {
322 if(viewer
!=null && viewer
.getControl()!=null && !viewer
.getControl().isDisposed() ){
323 viewer
.setInput(null);
325 if (!viewer
.getControl().isDisposed()){
326 viewer
.getControl().setEnabled(false);
328 }catch(SWTException e
){
329 if (PreferencesUtil
.isShowUpWidgetIsDisposedMessages() && e
.getMessage().equals("Widget is disposed")){
330 MessagingUtils
.errorDialog("Widget is disposed",
332 MessagingUtils
.WIDGET_IS_DISPOSED_MESSAGE
,
342 thisPart
.setLabel(getViewName());
346 protected IStructuredSelection
createSelection(Object selection
) {
350 IStructuredSelection structuredSelection
;
351 if(!(selection
instanceof IStructuredSelection
)){
352 structuredSelection
= new StructuredSelection(selection
);
355 structuredSelection
= (IStructuredSelection
) selection
;
357 return structuredSelection
;
361 public ConversationHolder
getConversationHolder() {
362 if(selectionProvidingPart
!= null && selectionProvidingPart
instanceof IConversationEnabled
) {
363 return ((IConversationEnabled
) selectionProvidingPart
).getConversationHolder();
369 public boolean postOperation(Object objectAffectedByOperation
) {
370 changed(objectAffectedByOperation
);
371 // Object part = selectionProvidingPart.getObject();
372 // if (part instanceof IBulkEditor){
373 // ((IBulkEditor)part).setSelection(createSelection(objectAffectedByOperation));
379 public boolean onComplete() {
385 public MPart
getSelectionProvidingPart() {
386 return selectionProvidingPart
;
390 private void dispose() {
393 private void reset(){
394 previousSelection
= null;
395 selectionProvidingPart
= null;
396 delaySelection
= null;
397 context
.deactivate();
401 private void persistState(){
406 public void update(CdmDataChangeMap arg0
) {
410 public void forceDirty() {
413 protected abstract String
getViewName();