blob: 4ba5a01192cf2041a66374ac6c8bc16515ebb3e2 (
plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
package bjc.utils.gui.panels;
import java.util.function.Consumer;
import java.util.function.Supplier;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import bjc.funcdata.ListEx;
import bjc.utils.gui.SimpleJList;
import bjc.utils.gui.layout.HLayout;
import bjc.utils.gui.layout.VLayout;
/**
* A panel that has a list of objects and ways of manipulating that list
*
* @author ben
*
* @param <E>
* The type of data stored in the list
*/
public class ListParameterPanel<E> extends JPanel {
// Version id for serialization
private static final long serialVersionUID = 3442971104975491571L;
/**
* Create a new panel using the specified actions for doing things
*
* @param add
* The action that provides items
* @param edit
* The action that edits items
* @param remove
* The action that removes items
*/
public ListParameterPanel(final Supplier<E> add, final Consumer<E> edit,
final Consumer<E> remove) {
this(add, edit, remove, null);
}
/**
* Create a new panel using the specified actions for doing things
*
* @param add
* The action that provides items
* @param edit
* The action that edits items
* @param remove
* The action that removes items
* @param defaults
* The default values to put in the list
*/
public ListParameterPanel(final Supplier<E> add, final Consumer<E> edit,
final Consumer<E> remove, final ListEx<E> defaults) {
setLayout(new VLayout(2));
JList<E> list;
if (defaults != null) {
list = SimpleJList.buildFromList(defaults.toIterable());
} else {
list = new JList<>(new DefaultListModel<>());
}
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
final JPanel buttonPanel = new JPanel();
int numButtons = 0;
if (add != null) {
numButtons++;
}
if (edit != null) {
numButtons++;
}
if (remove != null) {
numButtons++;
}
buttonPanel.setLayout(new HLayout(numButtons));
JButton addParam = null;
if (add != null) {
addParam = new JButton("Add...");
addParam.addActionListener(event -> {
final DefaultListModel<E> model = (DefaultListModel<E>) list.getModel();
model.addElement(add.get());
});
}
JButton editParam = null;
if (edit != null) {
editParam = new JButton("Edit...");
editParam.addActionListener(event -> {
edit.accept(list.getSelectedValue());
});
}
JButton removeParam = null;
if (remove != null) {
removeParam = new JButton("Remove...");
removeParam.addActionListener(event -> {
final DefaultListModel<E> model = (DefaultListModel<E>) list.getModel();
remove.accept(model.remove(list.getSelectedIndex()));
});
}
if (add != null) {
buttonPanel.add(addParam);
}
if (edit != null) {
buttonPanel.add(editParam);
}
if (remove != null) {
buttonPanel.add(removeParam);
}
add(list);
add(buttonPanel);
}
}
|