summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/gui
diff options
context:
space:
mode:
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/gui')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/gui/FormattedInputPanel.java66
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/gui/HolderOutputPanel.java60
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/gui/SliderInputPanel.java196
3 files changed, 322 insertions, 0 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/FormattedInputPanel.java b/BJC-Utils2/src/main/java/bjc/utils/gui/FormattedInputPanel.java
new file mode 100644
index 0000000..f11c3a8
--- /dev/null
+++ b/BJC-Utils2/src/main/java/bjc/utils/gui/FormattedInputPanel.java
@@ -0,0 +1,66 @@
+package bjc.utils.gui;
+
+import java.util.function.Consumer;
+
+import javax.swing.JFormattedTextField;
+import javax.swing.JFormattedTextField.AbstractFormatter;
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+
+import bjc.utils.gui.layout.HLayout;
+
+/**
+ * A simple panel allowing for input of a single formatted value
+ *
+ * @author ben
+ *
+ * @param <InputVal>
+ * The type of value being formatted
+ */
+public class FormattedInputPanel<InputVal> extends JPanel {
+ private static final long serialVersionUID = 5232016563558588031L;
+ private JFormattedTextField field;
+
+ /**
+ * Create a new formatted input panel
+ *
+ * @param label
+ * The label for this panel
+ * @param length
+ * The length of this panel
+ * @param formatter
+ * The formatter to use for input
+ * @param valueReciever
+ * The action to call whenever the value changes
+ */
+ @SuppressWarnings("unchecked")
+ public FormattedInputPanel(String label, int length,
+ AbstractFormatter formatter,
+ Consumer<InputVal> valueReciever) {
+ setLayout(new HLayout(2));
+
+ JLabel lab = new JLabel(label);
+ field = new JFormattedTextField(formatter);
+
+ field.setColumns(length);
+ field.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);
+ field.addPropertyChangeListener("value", (event) -> {
+ // This is safe, because InputVal should be the type of
+ // whatever object the formatter is returning
+ valueReciever.accept((InputVal) field.getValue());
+ });
+
+ add(lab);
+ add(field);
+ }
+
+ /**
+ * Reset the value in this panel to a specified value
+ *
+ * @param value
+ * The value to set the panel to
+ */
+ public void resetValues(InputVal value) {
+ field.setValue(value);
+ }
+}
diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/HolderOutputPanel.java b/BJC-Utils2/src/main/java/bjc/utils/gui/HolderOutputPanel.java
new file mode 100644
index 0000000..4cbfb61
--- /dev/null
+++ b/BJC-Utils2/src/main/java/bjc/utils/gui/HolderOutputPanel.java
@@ -0,0 +1,60 @@
+package bjc.utils.gui;
+
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+import javax.swing.Timer;
+
+import bjc.utils.data.IHolder;
+import bjc.utils.gui.layout.HLayout;
+
+/**
+ * A panel that outputs a value bound to a {@link IHolder}
+ *
+ * @author ben
+ *
+ */
+public class HolderOutputPanel extends JPanel {
+ private static final long serialVersionUID = 166573313903782080L;
+ private Timer updateTimer;
+ private JLabel value;
+
+ /**
+ * Create a new display panel, backed by a holder
+ *
+ * @param lab
+ * The label to attach to this field
+ * @param val
+ * The holder to get the value from
+ * @param nDelay
+ * The delay in ms between value updates
+ */
+ public HolderOutputPanel(String lab, IHolder<String> val, int nDelay) {
+ setLayout(new HLayout(2));
+
+ JLabel label = new JLabel(lab);
+ value = new JLabel(val.getValue() + " (stopped)");
+
+ updateTimer = new Timer(nDelay, (event) -> {
+ value.setText(val.getValue());
+ });
+
+ add(label);
+ add(value);
+ }
+
+ /**
+ * Start updating the contents of the field from the holder
+ */
+ public void startUpdating() {
+ updateTimer.start();
+ }
+
+ /**
+ * Stop updating the contents of the field from the holder
+ */
+ public void stopUpdating() {
+ updateTimer.stop();
+
+ value.setText(value.getText() + " (stopped)");
+ }
+}
diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/SliderInputPanel.java b/BJC-Utils2/src/main/java/bjc/utils/gui/SliderInputPanel.java
new file mode 100644
index 0000000..0988f0c
--- /dev/null
+++ b/BJC-Utils2/src/main/java/bjc/utils/gui/SliderInputPanel.java
@@ -0,0 +1,196 @@
+package bjc.utils.gui;
+
+import java.text.ParseException;
+import java.util.function.Consumer;
+
+import javax.swing.JFormattedTextField;
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+import javax.swing.JSlider;
+
+import bjc.utils.gui.layout.HLayout;
+
+/**
+ * A simple input panel for a slider-controlled value and a manual-input
+ * field for setting the slider
+ *
+ * @author ben
+ *
+ */
+public class SliderInputPanel extends JPanel {
+ private final class NumberFormatter
+ extends JFormattedTextField.AbstractFormatter {
+ private static final long serialVersionUID = -4448291795913908270L;
+
+ private int minValue;
+ private int maxValue;
+
+ private int initValue;
+
+ public NumberFormatter(SliderSettings settings) {
+ minValue = settings.minValue;
+ maxValue = settings.maxValue;
+
+ initValue = settings.initValue;
+ }
+
+ @Override
+ public Object stringToValue(String text) throws ParseException {
+ try {
+ int val = Integer.parseInt(text);
+
+ if (val < minValue) {
+ throw new ParseException(
+ "Value must be greater than " + minValue, 0);
+ } else if (val > maxValue) {
+ throw new ParseException(
+ "Value must be smaller than " + maxValue, 0);
+ } else {
+ return val;
+ }
+ } catch (NumberFormatException nfex) {
+ ParseException pex = new ParseException(
+ "Value must be a valid integer", 0);
+
+ pex.initCause(nfex);
+
+ throw pex;
+ }
+ }
+
+ @Override
+ public String valueToString(Object value) throws ParseException {
+ if (value == null) {
+ return Integer.toString(initValue);
+ }
+
+ return Integer.toString((Integer) value);
+ }
+ }
+
+ /**
+ * Represents the settings for a slider
+ *
+ * @author ben
+ *
+ */
+ public static class SliderSettings {
+ /**
+ * The minimum value of the slider
+ */
+ public final int minValue;
+ /**
+ * The maximum value of the slider
+ */
+ public final int maxValue;
+
+ /**
+ * The initial value of the slider
+ */
+ public final int initValue;
+
+ /**
+ * Create a new slider settings, with the initial value in the
+ * middle
+ *
+ * @param min
+ * The minimum value of the slider
+ * @param max
+ * The maximum value of the slider
+ */
+ public SliderSettings(int min, int max) {
+ this(min, max, (min + max) / 2);
+ }
+
+ /**
+ * Create a new set of slider sttings
+ *
+ * @param min
+ * The minimum slider value
+ * @param max
+ * The maximum slider value
+ * @param init
+ * Th initial slider value
+ */
+ public SliderSettings(int min, int max, int init) {
+ minValue = min;
+ maxValue = max;
+
+ initValue = init;
+ }
+ }
+
+ private static final long serialVersionUID = 2956394160569961404L;
+ private JSlider slider;
+ private JFormattedTextField field;
+
+ /**
+ * Create a new slider input panel
+ *
+ * @param lab
+ * The label for the field
+ * @param settings
+ * The settings for slider values
+ * @param majorTick
+ * The setting for where to place big ticks
+ * @param minorTick
+ * The setting for where to place small ticks
+ * @param action
+ * The action to execute for a given value
+ */
+ public SliderInputPanel(String lab, SliderSettings settings,
+ int majorTick, int minorTick, Consumer<Integer> action) {
+ setLayout(new HLayout(3));
+
+ JLabel label = new JLabel(lab);
+
+ slider = new JSlider(settings.minValue, settings.maxValue,
+ settings.initValue);
+ field = new JFormattedTextField(new NumberFormatter(settings));
+
+ slider.setMajorTickSpacing(majorTick);
+ slider.setMinorTickSpacing(minorTick);
+ slider.setPaintTicks(true);
+ slider.setPaintLabels(true);
+
+ slider.addChangeListener((event) -> {
+ if (slider.getValueIsAdjusting()) {
+ // Do nothing
+ } else {
+ int sliderVal = slider.getValue();
+
+ field.setValue(sliderVal);
+
+ action.accept(sliderVal);
+ }
+ });
+
+ field.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);
+ field.setColumns(15);
+ field.addPropertyChangeListener("value", (event) -> {
+ Object value = field.getValue();
+
+ if (value == null) {
+ // Do nothing
+ } else {
+ slider.setValue((Integer) value);
+ }
+ });
+
+ add(label);
+ add(slider);
+ add(field);
+ }
+
+ /**
+ * Reset the values in this panel to a specified value
+ *
+ * @param value
+ * The value to reset the fields to
+ */
+ public void resetValues(int value) {
+ slider.setValue(value);
+
+ field.setValue(value);
+ }
+}