Skip to content

Commit 6d484bc

Browse files
committed
Clean up EasySwingDisplayViewer code
And add maarzt as contributor. And bump the version due to new API.
1 parent e6c3d8e commit 6d484bc

File tree

2 files changed

+148
-116
lines changed

2 files changed

+148
-116
lines changed

pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111

1212
<artifactId>scijava-ui-swing</artifactId>
13-
<version>0.12.1-SNAPSHOT</version>
13+
<version>0.13.0-SNAPSHOT</version>
1414

1515
<name>SciJava UI: Swing</name>
1616
<description>SciJava user interface components for Java Swing.</description>
@@ -64,6 +64,11 @@
6464
<url>https://imagej.net/User:Eglinger</url>
6565
<properties><id>imagejan</id></properties>
6666
</contributor>
67+
<contributor>
68+
<name>Matthias Arzt</name>
69+
<url>https://imagej.net/User:Maarzt</url>
70+
<properties><id>maarzt</id></properties>
71+
</contributor>
6772
</contributors>
6873

6974
<mailingLists>
Lines changed: 142 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,40 @@
1+
/*
2+
* #%L
3+
* SciJava UI components for Java Swing.
4+
* %%
5+
* Copyright (C) 2010 - 2019 Board of Regents of the University of
6+
* Wisconsin-Madison.
7+
* %%
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright notice,
12+
* this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright notice,
14+
* this list of conditions and the following disclaimer in the documentation
15+
* and/or other materials provided with the distribution.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
21+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27+
* POSSIBILITY OF SUCH DAMAGE.
28+
* #L%
29+
*/
30+
131
package org.scijava.ui.swing.viewer;
232

33+
import java.awt.BorderLayout;
34+
35+
import javax.swing.JPanel;
36+
37+
import org.scijava.command.Command;
338
import org.scijava.display.Display;
439
import org.scijava.display.event.DisplayDeletedEvent;
540
import org.scijava.object.ObjectService;
@@ -8,126 +43,118 @@
843
import org.scijava.ui.swing.SwingUI;
944
import org.scijava.ui.viewer.AbstractDisplayViewer;
1045
import org.scijava.ui.viewer.DisplayPanel;
11-
import org.scijava.ui.viewer.DisplayViewer;
1246
import org.scijava.ui.viewer.DisplayWindow;
1347

14-
import javax.swing.JPanel;
15-
import java.awt.BorderLayout;
16-
1748
/**
18-
* Class helping to build a simple Swing JPanel viewer for any object of class T
19-
* declared as a {@link org.scijava.ItemIO} output {@link Parameter}
20-
* in a {@link org.scijava.command.Command} Command
21-
*
22-
* Usage example see:
23-
* https://github.com/maarzt/example-imagej-display
24-
*
25-
* Image.sc forum thread :
26-
* https://forum.image.sc/t/displaying-and-using-and-any-object-in-a-scijava-fiji-command
27-
*
49+
* Class helping to build a simple Swing {@link JPanel} viewer for any object of
50+
* class T declared as a {@link org.scijava.ItemIO} output {@link Parameter} in
51+
* a {@link Command}.
52+
*
2853
* @param <T> class of object needed to be displayed in a Swing UI
2954
* @author Matthias Arzt
55+
* @see <a href="https://github.com/maarzt/example-imagej-display">Usage
56+
* example</a>
57+
* @see <a href=
58+
* "https://forum.image.sc/t/displaying-and-using-and-any-object-in-a-scijava-fiji-command">Image.sc
59+
* forum thread</a>
3060
*/
31-
32-
abstract public class EasySwingDisplayViewer<T> extends
33-
AbstractDisplayViewer<T> implements DisplayViewer<T>
61+
public abstract class EasySwingDisplayViewer<T> extends
62+
AbstractDisplayViewer<T>
3463
{
35-
private final Class<T> classOfObject;
36-
37-
@Parameter
38-
ObjectService objectService;
39-
40-
protected EasySwingDisplayViewer( Class< T > classOfObject )
41-
{
42-
this.classOfObject = classOfObject;
43-
}
44-
45-
@Override
46-
public boolean isCompatible(final UserInterface ui) {
47-
return ui instanceof SwingUI;
48-
}
49-
50-
@Override
51-
public boolean canView(final Display<?> d) {
52-
Object object = d.get( 0 );
53-
if(! classOfObject.isInstance( object ) )
54-
return false;
55-
T value = ( T ) object;
56-
return canView( value );
57-
}
58-
59-
protected abstract boolean canView( T value );
60-
protected abstract void redoLayout();
61-
protected abstract void setLabel(final String s);
62-
protected abstract void redraw();
63-
protected abstract JPanel createDisplayPanel(T value);
64-
65-
@Override
66-
public void onDisplayDeletedEvent( DisplayDeletedEvent e )
67-
{
68-
super.onDisplayDeletedEvent( e );
69-
objectService.removeObject( getDisplay().get( 0 ) );
70-
}
71-
72-
@Override
73-
public void view(final DisplayWindow w, final Display<?> d) {
74-
objectService.addObject( d.get( 0 ) );
75-
super.view(w, d);
76-
final JPanel content = createDisplayPanel( getDisplay().get(0) );
77-
setPanel( new SwingDisplayPanel(w, d, this, content) );
78-
}
79-
80-
81-
public static class SwingDisplayPanel extends JPanel implements DisplayPanel
82-
{
83-
84-
// -- instance variables --
85-
86-
private final EasySwingDisplayViewer< ? > viewer;
87-
private final DisplayWindow window;
88-
private final Display< ? > display;
89-
90-
// -- PlotDisplayPanel methods --
91-
92-
public SwingDisplayPanel( DisplayWindow window, Display< ? > display, EasySwingDisplayViewer< ? > viewer, JPanel panel )
93-
{
94-
this.window = window;
95-
this.display = display;
96-
this.viewer = viewer;
97-
window.setContent(this);
98-
setLayout( new BorderLayout() );
99-
add(panel);
100-
}
101-
102-
@Override
103-
public Display< ? > getDisplay() {
104-
return display;
105-
}
106-
107-
// -- DisplayPanel methods --
108-
109-
@Override
110-
public DisplayWindow getWindow() {
111-
return window;
112-
}
113-
114-
@Override
115-
public void redoLayout()
116-
{
117-
viewer.redoLayout();
118-
}
119-
120-
@Override
121-
public void setLabel( String s )
122-
{
123-
viewer.setLabel( s );
124-
}
125-
126-
@Override
127-
public void redraw()
128-
{
129-
viewer.redraw();
130-
}
131-
}
132-
}
13364

