1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui.component;
21
22 import java.awt.event.ActionEvent;
23 import java.awt.event.ActionListener;
24
25 import javax.swing.Box;
26 import javax.swing.BoxLayout;
27 import javax.swing.JComboBox;
28 import javax.swing.*;
29
30 import org.xnap.XNap;
31
32 /***
33 * This class provides a box with a combo box selector and a number input
34 * field for a minimum value and maximum value.
35 */
36 public class RangeBox extends Box implements ActionListener {
37
38
39
40
41
42 private JComboBox jcbCompare;
43 private ValidatedTextField jtMinValue;
44 private JLabel jlAnd;
45 private ValidatedTextField jtMaxValue;
46 private JComponent unitComponent;
47
48
49
50 /***
51 * Constructs the range box.
52 */
53 public RangeBox()
54 {
55 super(BoxLayout.X_AXIS);
56
57 jcbCompare = new JComboBox();
58 jcbCompare.addItem(new CompareNotActive());
59 jcbCompare.addItem(new CompareAtLeast());
60 jcbCompare.addItem(new CompareEquals());
61 jcbCompare.addItem(new CompareAtBest());
62 jcbCompare.addItem(new CompareBetween());
63 jcbCompare.addActionListener(this);
64 add(jcbCompare);
65
66 jtMinValue
67 = new ValidatedTextField("", 3, ValidatedTextField.NUMBERS_INT);
68 jtMinValue.setMinimumSize(jtMinValue.getPreferredSize());
69 jtMinValue.setVisible(false);
70 add(jtMinValue);
71
72 jlAnd = new JLabel(XNap.tr("and", 1));
73 jlAnd.setVisible(false);
74 add(jlAnd);
75
76 jtMaxValue
77 = new ValidatedTextField("", 3, ValidatedTextField.NUMBERS_INT);
78 jtMaxValue.setMinimumSize(jtMaxValue.getPreferredSize());
79 jtMaxValue.setVisible(false);
80 add(jtMaxValue);
81 }
82
83
84
85 /***
86 * Invoked when an item is selected.
87 */
88 public void actionPerformed(ActionEvent e)
89 {
90 getSelectedItem().selected();
91 jlAnd.setVisible(jtMinValue.isVisible() && jtMaxValue.isVisible());
92 if (unitComponent != null) {
93 unitComponent.setVisible
94 (jtMinValue.isVisible() || jtMaxValue.isVisible());
95 }
96 }
97
98 /***
99 * Returns the max value.
100 *
101 * @return -1, if not set
102 */
103 public int getMaxValue()
104 {
105 return getSelectedItem().getMaxValue();
106 }
107
108 /***
109 * Returns the min value.
110 *
111 * @return -1, if not set
112 */
113 public int getMinValue()
114 {
115 return getSelectedItem().getMinValue();
116 }
117
118 public void setUnitComponent(JComponent unitComponent)
119 {
120 if (this.unitComponent != null) {
121 remove(unitComponent);
122 }
123
124 this.unitComponent = unitComponent;
125 if (this.unitComponent != null) {
126 this.unitComponent.setVisible
127 (jtMinValue.isVisible() || jtMaxValue.isVisible());
128 add(this.unitComponent);
129 }
130 }
131
132 public void setValues(int min, int max)
133 {
134 for (int i = 0; i < jcbCompare.getItemCount(); i++) {
135 AbstractCompareItem item
136 = (AbstractCompareItem)jcbCompare.getItemAt(i);
137 if (item.set(min, max)) {
138 item.selected();
139 return;
140 }
141 }
142 }
143
144 /***
145 * Returns the currently selected compare item.
146 */
147 private AbstractCompareItem getSelectedItem()
148 {
149 return (AbstractCompareItem)jcbCompare.getSelectedItem();
150 }
151
152
153
154 private abstract class AbstractCompareItem
155 {
156
157 public boolean needsMinValue;
158 public boolean needsMaxValue;
159
160 public AbstractCompareItem(boolean needsMinValue, boolean needsMaxValue)
161 {
162 this.needsMinValue = needsMinValue;
163 this.needsMaxValue = needsMaxValue;
164 }
165
166 public void selected()
167 {
168 jtMinValue.setVisible(needsMinValue);
169 jtMaxValue.setVisible(needsMaxValue);
170 RangeBox.this.validate();
171 }
172
173 public int getMaxValue()
174 {
175 return jtMaxValue.isVisible() ? jtMaxValue.getIntValue() : -1;
176 }
177
178 public int getMinValue()
179 {
180 return jtMinValue.isVisible() ? jtMinValue.getIntValue() : -1;
181 }
182
183 /***
184 * Returns true, if using this compare type makes sense.
185 */
186 public boolean set(int min, int max)
187 {
188 if ((min == -1 ^ needsMinValue) && (max == -1 ^ needsMaxValue)) {
189 jtMinValue.setText((needsMinValue) ? min + "" : "");
190 jtMaxValue.setText((needsMaxValue) ? max + "" : "");
191
192 return true;
193 }
194
195 return false;
196 }
197
198 }
199
200 private class CompareNotActive extends AbstractCompareItem
201 {
202
203 public CompareNotActive()
204 {
205 super(false, false);
206 }
207
208 public String toString()
209 {
210 return "";
211 }
212
213 }
214
215 private class CompareAtLeast extends AbstractCompareItem
216 {
217
218 public CompareAtLeast()
219 {
220 super(true, false);
221 }
222
223 public String toString()
224 {
225 return XNap.tr("at least");
226 }
227
228 }
229
230 private class CompareAtBest extends AbstractCompareItem
231 {
232
233 public CompareAtBest()
234 {
235 super(false, true);
236 }
237
238 public String toString()
239 {
240 return XNap.tr("at best");
241 }
242
243 }
244
245 private class CompareBetween extends AbstractCompareItem
246 {
247
248 public CompareBetween()
249 {
250 super(true, true);
251 }
252
253 public String toString()
254 {
255 return XNap.tr("between");
256 }
257
258 public boolean set(int min, int max)
259 {
260 if ((min != -1) && (max != -1) && (max != min)) {
261 jtMinValue.setText(min + "");
262 jtMaxValue.setText(max + "");
263
264 return true;
265 }
266
267 return false;
268 }
269
270 }
271
272 private class CompareEquals extends AbstractCompareItem
273 {
274
275 public CompareEquals()
276 {
277 super(true, false);
278 }
279
280 public int getMaxValue()
281 {
282 return jtMinValue.getIntValue();
283 }
284
285 public int getMinValue()
286 {
287 return jtMinValue.getIntValue();
288 }
289
290 public String toString()
291 {
292 return XNap.tr("equals");
293 }
294
295 public boolean set(int min, int max)
296 {
297 if ((min != -1) && (max != -1) && (max == min)) {
298 jtMinValue.setText(min + "");
299 jtMaxValue.setText("");
300
301 return true;
302 }
303
304 return false;
305 }
306
307 }
308
309 }