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