Skip to main content
summaryrefslogtreecommitdiffstats
blob: 45762bf06cce907fd0dbf2d9800cdc8588276b0a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*******************************************************************************
 * Copyright (c) 2007, 2010 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.utility.model.listener;

import java.lang.reflect.Method;

import org.eclipse.jpt.common.utility.internal.ReflectionTools;
import org.eclipse.jpt.common.utility.model.event.CollectionAddEvent;
import org.eclipse.jpt.common.utility.model.event.CollectionChangeEvent;
import org.eclipse.jpt.common.utility.model.event.CollectionClearEvent;
import org.eclipse.jpt.common.utility.model.event.CollectionEvent;
import org.eclipse.jpt.common.utility.model.event.CollectionRemoveEvent;
import org.eclipse.jpt.common.utility.model.event.ListAddEvent;
import org.eclipse.jpt.common.utility.model.event.ListChangeEvent;
import org.eclipse.jpt.common.utility.model.event.ListClearEvent;
import org.eclipse.jpt.common.utility.model.event.ListEvent;
import org.eclipse.jpt.common.utility.model.event.ListMoveEvent;
import org.eclipse.jpt.common.utility.model.event.ListRemoveEvent;
import org.eclipse.jpt.common.utility.model.event.ListReplaceEvent;
import org.eclipse.jpt.common.utility.model.event.TreeAddEvent;
import org.eclipse.jpt.common.utility.model.event.TreeChangeEvent;
import org.eclipse.jpt.common.utility.model.event.TreeClearEvent;
import org.eclipse.jpt.common.utility.model.event.TreeEvent;
import org.eclipse.jpt.common.utility.model.event.TreeRemoveEvent;

/**
 * This class is used by {@link ReflectiveChangeListener} when the requested listener
 * needs to implement multiple methods (i.e. {@link CollectionChangeListener},
 * {@link ListChangeListener}, or {@link TreeChangeListener}).
 */
class MultiMethodReflectiveChangeListener
	extends ReflectiveChangeListener 
	implements CollectionChangeListener, ListChangeListener, TreeChangeListener
{
	/** the methods we will invoke on the target object */
	private final Method addMethod;
	private final Method removeMethod;
	private final Method replaceMethod;	// this can be null
	private final Method moveMethod;	// this can be null
	private final Method clearMethod;
	private final Method changeMethod;


	/**
	 * The "replace" and "move" methods are optional.
	 */
	MultiMethodReflectiveChangeListener(Object target, Method addMethod, Method removeMethod, Method replaceMethod, Method moveMethod, Method clearMethod, Method changeMethod) {
		super(target);
		this.addMethod = addMethod;
		this.removeMethod = removeMethod;
		this.replaceMethod = replaceMethod;
		this.moveMethod = moveMethod;
		this.clearMethod = clearMethod;
		this.changeMethod = changeMethod;
	}

	/**
	 * No "replace" or "move" methods.
	 */
	MultiMethodReflectiveChangeListener(Object target, Method addMethod, Method removeMethod, Method clearMethod, Method changeMethod) {
		this(target, addMethod, removeMethod, null, null, clearMethod, changeMethod);
	}


	// ********** CollectionChangeListener implementation **********

	private void invoke(Method method, CollectionEvent event) {
		if (method.getParameterTypes().length == 0) {
			ReflectionTools.executeMethod(method, this.target, EMPTY_OBJECT_ARRAY);
		} else {
			ReflectionTools.executeMethod(method, this.target, new CollectionEvent[] {event});
		}
	}

	public void itemsAdded(CollectionAddEvent event) {
		this.invoke(this.addMethod, event);
	}

	public void itemsRemoved(CollectionRemoveEvent event) {
		this.invoke(this.removeMethod, event);
	}

	public void collectionCleared(CollectionClearEvent event) {
		this.invoke(this.clearMethod, event);
	}

	public void collectionChanged(CollectionChangeEvent event) {
		this.invoke(this.changeMethod, event);
	}


	// ********** ListChangeListener implementation **********

	private void invoke(Method method, ListEvent event) {
		if (method.getParameterTypes().length == 0) {
			ReflectionTools.executeMethod(method, this.target, EMPTY_OBJECT_ARRAY);
		} else {
			ReflectionTools.executeMethod(method, this.target, new ListEvent[] {event});
		}
	}

	public void itemsAdded(ListAddEvent event) {
		this.invoke(this.addMethod, event);
	}

	public void itemsRemoved(ListRemoveEvent event) {
		this.invoke(this.removeMethod, event);
	}

	public void itemsReplaced(ListReplaceEvent event) {
		this.invoke(this.replaceMethod, event);
	}

	public void itemsMoved(ListMoveEvent event) {
		this.invoke(this.moveMethod, event);
	}

	public void listCleared(ListClearEvent event) {
		this.invoke(this.clearMethod, event);
	}

	public void listChanged(ListChangeEvent event) {
		this.invoke(this.changeMethod, event);
	}


	// ********** TreeChangeListener implementation **********

	private void invoke(Method method, TreeEvent event) {
		if (method.getParameterTypes().length == 0) {
			ReflectionTools.executeMethod(method, this.target, EMPTY_OBJECT_ARRAY);
		} else {
			ReflectionTools.executeMethod(method, this.target, new TreeEvent[] {event});
		}
	}

	public void nodeAdded(TreeAddEvent event) {
		this.invoke(this.addMethod, event);
	}

	public void nodeRemoved(TreeRemoveEvent event) {
		this.invoke(this.removeMethod, event);
	}

	public void treeCleared(TreeClearEvent event) {
		this.invoke(this.clearMethod, event);
	}

	public void treeChanged(TreeChangeEvent event) {
		this.invoke(this.changeMethod, event);
	}

}

Back to the top