Skip to content

Commit 0f9a9b6

Browse files
committed
Add UI subsystem
It was migrated from ImageJ: https://github.com/imagej/imagej/tree/imagej-2.0.0-beta-7.9/core/ui
1 parent 780f0e9 commit 0f9a9b6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+4425
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2014 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.io.console;
33+
34+
import java.io.IOException;
35+
import java.util.LinkedList;
36+
37+
import org.scijava.console.AbstractConsoleArgument;
38+
import org.scijava.console.ConsoleArgument;
39+
import org.scijava.display.DisplayService;
40+
import org.scijava.io.IOService;
41+
import org.scijava.log.LogService;
42+
import org.scijava.plugin.Parameter;
43+
import org.scijava.plugin.Plugin;
44+
45+
/**
46+
* Handles the {@code --open} command line argument.
47+
*
48+
* @author Curtis Rueden
49+
*/
50+
@Plugin(type = ConsoleArgument.class)
51+
public class OpenArgument extends AbstractConsoleArgument {
52+
53+
@Parameter
54+
private IOService ioService;
55+
56+
@Parameter
57+
private DisplayService displayService;
58+
59+
@Parameter
60+
private LogService log;
61+
62+
// -- ConsoleArgument methods --
63+
64+
@Override
65+
public void handle(final LinkedList<String> args) {
66+
if (!supports(args)) return;
67+
68+
args.removeFirst(); // --open
69+
final String source = args.removeFirst();
70+
71+
try {
72+
final Object o = ioService.open(source);
73+
displayService.createDisplay(o);
74+
}
75+
catch (IOException exc) {
76+
log.error(exc);
77+
}
78+
}
79+
80+
// -- Typed methods --
81+
82+
@Override
83+
public boolean supports(final LinkedList<String> args) {
84+
return args != null && args.size() >= 2 && args.getFirst().equals("--open");
85+
}
86+
87+
}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2014 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;
33+
34+
/**
35+
* Represents a plane of ARGB pixel data. Useful for passing around data during
36+
* copy/paste operations to external programs.
37+
*
38+
* @author Barry DeZonia
39+
*/
40+
public class ARGBPlane {
41+
42+
// -- instance variables --
43+
44+
private final int width;
45+
private final int height;
46+
private final int[] data;
47+
48+
// -- constructors --
49+
50+
public ARGBPlane(final int width, final int height, final int[] data) {
51+
this.width = width;
52+
this.height = height;
53+
this.data = data;
54+
if (width < 1 || height < 1) {
55+
throw new IllegalArgumentException(
56+
"Both width and height must be greater than zero");
57+
}
58+
if ((long) width * height > Integer.MAX_VALUE) {
59+
throw new IllegalArgumentException("Plane is too large");
60+
}
61+
if (data.length != width * height) {
62+
throw new IllegalArgumentException("Data size mismatch");
63+
}
64+
}
65+
66+
public ARGBPlane(final int width, final int height) {
67+
this(width, height, new int[width * height]);
68+
}
69+
70+
// -- public interface --
71+
72+
public int getWidth() {
73+
return width;
74+
}
75+
76+
public int getHeight() {
77+
return height;
78+
}
79+
80+
public int[] getData() {
81+
return data;
82+
}
83+
84+
public int getARGB(final int x, final int y) {
85+
return data[index(x, y)];
86+
}
87+
88+
public int getAlpha(final int x, final int y) {
89+
return alpha(data[index(x, y)]);
90+
}
91+
92+
public int getRed(final int x, final int y) {
93+
return red(data[index(x, y)]);
94+
}
95+
96+
public int getGreen(final int x, final int y) {
97+
return green(data[index(x, y)]);
98+
}
99+
100+
public int getBlue(final int x, final int y) {
101+
return blue(data[index(x, y)]);
102+
}
103+
104+
public void setARGB(final int x, final int y, final int argb) {
105+
data[index(x, y)] = argb;
106+
}
107+
108+
public void setAlpha(final int x, final int y, final int v) {
109+
int argb = data[index(x, y)];
110+
final int component = v & 0xff;
111+
argb &= ~0xff000000;
112+
argb |= (component << 24);
113+
data[index(x, y)] = argb;
114+
}
115+
116+
public void setRed(final int x, final int y, final int v) {
117+
int argb = data[index(x, y)];
118+
final int component = v & 0xff;
119+
argb &= ~0xff0000;
120+
argb |= (component << 16);
121+
data[index(x, y)] = argb;
122+
}
123+
124+
public void setGreen(final int x, final int y, final int v) {
125+
int argb = data[index(x, y)];
126+
final int component = v & 0xff;
127+
argb &= ~0xff00;
128+
argb |= (component << 8);
129+
data[index(x, y)] = argb;
130+
}
131+
132+
public void setBlue(final int x, final int y, final int v) {
133+
int argb = data[index(x, y)];
134+
final int component = v & 0xff;
135+
argb &= ~0xff;
136+
argb |= (component << 0);
137+
data[index(x, y)] = argb;
138+
}
139+
140+
// -- helpers --
141+
142+
private int index(final int x, final int y) {
143+
return y * width + x;
144+
}
145+
146+
private int alpha(final int argb) {
147+
return (argb >> 24) & 0xff;
148+
}
149+
150+
private int red(final int argb) {
151+
return (argb >> 16) & 0xff;
152+
}
153+
154+
private int green(final int argb) {
155+
return (argb >> 8) & 0xff;
156+
}
157+
158+
private int blue(final int argb) {
159+
return (argb >> 0) & 0xff;
160+
}
161+
162+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2014 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;
33+
34+
import org.scijava.module.Module;
35+
import org.scijava.module.ModuleException;
36+
import org.scijava.module.process.PreprocessorPlugin;
37+
import org.scijava.plugin.Parameter;
38+
import org.scijava.widget.AbstractInputHarvester;
39+
import org.scijava.widget.InputHarvester;
40+
41+
/**
42+
* AbstractInputHarvesterPlugin is an {@link InputHarvester} that implements the
43+
* {@link PreprocessorPlugin} interface. It is intended to be extended by
44+
* UI-specific implementations such as {@code SwingInputHarvester}.
45+
* <p>
46+
* The input harvester will first check whether the default UI matches that of
47+
* its implementation; for example, the Swing-based input harvester plugin will
48+
* only harvest inputs if the Swing UI is currently the default one.
49+
* </p>
50+
*
51+
* @author Curtis Rueden
52+
* @author Barry DeZonia
53+
* @param <P> The type of UI component housing the input panel itself.
54+
* @param <W> The type of UI component housing each input widget.
55+
*/
56+
public abstract class AbstractInputHarvesterPlugin<P, W> extends
57+
AbstractInputHarvester<P, W> implements PreprocessorPlugin
58+
{
59+
60+
@Parameter(required = false)
61+
private UIService uiService;
62+
63+
private String cancelReason;
64+
65+
// -- ModuleProcessor methods --
66+
67+
@Override
68+
public void process(final Module module) {
69+
if (uiService == null) return; // no UI service means no input harvesting!
70+
71+
// do not harvest if the UI is inactive!
72+
if (!uiService.isVisible(getUI())) return;
73+
74+
// proceed with input harvesting
75+
try {
76+
harvest(module);
77+
}
78+
catch (final ModuleException e) {
79+
cancel(e.getMessage());
80+
}
81+
}
82+
83+
// -- Cancelable methods --
84+
85+
@Override
86+
public boolean isCanceled() {
87+
return cancelReason != null;
88+
}
89+
90+
@Override
91+
public void cancel(final String reason) {
92+
cancelReason = reason;
93+
}
94+
95+
@Override
96+
public String getCancelReason() {
97+
return cancelReason;
98+
}
99+
100+
// -- Internal methods --
101+
102+
/** Gets the name (or class name) of the input harvester's affiliated UI. */
103+
protected abstract String getUI();
104+
105+
}

0 commit comments

Comments
 (0)