Skip to content

Commit 4b0e9d3

Browse files
committed
Add widget for multiple choises
1 parent 342eb5f commit 4b0e9d3

2 files changed

Lines changed: 145 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package net.imagej.ui.swing.widget;
2+
3+
import java.util.Set;
4+
5+
/**
6+
* @author Matthais Arzt
7+
*/
8+
public interface MultipleChoices<T> {
9+
10+
void setChoices(Iterable<T> choices, PrettyPrinter<T> prettyPrinter);
11+
12+
boolean get(T choice);
13+
14+
Set<T> get();
15+
16+
void set(Set<T> selected);
17+
18+
void set(T choice, boolean value);
19+
20+
interface PrettyPrinter<T> {
21+
String toString(T value);
22+
}
23+
24+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package net.imagej.ui.swing.widget;
2+
3+
import org.scijava.module.MethodCallException;
4+
import org.scijava.plugin.Plugin;
5+
import org.scijava.ui.swing.widget.SwingInputWidget;
6+
import org.scijava.widget.InputWidget;
7+
import org.scijava.widget.WidgetModel;
8+
9+
import javax.swing.*;
10+
import java.awt.event.ActionEvent;
11+
import java.util.HashMap;
12+
import java.util.HashSet;
13+
import java.util.Map;
14+
import java.util.Set;
15+
16+
/**
17+
* @author Matthias Arzt
18+
*/
19+
@Plugin(type = InputWidget.class)
20+
public class SwingMultipleChoicesWidget<T> extends SwingInputWidget<MultipleChoices<T>> {
21+
22+
Map<JCheckBox, T> checkBoxes = new HashMap<>();
23+
24+
MyMultipleChoices<T> result;
25+
26+
@Override
27+
public void set(final WidgetModel model) {
28+
super.set(model);
29+
getComponent().setLayout(new BoxLayout(getComponent(), BoxLayout.PAGE_AXIS));
30+
result = new MyMultipleChoices<T>(this);
31+
get().setValue(result);
32+
try {
33+
model.getItem().initialize(model.getModule());
34+
model.getPanel().refresh();
35+
} catch (MethodCallException e) {
36+
e.printStackTrace();
37+
}
38+
}
39+
40+
@Override
41+
protected void doRefresh() {
42+
checkBoxes.forEach((checkBox, choice) -> checkBox.setSelected(result.get(choice)));
43+
}
44+
45+
@Override
46+
public MultipleChoices<T> getValue() {
47+
return result;
48+
}
49+
50+
@Override
51+
public boolean supports(final WidgetModel model) {
52+
return model.isType(MultipleChoices.class);
53+
}
54+
55+
private void setChoices(Iterable<T> choices, MultipleChoices.PrettyPrinter<T> prettyPrinter) {
56+
JPanel panel = getComponent();
57+
for(JCheckBox oldCheckBox : checkBoxes.keySet())
58+
panel.remove(oldCheckBox);
59+
checkBoxes.clear();
60+
result.get().clear();
61+
if(choices != null)
62+
for(T choice : choices) {
63+
String text = prettyPrinter.toString(choice);
64+
JCheckBox checkBox = new JCheckBox(text);
65+
checkBoxes.put(checkBox, choice);
66+
checkBox.addActionListener(e -> checkBoxChanged(e));
67+
panel.add(checkBox);
68+
}
69+
panel.revalidate();
70+
}
71+
72+
private void checkBoxChanged(ActionEvent e) {
73+
JCheckBox source = (JCheckBox) e.getSource();
74+
T choice = checkBoxes.get(source);
75+
result.set(choice, source.isSelected());
76+
get().setValue(result);
77+
}
78+
79+
static private class MyMultipleChoices<T> implements MultipleChoices<T> {
80+
81+
final SwingMultipleChoicesWidget<T> widget;
82+
83+
Set<T> selected;
84+
85+
private MyMultipleChoices(SwingMultipleChoicesWidget<T> widget) {
86+
this.widget = widget;
87+
selected = new HashSet<T>();
88+
}
89+
90+
@Override
91+
public void setChoices(Iterable<T> choices, PrettyPrinter<T> prettyPrinter) {
92+
widget.setChoices(choices, prettyPrinter);
93+
}
94+
95+
@Override
96+
public boolean get(T value) {
97+
return selected.contains(value);
98+
}
99+
100+
@Override
101+
public void set(T choice, boolean value) {
102+
if(value)
103+
selected.add(choice);
104+
else
105+
selected.remove(choice);
106+
}
107+
108+
@Override
109+
public Set<T> get() {
110+
return selected;
111+
}
112+
113+
@Override
114+
public void set(Set<T> s) {
115+
selected.clear();
116+
selected.addAll(s);
117+
}
118+
119+
}
120+
121+
}

0 commit comments

Comments
 (0)