Source code: myComponents/myButtonGroup.java
1 /* Evolvo - Image Generator
2 * Copyright (C) 2000 Andrew Molloy
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 package myComponents;
20
21 import java.awt.event.*;
22 import java.util.Vector;
23 import java.util.Enumeration;
24 import java.io.Serializable;
25 import javax.swing.*;
26
27 public class myButtonGroup extends ButtonGroup implements Serializable {
28
29 // the list of buttons participating in this group
30 protected Vector buttons = new Vector();
31
32 /**
33 * The current choice.
34 */
35 ButtonModel selection = null;
36
37 /**
38 * Creates a new ButtonGroup.
39 */
40 public myButtonGroup() {}
41
42 /**
43 * Adds the button to the group.
44 */
45 public void add(AbstractButton b) {
46 if(b == null) {
47 return;
48 }
49 buttons.addElement(b);
50 if(selection == null && b.isSelected()) {
51 selection = b.getModel();
52 }
53 b.getModel().setGroup(this);
54 }
55
56 /**
57 * Removes the button from the group.
58 */
59 public void remove(AbstractButton b) {
60 if(b == null) {
61 return;
62 }
63 buttons.removeElement(b);
64 if(b.getModel() == selection) {
65 selection = null;
66 }
67 b.getModel().setGroup(null);
68 }
69
70 /**
71 * Return all the buttons that are participating in
72 * this group.
73 */
74 public Enumeration getElements() {
75 return buttons.elements();
76 }
77
78 /**
79 * Return the selected button model.
80 */
81 public ButtonModel getSelection() {
82 return selection;
83 }
84
85 /**
86 * Sets the selected value for the button.
87 */
88 public void setSelected(ButtonModel m, boolean b)
89 {
90 if(b && m != selection)
91 {
92 ButtonModel oldSelection = selection;
93 selection = m;
94 if(oldSelection != null)
95 {
96 oldSelection.setSelected(false);
97 }
98 }
99 if (!b && m == selection)
100 {
101 ButtonModel oldSelection = selection;
102 selection = null;
103 if (oldSelection != null)
104 {
105 oldSelection.setSelected(false);
106 }
107 }
108 }
109
110 /**
111 * Returns the selected value for the button.
112 */
113 public boolean isSelected(ButtonModel m) {
114 return (m == selection);
115 }
116
117 /**
118 * Returns the number of buttons in the group.
119 */
120 public int getButtonCount() {
121 if (buttons == null) {
122 return 0;
123 } else {
124 return buttons.size();
125 }
126 }
127
128 }