Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
IconRegistryConverter |
|
| 2.5714285714285716;2.571 |
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 javax.swing.Icon; | |
13 | import javax.swing.ImageIcon; | |
14 | ||
15 | import org.jdesktop.application.ResourceConverter; | |
16 | import org.jdesktop.application.ResourceMap; | |
17 | ||
18 | ||
19 | /** | |
20 | * Resource converter that looks up icon keys in an icon registry. | |
21 | * <p>An icon key is a key enclosed in angle brackets, e.g. <tt><foo></tt>. Icon keys may | |
22 | * have annotations as in the following example: <tt><foo[add,error]></tt>.</p> | |
23 | * @since 0.3.0 | |
24 | */ | |
25 | public final class IconRegistryConverter | |
26 | extends ResourceConverter | |
27 | { | |
28 | ||
29 | //---- Static | |
30 | ||
31 | /** | |
32 | * The singleton icon registry converter instance. | |
33 | * <p>Due to the poor design of the resource converter class we have to use a singleton | |
34 | * which is plugged in at application startup (see {@link #initialize() initialize}) and | |
35 | * {@link #activate(IconRegistry) activate} at a later stage.</p> | |
36 | * @since 0.3.0 | |
37 | */ | |
38 | 0 | private static final IconRegistryConverter SINGLETON = new IconRegistryConverter(); |
39 | ||
40 | /** | |
41 | * Reserves a resource converter slot for the icon registry converter. | |
42 | * <p>The slot has to be reserver prior to initialization of the default icon resource | |
43 | * converter since the converter cannot be removed once its registered.</p> | |
44 | * @since 0.3.0 | |
45 | */ | |
46 | public static void initialize () { | |
47 | 0 | ResourceConverter.register(SINGLETON); |
48 | 0 | } |
49 | ||
50 | /** | |
51 | * Initializes the singleton resource converter. | |
52 | * @param iconRegistry the icon registry to use. | |
53 | * @since 0.3.0 | |
54 | */ | |
55 | public static void activate (IconRegistry iconRegistry) { | |
56 | 0 | SINGLETON.initialize(ResourceConverter.forType(Icon.class), iconRegistry); |
57 | 0 | } |
58 | ||
59 | /** | |
60 | * @since 0.3.0 | |
61 | */ | |
62 | private static final String ICON_REGISTRY_KEY_START = "<"; | |
63 | ||
64 | /** | |
65 | * @since 0.3.0 | |
66 | */ | |
67 | private static final String ICON_REGISTRY_KEY_END = ">"; | |
68 | ||
69 | /** | |
70 | * Returns the icon registry key for the specified key. | |
71 | * <p>If the key starts and ends with angle brackets it returns the contents between the | |
72 | * brackets. Returns {@code null} if the key is no icon registry key.</p> | |
73 | * @param key the key for which to retrieve the icon registry key. | |
74 | * @return the icon registry key or {@code null} if the key is no icon registry key. | |
75 | * @since 0.3.0 | |
76 | */ | |
77 | private static String getIconRegistryKey (String key) { | |
78 | 0 | if (key == null) { |
79 | 0 | return null; |
80 | } | |
81 | 0 | final String trimKey = key.trim(); |
82 | 0 | if ( |
83 | trimKey.startsWith(ICON_REGISTRY_KEY_START) && | |
84 | trimKey.endsWith(ICON_REGISTRY_KEY_END) | |
85 | ) { | |
86 | 0 | return trimKey.substring( |
87 | ICON_REGISTRY_KEY_START.length(), | |
88 | trimKey.length() - ICON_REGISTRY_KEY_END.length() | |
89 | ).toLowerCase(); | |
90 | } | |
91 | 0 | return null; |
92 | } | |
93 | ||
94 | //---- State | |
95 | ||
96 | /** | |
97 | * The resource converter to delegate unhandled requests to. | |
98 | * @since 0.3.0 | |
99 | */ | |
100 | private ResourceConverter delegate; | |
101 | ||
102 | /** | |
103 | * The primary handler for requests. | |
104 | * @since 0.3.0 | |
105 | */ | |
106 | private IconRegistry iconRegistry; | |
107 | ||
108 | //---- Constructors | |
109 | ||
110 | /** | |
111 | * Creates an icon registry converter that is not active yet. | |
112 | * @since 0.3.0 | |
113 | */ | |
114 | private IconRegistryConverter () { | |
115 | 0 | super(Icon.class); |
116 | 0 | this.delegate = null; |
117 | 0 | this.iconRegistry = null; |
118 | 0 | } |
119 | ||
120 | //---- ResourceConverter | |
121 | ||
122 | /** | |
123 | * Activates this resource converter. | |
124 | * @param resourceConverter the delegate to use. | |
125 | * @param registry the icon registry to use. | |
126 | * @since 0.3.0 | |
127 | */ | |
128 | private void initialize (ResourceConverter resourceConverter, IconRegistry registry) { | |
129 | 0 | this.delegate = resourceConverter; |
130 | 0 | this.iconRegistry = registry; |
131 | 0 | } |
132 | ||
133 | /** | |
134 | * @since 0.3.0 | |
135 | */ | |
136 | @Override | |
137 | public Object parseString ( | |
138 | String key, ResourceMap resourceMap | |
139 | ) | |
140 | throws ResourceConverterException | |
141 | { | |
142 | 0 | final String iconKey = getIconRegistryKey(key); |
143 | 0 | if (iconKey != null) { |
144 | 0 | final Icon icon = this.iconRegistry.getIcon(iconKey); |
145 | 0 | if (icon != null) { |
146 | 0 | return icon; |
147 | } | |
148 | } | |
149 | 0 | return this.delegate.parseString(key, resourceMap); |
150 | } | |
151 | ||
152 | /** | |
153 | * @since 0.3.0 | |
154 | */ | |
155 | @Override | |
156 | @SuppressWarnings("unchecked") | |
157 | public boolean supportsType (Class type) { | |
158 | 0 | if (this.delegate == null) { |
159 | // Only activate if the delegate has been set. | |
160 | 0 | return false; |
161 | } | |
162 | 0 | return type.equals(Icon.class) || type.equals(ImageIcon.class); |
163 | } | |
164 | ||
165 | } |