Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b87058c

Browse files
authored
Merge pull request #42 from scijava/scijava-table
Add UI to display org.scijava.table tables
2 parents 83030c1 + 969c771 commit b87058c

File tree

3 files changed

+303
-2
lines changed

3 files changed

+303
-2
lines changed

pom.xml

+10-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
<parent>
66
<groupId>org.scijava</groupId>
77
<artifactId>pom-scijava</artifactId>
8-
<version>19.2.0</version>
8+
<version>23.1.1</version>
99
<relativePath />
1010
</parent>
1111

1212
<artifactId>scijava-ui-swing</artifactId>
13-
<version>0.11.2-SNAPSHOT</version>
13+
<version>0.12.0-SNAPSHOT</version>
1414

1515
<name>SciJava UI: Swing</name>
1616
<description>SciJava user interface components for Java Swing.</description>
@@ -100,6 +100,8 @@ Wisconsin-Madison.</license.copyrightOwners>
100100

101101
<!-- NB: Deploy releases to the ImageJ Maven repository. -->
102102
<releaseProfiles>deploy-to-imagej</releaseProfiles>
103+
104+
<scijava-table.version>0.2.0</scijava-table.version>
103105
</properties>
104106

105107
<repositories>
@@ -115,6 +117,11 @@ Wisconsin-Madison.</license.copyrightOwners>
115117
<groupId>org.scijava</groupId>
116118
<artifactId>scijava-common</artifactId>
117119
</dependency>
120+
<dependency>
121+
<groupId>org.scijava</groupId>
122+
<artifactId>scijava-table</artifactId>
123+
<version>${scijava-table.version}</version>
124+
</dependency>
118125
<dependency>
119126
<groupId>org.scijava</groupId>
120127
<artifactId>scijava-ui-awt</artifactId>
@@ -145,6 +152,7 @@ Wisconsin-Madison.</license.copyrightOwners>
145152
<dependency>
146153
<groupId>junit</groupId>
147154
<artifactId>junit</artifactId>
155+
<scope>test</scope>
148156
</dependency>
149157
</dependencies>
150158
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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.JTable;
35+
36+
import org.scijava.table.Table;
37+
import org.scijava.ui.viewer.table.AbstractTableDisplayViewer;
38+
39+
import org.scijava.display.Display;
40+
import org.scijava.plugin.Plugin;
41+
import org.scijava.ui.UserInterface;
42+
import org.scijava.ui.swing.SwingUI;
43+
import org.scijava.ui.viewer.DisplayViewer;
44+
import org.scijava.ui.viewer.DisplayWindow;
45+
46+
/**
47+
* A Swing {@link Table} display viewer, which displays tables in a
48+
* {@link JTable}.
49+
*
50+
* @author Curtis Rueden
51+
*/
52+
@Plugin(type = DisplayViewer.class)
53+
public class SwingTableDisplayViewer extends AbstractTableDisplayViewer {
54+
55+
@Override
56+
public boolean isCompatible(final UserInterface ui) {
57+
// TODO: Consider whether to use an interface for Swing UIs instead?
58+
return ui instanceof SwingUI;
59+
}
60+
61+
@Override
62+
public void view(final DisplayWindow w, final Display<?> d) {
63+
super.view(w, d);
64+
setPanel(new SwingTableDisplayPanel(getDisplay(), w));
65+
}
66+
67+
}

0 commit comments

Comments
 (0)