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.JComponent; |
18 | |
import javax.swing.JList; |
19 | |
import javax.swing.JScrollPane; |
20 | |
import javax.swing.ListSelectionModel; |
21 | |
import javax.swing.SwingConstants; |
22 | |
import javax.swing.event.ListSelectionEvent; |
23 | |
import javax.swing.event.ListSelectionListener; |
24 | |
|
25 | |
import org.jdesktop.application.Action; |
26 | |
import org.jdesktop.application.ApplicationContext; |
27 | |
import org.jdesktop.beansbinding.BindingGroup; |
28 | |
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy; |
29 | |
import org.jdesktop.swingbinding.SwingBindings; |
30 | |
import org.jdesktop.swingx.JXList; |
31 | |
import org.jdesktop.swingx.decorator.HighlighterFactory; |
32 | |
|
33 | |
import bibliothek.gui.dock.common.CLocation; |
34 | |
|
35 | |
import org.openpermis.editor.policy.adapter.AdapterTrader; |
36 | |
import org.openpermis.editor.policy.gui.DoubleClickForwarder; |
37 | |
import org.openpermis.editor.policy.gui.IconAnnotation; |
38 | |
import org.openpermis.editor.policy.gui.PolicyIconRegistry; |
39 | |
import org.openpermis.editor.policy.presenter.PolicyContext; |
40 | |
import org.openpermis.editor.policy.presenter.Problem; |
41 | |
import org.openpermis.editor.policy.presenter.ProblemListPresenter; |
42 | |
import org.openpermis.policy.bean.PolicyBean; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 0 | public class ProblemListTool |
49 | |
extends AbstractToolView<ProblemListPresenter> |
50 | |
implements PropertyChangeListener, ListSelectionListener |
51 | |
{ |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | 0 | private static final CLocation DEFAULT_PROBLEM_LIST_LOCATION = |
60 | |
CLocation.base().normalSouth(0.2); |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
private JXList list; |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
private final PolicyIconRegistry iconRegistry; |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
public ProblemListTool ( |
85 | |
ApplicationContext context, AdapterTrader trader, PolicyIconRegistry iconRegistry |
86 | |
) { |
87 | 0 | super(context, trader); |
88 | 0 | this.iconRegistry = iconRegistry; |
89 | 0 | } |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
private boolean isEditable (Problem problem) { |
100 | 0 | return problem != null && canEditPart(problem.getPart()); |
101 | |
} |
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
@Action |
110 | |
public void editProblem () { |
111 | 0 | if (this.getPresenter() == null) { |
112 | 0 | return; |
113 | |
} |
114 | 0 | final Problem problem = (Problem) this.list.getSelectedValue(); |
115 | 0 | if (isEditable(problem)) { |
116 | 0 | editPart(problem.getPart()); |
117 | |
} |
118 | 0 | } |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
public void refresh (PolicyBean policy, PolicyContext context) { |
126 | 0 | if (policy == null) { |
127 | 0 | setPresenter(null); |
128 | |
} else { |
129 | 0 | setPresenter(new ProblemListPresenter(policy, context)); |
130 | |
} |
131 | 0 | } |
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
@Override |
139 | |
protected void updateActions () { |
140 | 0 | super.updateActions(); |
141 | 0 | final boolean hasPresenter = this.getPresenter() != null; |
142 | 0 | final Problem selectedProblem = (Problem) this.list.getSelectedValue(); |
143 | 0 | getActionMap().get("editProblem").setEnabled(hasPresenter && isEditable(selectedProblem)); |
144 | 0 | } |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
@Override |
150 | |
protected void detachPresenter (ProblemListPresenter presenter) { |
151 | 0 | super.detachPresenter(presenter); |
152 | 0 | presenter.removePropertyChangeListener(this); |
153 | 0 | } |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
@Override |
159 | |
protected void attachPresenter ( |
160 | |
ProblemListPresenter presenter, BindingGroup bindings |
161 | |
) { |
162 | 0 | super.attachPresenter(presenter, bindings); |
163 | 0 | presenter.addPropertyChangeListener(this); |
164 | 0 | bindings.addBinding( |
165 | |
SwingBindings.createJListBinding( |
166 | |
UpdateStrategy.READ, |
167 | |
this.getPresenter().getProblemList(), |
168 | |
this.list |
169 | |
) |
170 | |
); |
171 | 0 | } |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
@Override |
177 | |
public CLocation getDefaultLocation () { |
178 | 0 | return DEFAULT_PROBLEM_LIST_LOCATION; |
179 | |
} |
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
@Override |
187 | |
public JComponent createContentPane () { |
188 | 0 | this.list = new JXList(new Object[0]); |
189 | 0 | this.list.addHighlighter(HighlighterFactory.createAlternateStriping()); |
190 | 0 | this.list.setCellRenderer(new Renderer()); |
191 | 0 | this.list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
192 | 0 | this.list.addListSelectionListener(this); |
193 | 0 | DoubleClickForwarder.register(this.list, getActionMap().get("editProblem")); |
194 | 0 | return new JScrollPane(this.list); |
195 | |
} |
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
public void propertyChange (PropertyChangeEvent event) { |
203 | 0 | updateActions(); |
204 | 0 | } |
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
public void valueChanged (ListSelectionEvent e) { |
212 | 0 | updateActions(); |
213 | 0 | } |
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | 0 | private final class Renderer |
222 | |
extends DefaultListCellRenderer |
223 | |
{ |
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
private static final long serialVersionUID = 4749220399441492237L; |
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | 0 | private Renderer () { |
238 | 0 | setVerticalTextPosition(SwingConstants.TOP); |
239 | 0 | } |
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
@Override |
247 | |
public Component getListCellRendererComponent ( |
248 | |
JList source, Object value, int index, boolean selected, boolean focussed |
249 | |
) { |
250 | 0 | final Problem problem = (Problem) value; |
251 | 0 | setToolTipText(problem.toString()); |
252 | 0 | super.getListCellRendererComponent( |
253 | |
source, problem.toString(), index, selected, focussed |
254 | |
); |
255 | 0 | setIcon( |
256 | |
ProblemListTool.this.iconRegistry.getIcon( |
257 | |
problem.getPart(), IconAnnotation.WARNING |
258 | |
) |
259 | |
); |
260 | 0 | return this; |
261 | |
} |
262 | |
|
263 | |
} |
264 | |
|
265 | |
} |