1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.openpermis.editor.policy.view; |
11 | |
|
12 | |
import java.awt.Component; |
13 | |
import java.beans.PropertyChangeEvent; |
14 | |
import java.beans.PropertyChangeListener; |
15 | |
import java.net.URI; |
16 | |
|
17 | |
import javax.swing.DefaultListCellRenderer; |
18 | |
import javax.swing.Icon; |
19 | |
import javax.swing.JComponent; |
20 | |
import javax.swing.JList; |
21 | |
import javax.swing.JOptionPane; |
22 | |
import javax.swing.JScrollPane; |
23 | |
import javax.swing.SwingConstants; |
24 | |
|
25 | |
import org.jdesktop.application.Action; |
26 | |
import org.jdesktop.application.ApplicationContext; |
27 | |
import org.jdesktop.beansbinding.BeanProperty; |
28 | |
import org.jdesktop.beansbinding.BindingGroup; |
29 | |
import org.jdesktop.beansbinding.Bindings; |
30 | |
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy; |
31 | |
import org.jdesktop.swingbinding.SwingBindings; |
32 | |
import org.slf4j.Logger; |
33 | |
import org.slf4j.LoggerFactory; |
34 | |
|
35 | |
import bibliothek.gui.dock.common.CLocation; |
36 | |
|
37 | |
import org.openpermis.editor.policy.adapter.AdapterTrader; |
38 | |
import org.openpermis.editor.policy.gui.DoubleClickForwarder; |
39 | |
import org.openpermis.editor.policy.presenter.PolicyContext; |
40 | |
import org.openpermis.editor.policy.presenter.RoleHierarchyListPresenter; |
41 | |
import org.openpermis.policy.bean.PolicyBean; |
42 | |
import org.openpermis.policy.bean.RoleHierarchyBean; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 0 | public class RoleHierarchyListTool |
49 | |
extends AbstractToolView<RoleHierarchyListPresenter> |
50 | |
implements PropertyChangeListener |
51 | |
{ |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | 0 | private static final Logger LOGGER = |
60 | |
LoggerFactory.getLogger(RoleHierarchyListTool.class); |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | 0 | private static final CLocation DEFAULT_ROLE_HIERARCHY_LIST_LOCATION = |
67 | |
DEFAULT_LOCATION.south(0.6); |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
private JList list; |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
public RoleHierarchyListTool (ApplicationContext context, AdapterTrader trader) { |
85 | 0 | super(context, trader); |
86 | 0 | } |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
@Action |
95 | |
public void addHierarchy () { |
96 | 0 | if (this.getPresenter() == null) { |
97 | 0 | return; |
98 | |
} |
99 | 0 | LOGGER.debug("addHierarchy"); |
100 | |
|
101 | 0 | String identifier = (String) JOptionPane.showInputDialog( |
102 | |
null, |
103 | |
getResourceMap().getString("addHierarchy.Dialog.text"), |
104 | |
getResourceMap().getString("addHierarchy.Dialog.title"), |
105 | |
JOptionPane.PLAIN_MESSAGE, |
106 | |
null, |
107 | |
null, |
108 | |
"" |
109 | |
); |
110 | |
|
111 | |
|
112 | |
URI uri; |
113 | |
try { |
114 | 0 | uri = URI.create(identifier); |
115 | 0 | } catch (Exception e) { |
116 | 0 | uri = null; |
117 | 0 | } |
118 | 0 | if (uri != null) { |
119 | 0 | this.getPresenter().addRoleHierarchy(uri); |
120 | |
} |
121 | 0 | } |
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
@Action |
128 | |
public void removeHierarchy () { |
129 | 0 | if (this.getPresenter() == null) { |
130 | 0 | return; |
131 | |
} |
132 | 0 | LOGGER.debug("removeHierarchy"); |
133 | 0 | final RoleHierarchyBean roleHierarchy = this.getPresenter().getActive(); |
134 | 0 | if (roleHierarchy != null) { |
135 | 0 | this.getPresenter().removeRoleHierarchy(roleHierarchy); |
136 | |
} |
137 | 0 | } |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
@Action |
144 | |
public void editHierarchy () { |
145 | 0 | if (this.getPresenter() == null) { |
146 | 0 | return; |
147 | |
} |
148 | 0 | final RoleHierarchyBean roleHierarchy = this.getPresenter().getActive(); |
149 | 0 | if (roleHierarchy != null) { |
150 | 0 | editPart(roleHierarchy); |
151 | |
} |
152 | 0 | } |
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
public void refresh (PolicyBean policy, PolicyContext context) { |
160 | 0 | if (policy == null) { |
161 | 0 | setPresenter(null); |
162 | |
} else { |
163 | 0 | setPresenter(new RoleHierarchyListPresenter(policy, context)); |
164 | |
} |
165 | 0 | } |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
@Override |
173 | |
protected void updateActions () { |
174 | 0 | super.updateActions(); |
175 | 0 | final boolean hasPresenter = this.getPresenter() != null; |
176 | 0 | final boolean hasActive = hasPresenter && this.getPresenter().getActive() != null; |
177 | 0 | getActionMap().get("addHierarchy").setEnabled(hasPresenter); |
178 | 0 | getActionMap().get("removeHierarchy").setEnabled(hasActive); |
179 | 0 | getActionMap().get("editHierarchy").setEnabled(hasActive); |
180 | 0 | } |
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
@Override |
186 | |
protected void detachPresenter (RoleHierarchyListPresenter presenter) { |
187 | 0 | super.detachPresenter(presenter); |
188 | 0 | presenter.removePropertyChangeListener(this); |
189 | 0 | } |
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
@Override |
195 | |
protected void attachPresenter ( |
196 | |
RoleHierarchyListPresenter presenter, BindingGroup bindings |
197 | |
) { |
198 | 0 | super.attachPresenter(presenter, bindings); |
199 | 0 | presenter.addPropertyChangeListener(this); |
200 | 0 | bindings.addBinding( |
201 | |
SwingBindings.createJListBinding( |
202 | |
UpdateStrategy.READ, |
203 | |
this.getPresenter().getRoleHierarchies(), |
204 | |
this.list |
205 | |
) |
206 | |
); |
207 | 0 | bindings.addBinding( |
208 | |
Bindings.createAutoBinding( |
209 | |
UpdateStrategy.READ_WRITE, |
210 | |
this.getPresenter(), |
211 | |
BeanProperty.create("active"), |
212 | |
this.list, |
213 | |
BeanProperty.create("selectedElement") |
214 | |
) |
215 | |
); |
216 | 0 | } |
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
@Override |
222 | |
public CLocation getDefaultLocation () { |
223 | 0 | return DEFAULT_ROLE_HIERARCHY_LIST_LOCATION; |
224 | |
} |
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
@Override |
232 | |
public JComponent createContentPane () { |
233 | 0 | this.list = new JList(new Object[0]); |
234 | 0 | this.list.setCellRenderer(new Renderer()); |
235 | 0 | DoubleClickForwarder.register(this.list, getActionMap().get("editHierarchy")); |
236 | 0 | return new JScrollPane(this.list); |
237 | |
} |
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
public void propertyChange (PropertyChangeEvent event) { |
245 | 0 | updateActions(); |
246 | 0 | } |
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
@SuppressWarnings("serial") |
255 | 0 | private final class Renderer |
256 | |
extends DefaultListCellRenderer |
257 | |
{ |
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
private final Icon icon; |
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | 0 | private Renderer () { |
272 | 0 | this.icon = RoleHierarchyListTool.this.getIcon(); |
273 | 0 | setVerticalTextPosition(SwingConstants.TOP); |
274 | 0 | } |
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
@Override |
282 | |
public Component getListCellRendererComponent ( |
283 | |
JList source, Object value, int index, boolean selected, boolean focussed |
284 | |
) { |
285 | 0 | final RoleHierarchyBean role = (RoleHierarchyBean) value; |
286 | |
|
287 | 0 | final StringBuilder accessHierarchyLines = new StringBuilder(); |
288 | 0 | accessHierarchyLines.append( |
289 | |
tag( |
290 | |
getResourceMap().getString("roleHierarchy") + " \"" + role.getIdentity() + "\"", |
291 | |
"tr", |
292 | |
"td colspan= \"2\"", "i", "u", "b" |
293 | |
) |
294 | |
); |
295 | |
|
296 | 0 | final StringBuilder outerMostLines = new StringBuilder(); |
297 | 0 | outerMostLines.append( |
298 | |
tag(accessHierarchyLines.toString(), "html", "table border=\"0\"") |
299 | |
); |
300 | |
|
301 | 0 | setToolTipText(role.toString()); |
302 | 0 | super.getListCellRendererComponent( |
303 | |
source, outerMostLines.toString(), index, selected, focussed |
304 | |
); |
305 | 0 | setIcon(this.icon); |
306 | 0 | return this; |
307 | |
} |
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
private String tag (final String line, final String... tags) { |
313 | 0 | final StringBuilder sb = new StringBuilder(); |
314 | 0 | for (String s : tags) { |
315 | 0 | sb.append("<" + s + ">"); |
316 | |
} |
317 | 0 | sb.append(line); |
318 | 0 | for (int i = tags.length - 1; i >= 0; i--) { |
319 | 0 | sb.append("</" + tags[i] + ">"); |
320 | |
} |
321 | 0 | return sb.toString(); |
322 | |
} |
323 | |
|
324 | |
} |
325 | |
|
326 | |
} |