Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
IconRegistry |
|
| 7.0;7 |
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.HashMap; | |
13 | import java.util.Map; | |
14 | ||
15 | import javax.swing.Icon; | |
16 | ||
17 | import org.jdesktop.application.ResourceMap; | |
18 | ||
19 | ||
20 | /** | |
21 | * Registry for icons. | |
22 | * <p>Icons can be accessed by keys defined in a resource map. | |
23 | * Annotated icons in form "key[annotation1,annotation2]" are supported.</p> | |
24 | * @see IconAnnotation | |
25 | * @since 0.3.0 | |
26 | */ | |
27 | public class IconRegistry { | |
28 | ||
29 | //---- State | |
30 | ||
31 | /** | |
32 | * The parent registry to ask first if there is a request for an icon. | |
33 | * @since 0.3.0 | |
34 | */ | |
35 | private final IconRegistry parent; | |
36 | ||
37 | /** | |
38 | * The resource map to load icons from. | |
39 | * @since 0.3.0 | |
40 | */ | |
41 | private final ResourceMap resourceMap; | |
42 | ||
43 | /** | |
44 | * Registry of loaded icons. | |
45 | * @since 0.3.0 | |
46 | */ | |
47 | private final Map<String, Icon> registry; | |
48 | ||
49 | /** | |
50 | * The prefix to use when loading icons from the resource map. | |
51 | * @since 0.3.0 | |
52 | */ | |
53 | private final String prefix; | |
54 | ||
55 | //---- Constructors | |
56 | ||
57 | /** | |
58 | * Creates an icon registry that loads its icons according to the keys stored in | |
59 | * the specified resource map. | |
60 | * @param parent parent registry to ask first for icons, may be {@code null}. | |
61 | * @param resourceMap the resource map to load the icons from. | |
62 | * @param prefix the prefix to use when loading icons from the resource map. | |
63 | * @since 0.3.0 | |
64 | */ | |
65 | 0 | public IconRegistry (IconRegistry parent, ResourceMap resourceMap, String prefix) { |
66 | 0 | this.parent = parent; |
67 | 0 | this.resourceMap = resourceMap; |
68 | 0 | this.prefix = prefix; |
69 | 0 | this.registry = new HashMap<String, Icon>(); |
70 | 0 | } |
71 | ||
72 | //---- Methods | |
73 | ||
74 | /** | |
75 | * Loads an icon with the specified key. | |
76 | * @param key the resource map icon key to load the icon from. | |
77 | * @return the icon loaded, {@code null} if there is no such icon. | |
78 | * @since 0.3.0 | |
79 | */ | |
80 | private Icon load (String key) { | |
81 | 0 | if (key == null) { |
82 | 0 | return null; |
83 | } | |
84 | 0 | final String lookupKey = this.prefix + key.toLowerCase(); |
85 | 0 | if (!this.resourceMap.containsKey(lookupKey)) { |
86 | 0 | return null; |
87 | } | |
88 | 0 | return this.resourceMap.getIcon(lookupKey); |
89 | } | |
90 | ||
91 | /** | |
92 | * Returns an icon for the specified key. | |
93 | * <p>The key may be annotated.</p> | |
94 | * @param key the key for wihch to retrieve an icon. | |
95 | * @return the icon requested, {@code null} if there is no such icon. | |
96 | * @since 0.3.0 | |
97 | */ | |
98 | public Icon getIcon (String key) { | |
99 | 0 | if (IconAnnotation.isAnnotated(key)) { |
100 | 0 | final String[] keys = IconAnnotation.decompose(key); |
101 | 0 | if (keys.length == 0) { |
102 | 0 | return null; |
103 | 0 | } else if (keys.length == 1) { |
104 | 0 | return getIcon(keys[0]); |
105 | } | |
106 | 0 | final Icon[] icons = new Icon[keys.length]; |
107 | 0 | for (int i = 0 ; i < keys.length ; i++) { |
108 | 0 | icons[i] = getIcon(keys[i]); |
109 | } | |
110 | 0 | Icon combinedIcon = icons[0]; |
111 | 0 | for (int i = 1 ; i < icons.length ; i++) { |
112 | 0 | combinedIcon = new CompoundIcon(combinedIcon, icons[i]); |
113 | } | |
114 | 0 | return combinedIcon; |
115 | } | |
116 | 0 | Icon icon = this.parent != null ? this.parent.getIcon(key) : null; |
117 | 0 | if (icon != null) { |
118 | 0 | return icon; |
119 | } | |
120 | 0 | icon = this.registry.get(key); |
121 | 0 | if (icon != null) { |
122 | 0 | return icon; |
123 | } | |
124 | 0 | icon = load(key); |
125 | 0 | if (icon != null) { |
126 | 0 | this.registry.put(key, icon); |
127 | } | |
128 | 0 | return icon; |
129 | } | |
130 | ||
131 | } |