1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.openpermis.editor.policy.gui.checklist; |
11 | |
|
12 | |
import java.awt.Component; |
13 | |
import java.awt.Container; |
14 | |
import java.awt.Dimension; |
15 | |
import java.awt.EventQueue; |
16 | |
import java.awt.Rectangle; |
17 | |
import java.awt.event.ActionEvent; |
18 | |
import java.awt.event.ComponentEvent; |
19 | |
import java.awt.event.ComponentListener; |
20 | |
import java.awt.event.MouseEvent; |
21 | |
import java.util.Collections; |
22 | |
import java.util.EventObject; |
23 | |
|
24 | |
import javax.swing.Action; |
25 | |
import javax.swing.JScrollPane; |
26 | |
import javax.swing.JTable; |
27 | |
import javax.swing.JViewport; |
28 | |
import javax.swing.UIManager; |
29 | |
import javax.swing.border.Border; |
30 | |
import javax.swing.event.AncestorEvent; |
31 | |
import javax.swing.event.AncestorListener; |
32 | |
import javax.swing.plaf.UIResource; |
33 | |
import javax.swing.table.TableCellRenderer; |
34 | |
import javax.swing.table.TableColumn; |
35 | |
import javax.swing.table.TableModel; |
36 | |
|
37 | |
import org.jdesktop.observablecollections.ObservableCollections; |
38 | |
import org.jdesktop.observablecollections.ObservableList; |
39 | |
|
40 | |
import org.openpermis.policy.bean.PartBean; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | 0 | public class CheckList<T> |
48 | |
extends JTable |
49 | |
implements AncestorListener, ComponentListener |
50 | |
{ |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
private static final long serialVersionUID = -4074036755955844894L; |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
private static final int FIRST_ROW_SPACING = 8; |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
private static final int MIN_COLUMN_WIDTH = 16; |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
private Action doubleClickAction; |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
private TableCellRenderer itemRenderer; |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
public CheckList () { |
92 | 0 | this( |
93 | |
ObservableCollections.observableList(Collections.<T>emptyList()), |
94 | |
ObservableCollections.observableList(Collections.<T>emptyList()) |
95 | |
); |
96 | 0 | } |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
public CheckList (ObservableList<T> pool, ObservableList<T> selection) { |
105 | 0 | super(new CheckListModel<T>(pool, selection)); |
106 | 0 | addAncestorListener(this); |
107 | 0 | setAutoResizeMode(JTable.AUTO_RESIZE_OFF); |
108 | 0 | setIntercellSpacing(new Dimension(0, 0)); |
109 | 0 | setShowGrid(false); |
110 | 0 | setColumnSelectionAllowed(false); |
111 | 0 | } |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
public void setItemRenderer (TableCellRenderer itemRenderer) { |
121 | 0 | this.itemRenderer = itemRenderer; |
122 | 0 | } |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
public void setDoubleClickAction (Action action) { |
130 | 0 | this.doubleClickAction = action; |
131 | 0 | } |
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
public void bind (ObservableList<T> pool, ObservableList<T> selection) { |
140 | 0 | setModel(new CheckListModel<T>(pool, selection)); |
141 | 0 | } |
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
public void setVisibleRowCount (int rows) { |
149 | 0 | final int width = getPreferredSize().width; |
150 | 0 | final int height = rows * getRowHeight(); |
151 | 0 | setPreferredScrollableViewportSize(new Dimension(width, height)); |
152 | 0 | } |
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
private void addModelListeners () { |
159 | 0 | getModel().configureListeners(); |
160 | 0 | } |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
private void removeModelListeners () { |
167 | 0 | getModel().unconfigureListeners(); |
168 | 0 | } |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
private int getColumnWidth (int column) { |
177 | 0 | int width = MIN_COLUMN_WIDTH; |
178 | 0 | final TableCellRenderer renderer = getDefaultRenderer(getColumnClass(column)); |
179 | 0 | for (int row = 0 ; row < getRowCount() ; row++) { |
180 | 0 | final Component component = renderer.getTableCellRendererComponent( |
181 | |
this, getValueAt(row, column), false, false, row, column |
182 | |
); |
183 | 0 | width = Math.max(width, component.getPreferredSize().width); |
184 | |
} |
185 | 0 | return width; |
186 | |
} |
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
private int getViewportWidth () { |
194 | 0 | final Container p = getParent(); |
195 | 0 | if (p instanceof JViewport) { |
196 | 0 | return p.getWidth(); |
197 | |
} |
198 | 0 | return getWidth(); |
199 | |
} |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
private void layoutColumns () { |
206 | 0 | final int viewportWidth = getViewportWidth(); |
207 | 0 | int totalWidth = 0; |
208 | 0 | for (int i = 0 ; i < getColumnModel().getColumnCount() ; i++) { |
209 | 0 | final TableColumn column = getColumnModel().getColumn(i); |
210 | 0 | int width = getColumnWidth(i); |
211 | 0 | if (i == 0) { |
212 | |
|
213 | 0 | width += FIRST_ROW_SPACING; |
214 | 0 | } else if (i == getColumnModel().getColumnCount() - 1) { |
215 | |
|
216 | 0 | if (totalWidth + width < viewportWidth) { |
217 | 0 | width = viewportWidth - totalWidth; |
218 | |
} |
219 | |
} |
220 | 0 | column.setPreferredWidth(width); |
221 | 0 | column.setWidth(width); |
222 | 0 | totalWidth += width; |
223 | |
} |
224 | 0 | } |
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
@SuppressWarnings("unchecked") |
230 | |
public T getActivePart () { |
231 | 0 | if (this.getSelectedRow() != -1) { |
232 | |
|
233 | |
|
234 | |
|
235 | 0 | return (T) this.getValueAt( |
236 | |
this.getSelectionModel().getAnchorSelectionIndex(), 1); |
237 | |
} |
238 | 0 | return null; |
239 | |
} |
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
private void toggle (final int row) { |
248 | 0 | EventQueue.invokeLater( |
249 | |
new Runnable() { |
250 | 0 | public void run () { |
251 | 0 | final Boolean value = (Boolean) getModel().getValueAt(row, 0); |
252 | 0 | getModel().setValueAt(Boolean.valueOf(!value.booleanValue()), row, 0); |
253 | 0 | getSelectionModel().setSelectionInterval(row, row); |
254 | 0 | repaint(); |
255 | 0 | } |
256 | |
} |
257 | |
); |
258 | 0 | } |
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
@Override |
266 | |
public void setModel (TableModel dataModel) { |
267 | 0 | if (dataModel instanceof CheckListModel) { |
268 | 0 | super.setModel(dataModel); |
269 | 0 | return; |
270 | |
} |
271 | 0 | throw new IllegalStateException("Change of model not allowed."); |
272 | |
} |
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
@SuppressWarnings("unchecked") |
278 | |
@Override |
279 | |
public CheckListModel<T> getModel () { |
280 | 0 | return (CheckListModel<T>) super.getModel(); |
281 | |
} |
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
@Override |
287 | |
public TableCellRenderer getCellRenderer (int row, int column) { |
288 | 0 | final TableCellRenderer originalRenderer = super.getCellRenderer(row, column); |
289 | 0 | return new TableCellRenderer() { |
290 | 0 | public Component getTableCellRendererComponent ( |
291 | |
JTable table, Object value, boolean selected, boolean focus, int row, int column |
292 | |
) { |
293 | |
|
294 | 0 | if (value instanceof PartBean && CheckList.this.itemRenderer != null) { |
295 | 0 | return CheckList.this.itemRenderer.getTableCellRendererComponent( |
296 | |
table, value, selected, focus, row, column |
297 | |
); |
298 | |
} |
299 | 0 | return originalRenderer.getTableCellRendererComponent( |
300 | |
table, value, selected, false, row, column |
301 | |
); |
302 | |
} |
303 | |
}; |
304 | |
} |
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
@Override |
310 | |
public void doLayout () { |
311 | 0 | super.doLayout(); |
312 | 0 | layoutColumns(); |
313 | 0 | } |
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
@Override |
319 | |
public boolean editCellAt (int row, int column, EventObject e) { |
320 | 0 | if (column == 0) { |
321 | 0 | toggle(row); |
322 | 0 | return false; |
323 | 0 | } else if (e instanceof MouseEvent && ((MouseEvent) e).getClickCount() == 2) { |
324 | 0 | if (this.doubleClickAction != null) { |
325 | 0 | this.doubleClickAction.actionPerformed( |
326 | |
new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "") |
327 | |
); |
328 | 0 | return false; |
329 | |
} |
330 | |
} |
331 | 0 | return super.editCellAt(row, column, e); |
332 | |
} |
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
@Override |
339 | |
protected void configureEnclosingScrollPane () { |
340 | 0 | final Container p = getParent(); |
341 | 0 | if (p instanceof JViewport) { |
342 | 0 | p.addComponentListener(this); |
343 | 0 | final Container gp = p.getParent(); |
344 | 0 | if (gp instanceof JScrollPane) { |
345 | 0 | final JScrollPane scrollPane = (JScrollPane) gp; |
346 | 0 | final JViewport viewport = scrollPane.getViewport(); |
347 | 0 | if (viewport == null || viewport.getView() != this) { |
348 | 0 | return; |
349 | |
} |
350 | 0 | viewport.setBackground(getBackground()); |
351 | 0 | final Border border = scrollPane.getBorder(); |
352 | 0 | if (border == null || border instanceof UIResource) { |
353 | 0 | scrollPane.setBorder(UIManager.getBorder("Table.scrollPaneBorder")); |
354 | |
} |
355 | |
} |
356 | |
} |
357 | 0 | } |
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
@Override |
363 | |
protected void unconfigureEnclosingScrollPane () { |
364 | 0 | Container p = getParent(); |
365 | 0 | if (p instanceof JViewport) { |
366 | 0 | p.removeComponentListener(this); |
367 | |
} |
368 | 0 | } |
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | |
@Override |
374 | |
public void scrollRectToVisible (Rectangle rect) { |
375 | |
final Rectangle scrollRect; |
376 | 0 | if (rect.x > 0) { |
377 | 0 | scrollRect = new Rectangle(0, rect.y, rect.width + rect.x, rect.height); |
378 | |
} else { |
379 | 0 | scrollRect = rect; |
380 | |
} |
381 | 0 | super.scrollRectToVisible(scrollRect); |
382 | 0 | } |
383 | |
|
384 | |
|
385 | |
|
386 | |
|
387 | |
|
388 | |
|
389 | |
public void ancestorAdded (AncestorEvent event) { |
390 | 0 | addModelListeners(); |
391 | 0 | } |
392 | |
|
393 | |
|
394 | |
|
395 | |
|
396 | |
public void ancestorMoved (AncestorEvent event) { |
397 | |
|
398 | 0 | } |
399 | |
|
400 | |
|
401 | |
|
402 | |
|
403 | |
public void ancestorRemoved (AncestorEvent event) { |
404 | 0 | removeModelListeners(); |
405 | 0 | } |
406 | |
|
407 | |
|
408 | |
|
409 | |
|
410 | |
|
411 | |
|
412 | |
public void componentHidden (ComponentEvent e) { |
413 | |
|
414 | 0 | } |
415 | |
|
416 | |
|
417 | |
|
418 | |
|
419 | |
public void componentMoved (ComponentEvent e) { |
420 | |
|
421 | 0 | } |
422 | |
|
423 | |
|
424 | |
|
425 | |
|
426 | |
public void componentResized (ComponentEvent e) { |
427 | 0 | doLayout(); |
428 | 0 | resizeAndRepaint(); |
429 | 0 | } |
430 | |
|
431 | |
|
432 | |
|
433 | |
|
434 | |
public void componentShown (ComponentEvent e) { |
435 | |
|
436 | 0 | } |
437 | |
|
438 | |
} |