Project

General

Profile

Download (3.43 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
import eu.etaxonomy.taxeditor.store.singlesource.widget.DisplayProxy;
26

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

    
35
	private final PolytomousKeyViewPart source;
36

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

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