Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AbstractView |
|
| 1.5454545454545454;1.545 |
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.view; | |
11 | ||
12 | import javax.swing.ActionMap; | |
13 | import javax.swing.Icon; | |
14 | import javax.swing.JComponent; | |
15 | import javax.swing.JLabel; | |
16 | import javax.swing.JToolBar; | |
17 | ||
18 | import org.jdesktop.application.ApplicationContext; | |
19 | import org.jdesktop.application.ResourceMap; | |
20 | ||
21 | import org.openpermis.editor.policy.adapter.AdapterTrader; | |
22 | import org.openpermis.editor.policy.gui.ToolBarFactory; | |
23 | import org.openpermis.editor.policy.gui.binding.ErrorReporter; | |
24 | import org.openpermis.policy.bean.PartBean; | |
25 | ||
26 | /** | |
27 | * Abstract base class for views. | |
28 | * @since 0.1.0 | |
29 | */ | |
30 | public abstract class AbstractView | |
31 | implements View, ErrorReporter | |
32 | { | |
33 | ||
34 | //---- Static | |
35 | ||
36 | /** | |
37 | * Resource key for the title of this view. | |
38 | * @since 0.1.0 | |
39 | */ | |
40 | private static final String VIEW_TITLE = "View.title"; | |
41 | ||
42 | /** | |
43 | * Resource key for the view icon. | |
44 | * @since 0.1.0 | |
45 | */ | |
46 | private static final String VIEW_ICON = "View.icon"; | |
47 | ||
48 | //---- State | |
49 | ||
50 | /** | |
51 | * The action map attached to this view instance. | |
52 | * @see #getActionMap() | |
53 | * @since 0.1.0 | |
54 | */ | |
55 | private final ActionMap actionMap; | |
56 | ||
57 | /** | |
58 | * The resource map attached to this view instance. | |
59 | * @see #getResourceMap() | |
60 | * @since 0.1.0 | |
61 | */ | |
62 | private final ResourceMap resourceMap; | |
63 | ||
64 | /** | |
65 | * The context this view is attached to. | |
66 | * @since 0.1.0 | |
67 | */ | |
68 | private ViewContext context; | |
69 | ||
70 | /** | |
71 | * The adapter trader of this view. | |
72 | * @since 0.3.0 | |
73 | */ | |
74 | private AdapterTrader trader; | |
75 | ||
76 | /** | |
77 | * The content pane of this view, lazily initialized. | |
78 | * @see #getContentPane() | |
79 | * @since 0.1.0 | |
80 | */ | |
81 | private JComponent contentPane; | |
82 | ||
83 | //---- Constructors | |
84 | ||
85 | /** | |
86 | * Creates an abstract view that operates on an action and resource map derived | |
87 | * from the implementation class of this view. | |
88 | * @param context the application context used to lookup the action and resource map. | |
89 | * @since 0.1.0 | |
90 | */ | |
91 | 0 | public AbstractView (ApplicationContext context, AdapterTrader trader) { |
92 | 0 | this.actionMap = context.getActionMap(getClass(), this); |
93 | 0 | this.resourceMap = context.getResourceMap(getClass()); |
94 | 0 | this.trader = trader; |
95 | 0 | } |
96 | ||
97 | //---- Methods | |
98 | ||
99 | /** | |
100 | * Returns the adaptee trader of this view class. | |
101 | * @return the adaptee trader of this view class. | |
102 | * @since 0.3.0 | |
103 | */ | |
104 | public AdapterTrader getAdapteeTrader () { | |
105 | 0 | return this.trader; |
106 | } | |
107 | ||
108 | /** | |
109 | * Returns the context resource map of this view class. | |
110 | * @return the context resource map of this view class. | |
111 | * @since 0.1.0 | |
112 | */ | |
113 | protected ResourceMap getResourceMap () { | |
114 | 0 | return this.resourceMap; |
115 | } | |
116 | ||
117 | /** | |
118 | * Returns the application action map of this view class and instance. | |
119 | * @return the application action map of this view class and instance. | |
120 | * @since 0.1.0 | |
121 | */ | |
122 | protected ActionMap getActionMap () { | |
123 | 0 | return this.actionMap; |
124 | } | |
125 | ||
126 | /** | |
127 | * Creates the content pane of this view. | |
128 | * @return the content pane of this view, must not be {@code null}. | |
129 | * @since 0.1.0 | |
130 | */ | |
131 | protected abstract JComponent createContentPane (); | |
132 | ||
133 | /** | |
134 | * Returns the view context of this view. | |
135 | * @return the view context of this view, may be {@code null}. | |
136 | * @since 0.1.0 | |
137 | */ | |
138 | protected ViewContext getViewContext () { | |
139 | 0 | return this.context; |
140 | } | |
141 | ||
142 | /** | |
143 | * @since 0.1.0 | |
144 | */ | |
145 | protected void openView (View view) { | |
146 | 0 | if (this.context != null) { |
147 | 0 | this.context.openView(view); |
148 | } | |
149 | 0 | } |
150 | ||
151 | /** | |
152 | * @since 0.1.0 | |
153 | */ | |
154 | protected boolean closeView (View view, boolean force) { | |
155 | 0 | if (this.context != null) { |
156 | 0 | return this.context.closeView(view, force); |
157 | } | |
158 | 0 | return false; |
159 | } | |
160 | ||
161 | /** | |
162 | * @since 0.1.0 | |
163 | */ | |
164 | protected Editor editPart (PartBean part) { | |
165 | 0 | if (this.context != null) { |
166 | 0 | return this.context.editPart(part); |
167 | } | |
168 | 0 | return null; |
169 | } | |
170 | ||
171 | /** | |
172 | * @since 0.3.0 | |
173 | */ | |
174 | public boolean canEditPart (PartBean part) { | |
175 | 0 | if (this.context != null) { |
176 | 0 | return this.context.canEditPart(part); |
177 | } | |
178 | 0 | return false; |
179 | } | |
180 | ||
181 | /** | |
182 | * @since 0.1.0 | |
183 | */ | |
184 | protected void updateTitle () { | |
185 | 0 | if (this.context != null) { |
186 | 0 | this.context.updateViewTitle(this); |
187 | } | |
188 | 0 | } |
189 | ||
190 | /** | |
191 | * Factory method to create a label with properties defined in the resource map. | |
192 | * @param name the name of the label as it appears in the resource map. | |
193 | * @return the label requested. | |
194 | * @since 0.1.0 | |
195 | */ | |
196 | protected JLabel label (String name) { | |
197 | 0 | final JLabel label = new JLabel(name); |
198 | 0 | label.setName(name); |
199 | 0 | return label; |
200 | } | |
201 | ||
202 | /** | |
203 | * Factory method to create a tool bar with properties defined in the resource map. | |
204 | * @param name the name of the toolbar as it appears in the resource map. | |
205 | * @return the tool bar requested. | |
206 | * @since 0.1.0 | |
207 | */ | |
208 | protected JToolBar toolBar (String name) { | |
209 | 0 | final ToolBarFactory factory = new ToolBarFactory(getActionMap(), getResourceMap()); |
210 | 0 | return factory.createToolBar(name); |
211 | } | |
212 | ||
213 | //---- View | |
214 | ||
215 | /** | |
216 | * Attaches this view to the specified context. | |
217 | * @note Do not call this method directly, it is called by the context exclusively. | |
218 | * @param theContext the context to attach this view to. | |
219 | * @since 0.1.0 | |
220 | */ | |
221 | public void attach (ViewContext theContext) { | |
222 | 0 | this.context = theContext; |
223 | 0 | } |
224 | ||
225 | /** | |
226 | * Detaches this view from the specified context. | |
227 | * @note Do not call this method directly, it is called by the context exclusively. | |
228 | * @param theContext the context to detach this view from. | |
229 | * @since 0.1.0 | |
230 | */ | |
231 | public void detach (ViewContext theContext) { | |
232 | 0 | this.context = null; |
233 | 0 | } |
234 | ||
235 | /** | |
236 | * Returns the parameters for the title of this view. | |
237 | * <p>The default implementations returns an empty array.</p> | |
238 | * @return the parameters for the title of this view, must not be <code>null</code>. | |
239 | * @since 0.1.0 | |
240 | */ | |
241 | public Object[] getTitleParameters () { | |
242 | 0 | return new Object[0]; |
243 | } | |
244 | ||
245 | /** | |
246 | * Returns the title of this view. | |
247 | * @return the title of this view. | |
248 | * @since 0.1.0 | |
249 | */ | |
250 | public String getTitle () { | |
251 | 0 | return getResourceMap().getString(VIEW_TITLE, getTitleParameters()); |
252 | } | |
253 | ||
254 | /** | |
255 | * Returns the icon of this view. | |
256 | * @return the icon of this view. | |
257 | * @since 0.1.0 | |
258 | */ | |
259 | public Icon getIcon () { | |
260 | 0 | return getResourceMap().getIcon(VIEW_ICON); |
261 | } | |
262 | ||
263 | /** | |
264 | * Hook called after the content pane has been created. | |
265 | * @since 0.1.0 | |
266 | */ | |
267 | protected void contentPaneCreated () { | |
268 | // Nop. | |
269 | 0 | } |
270 | ||
271 | /** | |
272 | * Returns the Swing content pane of this view. | |
273 | * @return the Swing content pane of this view. | |
274 | * @since 0.1.0 | |
275 | */ | |
276 | public final synchronized JComponent getContentPane () { | |
277 | 0 | if (this.contentPane == null) { |
278 | 0 | this.contentPane = createContentPane(); |
279 | 0 | if (this.contentPane == null) { |
280 | 0 | throw new IllegalStateException( |
281 | "Error while creating content pane for [" + getClass().getSimpleName() + "]." | |
282 | ); | |
283 | } | |
284 | 0 | getResourceMap().injectComponents(this.contentPane); |
285 | 0 | contentPaneCreated(); |
286 | } | |
287 | 0 | return this.contentPane; |
288 | } | |
289 | ||
290 | /** | |
291 | * @since 0.1.0 | |
292 | */ | |
293 | public boolean canClose () { | |
294 | 0 | return true; |
295 | } | |
296 | ||
297 | //---- ErrorReporter | |
298 | ||
299 | /** | |
300 | * @since 0.1.0 | |
301 | */ | |
302 | public void showStatusError (String message) { | |
303 | 0 | if (this.context != null) { |
304 | 0 | this.context.showStatusError(message); |
305 | } | |
306 | 0 | } |
307 | ||
308 | } |