* Bug 13962 fixed: preselecting a groupnamed radiobutton impossible after exec>1
[scilab.git] / scilab / modules / gui / src / java / org / scilab / modules / gui / bridge / groupmanager / GroupManager.java
1 package org.scilab.modules.gui.bridge.groupmanager;
2
3 import java.util.Collection;
4 import java.util.Enumeration;
5 import java.util.HashMap;
6 import java.util.Map;
7
8 import javax.swing.AbstractButton;
9 import javax.swing.ButtonGroup;
10 import javax.swing.ButtonModel;
11
12 public class GroupManager {
13     private Map<String, ButtonGroup> buttonGroup = new HashMap<String, ButtonGroup>();
14     private static GroupManager me;
15
16     private GroupManager() {
17     }
18
19     public static GroupManager getGroupManager() {
20         if (me == null) {
21             me = new GroupManager();
22         }
23
24         return me;
25     }
26
27     public void addToGroup(String groupName, AbstractButton obj) {
28
29         //first is to remove obj from others group
30         removeFromGroup(obj);
31
32         ButtonGroup group = buttonGroup.get(groupName);
33
34         if (group == null) {
35             group = new ButtonGroup();
36             buttonGroup.put(groupName, group);
37         }
38
39         group.add(obj);
40     }
41
42     public void removeFromGroup(AbstractButton obj) {
43         Collection<ButtonGroup> groupList = buttonGroup.values();
44         for (ButtonGroup group : groupList) {
45             group.remove(obj);
46         }
47     }
48
49     public Enumeration<AbstractButton> getGroupElements(String groupname) {
50         ButtonGroup group = buttonGroup.get(groupname);
51         return group.getElements();
52     }
53
54     public void setSelected(ButtonModel model, String groupname, boolean status) {
55         ButtonGroup group = buttonGroup.get(groupname);
56         group.setSelected(model, status);
57     }
58
59     public boolean isSelected(String groupname) {
60         ButtonGroup group = buttonGroup.get(groupname);
61         ButtonModel model = group.getSelection();
62         return group.isSelected(model);
63     }
64 }