1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.openpermis.editor.policy.view; |
11 | |
|
12 | |
import java.beans.PropertyChangeEvent; |
13 | |
import java.beans.PropertyChangeListener; |
14 | |
|
15 | |
import javax.swing.JComponent; |
16 | |
import javax.swing.JList; |
17 | |
import javax.swing.JScrollPane; |
18 | |
import javax.swing.ListSelectionModel; |
19 | |
|
20 | |
import org.jdesktop.application.Action; |
21 | |
import org.jdesktop.application.ApplicationContext; |
22 | |
import org.jdesktop.beansbinding.BeanProperty; |
23 | |
import org.jdesktop.beansbinding.BindingGroup; |
24 | |
import org.jdesktop.beansbinding.Bindings; |
25 | |
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy; |
26 | |
import org.jdesktop.swingbinding.SwingBindings; |
27 | |
import org.slf4j.Logger; |
28 | |
import org.slf4j.LoggerFactory; |
29 | |
|
30 | |
import org.openpermis.editor.policy.adapter.AdapterTrader; |
31 | |
import org.openpermis.editor.policy.adapter.overview.Overview; |
32 | |
import org.openpermis.editor.policy.gui.DoubleClickForwarder; |
33 | |
import org.openpermis.editor.policy.presenter.PolicyContext; |
34 | |
import org.openpermis.editor.policy.presenter.TargetAccessRuleListPresenter; |
35 | |
import org.openpermis.editor.policy.renderer.RendererFactory; |
36 | |
|
37 | |
import org.openpermis.policy.bean.PolicyBean; |
38 | |
import org.openpermis.policy.bean.TargetAccessRuleBean; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 0 | public class TargetAccessRuleListTool |
46 | |
extends AbstractToolView<TargetAccessRuleListPresenter> |
47 | |
implements PropertyChangeListener |
48 | |
{ |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | 0 | private static final Logger LOGGER = |
57 | |
LoggerFactory.getLogger(TargetAccessRuleListTool.class); |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
private JList list; |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public TargetAccessRuleListTool (ApplicationContext context, AdapterTrader trader) { |
75 | 0 | super(context, trader); |
76 | 0 | } |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
@Action |
85 | |
public void addRule () { |
86 | 0 | if (this.getPresenter() == null) { |
87 | 0 | return; |
88 | |
} |
89 | 0 | LOGGER.debug("addRule"); |
90 | 0 | this.getPresenter().addTargetAccessRule(); |
91 | 0 | } |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
@Action |
99 | |
public void removeRule () { |
100 | 0 | if (this.getPresenter() == null) { |
101 | 0 | return; |
102 | |
} |
103 | 0 | LOGGER.debug("removeRule"); |
104 | 0 | final TargetAccessRuleBean targetAccessRule = this.getPresenter().getActive(); |
105 | 0 | if (targetAccessRule != null) { |
106 | 0 | this.getPresenter().removeTargetAccessRule(targetAccessRule); |
107 | |
} |
108 | 0 | } |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
@Action |
115 | |
public void editRule () { |
116 | 0 | if (this.getPresenter() == null) { |
117 | 0 | return; |
118 | |
} |
119 | 0 | final TargetAccessRuleBean targetAccessRule = this.getPresenter().getActive(); |
120 | 0 | if (targetAccessRule != null) { |
121 | 0 | editPart(targetAccessRule); |
122 | |
} |
123 | 0 | } |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
public void refresh (PolicyBean policy, PolicyContext context) { |
131 | 0 | if (policy == null) { |
132 | 0 | setPresenter(null); |
133 | |
} else { |
134 | 0 | setPresenter(new TargetAccessRuleListPresenter(policy, context)); |
135 | |
} |
136 | 0 | } |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
@Override |
144 | |
protected void updateActions () { |
145 | 0 | super.updateActions(); |
146 | 0 | final boolean hasPresenter = this.getPresenter() != null; |
147 | 0 | final boolean hasActive = hasPresenter && this.getPresenter().getActive() != null; |
148 | 0 | getActionMap().get("addRule").setEnabled(hasPresenter); |
149 | 0 | getActionMap().get("removeRule").setEnabled(hasActive); |
150 | 0 | getActionMap().get("editRule").setEnabled(hasActive); |
151 | 0 | } |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
@Override |
157 | |
protected void detachPresenter (TargetAccessRuleListPresenter presenter) { |
158 | 0 | super.detachPresenter(presenter); |
159 | 0 | presenter.removePropertyChangeListener(this); |
160 | 0 | } |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
@Override |
166 | |
protected void attachPresenter ( |
167 | |
TargetAccessRuleListPresenter presenter, BindingGroup bindings |
168 | |
) { |
169 | 0 | super.attachPresenter(presenter, bindings); |
170 | 0 | presenter.addPropertyChangeListener(this); |
171 | 0 | bindings.addBinding( |
172 | |
SwingBindings.createJListBinding( |
173 | |
UpdateStrategy.READ, |
174 | |
this.getPresenter().getTargetAccessRules(), |
175 | |
this.list |
176 | |
) |
177 | |
); |
178 | 0 | bindings.addBinding( |
179 | |
Bindings.createAutoBinding( |
180 | |
UpdateStrategy.READ_WRITE, |
181 | |
this.getPresenter(), |
182 | |
BeanProperty.create("active"), |
183 | |
this.list, |
184 | |
BeanProperty.create("selectedElement") |
185 | |
) |
186 | |
); |
187 | 0 | } |
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
@Override |
195 | |
public JComponent createContentPane () { |
196 | 0 | this.list = new JList(new Object[0]); |
197 | 0 | this.list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
198 | 0 | this.list.setCellRenderer( |
199 | |
RendererFactory.createListCellRenderer(this.getAdapteeTrader(), Overview.class) |
200 | |
); |
201 | 0 | DoubleClickForwarder.register(this.list, getActionMap().get("editRule")); |
202 | 0 | return new JScrollPane(this.list); |
203 | |
} |
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
public void propertyChange (PropertyChangeEvent event) { |
211 | 0 | updateActions(); |
212 | 0 | } |
213 | |
|
214 | |
} |