/
PreviewCheckbox.java
105 lines (82 loc) · 2.51 KB
/
PreviewCheckbox.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* To the extent possible under law, the ImageJ developers have waived
* all copyright and related or neighboring rights to this tutorial code.
*
* See the CC0 1.0 Universal license for details:
* http://creativecommons.org/publicdomain/zero/1.0/
*/
import ij.ImagePlus;
import net.imagej.Dataset;
import net.imagej.ImageJ;
import net.imagej.axis.Axes;
import net.imagej.axis.AxisType;
import org.scijava.ItemVisibility;
import org.scijava.command.Command;
import org.scijava.command.Previewable;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
/**
* An ImageJ2 command with preview that is triggered only when the Preview
* checkbox is active.
*/
@Plugin(type = Command.class,
menuPath = "Tutorials>Preview Checkbox")
public class PreviewCheckbox implements Command, Previewable {
// -- Parameters --
@Parameter
private ImagePlus imp;
@Parameter(persist = false, initializer = "initTitle")
private String title;
@Parameter(visibility = ItemVisibility.INVISIBLE, persist = false,
callback = "previewChanged")
private boolean preview;
// -- Other fields --
/** The original title of the image. */
private String initialTitle;
// -- Command methods --
@Override
public void run() {
// Set the image's title to the specified value.
imp.setTitle(title);
}
// -- Previewable methods --
@Override
public void preview() {
if (preview) run();
}
@Override
public void cancel() {
// Set the image's title back to the original value.
imp.setTitle(initialTitle);
}
// -- Initializer methods --
/** Initializes the {@link #title} parameter. */
protected void initTitle() {
title = initialTitle = imp.getTitle();
}
// -- Callback methods --
/** Called when the {@link #preview} parameter value changes. */
protected void previewChanged() {
// When preview box is unchecked, reset the image title back to original.
if (!preview) cancel();
}
// -- Main method --
/** Tests our command. */
public static void main(final String... args) throws Exception {
// Launch ImageJ as usual.
final ImageJ ij = new ImageJ();
ij.launch(args);
// Create a beautiful test image.
long[] dims = {512, 128};
String name = "A spiffy blank image";
AxisType[] axes = {Axes.X, Axes.Y};
int bitsPerPixel = 8;
boolean signed = false;
boolean floating = false;
final Dataset dataset =
ij.dataset().create(dims, name, axes, bitsPerPixel, signed, floating);
ij.ui().show(dataset);
// Launch the "PreviewCheckbox" command.
ij.command().run(PreviewCheckbox.class, true);
}
}