OSDN Git Service

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