001: /*
002: * Copyright (c) 2005-2008 Substance Kirill Grouchnikov. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of Substance Kirill Grouchnikov nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030: package test.check;
031:
032: import java.awt.*;
033: import java.awt.event.ActionEvent;
034: import java.awt.event.ActionListener;
035: import java.util.Hashtable;
036:
037: import javax.swing.*;
038:
039: import test.check.command.ConfigurationCommand;
040:
041: import com.jgoodies.forms.builder.DefaultFormBuilder;
042: import com.jgoodies.forms.layout.FormLayout;
043:
044: /**
045: * Test application panel for testing {@link JSlider} component.
046: *
047: * @author Kirill Grouchnikov
048: */
049: public class SliderPanel extends ControllablePanel {
050: /**
051: * A configure command that sets a titled border on the specified slider.
052: *
053: * @author Kirill Grouchnikov
054: */
055: public static class SetTitleBorderCommand implements
056: ConfigurationCommand<JSlider> {
057: /*
058: * (non-Javadoc)
059: *
060: * @see test.check.ConfigurationCommand#invoke(java.lang.Object)
061: */
062: public void configure(JSlider slider) {
063: slider.setBorder(BorderFactory.createTitledBorder("Title"));
064: }
065: }
066:
067: /**
068: * A configure command that clears the border of the specified slider.
069: *
070: * @author Kirill Grouchnikov
071: */
072: public static class ClearBorderCommand implements
073: ConfigurationCommand<JSlider> {
074: /*
075: * (non-Javadoc)
076: *
077: * @see test.check.ConfigurationCommand#invoke(java.lang.Object)
078: */
079: public void configure(JSlider slider) {
080: slider.setBorder(null);
081: }
082: }
083:
084: /**
085: * A configure command that configures the specified slider to snap to the
086: * ticks.
087: *
088: * @author Kirill Grouchnikov
089: */
090: public static class SnapToTicksCommand implements
091: ConfigurationCommand<JSlider> {
092: /*
093: * (non-Javadoc)
094: *
095: * @see test.check.ConfigurationCommand#invoke(java.lang.Object)
096: */
097: public void configure(JSlider slider) {
098: slider.setSnapToTicks(true);
099: }
100: }
101:
102: /**
103: * A configure command that configures the specified slider to not snap to
104: * the ticks.
105: *
106: * @author Kirill Grouchnikov
107: */
108: public static class UnsnapToTicksCommand implements
109: ConfigurationCommand<JSlider> {
110: /*
111: * (non-Javadoc)
112: *
113: * @see test.check.ConfigurationCommand#invoke(java.lang.Object)
114: */
115: public void configure(JSlider slider) {
116: slider.setSnapToTicks(false);
117: }
118: }
119:
120: /**
121: * Creates the test panel with sliders.
122: */
123: public SliderPanel() {
124: this .setLayout(new BorderLayout());
125:
126: JPanel mainPanel = new ScrollablePanel();
127: mainPanel.setLayout(new BorderLayout());
128: mainPanel.setOpaque(false);
129: this .add(new JScrollPane(mainPanel), BorderLayout.CENTER);
130:
131: JPanel horPanel = new JPanel();
132: BoxLayout horLayout = new BoxLayout(horPanel, BoxLayout.Y_AXIS);
133: horPanel.setLayout(horLayout);
134:
135: JSlider horizontalLTR3 = new JSlider(0, 100, 50);
136: horizontalLTR3.setSnapToTicks(true);
137: horizontalLTR3.setMajorTickSpacing(20);
138: horizontalLTR3.setMinorTickSpacing(5);
139: horPanel.add(horizontalLTR3);
140:
141: JSlider horizontalLTR2 = new JSlider(0, 100, 50);
142: horizontalLTR2.setSnapToTicks(true);
143: horizontalLTR2.setMajorTickSpacing(20);
144: horizontalLTR2.setMinorTickSpacing(5);
145: horizontalLTR2.setPaintTicks(true);
146: horPanel.add(horizontalLTR2);
147:
148: JSlider horizontalLTR1 = new JSlider(0, 100, 50);
149: horizontalLTR1.setSnapToTicks(true);
150: horizontalLTR1.setMajorTickSpacing(20);
151: horizontalLTR1.setMinorTickSpacing(5);
152: horizontalLTR1.setPaintLabels(true);
153: horPanel.add(horizontalLTR1);
154:
155: JSlider horizontalLTR = new JSlider(0, 100, 50);
156: horizontalLTR.setSnapToTicks(true);
157: horizontalLTR.setMajorTickSpacing(20);
158: horizontalLTR.setMinorTickSpacing(5);
159: horizontalLTR.setPaintTicks(true);
160: horizontalLTR.setPaintLabels(true);
161: horPanel.add(horizontalLTR);
162:
163: JSlider disHorizontalLTR3 = new JSlider(0, 100, 50);
164: disHorizontalLTR3.setSnapToTicks(true);
165: disHorizontalLTR3.setMajorTickSpacing(20);
166: disHorizontalLTR3.setMinorTickSpacing(5);
167: disHorizontalLTR3.setEnabled(false);
168: horPanel.add(disHorizontalLTR3);
169:
170: JSlider disHorizontalLTR2 = new JSlider(0, 100, 50);
171: disHorizontalLTR2.setSnapToTicks(true);
172: disHorizontalLTR2.setMajorTickSpacing(20);
173: disHorizontalLTR2.setMinorTickSpacing(5);
174: disHorizontalLTR2.setPaintTicks(true);
175: disHorizontalLTR2.setEnabled(false);
176: horPanel.add(disHorizontalLTR2);
177:
178: JSlider disHorizontalLTR1 = new JSlider(0, 100, 50);
179: disHorizontalLTR1.setSnapToTicks(true);
180: disHorizontalLTR1.setMajorTickSpacing(20);
181: disHorizontalLTR1.setMinorTickSpacing(5);
182: disHorizontalLTR1.setPaintLabels(true);
183: disHorizontalLTR1.setEnabled(false);
184: horPanel.add(disHorizontalLTR1);
185:
186: JSlider disHorizontalLTR = new JSlider(0, 100, 50);
187: disHorizontalLTR.setSnapToTicks(true);
188: disHorizontalLTR.setMajorTickSpacing(20);
189: disHorizontalLTR.setMinorTickSpacing(5);
190: disHorizontalLTR.setPaintTicks(true);
191: disHorizontalLTR.setPaintLabels(true);
192: disHorizontalLTR.setEnabled(false);
193: horPanel.add(disHorizontalLTR);
194:
195: JSlider horizontalRTL = new JSlider(0, 100, 50);
196: horizontalRTL.setSnapToTicks(true);
197: horizontalRTL.setMajorTickSpacing(20);
198: horizontalRTL.setMinorTickSpacing(5);
199: horizontalRTL.setPaintTicks(true);
200: horizontalRTL.setPaintLabels(true);
201: horizontalRTL
202: .applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
203: horPanel.add(horizontalRTL);
204:
205: JSlider disHorizontalRTL = new JSlider(0, 100, 50);
206: disHorizontalRTL.setSnapToTicks(true);
207: disHorizontalRTL.setMajorTickSpacing(20);
208: disHorizontalRTL.setMinorTickSpacing(5);
209: disHorizontalRTL.setPaintTicks(true);
210: disHorizontalRTL.setPaintLabels(true);
211: disHorizontalRTL.setEnabled(false);
212: disHorizontalRTL
213: .applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
214: horPanel.add(disHorizontalRTL);
215:
216: JSlider titledHorizontal = new JSlider(0, 100, 50);
217: titledHorizontal.setBorder(BorderFactory
218: .createTitledBorder("Title"));
219: horPanel.add(titledHorizontal);
220:
221: JSlider coloredLabels = getSliderWithColoredLabels();
222: horPanel.add(coloredLabels);
223:
224: JSlider htmlLabels = getSliderWithHTMLLabels();
225: horPanel.add(htmlLabels);
226:
227: mainPanel.add(horPanel, BorderLayout.WEST);
228:
229: JPanel verPanel = new JPanel(new FlowLayout());
230: mainPanel.add(verPanel, BorderLayout.CENTER);
231:
232: JSlider verticalLTR3 = new JSlider(JSlider.VERTICAL, 0, 100, 50);
233: verticalLTR3.setSnapToTicks(true);
234: verticalLTR3.setMajorTickSpacing(20);
235: verticalLTR3.setMinorTickSpacing(5);
236: // verticalLTR3.setPaintTicks(true);
237: // verticalLTR3.setPaintLabels(true);
238: verPanel.add(verticalLTR3);
239:
240: JSlider verticalLTR1 = new JSlider(JSlider.VERTICAL, 0, 100, 50);
241: verticalLTR1.setSnapToTicks(true);
242: verticalLTR1.setMajorTickSpacing(20);
243: verticalLTR1.setMinorTickSpacing(5);
244: verticalLTR1.setPaintTicks(true);
245: // verticalLTR1.setPaintLabels(true);
246: verPanel.add(verticalLTR1);
247:
248: JSlider verticalLTR2 = new JSlider(JSlider.VERTICAL, 0, 100, 50);
249: verticalLTR2.setSnapToTicks(true);
250: verticalLTR2.setMajorTickSpacing(20);
251: verticalLTR2.setMinorTickSpacing(5);
252: // verticalLTR2.setPaintTicks(true);
253: verticalLTR2.setPaintLabels(true);
254: verPanel.add(verticalLTR2);
255:
256: JSlider verticalLTR = new JSlider(JSlider.VERTICAL, 0, 100, 50);
257: verticalLTR.setSnapToTicks(true);
258: verticalLTR.setMajorTickSpacing(20);
259: verticalLTR.setMinorTickSpacing(5);
260: verticalLTR.setPaintTicks(true);
261: verticalLTR.setPaintLabels(true);
262: verPanel.add(verticalLTR);
263:
264: JSlider disVerticalLTR = new JSlider(JSlider.VERTICAL, 0, 100,
265: 50);
266: disVerticalLTR.setSnapToTicks(true);
267: disVerticalLTR.setMajorTickSpacing(20);
268: disVerticalLTR.setMinorTickSpacing(5);
269: disVerticalLTR.setPaintTicks(true);
270: disVerticalLTR.setPaintLabels(true);
271: disVerticalLTR.setEnabled(false);
272: verPanel.add(disVerticalLTR);
273:
274: JSlider verticalRTL3 = new JSlider(JSlider.VERTICAL, 0, 100, 50);
275: verticalRTL3.setSnapToTicks(true);
276: verticalRTL3.setMajorTickSpacing(20);
277: verticalRTL3.setMinorTickSpacing(5);
278: // verticalRTL3.setPaintTicks(true);
279: // verticalRTL3.setPaintLabels(true);
280: verticalRTL3
281: .applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
282: verPanel.add(verticalRTL3);
283:
284: JSlider verticalRTL1 = new JSlider(JSlider.VERTICAL, 0, 100, 50);
285: verticalRTL1.setSnapToTicks(true);
286: verticalRTL1.setMajorTickSpacing(20);
287: verticalRTL1.setMinorTickSpacing(5);
288: verticalRTL1.setPaintTicks(true);
289: // verticalRTL1.setPaintLabels(true);
290: verticalRTL1
291: .applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
292: verPanel.add(verticalRTL1);
293:
294: JSlider verticalRTL2 = new JSlider(JSlider.VERTICAL, 0, 100, 50);
295: verticalRTL2.setSnapToTicks(true);
296: verticalRTL2.setMajorTickSpacing(20);
297: verticalRTL2.setMinorTickSpacing(5);
298: // verticalRTL2.setPaintTicks(true);
299: verticalRTL2.setPaintLabels(true);
300: verticalRTL2
301: .applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
302: verPanel.add(verticalRTL2);
303:
304: JSlider verticalRTL = new JSlider(JSlider.VERTICAL, 0, 100, 50);
305: verticalRTL.setSnapToTicks(true);
306: verticalRTL.setMajorTickSpacing(20);
307: verticalRTL.setMinorTickSpacing(5);
308: verticalRTL.setPaintTicks(true);
309: verticalRTL.setPaintLabels(true);
310: verticalRTL
311: .applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
312: verPanel.add(verticalRTL);
313:
314: JSlider disVerticalRTL = new JSlider(JSlider.VERTICAL, 0, 100,
315: 50);
316: disVerticalRTL.setSnapToTicks(true);
317: disVerticalRTL.setMajorTickSpacing(20);
318: disVerticalRTL.setMinorTickSpacing(5);
319: disVerticalRTL.setPaintTicks(true);
320: disVerticalRTL.setPaintLabels(true);
321: disVerticalRTL.setEnabled(false);
322: disVerticalRTL
323: .applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
324: verPanel.add(disVerticalRTL);
325:
326: FormLayout lm = new FormLayout("fill:pref:grow", "");
327: DefaultFormBuilder builder = new DefaultFormBuilder(lm,
328: new ScrollablePanel());
329:
330: JButton setTitledBorderButton = new JButton("Set titled border");
331: setTitledBorderButton.addActionListener(new ActionListener() {
332: public void actionPerformed(ActionEvent e) {
333: SwingUtilities.invokeLater(new Runnable() {
334: public void run() {
335: SliderPanel.run(SliderPanel.this ,
336: new SetTitleBorderCommand());
337: }
338: });
339: }
340: });
341:
342: JButton clearBorderButton = new JButton("Clear border");
343: clearBorderButton.addActionListener(new ActionListener() {
344: public void actionPerformed(ActionEvent e) {
345: SwingUtilities.invokeLater(new Runnable() {
346: public void run() {
347: SliderPanel.run(SliderPanel.this ,
348: new ClearBorderCommand());
349: }
350: });
351: }
352: });
353:
354: JButton snapToTicksButton = new JButton("Snap to ticks");
355: snapToTicksButton.addActionListener(new ActionListener() {
356: public void actionPerformed(ActionEvent e) {
357: SwingUtilities.invokeLater(new Runnable() {
358: public void run() {
359: SliderPanel.run(SliderPanel.this ,
360: new SnapToTicksCommand());
361: }
362: });
363: }
364: });
365:
366: JButton unsnapToTicksButton = new JButton("Unsnap to ticks");
367: unsnapToTicksButton.addActionListener(new ActionListener() {
368: public void actionPerformed(ActionEvent e) {
369: SwingUtilities.invokeLater(new Runnable() {
370: public void run() {
371: SliderPanel.run(SliderPanel.this ,
372: new UnsnapToTicksCommand());
373: }
374: });
375: }
376: });
377:
378: builder.append(setTitledBorderButton);
379: builder.append(clearBorderButton);
380: builder.append(snapToTicksButton);
381: builder.append(unsnapToTicksButton);
382:
383: this .controlPanel = builder.getPanel();
384: }
385:
386: private JSlider getSliderWithColoredLabels() {
387: JSlider coloredLabels = new JSlider();
388: coloredLabels.setMinimum(0);
389: coloredLabels.setMaximum(2);
390: coloredLabels.setMajorTickSpacing(1);
391: coloredLabels.setPaintLabels(true);
392: Hashtable labels = new Hashtable();
393: JLabel l0 = new JLabel("Red");
394: l0.setForeground(new Color(255, 0, 0));
395: labels.put(new Integer(0), l0);
396: JLabel l1 = new JLabel("Green");
397: l1.setForeground(new Color(0, 255, 0));
398: labels.put(new Integer(1), l1);
399: JLabel l2 = new JLabel("Blue");
400: l2.setForeground(new Color(0, 0, 255));
401: labels.put(new Integer(2), l2);
402: coloredLabels.setLabelTable(labels);
403: return coloredLabels;
404: }
405:
406: private JSlider getSliderWithHTMLLabels() {
407: JSlider htmlLabels = new JSlider();
408: htmlLabels.setMinimum(0);
409: htmlLabels.setMaximum(2);
410: htmlLabels.setMajorTickSpacing(1);
411: htmlLabels.setPaintLabels(true);
412: Hashtable labels = new Hashtable();
413: JLabel l0 = new JLabel(
414: "<html><body>0<br><b>0</b><br><i>0</i></body></html>");
415: l0.setForeground(new Color(255, 0, 0));
416: labels.put(new Integer(0), l0);
417: JLabel l1 = new JLabel(
418: "<html><body>1<br><b>1</b><br><i>1</i></body></html>");
419: labels.put(new Integer(1), l1);
420: JLabel l2 = new JLabel(
421: "<html><body>2<br><b>2</b><br><i>2</i></body></html>");
422: labels.put(new Integer(2), l2);
423: htmlLabels.setLabelTable(labels);
424: return htmlLabels;
425: }
426:
427: /**
428: * Runs the specified configuration command on all sliders in the specified
429: * component.
430: *
431: * @param comp
432: * Component.
433: * @param command
434: * Configuration command to run.
435: */
436: public static void run(Component comp,
437: ConfigurationCommand<JSlider> command) {
438: if (comp instanceof JSlider) {
439: command.configure((JSlider) comp);
440: return;
441: }
442: if (comp instanceof Container) {
443: Container cont = (Container) comp;
444: for (int i = 0; i < cont.getComponentCount(); i++) {
445: run(cont.getComponent(i), command);
446: }
447: }
448: }
449:
450: }
|