Project

General

Profile

Download (4.12 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * 
3
 */
4
package eu.etaxonomy.taxeditor.model;
5

    
6
import java.util.HashMap;
7
import java.util.HashSet;
8
import java.util.Map;
9
import java.util.Set;
10

    
11
import org.eclipse.ui.IPartListener2;
12
import org.eclipse.ui.IPartService;
13
import org.eclipse.ui.IWorkbenchPartReference;
14
import org.eclipse.ui.PlatformUI;
15

    
16
/**
17
 * <p>TaxeditorPartService class.</p>
18
 *
19
 * @author n.hoffmann
20
 * @version $Id: $
21
 */
22
public class TaxeditorPartService implements IPartListener2{
23

    
24
	/** Constant <code>PART_ACTIVATED=10</code> */
25
	public static final int PART_ACTIVATED = 10;
26
	/** Constant <code>PART_BROUGHT_TO_TOP=20</code> */
27
	public static final int PART_BROUGHT_TO_TOP = 20;
28
	/** Constant <code>PART_CLOSED=30</code> */
29
	public static final int PART_CLOSED = 30;
30
	/** Constant <code>PART_DEAVTICATED=40</code> */
31
	public static final int PART_DEAVTICATED = 40;
32
	/** Constant <code>PART_HIDDEN=50</code> */
33
	public static final int PART_HIDDEN = 50;
34
	/** Constant <code>PART_INPUT_CHANGED=60</code> */
35
	public static final int PART_INPUT_CHANGED = 60;
36
	/** Constant <code>PART_OPENED=70</code> */
37
	public static final int PART_OPENED = 70;
38
	/** Constant <code>PART_VISIBLE=80</code> */
39
	public static final int PART_VISIBLE = 80;
40
	
41
	/** Constant <code>instance</code> */
42
	public static TaxeditorPartService instance = new TaxeditorPartService();
43
	
44
	private IPartService partService;
45
	
46
	private Map<Integer, Set<IPartChangeListener>> listenerMap = new HashMap<Integer, Set<IPartChangeListener>>();
47
	
48
	private TaxeditorPartService() {
49
		partService = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService();
50
		partService.addPartListener(this);
51
	}
52
	
53
	/**
54
	 * <p>Getter for the field <code>instance</code>.</p>
55
	 *
56
	 * @return a {@link eu.etaxonomy.taxeditor.model.TaxeditorPartService} object.
57
	 */
58
	public static TaxeditorPartService getInstance(){
59
		return instance;
60
	}
61
	
62
	/**
63
	 * <p>addListener</p>
64
	 *
65
	 * @param eventType a {@link java.lang.Integer} object.
66
	 * @param listener a {@link eu.etaxonomy.taxeditor.model.IPartChangeListener} object.
67
	 */
68
	public void addListener(Integer eventType, IPartChangeListener listener){
69
		Set<IPartChangeListener> partChangeListeners = listenerMap.get(eventType);
70
		
71
		if(partChangeListeners == null){
72
			partChangeListeners = new HashSet<IPartChangeListener>();
73
			listenerMap.put(eventType, partChangeListeners);
74
		}
75
		
76
		partChangeListeners.add(listener);
77
	}
78
	
79
	/**
80
	 * <p>removeListener</p>
81
	 *
82
	 * @param eventType a {@link java.lang.Integer} object.
83
	 * @param listener a {@link eu.etaxonomy.taxeditor.model.IPartChangeListener} object.
84
	 */
85
	public void removeListener(Integer eventType, IPartChangeListener listener){
86
		Set<IPartChangeListener> partChangeListeners = listenerMap.get(eventType);
87
		
88
		if(partChangeListeners == null){
89
			return;
90
		}
91
		
92
		partChangeListeners.remove(listener);
93
	}
94
	
95
	private void notifyListeners(Integer eventType, IWorkbenchPartReference partRef){
96
		Set<IPartChangeListener> partChangeListeners = listenerMap.get(eventType);
97
		
98
		if(partChangeListeners == null){
99
			return;
100
		}
101
		
102
		for(IPartChangeListener listener : partChangeListeners){
103
			listener.partChanged(eventType, partRef);
104
		}
105
	}
106
	
107

    
108
	/** {@inheritDoc} */
109
	public void partActivated(IWorkbenchPartReference partRef) {
110
		notifyListeners(PART_ACTIVATED, partRef);
111
	}
112

    
113
	/** {@inheritDoc} */
114
	public void partBroughtToTop(IWorkbenchPartReference partRef) {
115
		notifyListeners(PART_BROUGHT_TO_TOP, partRef);
116
	}
117

    
118
	/** {@inheritDoc} */
119
	public void partClosed(IWorkbenchPartReference partRef) {
120
		notifyListeners(PART_CLOSED, partRef);
121
	}
122

    
123
	/** {@inheritDoc} */
124
	public void partDeactivated(IWorkbenchPartReference partRef) {
125
		notifyListeners(PART_DEAVTICATED, partRef);
126
	}
127

    
128
	/** {@inheritDoc} */
129
	public void partOpened(IWorkbenchPartReference partRef) {
130
		notifyListeners(PART_OPENED, partRef);
131
	}
132

    
133
	/** {@inheritDoc} */
134
	public void partHidden(IWorkbenchPartReference partRef) {
135
		notifyListeners(PART_HIDDEN, partRef);
136
	}
137

    
138
	/** {@inheritDoc} */
139
	public void partVisible(IWorkbenchPartReference partRef) {
140
		notifyListeners(PART_VISIBLE, partRef);
141
	}
142

    
143
	/** {@inheritDoc} */
144
	public void partInputChanged(IWorkbenchPartReference partRef) {
145
		notifyListeners(PART_INPUT_CHANGED, partRef);
146
	}
147
}
(37-37/41)