2 * Copyright (C) 2007 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.
10 package eu
.etaxonomy
.taxeditor
.navigation
.navigator
;
12 import java
.util
.HashSet
;
15 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
16 import org
.eclipse
.core
.runtime
.IStatus
;
17 import org
.eclipse
.core
.runtime
.Status
;
18 import org
.eclipse
.core
.runtime
.jobs
.Job
;
19 import org
.eclipse
.swt
.widgets
.Display
;
21 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
22 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
23 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
24 import eu
.etaxonomy
.cdm
.model
.taxon
.ITaxonTreeNode
;
25 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
26 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeEvent
;
27 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeEvent
.EventType
;
28 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
29 import eu
.etaxonomy
.taxeditor
.model
.AbstractDataChangeBehaviour
;
30 import eu
.etaxonomy
.taxeditor
.model
.IDataChangeBehavior
;
31 import eu
.etaxonomy
.taxeditor
.navigation
.l10n
.Messages
;
34 * <p>TaxonNavigatorDataChangeBehavior class.</p>
40 public class TaxonNavigatorDataChangeBehavior
extends AbstractDataChangeBehaviour
implements
43 private static final String UPDATING_TAXON_NAVIGATOR
= Messages
.TaxonNavigatorDataChangeBehavior_UPDATE_NAVIGATOR
;
45 private final TaxonNavigator source
;
47 private Set
<CdmBase
> staleObjects
;
50 * <p>Constructor for TaxonNavigatorDataChangeBehavior.</p>
52 * @param taxonNavigator a {@link eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator} object.
54 public TaxonNavigatorDataChangeBehavior(TaxonNavigator taxonNavigator
) {
55 source
= taxonNavigator
;
59 * @see eu.etaxonomy.taxeditor.store.model.IDataChangeBehavior#isRelevant(java.lang.Object, eu.etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
64 * @param events a {@link eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap} object.
67 public boolean isRelevant(CdmDataChangeMap events
) {
69 // TODO react only on insert/update/delete of taxon and synonym objects
70 // and on update of name objects
71 boolean relevant
= false;
72 staleObjects
= new HashSet
<CdmBase
>();
74 for(CdmDataChangeEvent event
: events
.getAllEvents()){
75 EventType eventType
= event
.getEventType();
76 CdmBase eventEntity
= event
.getEntity();
78 Set
<CdmBase
> affectedObjects
= event
.getAffectedObjects();
79 if(affectedObjects
!= null) {
80 for(CdmBase cb
: affectedObjects
) {
81 staleObjects
.add(HibernateProxyHelper
.deproxy(cb
));
85 // all tree node changes are relevant
86 if((eventType
== EventType
.INSERT
|| eventType
== EventType
.DELETE
|| eventType
== EventType
.UPDATE
)
87 && event
.getEntity() instanceof ITaxonTreeNode
){
91 if (eventType
== EventType
.DELETE
){
97 if(eventType
== EventType
.UPDATE
&& event
.getEntity() instanceof Taxon
){
98 TaxonName name
= null;
99 if(eventEntity
instanceof Taxon
){
100 name
= ((Taxon
) eventEntity
).getName();
105 // Set<IEditorPart> openEditors = NavigationUtil.getOpenEditors();
106 /*for(IEditorPart editor : openEditors){
108 if(name.equals(((TaxonEditorInput) editor.getEditorInput()).getTaxon().getName())){
114 // if(eventType == EventType.UPDATE){
116 // CdmBase entity = event.getEntity();
117 // if((entity instanceof TaxonNameBase)
118 // || (entity instanceof Taxon)
119 // || (entity instanceof Synonym)){
120 // staleObjects.add(entity);
128 // react on everything except load
129 // if(events.sizeByEventType(EventType.INSERT) > 0){
131 // }else if(events.sizeByEventType(EventType.UPDATE) > 0){
133 // }else if(events.sizeByEventType(EventType.DELETE) > 0){
142 public void reactOnDataChange(CdmDataChangeMap events
) {
143 if(isRelevant(events
)){
145 final Display display
= Display
.getCurrent();
146 Job job
= new Job(UPDATING_TAXON_NAVIGATOR
) {
149 protected IStatus
run(IProgressMonitor monitor
) {
150 monitor
.beginTask(UPDATING_TAXON_NAVIGATOR
, 3);
153 // clear the session completely
154 monitor
.subTask(Messages
.TaxonNavigatorDataChangeBehavior_CLEAR_SESSION
);
155 display
.asyncExec(new Runnable() {
158 source
.getConversationHolder().clear();
161 // FIXME completely clearing the session is a brute force approach.
162 // It would be much more elegant to clear only those elements that have been changed.
163 // I could not get that to work but we should consider workin on this because we might
164 // run into serious performance issues, especially when it comes to large trees
166 // at least, we moved this to a job so it can run in a background thred
167 // seems to improve the situation but not sure if final solution
170 monitor
.subTask(Messages
.TaxonNavigatorDataChangeBehavior_REFRESH_VIEWER
);
172 display
.asyncExec(new Runnable() {
175 if(staleObjects
!= null && staleObjects
.size() > 0) {
176 source
.refresh(staleObjects
);
187 return Status
.OK_STATUS
;
191 job
.setPriority(Job
.SHORT
);