Project

General

Profile

Download (3.36 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy 
5
* http://www.e-taxonomy.eu
6
* 
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10

    
11
package eu.etaxonomy.taxeditor.navigation.key.polytomous;
12

    
13
import org.eclipse.core.runtime.IProgressMonitor;
14
import org.eclipse.core.runtime.IStatus;
15
import org.eclipse.core.runtime.Status;
16
import org.eclipse.core.runtime.jobs.Job;
17
import org.eclipse.swt.widgets.Display;
18

    
19
import eu.etaxonomy.cdm.model.common.CdmBase;
20
import eu.etaxonomy.cdm.model.description.PolytomousKey;
21
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeEvent;
22
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeEvent.EventType;
23
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
24
import eu.etaxonomy.taxeditor.model.AbstractDataChangeBehaviour;
25

    
26
/**
27
 * @author n.hoffmann
28
 * @created May 5, 2011
29
 * @version 1.0
30
 */
31
public class PolytomousKeyViewPartDataChangeBehavior extends
32
		AbstractDataChangeBehaviour {
33

    
34
	private final PolytomousKeyViewPart source;
35

    
36
	/**
37
	 * @param polytomousKeyViewPart
38
	 */
39
	public PolytomousKeyViewPartDataChangeBehavior(
40
			PolytomousKeyViewPart polytomousKeyViewPart) {
41
		source = polytomousKeyViewPart;
42
	}
43

    
44
	/* (non-Javadoc)
45
	 * @see eu.etaxonomy.taxeditor.model.AbstractDataChangeBehaviour#reactOnDataChange(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
46
	 */
47
	@Override
48
	public void reactOnDataChange(CdmDataChangeMap changeEvents) {
49
		if(isRelevant(changeEvents)){
50
			final Display display = Display.getCurrent();
51
			Job job = new Job("Updating Polytomous Key Viewer") {
52
				
53
				@Override
54
				protected IStatus run(IProgressMonitor monitor) {
55
					monitor.beginTask("Updating Polytomous Key Viewer", 3);
56
					monitor.worked(1);
57
					
58
					// clear the session completely
59
					monitor.subTask("Clearing Polytomous Key Viewer session");
60
					display.asyncExec(new Runnable() {
61
						 public void run() {
62
							 source.getConversationHolder().clear();
63
						 }
64
					});					
65
					// FIXME completely clearing the session is a brute force approach. 
66
					// It would be much more elegant to clear only those elements that have been changed.
67
					// I could not get that to work but we should consider workin on this because we might
68
					// run into serious performance issues, especially when it comes to large trees
69
					//
70
					// at least, we moved this to a job so it can run in a background thred
71
					// seems to improve the situation but not sure if final solution
72
					monitor.worked(1);
73
					
74
					monitor.subTask("Refreshing viewer");
75
					
76
					display.asyncExec(new Runnable() {
77
					    public void run() {
78
					    	source.refresh();
79
					    }
80
					});
81
					
82
					
83
					
84
					monitor.worked(1);
85
					monitor.done();
86
					return Status.OK_STATUS;
87
				}
88
			};
89
			
90
			job.setPriority(Job.SHORT);
91
			job.schedule();
92
			
93
		}
94
	}
95
	
96
	/**
97
	 * @return
98
	 */
99
	private boolean isRelevant(CdmDataChangeMap changeEvents) {
100
		for(CdmDataChangeEvent event : changeEvents.getAllEvents()){
101
			EventType eventType = event.getEventType();
102
			CdmBase eventEntity = event.getEntity();
103
			
104
			// all poyltomous key changes are relevant
105
			if((eventType == EventType.INSERT || eventType == EventType.DELETE || eventType == EventType.UPDATE) 
106
					&& eventEntity instanceof PolytomousKey){
107
				return true;
108
			}
109
		}
110
		return false;
111
	}
112
}
(4-4/4)