Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AbstractMenu |
|
| 1.5;1.5 | ||||
AbstractMenu$1 |
|
| 1.5;1.5 |
1 | package org.jtheque.core.managers.feature; | |
2 | ||
3 | import java.util.Arrays; | |
4 | import java.util.HashMap; | |
5 | import java.util.List; | |
6 | import java.util.Map; | |
7 | ||
8 | import org.jtheque.core.managers.Managers; | |
9 | import org.jtheque.core.managers.feature.IFeatureManager.CoreFeature; | |
10 | import org.jtheque.core.managers.resource.IResourceManager; | |
11 | import org.jtheque.core.managers.resource.ImageType; | |
12 | import org.jtheque.core.managers.view.able.IView; | |
13 | import org.jtheque.core.managers.view.impl.actions.ActionFactory; | |
14 | import org.jtheque.core.utils.CoreUtils; | |
15 | import org.jtheque.utils.collections.CollectionUtils; | |
16 | ||
17 | import javax.swing.Action; | |
18 | ||
19 | /* | |
20 | * This file is part of JTheque. | |
21 | * | |
22 | * JTheque is free software: you can redistribute it and/or modify | |
23 | * it under the terms of the GNU General Public License as published by | |
24 | * the Free Software Foundation, either version 3 of the License. | |
25 | * | |
26 | * JTheque is distributed in the hope that it will be useful, | |
27 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
28 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
29 | * GNU General Public License for more details. | |
30 | * | |
31 | * You should have received a copy of the GNU General Public License | |
32 | * along with JTheque. If not, see <http://www.gnu.org/licenses/>. | |
33 | */ | |
34 | ||
35 | /** | |
36 | * An abstract menu. This class provide several a method to specify features for each of the core features. | |
37 | * This methods are only called once and the result are kept in cache for the next call of the Menu methods. | |
38 | * On the same way, this class provide a method to specify the main features. This method are also cached. | |
39 | * | |
40 | * @author Baptiste Wicht | |
41 | */ | |
42 | 0 | public abstract class AbstractMenu implements Menu{ |
43 | 0 | private final Map<String, List<Feature>> cache = new HashMap<String, List<Feature>>(5); |
44 | ||
45 | //Public methods | |
46 | ||
47 | @Override | |
48 | public final List<Feature> getSubFeatures(CoreFeature feature){ | |
49 | 0 | switch (feature){ |
50 | case FILE: | |
51 | 0 | if(!cache.containsKey(CoreFeature.FILE.name())){ |
52 | 0 | cache.put(CoreFeature.FILE.name(), getFileMenuSubFeatures()); |
53 | } | |
54 | ||
55 | break; | |
56 | case EDIT: | |
57 | 0 | if(!cache.containsKey(CoreFeature.EDIT.name())){ |
58 | 0 | cache.put(CoreFeature.EDIT.name(), getEditMenuSubFeatures()); |
59 | } | |
60 | ||
61 | break; | |
62 | case ADVANCED: | |
63 | 0 | if(!cache.containsKey(CoreFeature.ADVANCED.name())){ |
64 | 0 | cache.put(CoreFeature.ADVANCED.name(), getAdvancedMenuSubFeatures()); |
65 | } | |
66 | ||
67 | break; | |
68 | case HELP: | |
69 | 0 | if(!cache.containsKey(CoreFeature.HELP.name())){ |
70 | 0 | cache.put(CoreFeature.HELP.name(), getHelpMenuSubFeatures()); |
71 | } | |
72 | ||
73 | break; | |
74 | } | |
75 | ||
76 | 0 | return cache.get(feature.name()); |
77 | } | |
78 | ||
79 | @Override | |
80 | public final List<Feature> getMainFeatures(){ | |
81 | 0 | if(!cache.containsKey("MAIN")){ |
82 | 0 | cache.put("MAIN", getMenuMainFeatures()); |
83 | } | |
84 | ||
85 | 0 | return cache.get("MAIN"); |
86 | } | |
87 | ||
88 | //Methods to override | |
89 | ||
90 | /** | |
91 | * Return the main features of this menu. This method will only be called once and the result | |
92 | * will be cached. | |
93 | * | |
94 | * @return A List containing all the main features of this menu. | |
95 | */ | |
96 | protected List<Feature> getMenuMainFeatures(){ | |
97 | 0 | return CollectionUtils.emptyList(); |
98 | } | |
99 | ||
100 | /** | |
101 | * Return all the sub features of the "File" menu. This method will only be called once and the result | |
102 | * will be cached. | |
103 | * | |
104 | * @return A List containing all the sub features of "File" menu. | |
105 | */ | |
106 | protected List<Feature> getFileMenuSubFeatures(){ | |
107 | 0 | return CollectionUtils.emptyList(); |
108 | } | |
109 | ||
110 | /** | |
111 | * Return all the sub features of the "Edit" menu. This method will only be called once and the result | |
112 | * will be cached. | |
113 | * | |
114 | * @return A List containing all the sub features of "Edit" menu. | |
115 | */ | |
116 | protected List<Feature> getEditMenuSubFeatures(){ | |
117 | 0 | return CollectionUtils.emptyList(); |
118 | } | |
119 | ||
120 | /** | |
121 | * Return all the sub features of the "Advanced" menu. This method will only be called once and the result | |
122 | * will be cached. | |
123 | * | |
124 | * @return A List containing all the sub features of "Advanced" menu. | |
125 | */ | |
126 | protected List<Feature> getAdvancedMenuSubFeatures(){ | |
127 | 0 | return CollectionUtils.emptyList(); |
128 | } | |
129 | ||
130 | /** | |
131 | * Return all the sub features of the "Help" menu. This method will only be called once and the result | |
132 | * will be cached. | |
133 | * | |
134 | * @return A List containing all the sub features of "Help" menu. | |
135 | */ | |
136 | protected List<Feature> getHelpMenuSubFeatures(){ | |
137 | 0 | return CollectionUtils.emptyList(); |
138 | } | |
139 | ||
140 | //Utility methods | |
141 | ||
142 | /** | |
143 | * Create a main feature. | |
144 | * | |
145 | * @param position The position of the feature in the menu bar. | |
146 | * @param key The i18n key of the feature. | |
147 | * @param features The sub features. | |
148 | * | |
149 | * @return The created main feature. | |
150 | */ | |
151 | protected static Feature createMainFeature(int position, String key, Feature... features){ | |
152 | 0 | Feature feature = new Feature(Feature.FeatureType.PACK, key, position); |
153 | ||
154 | 0 | for(Feature f : features){ |
155 | 0 | feature.addSubFeature(f); |
156 | } | |
157 | ||
158 | 0 | return feature; |
159 | } | |
160 | ||
161 | /** | |
162 | * Create a separated (it seems with a line separator) feature. | |
163 | * | |
164 | * @param position The position of the feature. | |
165 | * @param key The i18n key of the feature. | |
166 | * @param features The sub features. | |
167 | * | |
168 | * @return The created separated feature. | |
169 | */ | |
170 | protected static Feature createSeparatedSubFeature(int position, String key, Feature... features){ | |
171 | 0 | Feature feature = new Feature(Feature.FeatureType.SEPARATED_ACTIONS, key, position); |
172 | ||
173 | 0 | for(Feature f : features){ |
174 | 0 | feature.addSubFeature(f); |
175 | } | |
176 | ||
177 | 0 | return feature; |
178 | } | |
179 | ||
180 | /** | |
181 | * Create a feature. | |
182 | * | |
183 | * @param position The position of the feature. | |
184 | * @param key The i18n key of the feature. | |
185 | * @param features The sub features. | |
186 | * | |
187 | * @return The created feature. | |
188 | */ | |
189 | protected static Feature createSubFeature(int position, String key, Feature... features){ | |
190 | 0 | Feature feature = new Feature(Feature.FeatureType.ACTIONS, key, position); |
191 | ||
192 | 0 | for(Feature f : features){ |
193 | 0 | feature.addSubFeature(f); |
194 | } | |
195 | ||
196 | 0 | return feature; |
197 | } | |
198 | ||
199 | /** | |
200 | * Create a separated (it seems with a line separator) feature. | |
201 | * | |
202 | * @param position The position of the feature. | |
203 | * @param action The name of the action. This action will searched in Spring context. | |
204 | * @param imagesBaseName The image's base name. | |
205 | * @param image The image name. | |
206 | * | |
207 | * @return The created separated feature. | |
208 | */ | |
209 | protected static Feature createSeparatedSubFeature(int position, String action, String imagesBaseName, String image){ | |
210 | 0 | Feature f = createSeparatedSubFeature(position, action); |
211 | ||
212 | 0 | putIconOnAction(imagesBaseName, image, f); |
213 | ||
214 | 0 | return f; |
215 | } | |
216 | ||
217 | /** | |
218 | * Create a separated (it seems with a line separator) feature. | |
219 | * | |
220 | * @param position The position of the feature. | |
221 | * @param action The action. | |
222 | * @param imagesBaseName The image's base name. | |
223 | * @param image The image name. | |
224 | * | |
225 | * @return The created separated feature. | |
226 | */ | |
227 | protected static Feature createSeparatedSubFeature(int position, Action action, String imagesBaseName, String image){ | |
228 | 0 | Feature f = createSeparatedSubFeature(position, action); |
229 | ||
230 | 0 | putIconOnAction(imagesBaseName, image, f); |
231 | ||
232 | 0 | return f; |
233 | } | |
234 | ||
235 | /** | |
236 | * Create a separated (it seems with a line separator) feature. | |
237 | * | |
238 | * @param position The position of the feature. | |
239 | * @param action The name of the action. This action will searched in Spring context. | |
240 | * | |
241 | * @return The created separated feature. | |
242 | */ | |
243 | protected static Feature createSeparatedSubFeature(int position, String action){ | |
244 | 0 | return createSeparatedSubFeature(position, CoreUtils.<Action>getBean(action)); |
245 | } | |
246 | ||
247 | /** | |
248 | * Create a separated (it seems with a line separator) feature. | |
249 | * | |
250 | * @param position The position of the feature. | |
251 | * @param action The action of the feature. | |
252 | * | |
253 | * @return The created separated feature. | |
254 | */ | |
255 | protected static Feature createSeparatedSubFeature(int position, Action action){ | |
256 | 0 | return new Feature(Feature.FeatureType.SEPARATED_ACTION, position, action); |
257 | } | |
258 | ||
259 | /** | |
260 | * Create a feature. | |
261 | * | |
262 | * @param position The position of the feature. | |
263 | * @param action The name of the action. This action will searched in Spring context. | |
264 | * @param imagesBaseName The image's base name. | |
265 | * @param image The image name. | |
266 | * | |
267 | * @return The created feature. | |
268 | */ | |
269 | protected static Feature createSubFeature(int position, String action, String imagesBaseName, String image){ | |
270 | 0 | Feature f = createSubFeature(position, action); |
271 | ||
272 | 0 | putIconOnAction(imagesBaseName, image, f); |
273 | ||
274 | 0 | return f; |
275 | } | |
276 | ||
277 | /** | |
278 | * Create a feature. | |
279 | * | |
280 | * @param position The position of the feature. | |
281 | * @param action The action. | |
282 | * @param imagesBaseName The image's base name. | |
283 | * @param image The image name. | |
284 | * | |
285 | * @return The created feature. | |
286 | */ | |
287 | protected static Feature createSubFeature(int position, Action action, String imagesBaseName, String image){ | |
288 | 0 | Feature f = createSubFeature(position, action); |
289 | ||
290 | 0 | putIconOnAction(imagesBaseName, image, f); |
291 | ||
292 | 0 | return f; |
293 | } | |
294 | ||
295 | /** | |
296 | * Create a feature. | |
297 | * | |
298 | * @param position The position of the feature. | |
299 | * @param action The name of the action. This action will searched in Spring context. | |
300 | * | |
301 | * @return The created feature. | |
302 | */ | |
303 | protected static Feature createSubFeature(int position, String action){ | |
304 | 0 | return createSubFeature(position, CoreUtils.<Action>getBean(action)); |
305 | } | |
306 | ||
307 | /** | |
308 | * Create a feature. | |
309 | * | |
310 | * @param position The position of the feature. | |
311 | * @param action The action of the feature. | |
312 | * | |
313 | * @return The created feature. | |
314 | */ | |
315 | protected static Feature createSubFeature(int position, Action action){ | |
316 | 0 | return new Feature(Feature.FeatureType.ACTION, position, action); |
317 | } | |
318 | ||
319 | /** | |
320 | * Transform all the features into a List of features. | |
321 | * | |
322 | * @param features The features to put in the list. | |
323 | * | |
324 | * @return A List of features containing all the features in parameters. | |
325 | */ | |
326 | protected static List<Feature> features(Feature... features){ | |
327 | 0 | return Arrays.asList(features); |
328 | } | |
329 | ||
330 | //Utility action methods | |
331 | ||
332 | /** | |
333 | * Create an action to close the view. | |
334 | * | |
335 | *@param key The i18n key. | |
336 | * @param view The view to close. | |
337 | * | |
338 | * @return An action to close the view. | |
339 | */ | |
340 | public static Action createCloseViewAction(String key, IView view){ | |
341 | 0 | return ActionFactory.createCloseViewAction(key, view); |
342 | } | |
343 | ||
344 | /** | |
345 | * Create an action to close the view. | |
346 | * | |
347 | *@param key The i18n key. | |
348 | * @param view The name of the view to close. The action will be searched in Spring context. | |
349 | * | |
350 | * @return An action to close the view. | |
351 | */ | |
352 | public static Action createCloseViewAction(String key, String view){ | |
353 | 0 | return ActionFactory.createCloseViewAction(key, view); |
354 | } | |
355 | ||
356 | /** | |
357 | * Create an action to display the view. | |
358 | * | |
359 | *@param key The i18n key. | |
360 | * @param view The view to close. | |
361 | * | |
362 | * @return An action to close the view. | |
363 | */ | |
364 | public static Action createDisplayViewAction(String key, IView view){ | |
365 | 0 | return ActionFactory.createDisplayViewAction(key, view); |
366 | } | |
367 | ||
368 | /** | |
369 | * Create an action to display the view. | |
370 | * | |
371 | *@param key The i18n key. | |
372 | * @param view The name of the view to close. The action will be searched in Spring context. | |
373 | * | |
374 | * @return An action to close the view. | |
375 | */ | |
376 | public static Action createDisplayViewAction(String key, String view){ | |
377 | 0 | return ActionFactory.createDisplayViewAction(key, view); |
378 | } | |
379 | ||
380 | //Private methods | |
381 | ||
382 | /** | |
383 | * Put an icon in the action of the feature. | |
384 | * | |
385 | * @param imagesBaseName The images base name. | |
386 | * @param image The image name. | |
387 | * @param f The feature. | |
388 | */ | |
389 | private static void putIconOnAction(String imagesBaseName, String image, Feature f){ | |
390 | 0 | f.getAction().putValue(Action.SMALL_ICON, Managers.getManager(IResourceManager.class).getIcon( |
391 | imagesBaseName, | |
392 | image, ImageType.PNG)); | |
393 | 0 | } |
394 | } |