1
4
5 import org.aswing.*;
6 import org.aswing.utils.ArrayUtils;
7
8
12 class org.aswing.ButtonGroup{
13
14 private var buttons:Array;
15
16
19 private var selection:ButtonModel = null;
20
21
24 public function ButtonGroup() {
25 buttons = new Array();
26 }
27
28
33 public function append(b:AbstractButton):Void {
34 if(b == null) {
35 return;
36 }
37 buttons.push(b);
38
39 if (b.isSelected()) {
40 if (selection == null) {
41 selection = b.getModel();
42 } else {
43 b.setSelected(false);
44 }
45 }
46
47 b.getModel().setGroup(this);
48 }
49
50
55 public function remove(b:AbstractButton):Void {
56 if(b == null) {
57 return;
58 }
59 ArrayUtils.removeFromArray(buttons, b);
60 if(b.getModel() == selection) {
61 selection = null;
62 }
63 b.getModel().setGroup(null);
64 }
65
66
71 public function getElements():Array {
72 return ArrayUtils.cloneArray(buttons);
73 }
74
75
80 public function getSelection():ButtonModel {
81 return selection;
82 }
83
84
92 public function setSelected(m:ButtonModel, b:Boolean):Void {
93 if (b && m != null && m != selection) {
94 var oldSelection:ButtonModel = selection;
95 selection = m;
96 if (oldSelection != null) {
97 oldSelection.setSelected(false);
98 }
99 m.setSelected(true);
100 }
101 }
102
103
109 public function isSelected(m:ButtonModel):Boolean {
110 return (m == selection);
111 }
112
113
118 public function getButtonCount():Number {
119 return buttons.length;
120 }
121 }
122