01: /*
02: ItsNat Java Web Application Framework
03: Copyright (C) 2007 Innowhere Software Services S.L., Spanish Company
04: Author: Jose Maria Arranz Santamaria
05:
06: This program is free software: you can redistribute it and/or modify
07: it under the terms of the GNU Affero General Public License as published by
08: the Free Software Foundation, either version 3 of the License, or
09: (at your option) any later version. See the GNU Affero General Public
10: License for more details. See the copy of the GNU Affero General Public License
11: included in this program. If not, see <http://www.gnu.org/licenses/>.
12: */
13:
14: package org.itsnat.impl.comp;
15:
16: import org.itsnat.core.ItsNatException;
17: import org.itsnat.comp.ItsNatButtonGroup;
18: import org.itsnat.comp.ItsNatButtonRadio;
19: import org.itsnat.comp.ItsNatComponent;
20: import java.util.LinkedList;
21: import java.util.List;
22: import javax.swing.ButtonGroup;
23: import javax.swing.JToggleButton.ToggleButtonModel;
24:
25: /**
26: * Clase auxiliar y no pública (por ahora)
27: *
28: * @author jmarranz
29: */
30: public class ItsNatButtonGroupImpl implements ItsNatButtonGroup {
31: protected String name;
32: protected ButtonGroup group;
33: protected List buttonList = new LinkedList();
34:
35: /**
36: * Creates a new instance of ItsNatButtonGroupImpl
37: */
38: public ItsNatButtonGroupImpl(String name, ButtonGroup group) {
39: if (group == null)
40: throw new ItsNatException("ButtonGroup cannot be null");
41: this .name = name;
42: this .group = group;
43: }
44:
45: public static void checkRadioButton(ItsNatComponent button) {
46: if (!(button instanceof ItsNatButtonRadio))
47: throw new ItsNatException("Only "
48: + ItsNatButtonRadio.class.getName()
49: + " components are supported");
50: }
51:
52: public void addButton(ItsNatComponent button) {
53: checkRadioButton(button);
54: addButton((ItsNatButtonRadioInternal) button, true);
55: }
56:
57: public void addButton(ItsNatButtonRadioInternal button,
58: boolean setInComponent) {
59: buttonList.remove(button); // Para asegurarnos de que no se añade dos veces
60: buttonList.add(button);
61: if (setInComponent) {
62: button.setItsNatButtonGroup(this , false);
63: }
64: }
65:
66: public void removeButton(ItsNatComponent button) {
67: checkRadioButton(button);
68: removeButton((ItsNatButtonRadio) button, true);
69: }
70:
71: public void removeButton(ItsNatButtonRadio button,
72: boolean setInModel) {
73: buttonList.remove(button);
74: if (setInModel) {
75: ToggleButtonModel model = (ToggleButtonModel) button
76: .getButtonModel();
77: model.setGroup(null);
78:
79: button.setItsNatButtonGroup((ItsNatButtonGroup) null); // Pues el setGroup no genera evento hay que sincronizar explícitamente
80: }
81: }
82:
83: public int getButtonCount() {
84: return buttonList.size();
85: }
86:
87: public ItsNatComponent getButton(int index) {
88: return (ItsNatComponent) buttonList.get(index);
89: }
90:
91: public String getName() {
92: return name;
93: }
94:
95: public ButtonGroup getButtonGroup() {
96: return group;
97: }
98: }
|