Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
IconAnnotation |
|
| 2.6363636363636362;2.636 |
1 | /* | |
2 | * Copyright (c) 2009, Swiss Federal Department of Defence Civil Protection and Sport | |
3 | * (http://www.vbs.admin.ch) | |
4 | * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch) | |
5 | * All rights reserved. | |
6 | * | |
7 | * Licensed under the Open Permis License which accompanies this distribution, | |
8 | * and is available at http://www.openpermis.org/BSDlicenceKent.txt | |
9 | */ | |
10 | package org.openpermis.editor.policy.gui; | |
11 | ||
12 | import java.util.ArrayList; | |
13 | import java.util.List; | |
14 | ||
15 | /** | |
16 | * Annotations for icons. | |
17 | * @since 0.3.0 | |
18 | */ | |
19 | 15 | public enum IconAnnotation { |
20 | ||
21 | //---- Static | |
22 | ||
23 | /** | |
24 | * Add annotation. | |
25 | * @since 0.3.0 | |
26 | */ | |
27 | 1 | ADD("add"), |
28 | ||
29 | /** | |
30 | * Delete annotation. | |
31 | * @since 0.3.0 | |
32 | */ | |
33 | 1 | DELETE("delete"), |
34 | ||
35 | /** | |
36 | * Edit annotation. | |
37 | * @since 0.3.0 | |
38 | */ | |
39 | 1 | EDIT("edit"), |
40 | ||
41 | /** | |
42 | * Warning sign annotation. | |
43 | * @since 0.3.0 | |
44 | */ | |
45 | 1 | WARNING("warning"); |
46 | ||
47 | /** | |
48 | * Start character for annotations. | |
49 | * @since 0.3.0 | |
50 | */ | |
51 | private static final char START = '['; | |
52 | ||
53 | /** | |
54 | * End character for annotations. | |
55 | * @since 0.3.0 | |
56 | */ | |
57 | private static final char END = ']'; | |
58 | ||
59 | /** | |
60 | * Separator string for annotations. | |
61 | * @since 0.3.0 | |
62 | */ | |
63 | private static final String SEPARATOR = ","; | |
64 | ||
65 | /** | |
66 | * Tests if an icon key is annotated. | |
67 | * @param iconKey the icon key to test. | |
68 | * @return {@code true} if the key is annotated, {@code false} otherwise. | |
69 | * @since 0.3.0 | |
70 | */ | |
71 | public static boolean isAnnotated (String iconKey) { | |
72 | 14 | if (iconKey == null || iconKey.length() < 2) { |
73 | 3 | return false; |
74 | } | |
75 | 11 | iconKey = iconKey.trim(); |
76 | 11 | final int start = iconKey.indexOf(START); |
77 | 11 | final int end = iconKey.length() - 1; |
78 | 11 | return start != -1 && iconKey.charAt(end) == END; |
79 | } | |
80 | ||
81 | /** | |
82 | * Splits the icon key specified into a key and an annotation part. | |
83 | * @param iconKey the icon key to split. | |
84 | * @return an array consisting of the key and annotation part. | |
85 | * @since 0.3.0 | |
86 | */ | |
87 | static final String[] split (String iconKey) { | |
88 | 23 | final String[] split = new String[2]; |
89 | 23 | if (iconKey == null) { |
90 | 1 | split[0] = ""; |
91 | 1 | split[1] = ""; |
92 | 1 | return split; |
93 | } | |
94 | 22 | iconKey = iconKey.trim(); |
95 | 22 | final int start = iconKey.indexOf(START); |
96 | 22 | final int end = iconKey.length() - 1; |
97 | 22 | if (start != -1 && iconKey.charAt(end) == END) { |
98 | 18 | split[0] = iconKey.substring(0, start).trim(); |
99 | 18 | split[1] = iconKey.substring(start + 1, end).trim(); |
100 | } else { | |
101 | 4 | split[0] = iconKey.trim(); |
102 | 4 | split[1] = ""; |
103 | } | |
104 | 22 | return split; |
105 | } | |
106 | ||
107 | /** | |
108 | * Performs a lookup of an annotation. | |
109 | * @param annotation the annotation to lookup. | |
110 | * @return the corresponding icon annotation. | |
111 | * @since 0.3.0 | |
112 | */ | |
113 | private static final IconAnnotation getIconAnnotation (String annotation) { | |
114 | 43 | for (IconAnnotation iconAnnotation : IconAnnotation.values()) { |
115 | 38 | if (iconAnnotation.isAnnotation(annotation)) { |
116 | 9 | return iconAnnotation; |
117 | } | |
118 | } | |
119 | 5 | return null; |
120 | } | |
121 | ||
122 | /** | |
123 | * Returns an array of annotations defined in the specified icon key. | |
124 | * @param iconKey the icon key for which to retrieve the annotations. | |
125 | * @return the array of annotations, never {@code null}. | |
126 | * @throws IllegalArgumentException if an unknown annotation is encountered. | |
127 | * @since 0.3.0 | |
128 | */ | |
129 | static IconAnnotation[] getAnnotations (String iconKey) { | |
130 | 9 | final String[] split = split(iconKey); |
131 | 9 | final List<IconAnnotation> list = new ArrayList<IconAnnotation>(); |
132 | 18 | for (String annotation : split[1].split(SEPARATOR, -1)) { |
133 | 14 | IconAnnotation iconAnnotation = getIconAnnotation(annotation); |
134 | 14 | if (iconAnnotation == null) { |
135 | 5 | throw new IllegalArgumentException( |
136 | "No annotation for [" + annotation + "] for key [" + iconKey + "]." | |
137 | ); | |
138 | } | |
139 | 9 | list.add(iconAnnotation); |
140 | } | |
141 | 4 | return list.toArray(new IconAnnotation[list.size()]); |
142 | } | |
143 | ||
144 | /** | |
145 | * Returns the key portion of a possibly annotated icon key. | |
146 | * @param iconKey the icon key for which to retrieve the key portion. | |
147 | * @return the key requested. | |
148 | * @since 0.3.0 | |
149 | */ | |
150 | public static String getKey (String iconKey) { | |
151 | 0 | return split(iconKey)[0]; |
152 | } | |
153 | ||
154 | /** | |
155 | * Decomposes an annotated key into its parts. | |
156 | * @param iconKey the icon key to decompose. | |
157 | * @return an array of individual keys. | |
158 | * @throws IllegalArgumentException if an unknown annotation is encountered. | |
159 | * @since 0.3.0 | |
160 | */ | |
161 | public static String[] decompose (String iconKey) { | |
162 | 0 | final IconAnnotation[] annotations = getAnnotations(iconKey); |
163 | 0 | final String[] keys = new String[annotations.length + 1]; |
164 | 0 | keys[0] = getKey(iconKey); |
165 | 0 | for (int i = 0 ; i < annotations.length ; i++) { |
166 | 0 | keys[i + 1] = annotations[i].getKey(); |
167 | } | |
168 | 0 | return keys; |
169 | } | |
170 | ||
171 | //---- State | |
172 | ||
173 | /** | |
174 | * The annotation key. | |
175 | * @since 0.3.0 | |
176 | */ | |
177 | private final String key; | |
178 | ||
179 | //---- Constructors | |
180 | ||
181 | /** | |
182 | * Creates a new annotation for the specified key. | |
183 | * @param key the key (the lower case version is taken). | |
184 | * @since 0.3.0 | |
185 | */ | |
186 | 4 | private IconAnnotation (String key) { |
187 | 4 | this.key = key.toLowerCase(); |
188 | 4 | } |
189 | ||
190 | //---- Methods | |
191 | ||
192 | /** | |
193 | * Check if the specified annotation is string denotes the same annotation as this one. | |
194 | * @param annotation the annotation to test. | |
195 | * @return {@code true} if the annotation corresponds to this one, {@code false} otherwise. | |
196 | * @since 0.3.0 | |
197 | */ | |
198 | boolean isAnnotation (String annotation) { | |
199 | 38 | return getKey().equals(annotation.trim().toLowerCase()); |
200 | } | |
201 | ||
202 | /** | |
203 | * Annotates the specified icon key with this annotation. | |
204 | * @param iconKey the icon key to annotate, must not be {@code null}. | |
205 | * @return the annotated icon key. | |
206 | * @since 0.3.0 | |
207 | */ | |
208 | public String annotate (String iconKey) { | |
209 | 0 | if (iconKey == null) { |
210 | 0 | throw new IllegalArgumentException("Icon key must not be [null]."); |
211 | } | |
212 | 0 | final String[] split = split(iconKey); |
213 | 0 | if (split[1].length() > 0) { |
214 | 0 | split[1] += SEPARATOR; |
215 | } | |
216 | 0 | split[1] += getKey(); |
217 | 0 | return new StringBuilder(split[0]).append(START).append(split[1]).append(END).toString(); |
218 | } | |
219 | ||
220 | /** | |
221 | * Returns the lower case key of the icon annotation. | |
222 | * @return the lower case key of the icon annotation. | |
223 | * @since 0.3.0 | |
224 | */ | |
225 | public String getKey () { | |
226 | 38 | return this.key; |
227 | } | |
228 | ||
229 | //---- Enum | |
230 | ||
231 | /** | |
232 | * @since 0.3.0 | |
233 | */ | |
234 | @Override | |
235 | public String toString () { | |
236 | 0 | return getKey(); |
237 | } | |
238 | ||
239 | } |