source: josm/trunk/src/org/openstreetmap/josm/gui/util/TableHelper.java@ 15226

Last change on this file since 15226 was 15226, checked in by Don-vip, 5 years ago

fix #14208 - add dedicated buttons in filter dialog to sort/reverse filters order.

Major overhaul/harmonization of our reorderable/sortable models.

  • Property svn:eol-style set to native
File size: 5.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.util;
3
4import java.awt.Component;
5
6import javax.swing.JTable;
7import javax.swing.ListSelectionModel;
8import javax.swing.event.ListDataEvent;
9import javax.swing.event.ListSelectionEvent;
10import javax.swing.table.AbstractTableModel;
11import javax.swing.table.TableCellRenderer;
12import javax.swing.table.TableColumn;
13
14import org.openstreetmap.josm.gui.dialogs.IEnabledStateUpdating;
15
16/**
17 * The class that provide common JTable customization methods
18 * @since 5785
19 */
20public final class TableHelper {
21
22 private TableHelper() {
23 // Hide default constructor for utils classes
24 }
25
26 /**
27 * Wires <code>listener</code> to <code>listSelectionModel</code> in such a way, that
28 * <code>listener</code> receives a {@link IEnabledStateUpdating#updateEnabledState()}
29 * on every {@link ListSelectionEvent}.
30 *
31 * @param listener the listener
32 * @param listSelectionModel the source emitting {@link ListSelectionEvent}s
33 * @since 15226
34 */
35 public static void adaptTo(final IEnabledStateUpdating listener, ListSelectionModel listSelectionModel) {
36 listSelectionModel.addListSelectionListener(e -> listener.updateEnabledState());
37 }
38
39 /**
40 * Wires <code>listener</code> to <code>listModel</code> in such a way, that
41 * <code>listener</code> receives a {@link IEnabledStateUpdating#updateEnabledState()}
42 * on every {@link ListDataEvent}.
43 *
44 * @param listener the listener
45 * @param listModel the source emitting {@link ListDataEvent}s
46 * @since 15226
47 */
48 public static void adaptTo(final IEnabledStateUpdating listener, AbstractTableModel listModel) {
49 listModel.addTableModelListener(e -> listener.updateEnabledState());
50 }
51
52 static int getColumnHeaderWidth(JTable tbl, int col) {
53 TableColumn tableColumn = tbl.getColumnModel().getColumn(col);
54 TableCellRenderer renderer = tableColumn.getHeaderRenderer();
55
56 if (renderer == null && tbl.getTableHeader() != null)
57 renderer = tbl.getTableHeader().getDefaultRenderer();
58
59 if (renderer == null)
60 return 0;
61
62 Component c = renderer.getTableCellRendererComponent(tbl, tableColumn.getHeaderValue(), false, false, -1, col);
63 return c.getPreferredSize().width;
64 }
65
66 static int getMaxWidth(JTable tbl, int col) {
67 int maxwidth = getColumnHeaderWidth(tbl, col);
68 for (int row = 0; row < tbl.getRowCount(); row++) {
69 TableCellRenderer tcr = tbl.getCellRenderer(row, col);
70 Object val = tbl.getValueAt(row, col);
71 Component comp = tcr.getTableCellRendererComponent(tbl, val, false, false, row, col);
72 maxwidth = Math.max(comp.getPreferredSize().width, maxwidth);
73 }
74 return maxwidth;
75 }
76
77 /**
78 * adjust the preferred width of column col to the maximum preferred width of the cells (including header)
79 * @param tbl table
80 * @param col column index
81 * @param resizable if true, resizing is allowed
82 * @since 15176
83 */
84 public static void adjustColumnWidth(JTable tbl, int col, boolean resizable) {
85 int maxwidth = getMaxWidth(tbl, col);
86 TableColumn column = tbl.getColumnModel().getColumn(col);
87 column.setPreferredWidth(maxwidth);
88 column.setResizable(resizable);
89 if (!resizable) {
90 column.setMaxWidth(maxwidth);
91 }
92 }
93
94 /**
95 * adjust the preferred width of column col to the maximum preferred width of the cells (including header)
96 * requires JTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
97 * @param tbl table
98 * @param col column index
99 * @param maxColumnWidth maximum column width
100 */
101 public static void adjustColumnWidth(JTable tbl, int col, int maxColumnWidth) {
102 int maxwidth = getMaxWidth(tbl, col);
103 tbl.getColumnModel().getColumn(col).setPreferredWidth(Math.min(maxwidth+10, maxColumnWidth));
104 }
105
106 /**
107 * adjust the table's columns to fit their content best
108 * requires JTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
109 * @param tbl table
110 * @since 14476
111 */
112 public static void computeColumnsWidth(JTable tbl) {
113 for (int column = 0; column < tbl.getColumnCount(); column++) {
114 adjustColumnWidth(tbl, column, Integer.MAX_VALUE);
115 }
116 }
117
118 /**
119 * Returns an array of all of the selected indices in the selection model, in increasing order.
120 * Unfortunately this method is not available in OpenJDK before version 11, see
121 * https://bugs.openjdk.java.net/browse/JDK-8199395
122 * Code taken from OpenJDK 11. To be removed when we switch to Java 11 or later.
123 *
124 * @param selectionModel list selection model.
125 *
126 * @return all of the selected indices, in increasing order,
127 * or an empty array if nothing is selected
128 * @since 15226
129 */
130 public static int[] getSelectedIndices(ListSelectionModel selectionModel) {
131 int iMin = selectionModel.getMinSelectionIndex();
132 int iMax = selectionModel.getMaxSelectionIndex();
133
134 if (iMin < 0 || iMax < 0) {
135 return new int[0];
136 }
137
138 int[] rvTmp = new int[1 + iMax - iMin];
139 int n = 0;
140 for (int i = iMin; i <= iMax; i++) {
141 if (selectionModel.isSelectedIndex(i)) {
142 rvTmp[n++] = i;
143 }
144 }
145 int[] rv = new int[n];
146 System.arraycopy(rvTmp, 0, rv, 0, n);
147 return rv;
148 }
149}
Note: See TracBrowser for help on using the repository browser.