summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/gui/panels/SliderInputPanel.java
blob: da87357b43c12366e49039a574a4066bcb682d91 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package bjc.utils.gui.panels;

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 final int minValue;
		private final int maxValue;

		private final int initValue;

		public NumberFormatter(final SliderSettings settings) {
			minValue = settings.minValue;
			maxValue = settings.maxValue;

			initValue = settings.initValue;
		}

		@Override
		public Object stringToValue(final String text) throws ParseException {
			try {
				final 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(final NumberFormatException nfex) {
				final ParseException pex = new ParseException("Value must be a valid integer", 0);

				pex.initCause(nfex);

				throw pex;
			}
		}

		@Override
		public String valueToString(final 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(final int min, final 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(final int min, final int max, final int init) {
			minValue = min;
			maxValue = max;

			initValue = init;
		}
	}

	private static final long serialVersionUID = 2956394160569961404L;
	private final JSlider slider;
	private final 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(final String lab, final SliderSettings settings, final int majorTick,
			final int minorTick, final Consumer<Integer> action) {
		setLayout(new HLayout(3));

		final 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 {
				final int val = slider.getValue();

				field.setValue(val);

				action.accept(val);
			}
		});

		field.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);
		field.setColumns(15);
		field.addPropertyChangeListener("value", (event) -> {
			final 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(final int value) {
		slider.setValue(value);

		field.setValue(value);
	}
}