Project

General

Profile

Download (4.02 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
 */
21
public class TaxeditorPartService implements IPartListener2{
22

    
23
	/** Constant <code>PART_ACTIVATED=10</code> */
24
	public static final int PART_ACTIVATED = 10;
25
	/** Constant <code>PART_BROUGHT_TO_TOP=20</code> */
26
	public static final int PART_BROUGHT_TO_TOP = 20;
27
	/** Constant <code>PART_CLOSED=30</code> */
28
	public static final int PART_CLOSED = 30;
29
	/** Constant <code>PART_DEAVTICATED=40</code> */
30
	public static final int PART_DEAVTICATED = 40;
31
	/** Constant <code>PART_HIDDEN=50</code> */
32
	public static final int PART_HIDDEN = 50;
33
	/** Constant <code>PART_INPUT_CHANGED=60</code> */
34
	public static final int PART_INPUT_CHANGED = 60;
35
	/** Constant <code>PART_OPENED=70</code> */
36
	public static final int PART_OPENED = 70;
37
	/** Constant <code>PART_VISIBLE=80</code> */
38
	public static final int PART_VISIBLE = 80;
39

    
40
	/** Constant <code>instance</code> */
41
	public static TaxeditorPartService instance = new TaxeditorPartService();
42

    
43
	private IPartService partService;
44

    
45
	private Map<Integer, Set<IPartChangeListener>> listenerMap = new HashMap<Integer, Set<IPartChangeListener>>();
46

    
47
	private TaxeditorPartService() {
48
		partService = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService();
49
		partService.addPartListener(this);
50
	}
51

    
52
	/**
53
	 * <p>Getter for the field <code>instance</code>.</p>
54
	 *
55
	 * @return a {@link eu.etaxonomy.taxeditor.model.TaxeditorPartService} object.
56
	 */
57
	public static TaxeditorPartService getInstance(){
58
		return instance;
59
	}
60

    
61
	/**
62
	 * <p>addListener</p>
63
	 *
64
	 * @param eventType a {@link java.lang.Integer} object.
65
	 * @param listener a {@link eu.etaxonomy.taxeditor.model.IPartChangeListener} object.
66
	 */
67
	public void addListener(Integer eventType, IPartChangeListener listener){
68
		Set<IPartChangeListener> partChangeListeners = listenerMap.get(eventType);
69

    
70
		if(partChangeListeners == null){
71
			partChangeListeners = new HashSet<IPartChangeListener>();
72
			listenerMap.put(eventType, partChangeListeners);
73
		}
74

    
75
		partChangeListeners.add(listener);
76
	}
77

    
78
	/**
79
	 * <p>removeListener</p>
80
	 *
81
	 * @param eventType a {@link java.lang.Integer} object.
82
	 * @param listener a {@link eu.etaxonomy.taxeditor.model.IPartChangeListener} object.
83
	 */
84
	public void removeListener(Integer eventType, IPartChangeListener listener){
85
		Set<IPartChangeListener> partChangeListeners = listenerMap.get(eventType);
86

    
87
		if(partChangeListeners == null){
88
			return;
89
		}
90

    
91
		partChangeListeners.remove(listener);
92
	}
93

    
94
	private void notifyListeners(Integer eventType, IWorkbenchPartReference partRef){
95
		Set<IPartChangeListener> partChangeListeners = listenerMap.get(eventType);
96

    
97
		if(partChangeListeners == null){
98
			return;
99
		}
100

    
101
		for(IPartChangeListener listener : partChangeListeners){
102
			listener.partChanged(eventType, partRef);
103
		}
104
	}
105

    
106
	@Override
107
    public void partActivated(IWorkbenchPartReference partRef) {
108
		notifyListeners(PART_ACTIVATED, partRef);
109
	}
110

    
111
	@Override
112
    public void partBroughtToTop(IWorkbenchPartReference partRef) {
113
		notifyListeners(PART_BROUGHT_TO_TOP, partRef);
114
	}
115

    
116
	@Override
117
    public void partClosed(IWorkbenchPartReference partRef) {
118
		notifyListeners(PART_CLOSED, partRef);
119
	}
120

    
121
	@Override
122
    public void partDeactivated(IWorkbenchPartReference partRef) {
123
		notifyListeners(PART_DEAVTICATED, partRef);
124
	}
125

    
126
	@Override
127
    public void partOpened(IWorkbenchPartReference partRef) {
128
		notifyListeners(PART_OPENED, partRef);
129
	}
130

    
131
	@Override
132
    public void partHidden(IWorkbenchPartReference partRef) {
133
		notifyListeners(PART_HIDDEN, partRef);
134
	}
135

    
136
	@Override
137
    public void partVisible(IWorkbenchPartReference partRef) {
138
		notifyListeners(PART_VISIBLE, partRef);
139
	}
140

    
141
	@Override
142
    public void partInputChanged(IWorkbenchPartReference partRef) {
143
		notifyListeners(PART_INPUT_CHANGED, partRef);
144
	}
145
}
(37-37/41)