Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PolicyIconRegistry |
|
| 2.5;2.5 |
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 | ||
14 | import org.jdesktop.application.ApplicationContext; | |
15 | import org.jdesktop.application.ResourceMap; | |
16 | ||
17 | import org.openpermis.policy.bean.PartBean; | |
18 | ||
19 | /** | |
20 | * Helper class to create the policy icon registry. | |
21 | * <p>This class is mainly used since the swing application framework only permits creation | |
22 | * of resource maps based on classes.</p> | |
23 | * @since 0.3.0 | |
24 | */ | |
25 | public final class PolicyIconRegistry { | |
26 | ||
27 | //---- Static | |
28 | ||
29 | /** | |
30 | * Prefix for annotation icons. | |
31 | * @since 0.3.0 | |
32 | */ | |
33 | private static final String ANNOTATION_PREFIX = "annotation."; | |
34 | ||
35 | /** | |
36 | * Prefix for part type icons. | |
37 | * @since 0.3.0 | |
38 | */ | |
39 | private static final String PART_TYPE_PREFIX = "part."; | |
40 | ||
41 | /** | |
42 | * Unfortunately we are forced to use a singleton here. | |
43 | * <p>See {@link IconRegistryConverter} for the gory details.</p> | |
44 | * @since 0.3.0 | |
45 | */ | |
46 | private static IconRegistry singleton; | |
47 | ||
48 | /** | |
49 | * Creates a policy icon registry. | |
50 | * @param context the application context to create the registry for. | |
51 | * @return the icon registry requested. | |
52 | * @since 0.3.0 | |
53 | */ | |
54 | public static final PolicyIconRegistry create (ApplicationContext context) { | |
55 | 0 | if (singleton == null) { |
56 | 0 | final ResourceMap resourceMap = context.getResourceMap(PolicyIconRegistry.class); |
57 | 0 | singleton = new IconRegistry( |
58 | new IconRegistry(null, resourceMap, ANNOTATION_PREFIX), | |
59 | resourceMap, | |
60 | PART_TYPE_PREFIX | |
61 | ); | |
62 | 0 | IconRegistryConverter.activate(singleton); |
63 | } | |
64 | 0 | return new PolicyIconRegistry(singleton); |
65 | } | |
66 | ||
67 | //---- Static | |
68 | ||
69 | /** | |
70 | * The actual icon registry used internally. | |
71 | * @since 0.3.0 | |
72 | */ | |
73 | private final IconRegistry iconRegistry; | |
74 | ||
75 | //---- Constructors | |
76 | ||
77 | /** | |
78 | * Creates a new policy icon registry for the specified icon registry. | |
79 | * @see #create(ApplicationContext) | |
80 | * @since 0.3.0 | |
81 | */ | |
82 | 0 | private PolicyIconRegistry (IconRegistry iconRegistry) { |
83 | 0 | this.iconRegistry = iconRegistry; |
84 | 0 | } |
85 | ||
86 | //---- Methods | |
87 | ||
88 | /** | |
89 | * Returns an icon for the specified part bean. | |
90 | * @param partBean the part bean for which to retrieve an icon. | |
91 | * @param annotations an optional list of annotations for the icon. | |
92 | * @return the icon requested or <code>null</code> if there is no icon available. | |
93 | * @since 0.3.0 | |
94 | */ | |
95 | public Icon getIcon (PartBean partBean, IconAnnotation... annotations) { | |
96 | 0 | if (partBean == null) { |
97 | 0 | return null; |
98 | } | |
99 | 0 | return getIcon(partBean.getPartBeanType(), annotations); |
100 | } | |
101 | ||
102 | /** | |
103 | * Returns an icon for the specified part type. | |
104 | * @param partBeanType the part bean type for which to return an icon. | |
105 | * @param annotations an optional list of annotations for the icon. | |
106 | * @return the icon requested or <code>null</code> if there is no such icon. | |
107 | * @since 0.3.0 | |
108 | */ | |
109 | public Icon getIcon (Class<? extends PartBean> partBeanType, IconAnnotation... annotations) { | |
110 | 0 | if (partBeanType == null) { |
111 | 0 | return null; |
112 | } | |
113 | 0 | String iconKey = partBeanType.getSimpleName(); |
114 | 0 | for (IconAnnotation annotation : annotations) { |
115 | 0 | iconKey = annotation.annotate(iconKey); |
116 | } | |
117 | 0 | return this.iconRegistry.getIcon(iconKey); |
118 | } | |
119 | ||
120 | } |