65+
private final Class<T> classOfObject;
66+
67+
@Parameter
68+
ObjectService objectService;
69+
70+
protected EasySwingDisplayViewer(Class<T> classOfObject) {
71+
this.classOfObject = classOfObject;
72+
}
73+
74+
@Override
75+
public boolean isCompatible(final UserInterface ui) {
76+
return ui instanceof SwingUI;
77+
}
78+
79+
@Override
80+
public boolean canView(final Display<?> d) {
81+
final Object object = d.get(0);
82+
if (!classOfObject.isInstance(object)) return false;
83+
@SuppressWarnings("unchecked")
84+
final T value = (T) object;
85+
return canView(value);
86+
}
87+
88+
protected abstract boolean canView(T value);
89+
90+
protected abstract void redoLayout();
91+
92+
protected abstract void setLabel(final String s);
93+
94+
protected abstract void redraw();
95+
96+
protected abstract JPanel createDisplayPanel(T value);
97+
98+
@Override
99+
public void onDisplayDeletedEvent(DisplayDeletedEvent e) {
100+
super.onDisplayDeletedEvent(e);
101+
objectService.removeObject(getDisplay().get(0));
102+
}
103+
104+
@Override
105+
public void view(final DisplayWindow w, final Display<?> d) {
106+
objectService.addObject(d.get(0));
107+
super.view(w, d);
108+
final JPanel content = createDisplayPanel(getDisplay().get(0));
109+
setPanel(new SwingDisplayPanel(w, d, this, content));
110+
}
111+
112+
public static class SwingDisplayPanel extends JPanel implements DisplayPanel {
113+
114+
// -- instance variables --
115+
116+
private final EasySwingDisplayViewer<?> viewer;
117+
private final DisplayWindow window;
118+
private final Display<?> display;
119+
120+
// -- PlotDisplayPanel methods --
121+
122+
public SwingDisplayPanel(DisplayWindow window, Display<?> display,
123+
EasySwingDisplayViewer<?> viewer, JPanel panel)
124+
{
125+
this.window = window;
126+
this.display = display;
127+
this.viewer = viewer;
128+
window.setContent(this);
129+
setLayout(new BorderLayout());
130+
add(panel);
131+
}
132+
133+
@Override
134+
public Display<?> getDisplay() {
135+
return display;
136+
}
137+
138+
// -- DisplayPanel methods --
139+
140+
@Override
141+
public DisplayWindow getWindow() {
142+
return window;
143+
}
144+
145+
@Override
146+
public void redoLayout() {
147+
viewer.redoLayout();
148+
}
149+
150+
@Override
151+
public void setLabel(String s) {
152+
viewer.setLabel(s);
153+
}
154+
155+
@Override
156+
public void redraw() {
157+
viewer.redraw();
158+
}
159+
}
160+
}

0 commit comments

Comments
 (0)