01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package javax.swing;
19:
20: import java.io.Serializable;
21: import java.util.Enumeration;
22: import java.util.Vector;
23:
24: public class ButtonGroup implements Serializable {
25: private static final long serialVersionUID = 4259076101881721375L;
26:
27: protected Vector<AbstractButton> buttons = new Vector<AbstractButton>();
28:
29: private ButtonModel selection;
30:
31: public void add(final AbstractButton button) {
32: if (button == null) {
33: return;
34: }
35:
36: buttons.add(button);
37:
38: ButtonModel model = button.getModel();
39: if (button.isSelected()) {
40: if (selection != null && model != selection) {
41: button.setSelected(false);
42: } else {
43: selection = model;
44: }
45: }
46: model.setGroup(this );
47: }
48:
49: public int getButtonCount() {
50: return buttons.size();
51: }
52:
53: public Enumeration<javax.swing.AbstractButton> getElements() {
54: return buttons.elements();
55: }
56:
57: public ButtonModel getSelection() {
58: return selection;
59: }
60:
61: public boolean isSelected(final ButtonModel model) {
62: return (model == selection);
63: }
64:
65: public void remove(final AbstractButton button) {
66: if (button == null) {
67: return;
68: }
69:
70: buttons.remove(button);
71: ButtonModel model = button.getModel();
72: if (selection == model) {
73: selection = null;
74: }
75: model.setGroup(null);
76: }
77:
78: public void setSelected(final ButtonModel model,
79: final boolean selected) {
80: if (!selected || model == null || selection == model) {
81: return;
82: }
83: if (!model.isSelected()) {
84: model.setSelected(true);
85: }
86: ButtonModel prevSelection = selection;
87: selection = model;
88: if (prevSelection != null) {
89: prevSelection.setSelected(false);
90: }
91: }
92: }
|