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 | |
|
16 | |
import javax.swing.DefaultListCellRenderer; |
17 | |
import javax.swing.Icon; |
18 | |
import javax.swing.JComponent; |
19 | |
import javax.swing.JList; |
20 | |
import javax.swing.JScrollPane; |
21 | |
import javax.swing.ListSelectionModel; |
22 | |
import javax.swing.SwingConstants; |
23 | |
|
24 | |
import org.jdesktop.application.Action; |
25 | |
import org.jdesktop.application.ApplicationContext; |
26 | |
import org.jdesktop.beansbinding.BeanProperty; |
27 | |
import org.jdesktop.beansbinding.BindingGroup; |
28 | |
import org.jdesktop.beansbinding.Bindings; |
29 | |
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy; |
30 | |
import org.jdesktop.swingbinding.SwingBindings; |
31 | |
import org.slf4j.Logger; |
32 | |
import org.slf4j.LoggerFactory; |
33 | |
|
34 | |
import org.openpermis.editor.policy.adapter.AdapterTrader; |
35 | |
import org.openpermis.editor.policy.gui.DoubleClickForwarder; |
36 | |
import org.openpermis.editor.policy.presenter.PolicyContext; |
37 | |
import org.openpermis.editor.policy.presenter.TargetAccessRuleListPresenter; |
38 | |
import org.openpermis.policy.Role; |
39 | |
import org.openpermis.policy.bean.ActionBean; |
40 | |
import org.openpermis.policy.bean.ActionBeanCollection; |
41 | |
import org.openpermis.policy.bean.DomainBean; |
42 | |
import org.openpermis.policy.bean.PolicyBean; |
43 | |
import org.openpermis.policy.bean.RoleCollection; |
44 | |
import org.openpermis.policy.bean.TargetAccessRuleBean; |
45 | |
import org.openpermis.policy.bean.TargetBean; |
46 | |
import org.openpermis.policy.bean.TargetBeanCollection; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | 0 | public class TargetAccessRuleListTool |
53 | |
extends AbstractToolView<TargetAccessRuleListPresenter> |
54 | |
implements PropertyChangeListener |
55 | |
{ |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | 0 | private static final Logger LOGGER = |
64 | |
LoggerFactory.getLogger(TargetAccessRuleListTool.class); |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
private JList list; |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
public TargetAccessRuleListTool (ApplicationContext context, AdapterTrader trader) { |
82 | 0 | super(context, trader); |
83 | 0 | } |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
@Action |
92 | |
public void addRule () { |
93 | 0 | if (this.getPresenter() == null) { |
94 | 0 | return; |
95 | |
} |
96 | 0 | LOGGER.debug("addRule"); |
97 | 0 | this.getPresenter().addTargetAccessRule(); |
98 | 0 | } |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
@Action |
106 | |
public void removeRule () { |
107 | 0 | if (this.getPresenter() == null) { |
108 | 0 | return; |
109 | |
} |
110 | 0 | LOGGER.debug("removeRule"); |
111 | 0 | final TargetAccessRuleBean targetAccessRule = this.getPresenter().getActive(); |
112 | 0 | if (targetAccessRule != null) { |
113 | 0 | this.getPresenter().removeTargetAccessRule(targetAccessRule); |
114 | |
} |
115 | 0 | } |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
@Action |
122 | |
public void editRule () { |
123 | 0 | if (this.getPresenter() == null) { |
124 | 0 | return; |
125 | |
} |
126 | 0 | final TargetAccessRuleBean targetAccessRule = this.getPresenter().getActive(); |
127 | 0 | if (targetAccessRule != null) { |
128 | 0 | editPart(targetAccessRule); |
129 | |
} |
130 | 0 | } |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
public void refresh (PolicyBean policy, PolicyContext context) { |
138 | 0 | if (policy == null) { |
139 | 0 | setPresenter(null); |
140 | |
} else { |
141 | 0 | setPresenter(new TargetAccessRuleListPresenter(policy, context)); |
142 | |
} |
143 | 0 | } |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
@Override |
151 | |
protected void updateActions () { |
152 | 0 | super.updateActions(); |
153 | 0 | final boolean hasPresenter = this.getPresenter() != null; |
154 | 0 | final boolean hasActive = hasPresenter && this.getPresenter().getActive() != null; |
155 | 0 | getActionMap().get("addRule").setEnabled(hasPresenter); |
156 | 0 | getActionMap().get("removeRule").setEnabled(hasActive); |
157 | 0 | getActionMap().get("editRule").setEnabled(hasActive); |
158 | 0 | } |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
@Override |
164 | |
protected void detachPresenter (TargetAccessRuleListPresenter presenter) { |
165 | 0 | super.detachPresenter(presenter); |
166 | 0 | presenter.removePropertyChangeListener(this); |
167 | 0 | } |
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
@Override |
173 | |
protected void attachPresenter ( |
174 | |
TargetAccessRuleListPresenter presenter, BindingGroup bindings |
175 | |
) { |
176 | 0 | super.attachPresenter(presenter, bindings); |
177 | 0 | presenter.addPropertyChangeListener(this); |
178 | 0 | bindings.addBinding( |
179 | |
SwingBindings.createJListBinding( |
180 | |
UpdateStrategy.READ, |
181 | |
this.getPresenter().getTargetAccessRules(), |
182 | |
this.list |
183 | |
) |
184 | |
); |
185 | 0 | bindings.addBinding( |
186 | |
Bindings.createAutoBinding( |
187 | |
UpdateStrategy.READ_WRITE, |
188 | |
this.getPresenter(), |
189 | |
BeanProperty.create("active"), |
190 | |
this.list, |
191 | |
BeanProperty.create("selectedElement") |
192 | |
) |
193 | |
); |
194 | 0 | } |
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
@Override |
202 | |
public JComponent createContentPane () { |
203 | 0 | this.list = new JList(new Object[0]); |
204 | 0 | this.list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
205 | 0 | this.list.setCellRenderer(new Renderer()); |
206 | 0 | DoubleClickForwarder.register(this.list, getActionMap().get("editRule")); |
207 | 0 | return new JScrollPane(this.list); |
208 | |
} |
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
public void propertyChange (PropertyChangeEvent event) { |
216 | 0 | updateActions(); |
217 | 0 | } |
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | 0 | private final class Renderer |
226 | |
extends DefaultListCellRenderer |
227 | |
{ |
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
private static final long serialVersionUID = 5561279544864812361L; |
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
private final Icon icon; |
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | 0 | private Renderer () { |
249 | 0 | this.icon = TargetAccessRuleListTool.this.getIcon(); |
250 | 0 | setVerticalTextPosition(SwingConstants.TOP); |
251 | 0 | } |
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
private String getDomainText (DomainBean domain) { |
259 | 0 | if (domain != null && domain.getIdentity() != null) { |
260 | 0 | return domain.getIdentity().toString(); |
261 | |
} |
262 | 0 | return ""; |
263 | |
} |
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
private String getActionsText (ActionBeanCollection actions) { |
269 | 0 | final StringBuilder sb = new StringBuilder(); |
270 | 0 | for (ActionBean action : actions) { |
271 | 0 | if (sb.length() > 0) { |
272 | 0 | sb.append(", "); |
273 | |
} |
274 | 0 | sb.append(action.getName()); |
275 | |
} |
276 | 0 | return sb.toString(); |
277 | |
} |
278 | |
|
279 | |
private String getRoleText (RoleCollection roles) { |
280 | 0 | final StringBuilder sb = new StringBuilder(); |
281 | 0 | for (Role role : roles) { |
282 | 0 | if (sb.length() > 0) { |
283 | 0 | sb.append(", "); |
284 | |
} |
285 | 0 | sb.append(role.getName()); |
286 | |
} |
287 | 0 | return sb.toString(); |
288 | |
} |
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
@Override |
296 | |
public Component getListCellRendererComponent ( |
297 | |
JList source, Object value, int index, boolean selected, boolean focussed |
298 | |
) { |
299 | 0 | final TargetAccessRuleBean rule = (TargetAccessRuleBean) value; |
300 | 0 | final TargetBeanCollection targets = rule.getTargets(); |
301 | |
|
302 | 0 | final StringBuilder accessRuleLines = new StringBuilder(); |
303 | 0 | accessRuleLines.append( |
304 | |
tag( |
305 | |
getResourceMap().getString("targetAccessRule") + |
306 | |
" " + |
307 | |
rule.getSerialNumber().toString(), |
308 | |
"tr", "td colspan= \"2\"", "i", "u", "b" |
309 | |
) |
310 | |
); |
311 | |
|
312 | 0 | accessRuleLines.append( |
313 | |
tag( |
314 | |
getResourceMap().getString("authorizedRoles"), |
315 | |
"tr", "td colspan= \"2\"", "i", "u" |
316 | |
) |
317 | |
); |
318 | 0 | accessRuleLines.append(tag(getRoleText(rule.getRoles()), "tr", "td colspan= \"2\"")); |
319 | |
|
320 | 0 | accessRuleLines.append(tag("", "tr")); |
321 | |
|
322 | 0 | for (TargetBean t : targets) { |
323 | 0 | accessRuleLines.append( |
324 | |
tag(getResourceMap().getString("target") + " " + t.getSerialNumber().toString(), |
325 | |
"tr", "td colspan= \"2\"", "i", "u") |
326 | |
); |
327 | |
|
328 | 0 | final StringBuilder domainLines = new StringBuilder(); |
329 | 0 | domainLines.append(tag(getResourceMap().getString("domain") + |
330 | |
(t.getResourceDomain() == null |
331 | |
? "" |
332 | |
: " " + t.getResourceDomain().getSerialNumber().toString() + " "), |
333 | |
"td", "i")); |
334 | 0 | domainLines.append(tag(getDomainText(t.getResourceDomain()), "td")); |
335 | 0 | accessRuleLines.append(tag(domainLines.toString(), "tr")); |
336 | |
|
337 | 0 | final StringBuilder actionsLines = new StringBuilder(); |
338 | 0 | actionsLines.append(tag(getResourceMap().getString("actions"), "td", "i")); |
339 | 0 | actionsLines.append(tag(getActionsText(t.getActions()), "td")); |
340 | 0 | accessRuleLines.append(tag(actionsLines.toString(), "tr")); |
341 | |
|
342 | 0 | accessRuleLines.append(tag("", "tr")); |
343 | 0 | } |
344 | |
|
345 | 0 | accessRuleLines.append(tag("", "tr")); |
346 | |
|
347 | 0 | final StringBuilder outerMostLines = new StringBuilder(); |
348 | 0 | outerMostLines.append(tag(accessRuleLines.toString(), "html", "table border=\"0\"")); |
349 | |
|
350 | 0 | setToolTipText(rule.toString()); |
351 | 0 | super.getListCellRendererComponent( |
352 | |
source, outerMostLines.toString(), index, selected, focussed |
353 | |
); |
354 | 0 | setIcon(this.icon); |
355 | 0 | return this; |
356 | |
} |
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
private String tag (final String line, final String... tags) { |
362 | 0 | final StringBuilder sb = new StringBuilder(); |
363 | 0 | for (String s : tags) { |
364 | 0 | sb.append("<" + s + ">"); |
365 | |
} |
366 | 0 | sb.append(line); |
367 | 0 | for (int i = tags.length - 1; i >= 0; i--) { |
368 | 0 | sb.append("</" + tags[i] + ">"); |
369 | |
} |
370 | 0 | return sb.toString(); |
371 | |
} |
372 | |
|
373 | |
} |
374 | |
|
375 | |
} |