Project

General

Profile

Download (6.26 KB) Statistics
| Branch: | Tag: | Revision:
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.draw2d;
12

    
13
import java.beans.PropertyChangeEvent;
14
import java.beans.PropertyChangeListener;
15
import java.util.ArrayList;
16
import java.util.List;
17

    
18
/**
19
 * A ButtonGroup holds a group of {@link Clickable Clickable's} models and
20
 * provides unique selection in them. There is capability to add a default
21
 * selection. Models who want to belong to the group should just add themselves
22
 * to this group. By doing so they listen to this group for changes.
23
 * <p>
24
 * Setting of the default selection results in its being selected any time
25
 * {@link #setSelected(ButtonModel, boolean)} is called. If no default selection
26
 * is set, the last entry selected is not allowed to deselect.
27
 */
28
public class ButtonGroup {
29

    
30
	private ButtonModel selectedModel;
31
	private ButtonModel defaultModel;
32
	private List members = new ArrayList();
33
	private List listeners = new ArrayList();
34

    
35
	/**
36
	 * Constructs a ButtonGroup with no default selection.
37
	 * 
38
	 * @since 2.0
39
	 */
40
	public ButtonGroup() {
41
	}
42

    
43
	/**
44
	 * Adds the passed ButtonModel to the ButtonGroup.
45
	 * 
46
	 * @param model
47
	 *            ButtonModel to be added to this group
48
	 * @since 2.0
49
	 */
50
	public void add(ButtonModel model) {
51
		if (!members.contains(model)) {
52
			model.setGroup(this);
53
			members.add(model);
54
		}
55
	}
56

    
57
	/**
58
	 * Adds the passed listener. ButtonGroups use PropertyChangeListeners to
59
	 * react to selection changes in the ButtonGroup.
60
	 * 
61
	 * @param listener
62
	 *            Listener to be added to this group
63
	 * @since 2.0
64
	 */
65
	public void addPropertyChangeListener(PropertyChangeListener listener) {
66
		listeners.add(listener);
67
	}
68

    
69
	/**
70
	 * Fires a PropertyChangeEvent to all PropertyChangeListeners added to this
71
	 * ButtonGroup.
72
	 * 
73
	 * @param oldValue
74
	 *            Old selection value
75
	 * @param newValue
76
	 *            New selection value
77
	 * @since 2.0
78
	 */
79
	protected void firePropertyChange(Object oldValue, Object newValue) {
80
		PropertyChangeEvent event = new PropertyChangeEvent(this,
81
				ButtonModel.SELECTED_PROPERTY, oldValue, newValue);
82
		for (int i = 0; i < listeners.size(); i++)
83
			((PropertyChangeListener) listeners.get(i)).propertyChange(event);
84
	}
85

    
86
	/**
87
	 * Returns the ButtonModel which is selected by default for this
88
	 * ButtonGroup.
89
	 * 
90
	 * @return The default ButtonModel
91
	 * @since 2.0
92
	 */
93
	public ButtonModel getDefault() {
94
		return defaultModel;
95
	}
96

    
97
	/**
98
	 * Returns a List which contains all of the {@link ButtonModel ButtonModels}
99
	 * added to this ButtonGroup.
100
	 * 
101
	 * @return The List of ButtonModels in this ButtonGroup
102
	 * @since 2.0
103
	 */
104
	public List getElements() {
105
		return members;
106
	}
107

    
108
	/**
109
	 * Returns the ButtonModel for the currently selected button.
110
	 * 
111
	 * @return The ButtonModel for the currently selected button
112
	 * @since 2.0
113
	 */
114
	public ButtonModel getSelected() {
115
		return selectedModel;
116
	}
117

    
118
	/**
119
	 * Determines if the given ButtonModel is selected or not.
120
	 * 
121
	 * @param model
122
	 *            Model being tested for selected status
123
	 * @return Selection state of the given model
124
	 * @since 2.0
125
	 */
126
	public boolean isSelected(ButtonModel model) {
127
		return (model == getSelected());
128
	}
129

    
130
	/**
131
	 * Removes the given ButtonModel from this ButtonGroup.
132
	 * 
133
	 * @param model
134
	 *            ButtonModel being removed
135
	 * @since 2.0
136
	 */
137
	public void remove(ButtonModel model) {
138
		if (getSelected() == model)
139
			setSelected(getDefault(), true);
140
		if (defaultModel == model)
141
			defaultModel = null;
142
		members.remove(model);
143
	}
144

    
145
	/**
146
	 * Removes the passed PropertyChangeListener from this ButtonGroup.
147
	 * 
148
	 * @param listener
149
	 *            PropertyChangeListener to be removed
150
	 * @since 2.0
151
	 */
152
	public void removePropertyChangeListener(PropertyChangeListener listener) {
153
		listeners.remove(listener);
154
	}
155

    
156
	/**
157
	 * Sets the passed ButtonModel to be the currently selected ButtonModel of
158
	 * this ButtonGroup. Fires a property change.
159
	 * 
160
	 * @param model
161
	 *            ButtonModel to be selected
162
	 * @since 2.0
163
	 */
164
	protected void selectNewModel(ButtonModel model) {
165
		ButtonModel oldModel = selectedModel;
166
		selectedModel = model;
167
		if (oldModel != null)
168
			oldModel.setSelected(false);
169
		firePropertyChange(oldModel, model);
170
	}
171

    
172
	/**
173
	 * Sets the default selection of this ButtonGroup. Does nothing if it is not
174
	 * present in the group. Sets selection to the passed ButtonModel.
175
	 * 
176
	 * @param model
177
	 *            ButtonModel which is to be the default selection.
178
	 * @since 2.0
179
	 */
180
	public void setDefault(ButtonModel model) {
181
		defaultModel = model;
182
		if (getSelected() == null && defaultModel != null)
183
			defaultModel.setSelected(true);
184
	}
185

    
186
	/**
187
	 * Sets the button with the given ButtonModel to be selected.
188
	 * 
189
	 * @param model
190
	 *            The ButtonModel to be selected
191
	 * @since 2.0
192
	 */
193
	public void setSelected(ButtonModel model) {
194
		if (model == null)
195
			selectNewModel(null);
196
		else
197
			model.setSelected(true);
198
	}
199

    
200
	/**
201
	 * Sets model to the passed state.
202
	 * <p>
203
	 * If <i>value</i> is
204
	 * <ul>
205
	 * <li><code>true</code>:
206
	 * <ul>
207
	 * <li>The passed ButtonModel will own selection.
208
	 * </ul>
209
	 * <li><code>false</code>:
210
	 * <ul>
211
	 * <li>If the passed model owns selection, it will lose selection, and
212
	 * selection will be given to the default ButonModel. If no default
213
	 * ButtonModel was set, selection will remain as it was, as one ButtonModel
214
	 * must own selection at all times.
215
	 * <li>If the passed model does not own selection, then selection will
216
	 * remain as it was.
217
	 * </ul>
218
	 * </ul>
219
	 * 
220
	 * @param model
221
	 *            The model to be affected
222
	 * @param value
223
	 *            The selected state
224
	 * @since 2.0
225
	 */
226
	public void setSelected(ButtonModel model, boolean value) {
227
		if (value) {
228
			if (model == getSelected())
229
				return;
230
			selectNewModel(model);
231
		} else {
232
			if (model != getSelected())
233
				return;
234
			if (getDefault() == null)
235
				return;
236
			getDefault().setSelected(true);
237
		}
238
	}
239

    
240
}
(31-31/171)