summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/gui/ListParameterPanel.java
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-02-21 15:42:14 -0500
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-02-21 15:42:14 -0500
commitc2089e5a3e604424c7cd00bbddcb03731ff5a0ea (patch)
tree31854f794e47b8dc67ea10d6699368810ca78f55 /BJC-Utils2/src/main/java/bjc/utils/gui/ListParameterPanel.java
parenteb30c23bb03579bf839189ab0d2ad172d5b07766 (diff)
Added new control and some commenting
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/gui/ListParameterPanel.java')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/gui/ListParameterPanel.java95
1 files changed, 95 insertions, 0 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/ListParameterPanel.java b/BJC-Utils2/src/main/java/bjc/utils/gui/ListParameterPanel.java
new file mode 100644
index 0000000..3cdfd47
--- /dev/null
+++ b/BJC-Utils2/src/main/java/bjc/utils/gui/ListParameterPanel.java
@@ -0,0 +1,95 @@
+package bjc.utils.gui;
+
+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.utils.funcdata.FunctionalList;
+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 addAct
+ * The action that provides items
+ * @param editAct
+ * The action that edits items
+ * @param removeAct
+ * The action that removes items
+ */
+ public ListParameterPanel(Supplier<E> addAct, Consumer<E> editAct,
+ Consumer<E> removeAct) {
+ this(addAct, editAct, removeAct, null);
+ }
+
+ /**
+ * Create a new panel using the specified actions for doing things
+ *
+ * @param addAct
+ * The action that provides items
+ * @param editAct
+ * The action that edits items
+ * @param removeAct
+ * The action that removes items
+ * @param defVals
+ * The default values to put in the list
+ */
+ public ListParameterPanel(Supplier<E> addAct, Consumer<E> editAct,
+ Consumer<E> removeAct, FunctionalList<E> defVals) {
+ setLayout(new VLayout(2));
+
+ JList<E> list;
+
+ if (defVals != null) {
+ list = SimpleJList.buildFromList(defVals.toIterable());
+ } else {
+ list = new JList<>(new DefaultListModel<>());
+ }
+
+ list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
+
+ JPanel buttonPanel = new JPanel();
+ buttonPanel.setLayout(new HLayout(3));
+
+ JButton addParam = new JButton("Add...");
+ addParam.addActionListener(
+ (ev) -> ((DefaultListModel<E>) list.getModel())
+ .addElement(addAct.get()));
+
+ JButton editParam = new JButton("Edit...");
+ editParam.addActionListener(
+ (ev) -> editAct.accept(list.getSelectedValue()));
+
+ JButton removeParam = new JButton("Remove...");
+ removeParam.addActionListener((ev) -> removeAct
+ .accept(((DefaultListModel<E>) list.getModel())
+ .remove(list.getSelectedIndex())));
+
+ buttonPanel.add(addParam);
+ buttonPanel.add(editParam);
+ buttonPanel.add(removeParam);
+
+ add(list);
+ add(buttonPanel);
+ }
+}