OSDN Git Service

c6c4e11a7ee4edde90dbdb02f0f711e765a7ebb6
[stigmata/stigmata.git] / src / main / java / jp / sourceforge / stigmata / ui / swing / mds / LabelMap.java
1 package jp.sourceforge.stigmata.ui.swing.mds;
2 /*
3  * $Id$
4  */
5
6 import java.util.ArrayList;
7 import java.util.Collections;
8 import java.util.HashMap;
9 import java.util.List;
10 import java.util.Map;
11
12 import jp.sourceforge.talisman.mds.ui.swing.GeometoryType;
13
14 /**
15  * @author Haruaki TAMADA
16  * @version $Revision$ 
17  */
18 class LabelMap{
19     private List<String> labels = new ArrayList<String>();
20     private Map<String, String> groups = new HashMap<String, String>();
21     private Map<String, Integer> gids = new HashMap<String, Integer>();
22     private boolean groupEnabled = true;
23
24     public LabelMap(){
25         gids.put("", 0);
26     }
27
28     public LabelMap(String[] labels){
29         this();
30         for(String label: labels){
31             addLabel(label);
32         }
33     }
34
35     public boolean isAvailableLabel(int index){
36         return index >= 0 && index < labels.size();
37     }
38
39     public void setGroupEnabled(boolean flag){
40         this.groupEnabled = flag;
41     }
42
43     public boolean isGroupEnabled(){
44         return groupEnabled && getGroupCount() < GeometoryType.getMaxGroupCount();
45     }
46
47     public void addLabel(String label){
48         labels.add(label);
49     }
50
51     public String getLabel(int index){
52         return labels.get(index);
53     }
54
55     public void setGroup(String label, String groupLabel){
56         groups.put(label, groupLabel);
57         if(gids.get(groupLabel) == null){
58             gids.put(groupLabel, gids.size());
59         }
60     }
61
62     public String getGroup(String label){
63         String group = groups.get(label);
64         if(group == null){
65             group = "";
66         }
67         return group;
68     }
69
70     public int getGroupIdFromElementName(String label){
71         return getGroupId(groups.get(label));
72     }
73
74     public int getGroupId(String groupLabel){
75         Integer i = new Integer(0);
76         if(isGroupEnabled()){
77             i = gids.get(groupLabel);
78             if(i == null){
79                 i = new Integer(0);
80             }
81         }
82         return i;
83     }
84
85     public int getGroupCount(){
86         return gids.size();
87     }
88
89     public String[] getGroupNames(){
90         String[] names = new String[gids.size()];
91         int index = 0;
92         for(String name: gids.keySet()){
93             names[index] = name;
94             index++;
95         }
96
97         return names;
98     }
99
100     public synchronized Map<String, Integer> getGroupElementCounts(){
101         Map<String, Integer> map = new HashMap<String, Integer>();
102
103         for(Map.Entry<String, String> entry: groups.entrySet()){
104             Integer i = map.get(entry.getValue());
105             if(i == null){
106                 i = new Integer(0);
107             }
108             map.put(entry.getValue(), i + 1);
109         }
110         return Collections.unmodifiableMap(map);
111     }
112
113     public synchronized int getGroupElementCount(String group){
114         int count = 0;
115         for(Map.Entry<String, String> entry: groups.entrySet()){
116             if(group.equals(entry.getValue())){
117                 count++;
118             }
119         }
120         return count;
121     }
122
123     public synchronized String[] getGroupElements(String group){
124         List<String> elements = new ArrayList<String>();
125         for(Map.Entry<String, String> entry: groups.entrySet()){
126             if(group.equals(entry.getValue())){
127                 elements.add(entry.getKey());
128             }
129         }
130         return elements.toArray(new String[elements.size()]);
131     }
132 }