|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * ImageJ software for multidimensional image processing and analysis. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 2016 Board of Regents of the University of |
| 6 | + * Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck |
| 7 | + * Institute of Molecular Cell Biology and Genetics. |
| 8 | + * %% |
| 9 | + * Redistribution and use in source and binary forms, with or without |
| 10 | + * modification, are permitted provided that the following conditions are met: |
| 11 | + * |
| 12 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer. |
| 14 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | + * this list of conditions and the following disclaimer in the documentation |
| 16 | + * and/or other materials provided with the distribution. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 22 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | + * POSSIBILITY OF SUCH DAMAGE. |
| 29 | + * #L% |
| 30 | + */ |
| 31 | + |
| 32 | +package org.scijava.ui.swing.viewer.table; |
| 33 | + |
| 34 | +import javax.swing.JScrollPane; |
| 35 | +import javax.swing.JTable; |
| 36 | +import javax.swing.table.AbstractTableModel; |
| 37 | + |
| 38 | +import org.scijava.table.Table; |
| 39 | +import org.scijava.table.TableDisplay; |
| 40 | +import org.scijava.ui.viewer.table.TableDisplayPanel; |
| 41 | + |
| 42 | +import org.scijava.ui.viewer.DisplayWindow; |
| 43 | + |
| 44 | +/** |
| 45 | + * This is the display panel for {@link Table}s. |
| 46 | + * |
| 47 | + * @author Curtis Rueden |
| 48 | + * @author Barry DeZonia |
| 49 | + */ |
| 50 | +public class SwingTableDisplayPanel extends JScrollPane implements |
| 51 | + TableDisplayPanel |
| 52 | +{ |
| 53 | + |
| 54 | + // -- instance variables -- |
| 55 | + |
| 56 | + private final DisplayWindow window; |
| 57 | + private final TableDisplay display; |
| 58 | + private final JTable table; |
| 59 | + private final NullTableModel nullModel; |
| 60 | + |
| 61 | + // -- constructor -- |
| 62 | + |
| 63 | + public SwingTableDisplayPanel(final TableDisplay display, |
| 64 | + final DisplayWindow window) |
| 65 | + { |
| 66 | + this.display = display; |
| 67 | + this.window = window; |
| 68 | + nullModel = new NullTableModel(); |
| 69 | + table = makeTable(); |
| 70 | + table.setAutoCreateRowSorter(true); |
| 71 | + setViewportView(table); |
| 72 | + window.setContent(this); |
| 73 | + } |
| 74 | + |
| 75 | + // -- TableDisplayPanel methods -- |
| 76 | + |
| 77 | + @Override |
| 78 | + public TableDisplay getDisplay() { |
| 79 | + return display; |
| 80 | + } |
| 81 | + |
| 82 | + // -- DisplayPanel methods -- |
| 83 | + |
| 84 | + @Override |
| 85 | + public DisplayWindow getWindow() { |
| 86 | + return window; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void redoLayout() { |
| 91 | + // Nothing to layout |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void setLabel(final String s) { |
| 96 | + // The label is not shown. |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void redraw() { |
| 101 | + // BDZ - I found a TODO here saying implement me. Not sure if my one liner |
| 102 | + // is correct but it seems to work. |
| 103 | + // one liner: |
| 104 | + // table.update(table.getGraphics()); |
| 105 | + // BDZ now try something more intuitive |
| 106 | + // table.repaint(); // nope |
| 107 | + // BDZ this? |
| 108 | + // table.doLayout(); // nope |
| 109 | + |
| 110 | + // note that our table is not attached to the table model as a listener. |
| 111 | + // we might need to do that. but try to find a simple way to enforce a |
| 112 | + // reread of the table because when it gets in here it's contents are ok |
| 113 | + |
| 114 | + javax.swing.table.TableModel model = table.getModel(); |
| 115 | + |
| 116 | + // BDZ attempt to force a rebuild. no luck. and also fails the Clear test. |
| 117 | + // Object v = model.getValueAt(0, 0); |
| 118 | + // model.setValueAt(v, 0, 0); |
| 119 | + |
| 120 | + // BDZ hacky hack way that works |
| 121 | + table.setModel(nullModel); |
| 122 | + table.setModel(model); |
| 123 | + // table.repaint(); |
| 124 | + } |
| 125 | + |
| 126 | + // -- Helper methods -- |
| 127 | + |
| 128 | + private JTable makeTable() { |
| 129 | + return new JTable(new TableModel(getTable())); |
| 130 | + } |
| 131 | + |
| 132 | + private Table<?, ?> getTable() { |
| 133 | + return display.size() == 0 ? null : display.get(0); |
| 134 | + } |
| 135 | + |
| 136 | + // -- Helper classes -- |
| 137 | + |
| 138 | + public static class NullTableModel extends AbstractTableModel { |
| 139 | + |
| 140 | + @Override |
| 141 | + public int getColumnCount() { |
| 142 | + return 0; |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public int getRowCount() { |
| 147 | + return 0; |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public Object getValueAt(int rowIndex, int columnIndex) { |
| 152 | + return null; |
| 153 | + } |
| 154 | + |
| 155 | + } |
| 156 | + |
| 157 | + /** A Swing {@link TableModel} backed by an ImageJ {@link Table}. */ |
| 158 | + public static class TableModel extends AbstractTableModel { |
| 159 | + |
| 160 | + private final Table<?, ?> tab; |
| 161 | + |
| 162 | + public TableModel(final Table<?, ?> table) { |
| 163 | + this.tab = table; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public String getColumnName(final int col) { |
| 168 | + if (col == 0) return ""; |
| 169 | + return tab.getColumnHeader(col - 1); |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public int getRowCount() { |
| 174 | + return tab.getRowCount(); |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public int getColumnCount() { |
| 179 | + return tab.getColumnCount() + 1; // +1 for row header column |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + public Object getValueAt(final int row, final int col) { |
| 184 | + if (row < 0 || row >= getRowCount()) return null; |
| 185 | + if (col < 0 || col >= getColumnCount()) return null; |
| 186 | + |
| 187 | + if (col == 0) { |
| 188 | + // get row header, or row number if none |
| 189 | + // NB: Assumes the JTable can handle Strings equally as well as the |
| 190 | + // underlying type T of the Table. |
| 191 | + final String header = tab.getRowHeader(row); |
| 192 | + if (header != null) return header; |
| 193 | + return "" + (row + 1); |
| 194 | + } |
| 195 | + |
| 196 | + // get the underlying table value |
| 197 | + // NB: The column is offset by one to accommodate the row header/number. |
| 198 | + return tab.get(col - 1, row); |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + public void setValueAt(final Object value, final int row, final int col) { |
| 203 | + if (row < 0 || row >= getRowCount()) return; |
| 204 | + if (col < 0 || col >= getColumnCount()) return; |
| 205 | + if (col == 0) { |
| 206 | + // set row header |
| 207 | + tab.setRowHeader(row, value == null ? null : value.toString()); |
| 208 | + return; |
| 209 | + } |
| 210 | + set(tab, col - 1, row, value); |
| 211 | + fireTableCellUpdated(row, col); |
| 212 | + } |
| 213 | + |
| 214 | + // -- Helper methods -- |
| 215 | + |
| 216 | + private <T> void set(final Table<?, T> table, |
| 217 | + final int col, final int row, final Object value) |
| 218 | + { |
| 219 | + @SuppressWarnings("unchecked") |
| 220 | + final T typedValue = (T) value; |
| 221 | + table.set(col, row, typedValue); |
| 222 | + } |
| 223 | + |
| 224 | + } |
| 225 | + |
| 226 | +} |
0 commit comments