OSDN Git Service

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