Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
UMLRadioButtonPanel |
|
| 2.0;2 |
1 | /* $Id: UMLRadioButtonPanel.java 17881 2010-01-12 21:09:28Z linus $ | |
2 | ***************************************************************************** | |
3 | * Copyright (c) 2009 Contributors - see below | |
4 | * All rights reserved. This program and the accompanying materials | |
5 | * are made available under the terms of the Eclipse Public License v1.0 | |
6 | * which accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * tfmorris | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 1996-2007 The Regents of the University of California. All | |
17 | // Rights Reserved. Permission to use, copy, modify, and distribute this | |
18 | // software and its documentation without fee, and without a written | |
19 | // agreement is hereby granted, provided that the above copyright notice | |
20 | // and this paragraph appear in all copies. This software program and | |
21 | // documentation are copyrighted by The Regents of the University of | |
22 | // California. The software program and documentation are supplied "AS | |
23 | // IS", without any accompanying services from The Regents. The Regents | |
24 | // does not warrant that the operation of the program will be | |
25 | // uninterrupted or error-free. The end-user understands that the program | |
26 | // was developed for research purposes and is advised not to rely | |
27 | // exclusively on the program for any reason. IN NO EVENT SHALL THE | |
28 | // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, | |
29 | // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, | |
30 | // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF | |
31 | // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF | |
32 | // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY | |
33 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
34 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE | |
35 | // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF | |
36 | // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, | |
37 | // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
38 | ||
39 | package org.argouml.uml.ui; | |
40 | ||
41 | import java.awt.Font; | |
42 | import java.awt.GridLayout; | |
43 | import java.beans.PropertyChangeEvent; | |
44 | import java.beans.PropertyChangeListener; | |
45 | import java.util.ArrayList; | |
46 | import java.util.Enumeration; | |
47 | import java.util.List; | |
48 | import java.util.Map; | |
49 | ||
50 | import javax.swing.AbstractButton; | |
51 | import javax.swing.Action; | |
52 | import javax.swing.ButtonGroup; | |
53 | import javax.swing.JPanel; | |
54 | import javax.swing.JRadioButton; | |
55 | import javax.swing.border.TitledBorder; | |
56 | ||
57 | import org.argouml.model.Model; | |
58 | import org.argouml.i18n.Translator; | |
59 | import org.argouml.ui.LookAndFeelMgr; | |
60 | import org.argouml.ui.targetmanager.TargetEvent; | |
61 | import org.argouml.ui.targetmanager.TargetListener; | |
62 | import org.tigris.gef.presentation.Fig; | |
63 | ||
64 | /** | |
65 | * A panel that shows a group of radiobuttons. An action can be added | |
66 | * to the panel which will be executed when one of the radiobuttons is | |
67 | * pressed. Via the name of the button (settext), the action can find | |
68 | * out which button is pressed. | |
69 | * | |
70 | * @author jaap.branderhorst@xs4all.nl | |
71 | * @since Jan 4, 2003 | |
72 | */ | |
73 | public abstract class UMLRadioButtonPanel | |
74 | extends JPanel | |
75 | implements TargetListener, PropertyChangeListener { | |
76 | ||
77 | 0 | private static Font stdFont = |
78 | LookAndFeelMgr.getInstance().getStandardFont(); | |
79 | ||
80 | /** | |
81 | * The target object of which some attribute is shown via this panel. | |
82 | */ | |
83 | private Object panelTarget; | |
84 | ||
85 | /** | |
86 | * The name of the event that is fired when the target object has changed | |
87 | * the attribute that is shown here. | |
88 | */ | |
89 | private String propertySetName; | |
90 | ||
91 | /** | |
92 | * The group of buttons | |
93 | */ | |
94 | 0 | private ButtonGroup buttonGroup = new ButtonGroup(); |
95 | ||
96 | /** | |
97 | * Constructs a new UMLRadioButtonPanel. | |
98 | * | |
99 | * @param isDoubleBuffered see {@link JPanel}. | |
100 | * @param title | |
101 | * The title of the titledborder around the buttons. If the title | |
102 | * is null, there is no border shown. | |
103 | * @param labeltextsActioncommands | |
104 | * A map of keys containing the texts for the buttons and values | |
105 | * containing the actioncommand that permits the setAction to | |
106 | * logically recognize the button. | |
107 | * @param thePropertySetName | |
108 | * the name of the MEvent that is fired when the property that it | |
109 | * shows changes value. | |
110 | * @param setAction | |
111 | * the action that should be registered with the buttons and | |
112 | * that's executed when one of the buttons is pressed. | |
113 | * @param horizontal | |
114 | * when true the buttons should be laid out horizontally. | |
115 | */ | |
116 | public UMLRadioButtonPanel( | |
117 | boolean isDoubleBuffered, | |
118 | String title, | |
119 | List<String[]> labeltextsActioncommands, | |
120 | String thePropertySetName, | |
121 | Action setAction, | |
122 | boolean horizontal) { | |
123 | 0 | super(isDoubleBuffered); |
124 | 0 | setLayout(horizontal ? new GridLayout() : new GridLayout(0, 1)); |
125 | 0 | setDoubleBuffered(true); |
126 | 0 | if (Translator.localize(title) != null) { |
127 | 0 | TitledBorder border = new TitledBorder(Translator.localize(title)); |
128 | 0 | border.setTitleFont(stdFont); |
129 | 0 | setBorder(border); |
130 | } | |
131 | 0 | setButtons(labeltextsActioncommands, setAction); |
132 | 0 | setPropertySetName(thePropertySetName); |
133 | 0 | } |
134 | ||
135 | /** | |
136 | * Constructs a new UMLRadioButtonPanel. | |
137 | * | |
138 | * @param title | |
139 | * The title of the titledborder around the buttons. | |
140 | * @param labeltextsActioncommands | |
141 | * A map of keys containing the texts for the buttons and values | |
142 | * containing the actioncommand that permits the setAction to | |
143 | * logically recognize the button. | |
144 | * @param thePropertySetName | |
145 | * the name of the MEvent that is fired when the property that is | |
146 | * shown changes value. | |
147 | * @param setAction | |
148 | * the action that should be registered with the buttons and | |
149 | * that's executed when one of the buttons is pressed | |
150 | * @param horizontal | |
151 | * when true the buttons should be laid out horizontally. | |
152 | */ | |
153 | public UMLRadioButtonPanel(String title, | |
154 | List<String[]> labeltextsActioncommands, | |
155 | String thePropertySetName, | |
156 | Action setAction, | |
157 | boolean horizontal) { | |
158 | 0 | this(true, title, labeltextsActioncommands, |
159 | thePropertySetName, setAction, horizontal); | |
160 | 0 | } |
161 | ||
162 | ||
163 | ||
164 | private static List<String[]> toList(Map<String, String> map) { | |
165 | 0 | List<String[]> list = new ArrayList<String[]>(); |
166 | 0 | for (Map.Entry<String, String> entry : map.entrySet()) { |
167 | 0 | list.add(new String[] {entry.getKey(), entry.getValue()}); |
168 | } | |
169 | 0 | return list; |
170 | } | |
171 | ||
172 | ||
173 | /** | |
174 | * Construct the buttons and place them in the panel as well as the button | |
175 | * group. | |
176 | * | |
177 | * @param labeltextsActioncommands | |
178 | * A list of string arrays containing a pair of strings with the | |
179 | * texts for the buttons (already localized) and string value for | |
180 | * the actioncommand that permits the setAction to logically | |
181 | * recognize the button. | |
182 | * @param setAction | |
183 | * the action that should be registered with the buttons and | |
184 | * that's executed when one of the buttons is pressed | |
185 | */ | |
186 | private void setButtons(List<String[]> labeltextsActioncommands, | |
187 | Action setAction) { | |
188 | 0 | Enumeration en = buttonGroup.getElements(); |
189 | 0 | while (en.hasMoreElements()) { |
190 | 0 | AbstractButton button = (AbstractButton) en.nextElement(); |
191 | 0 | buttonGroup.remove(button); |
192 | 0 | } |
193 | 0 | removeAll(); |
194 | ||
195 | // Add an invisible button to be used when everything is off | |
196 | 0 | buttonGroup.add(new JRadioButton()); |
197 | ||
198 | 0 | for (String[] keyAndLabelX : labeltextsActioncommands) { |
199 | 0 | JRadioButton button = new JRadioButton(keyAndLabelX[0]); |
200 | 0 | button.addActionListener(setAction); |
201 | 0 | String actionCommand = keyAndLabelX[1]; |
202 | 0 | button.setActionCommand(actionCommand); |
203 | 0 | button.setFont(LookAndFeelMgr.getInstance().getStandardFont()); |
204 | 0 | buttonGroup.add(button); |
205 | 0 | add(button); |
206 | 0 | } |
207 | 0 | } |
208 | ||
209 | ||
210 | /* | |
211 | * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent) | |
212 | */ | |
213 | public void propertyChange(PropertyChangeEvent e) { | |
214 | 0 | if (e.getPropertyName().equals(propertySetName)) { |
215 | 0 | buildModel(); |
216 | } | |
217 | 0 | } |
218 | ||
219 | /** | |
220 | * Returns the target. | |
221 | * @return Object | |
222 | */ | |
223 | public Object getTarget() { | |
224 | 0 | return panelTarget; |
225 | } | |
226 | ||
227 | /** | |
228 | * Sets the target and removes/adds this as a listener to the target. | |
229 | * @param target The target to set | |
230 | */ | |
231 | public void setTarget(Object target) { | |
232 | 0 | target = target instanceof Fig ? ((Fig) target).getOwner() : target; |
233 | 0 | if (Model.getFacade().isAModelElement(panelTarget)) { |
234 | 0 | Model.getPump().removeModelEventListener(this, panelTarget, |
235 | propertySetName); | |
236 | } | |
237 | 0 | panelTarget = target; |
238 | 0 | if (Model.getFacade().isAModelElement(panelTarget)) { |
239 | 0 | Model.getPump().addModelEventListener(this, panelTarget, |
240 | propertySetName); | |
241 | } | |
242 | 0 | if (panelTarget != null) { |
243 | 0 | buildModel(); |
244 | } | |
245 | 0 | } |
246 | ||
247 | /** | |
248 | * Returns the propertySetName. | |
249 | * @return String | |
250 | */ | |
251 | public String getPropertySetName() { | |
252 | 0 | return propertySetName; |
253 | } | |
254 | ||
255 | /** | |
256 | * Sets the propertySetName. | |
257 | * @param name The propertySetName to set | |
258 | */ | |
259 | public void setPropertySetName(String name) { | |
260 | 0 | propertySetName = name; |
261 | 0 | } |
262 | ||
263 | /** | |
264 | * Builds the model. That is: it selects the radiobutton showing the value | |
265 | * of the attribute shown. The name of this method is chosen to be | |
266 | * compliant with for example UMLModelElementListModel2 | |
267 | */ | |
268 | public abstract void buildModel(); | |
269 | ||
270 | /** | |
271 | * Selects the radiobutton with the given actionCommand. If a null parameter | |
272 | * is passed, all buttons in the group will be deselected. | |
273 | * | |
274 | * @param actionCommand | |
275 | * The actionCommand of the button that should be selected or | |
276 | * null to deselect all buttons. | |
277 | */ | |
278 | public void setSelected(String actionCommand) { | |
279 | 0 | Enumeration<AbstractButton> en = buttonGroup.getElements(); |
280 | 0 | if (actionCommand == null) { |
281 | // Our first button is invisible. | |
282 | // Selecting it deselects all visible buttons. | |
283 | 0 | en.nextElement().setSelected(true); |
284 | 0 | return; |
285 | } | |
286 | 0 | while (en.hasMoreElements()) { |
287 | 0 | AbstractButton b = en.nextElement(); |
288 | 0 | if (actionCommand.equals(b.getModel().getActionCommand())) { |
289 | 0 | b.setSelected(true); |
290 | 0 | break; |
291 | } | |
292 | 0 | } |
293 | 0 | } |
294 | ||
295 | /* | |
296 | * @see org.argouml.ui.targetmanager.TargetListener#targetAdded(org.argouml.ui.targetmanager.TargetEvent) | |
297 | */ | |
298 | public void targetAdded(TargetEvent e) { | |
299 | 0 | setTarget(e.getNewTarget()); |
300 | 0 | } |
301 | ||
302 | /* | |
303 | * @see org.argouml.ui.targetmanager.TargetListener#targetRemoved(org.argouml.ui.targetmanager.TargetEvent) | |
304 | */ | |
305 | public void targetRemoved(TargetEvent e) { | |
306 | 0 | setTarget(e.getNewTarget()); |
307 | 0 | } |
308 | ||
309 | /* | |
310 | * @see org.argouml.ui.targetmanager.TargetListener#targetSet(org.argouml.ui.targetmanager.TargetEvent) | |
311 | */ | |
312 | public void targetSet(TargetEvent e) { | |
313 | 0 | setTarget(e.getNewTarget()); |
314 | 0 | } |
315 | ||
316 | } |