Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ToolBarFactory |
|
| 2.0;2 |
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 | ||
13 | import javax.swing.ActionMap; | |
14 | import javax.swing.Box; | |
15 | import javax.swing.JToolBar; | |
16 | ||
17 | import org.jdesktop.application.ResourceMap; | |
18 | ||
19 | /** | |
20 | * Factory for tool bars. | |
21 | * <p>The factory creates tool bars from a resource description.</p> | |
22 | * @since 0.1.0 | |
23 | */ | |
24 | public final class ToolBarFactory | |
25 | extends ButtonFactory | |
26 | { | |
27 | ||
28 | //---- Static | |
29 | ||
30 | /** | |
31 | * Convenience method to create a tool bar. | |
32 | * <p>Except for performance this convenience method is equivalent to creating a tool bar | |
33 | * factory for the specfied action and resource map and then creating the tool bar using | |
34 | * the newly created factory.</p> | |
35 | * @param actionMap the action map this factory operates on. | |
36 | * @param resourceMap the resource map this factory operates on. | |
37 | * @param key the resource key for the tool bar. | |
38 | * @return the tool bar requested. | |
39 | * @since 0.1.0 | |
40 | */ | |
41 | public static JToolBar createToolBar ( | |
42 | ActionMap actionMap, | |
43 | ResourceMap resourceMap, | |
44 | String key | |
45 | ) { | |
46 | 0 | return new ToolBarFactory(actionMap, resourceMap).createToolBar(key); |
47 | } | |
48 | ||
49 | //---- Constructors | |
50 | ||
51 | /** | |
52 | * Creates a tool bar factory that operates on the specified action and resource map. | |
53 | * @param actionMap the action map this factory operates on. | |
54 | * @param resourceMap the resource map this factory operates on. | |
55 | * @since 0.1.0 | |
56 | */ | |
57 | public ToolBarFactory (ActionMap actionMap, ResourceMap resourceMap) { | |
58 | 0 | super(actionMap, resourceMap); |
59 | 0 | } |
60 | ||
61 | //---- Methods | |
62 | ||
63 | /** | |
64 | * Recursively adds all entries to the tool bar specified. | |
65 | * @param toolBar the tool bar to add entries to. | |
66 | * @param toolBarName the resource key of the tool bar. | |
67 | * @param entries the entries to add. | |
68 | * @since 0.1.0 | |
69 | */ | |
70 | private void addToolBarEntries (JToolBar toolBar, String toolBarName, String[] entries) { | |
71 | 0 | addToolBarEntries(toolBar, toolBarName, entries, false); |
72 | 0 | } |
73 | ||
74 | /** | |
75 | * Primitive to add tool bar entries. | |
76 | * <p>Struts are added between non-separator tool bar entries.</p> | |
77 | * @param toolBar the tool bar to add entries to. | |
78 | * @param toolBarName the resource key of the tool bar. | |
79 | * @param entries the entries to add. | |
80 | * @param addStrut {@code true} if a strut should be added before adding an entry. | |
81 | * @return {@code true} if a strut should be added before adding the next entry. | |
82 | * @since 0.1.0 | |
83 | */ | |
84 | private boolean addToolBarEntries ( | |
85 | JToolBar toolBar, String toolBarName, String[] entries, boolean addStrut | |
86 | ) { | |
87 | 0 | for (String entry : entries) { |
88 | 0 | if (isSeparator(entry)) { |
89 | 0 | toolBar.addSeparator(); |
90 | 0 | addStrut = false; |
91 | 0 | } else if (isGlue(entry)) { |
92 | 0 | toolBar.add(Box.createHorizontalGlue()); |
93 | 0 | addStrut = false; |
94 | 0 | } else if (isComposite(entry)) { |
95 | 0 | final String key = toolBarName + '.' + unmaskComposite(entry); |
96 | 0 | addStrut = addToolBarEntries(toolBar, toolBarName, getEntries(key), addStrut); |
97 | 0 | } else { |
98 | 0 | if (addStrut) { |
99 | 0 | toolBar.add(Box.createHorizontalStrut(2)); |
100 | } | |
101 | 0 | toolBar.add(createToolButton(entry)); |
102 | 0 | addStrut = true; |
103 | } | |
104 | } | |
105 | 0 | return addStrut; |
106 | } | |
107 | ||
108 | /** | |
109 | * Creates a tool bar from the specfied resource key. | |
110 | * @param key the resource key from which to create tool bar. | |
111 | * @return the tool bar created. | |
112 | * @since 0.1.0 | |
113 | */ | |
114 | public JToolBar createToolBar (String key) { | |
115 | 0 | final JToolBar toolBar = new JToolBar(); |
116 | 0 | toolBar.setFloatable(false); |
117 | 0 | toolBar.setRollover(true); |
118 | 0 | addToolBarEntries(toolBar, key, getEntries(key)); |
119 | 0 | return toolBar; |
120 | } | |
121 | ||
122 | } |