Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AbstractToolView |
|
| 1.3636363636363635;1.364 |
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.BorderFactory; | |
13 | import javax.swing.JToolBar; | |
14 | import javax.swing.border.Border; | |
15 | ||
16 | import org.jdesktop.application.ApplicationContext; | |
17 | import org.jdesktop.beansbinding.BindingGroup; | |
18 | ||
19 | import bibliothek.gui.dock.common.CLocation; | |
20 | import bibliothek.gui.dock.common.location.TreeLocationRoot; | |
21 | ||
22 | import org.openpermis.editor.policy.adapter.AdapterTrader; | |
23 | import org.openpermis.editor.policy.gui.ToolBarFactory; | |
24 | import org.openpermis.editor.policy.presenter.Presenter; | |
25 | ||
26 | /** | |
27 | * Abstract base class for tool views. | |
28 | * @param <P> the presenter model type. | |
29 | * @since 0.1.0 | |
30 | */ | |
31 | public abstract class AbstractToolView<P extends Presenter<?>> | |
32 | extends AbstractView | |
33 | implements Tool | |
34 | { | |
35 | ||
36 | //---- Static | |
37 | ||
38 | /** | |
39 | * Resource key for the view toolbar. | |
40 | * @since 0.1.0 | |
41 | */ | |
42 | private static final String VIEW_TOOLBAR = "View.toolbar"; | |
43 | ||
44 | /** | |
45 | * The border used for toolbars. | |
46 | * @since 0.1.0 | |
47 | */ | |
48 | 0 | private static final Border TOOLBAR_BORDER = BorderFactory.createEmptyBorder(2, 2, 2, 2); |
49 | ||
50 | /** | |
51 | * The default location for tool views. | |
52 | * @since 0.1.0 | |
53 | */ | |
54 | 0 | protected static final TreeLocationRoot DEFAULT_LOCATION = CLocation.base().normalWest(0.2); |
55 | ||
56 | //---- State | |
57 | ||
58 | /** | |
59 | * The tool bar of this policy view, lazily intialized. | |
60 | * @see #getToolBar() | |
61 | * @since 0.1.0 | |
62 | */ | |
63 | private JToolBar toolBar; | |
64 | ||
65 | /** | |
66 | * The presenter that this view renders. | |
67 | * @since 0.1.0 | |
68 | */ | |
69 | private P currentPresenter; | |
70 | ||
71 | /** | |
72 | * The binding group used to wire view components to the presenter. | |
73 | * @since 0.1.0 | |
74 | */ | |
75 | private BindingGroup bindingGroup; | |
76 | ||
77 | //---- Constructors | |
78 | ||
79 | /** | |
80 | * Creates an abstract tool view for the specified application context. | |
81 | * @param context the application context used to lookup the action and resource map. | |
82 | * @since 0.1.0 | |
83 | */ | |
84 | public AbstractToolView (ApplicationContext context, AdapterTrader trader) { | |
85 | 0 | super(context, trader); |
86 | 0 | } |
87 | ||
88 | //---- Methods | |
89 | ||
90 | /** | |
91 | * Prompts the tool to update its actions according to the presenter state. | |
92 | * <p>The default implementation does nothing.</p> | |
93 | * @note Super call mandatory if overwritten. | |
94 | * @since 0.1.0 | |
95 | */ | |
96 | protected void updateActions () { | |
97 | // Nop. | |
98 | 0 | } |
99 | ||
100 | /** | |
101 | * Unlinks any listeners from the specified old presenter. | |
102 | * <p>This is called before a new presenter is set.</p> | |
103 | * <p>The default implementation removes any bean bindings.</p> | |
104 | * @note Super call mandatory if overwritten. | |
105 | * @param presenter the presenter to unlink. | |
106 | * @see #setPresenter(Presenter) | |
107 | * @since 0.1.0 | |
108 | */ | |
109 | protected void detachPresenter (P presenter) { | |
110 | 0 | if (this.bindingGroup != null) { |
111 | 0 | this.bindingGroup.unbind(); |
112 | 0 | this.bindingGroup = null; |
113 | } | |
114 | 0 | } |
115 | ||
116 | /** | |
117 | * Attaches any listeners required to the new presenter and binds components. | |
118 | * <p>The default implementation initializes the content pane if necessary.</p> | |
119 | * @note Super call mandatory if overwritten. | |
120 | * @param presenter the new presenter to attach to. | |
121 | * @param bindings the binding group to add bindings to. | |
122 | * @since 0.1.0 | |
123 | */ | |
124 | protected void attachPresenter (P presenter, BindingGroup bindings) { | |
125 | // Guarantee that the content pane has been created. | |
126 | 0 | getContentPane(); |
127 | 0 | } |
128 | ||
129 | /** | |
130 | * Sets the presenter that this view renders. | |
131 | * <p>The view needs to take appropriate actions to rebind and render the presenter | |
132 | * passed in, bindings to an old presenter are to be discarded in the process.</p> | |
133 | * @param presenter the presenter to set. | |
134 | * @since 0.1.0 | |
135 | */ | |
136 | protected void setPresenter (P presenter) { | |
137 | 0 | if (this.currentPresenter != null) { |
138 | 0 | detachPresenter(this.currentPresenter); |
139 | } | |
140 | 0 | this.currentPresenter = presenter; |
141 | 0 | if (this.currentPresenter != null) { |
142 | 0 | this.bindingGroup = new BindingGroup(); |
143 | 0 | attachPresenter(this.currentPresenter, this.bindingGroup); |
144 | 0 | this.bindingGroup.bind(); |
145 | } | |
146 | 0 | updateActions(); |
147 | 0 | } |
148 | ||
149 | /** | |
150 | * Returns the presenter rendered by this view. | |
151 | * @return the presenter rendered by this view. | |
152 | * @since 0.1.0 | |
153 | */ | |
154 | protected P getPresenter () { | |
155 | 0 | return this.currentPresenter; |
156 | } | |
157 | ||
158 | /** | |
159 | * Factory method to lazily create the view tool bar. | |
160 | * <p>The default implementation creates a tool bar based on the resource key | |
161 | * {@code View.toolbar}.</p> | |
162 | * @return the tool bar or <code>null</code> if this view does not feature a tool bar. | |
163 | * @since 0.1.0 | |
164 | */ | |
165 | protected JToolBar createToolBar () { | |
166 | 0 | final JToolBar newToolBar = ToolBarFactory.createToolBar( |
167 | getActionMap(), getResourceMap(), VIEW_TOOLBAR | |
168 | ); | |
169 | 0 | newToolBar.setFloatable(false); |
170 | 0 | newToolBar.setBorder(TOOLBAR_BORDER); |
171 | 0 | return newToolBar; |
172 | } | |
173 | ||
174 | //---- Tool | |
175 | ||
176 | /** | |
177 | * @since 0.1.0 | |
178 | */ | |
179 | public final String getToolIdentifier () { | |
180 | 0 | return getClass().getName(); |
181 | } | |
182 | ||
183 | /** | |
184 | * @since 0.1.0 | |
185 | */ | |
186 | public CLocation getDefaultLocation () { | |
187 | 0 | return DEFAULT_LOCATION; |
188 | } | |
189 | ||
190 | /** | |
191 | * @since 0.1.0 | |
192 | */ | |
193 | public synchronized JToolBar getToolBar () { | |
194 | 0 | if (this.toolBar == null) { |
195 | 0 | this.toolBar = createToolBar(); |
196 | } | |
197 | 0 | return this.toolBar; |
198 | } | |
199 | ||
200 | //---- AbstractView | |
201 | ||
202 | /** | |
203 | * @since 0.1.0 | |
204 | */ | |
205 | @Override | |
206 | protected void contentPaneCreated () { | |
207 | 0 | updateActions(); |
208 | 0 | } |
209 | ||
210 | } |