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