Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
MenuBarFactory |
|
| 3.0;3 |
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.ActionMap; | |
13 | import javax.swing.Box; | |
14 | import javax.swing.JMenu; | |
15 | import javax.swing.JMenuBar; | |
16 | import javax.swing.JMenuItem; | |
17 | ||
18 | import org.jdesktop.application.ResourceMap; | |
19 | ||
20 | /** | |
21 | * Factory for menu bars. | |
22 | * @since 0.1.0 | |
23 | */ | |
24 | public class MenuBarFactory | |
25 | extends ComponentFactory | |
26 | { | |
27 | ||
28 | //---- Static | |
29 | ||
30 | /** | |
31 | * Creates a menu bar from a resource map and a resource key. | |
32 | * @param actionMap the action map this factory operates on. | |
33 | * @param resourceMap the resource map this factory operates on. | |
34 | * @param key the name of the menu bar configuration in the resource map. | |
35 | * @return the menu bar requested. | |
36 | * @since 0.1.0 | |
37 | */ | |
38 | public static JMenuBar createMenuBar ( | |
39 | final ActionMap actionMap, | |
40 | final ResourceMap resourceMap, | |
41 | final String key | |
42 | ) { | |
43 | 0 | return new MenuBarFactory(actionMap, resourceMap).createMenuBar(key); |
44 | } | |
45 | ||
46 | //---- Constructors | |
47 | ||
48 | /** | |
49 | * Creates a menu bar factory that operates on the specified action and resource map. | |
50 | * @param actionMap the action map this factory operates on. | |
51 | * @param resourceMap the resource map this factory operates on. | |
52 | * @since 0.1.0 | |
53 | */ | |
54 | public MenuBarFactory (ActionMap actionMap, ResourceMap resourceMap) { | |
55 | 0 | super(actionMap, resourceMap); |
56 | 0 | } |
57 | ||
58 | //---- Methods | |
59 | ||
60 | /** | |
61 | * Creates a menu item for the specified resource key name. | |
62 | * @param key the name of the menu item configuration in the resource map. | |
63 | * @return the menu item requested. | |
64 | * @since 0.1.0 | |
65 | */ | |
66 | protected JMenuItem createMenuItem (String key) { | |
67 | 0 | if (isPlaceholder(key)) { |
68 | // Special menu item to be configured by the view. | |
69 | 0 | final JMenuItem menuItem = new JMenuItem(key); |
70 | 0 | menuItem.setEnabled(false); |
71 | 0 | return menuItem; |
72 | } | |
73 | 0 | final javax.swing.Action action = getAction(key); |
74 | 0 | if (action.getValue(javax.swing.Action.SMALL_ICON) == null) { |
75 | 0 | action.putValue( |
76 | javax.swing.Action.SMALL_ICON, | |
77 | EmptyIcon.getIcon(getResourceMap().getString("small-icon-size")) | |
78 | ); | |
79 | } | |
80 | 0 | final JMenuItem menuItem = new JMenuItem(action); |
81 | 0 | menuItem.setToolTipText(null); |
82 | 0 | return menuItem; |
83 | } | |
84 | ||
85 | /** | |
86 | * Creates a single menu of a menu bar. | |
87 | * @param menuBarName the resource key of the menu bar. | |
88 | * @param name the resource key of the menu entry to create. | |
89 | * @param addIcon indicates if an icon is added to the menu. | |
90 | * @return the menu requested. | |
91 | * @since 0.1.0 | |
92 | */ | |
93 | protected JMenu createMenu (String menuBarName, final String name, boolean addIcon) { | |
94 | 0 | final String fullName = menuBarName + '.' + unmaskComposite(name); |
95 | 0 | final JMenu menu = new JMenu(getResourceMap().getString(fullName)); |
96 | 0 | if (addIcon) { |
97 | 0 | menu.setIcon(EmptyIcon.getIcon(getResourceMap().getString("small-icon-size"))); |
98 | } | |
99 | 0 | for (String entry : getEntries(fullName)) { |
100 | 0 | if (isSeparator(entry)) { |
101 | 0 | menu.addSeparator(); |
102 | 0 | } else if (isComposite(entry)) { |
103 | 0 | menu.add(createMenu(menuBarName, entry, true)); |
104 | } else { | |
105 | 0 | menu.add(createMenuItem(entry)); |
106 | } | |
107 | } | |
108 | 0 | return menu; |
109 | } | |
110 | ||
111 | /** | |
112 | * Creates a menubar with the specified resource key name. | |
113 | * @param key the name of the menu bar configuration in the resource map. | |
114 | * @return the menu bar requested. | |
115 | * @since 0.1.0 | |
116 | */ | |
117 | public JMenuBar createMenuBar (String key) { | |
118 | 0 | final JMenuBar menuBar = new JMenuBar(); |
119 | 0 | for (String entry : getEntries(key)) { |
120 | 0 | if (isSeparator(entry) || isGlue(entry)) { |
121 | 0 | menuBar.add(Box.createHorizontalGlue()); |
122 | } else { | |
123 | 0 | menuBar.add(createMenu(key, entry, false)); |
124 | } | |
125 | } | |
126 | 0 | return menuBar; |
127 | } | |
128 | ||
129 | ||
130 | } |