View Javadoc
1   package com.bric.multislider;
2   
3   import java.awt.Color;
4   import java.awt.FlowLayout;
5   import java.awt.GridBagConstraints;
6   import java.awt.GridBagLayout;
7   import java.awt.Insets;
8   import java.awt.Window;
9   import java.awt.event.ActionEvent;
10  import java.awt.event.ActionListener;
11  import java.lang.reflect.Constructor;
12  
13  import javax.swing.ButtonGroup;
14  import javax.swing.JComboBox;
15  import javax.swing.JComponent;
16  import javax.swing.JFrame;
17  import javax.swing.JLabel;
18  import javax.swing.JPanel;
19  import javax.swing.JRadioButton;
20  import javax.swing.SwingUtilities;
21  import javax.swing.event.ChangeEvent;
22  import javax.swing.event.ChangeListener;
23  
24  import com.bric.multislider.MultiThumbSlider.Collision;
25  import com.bric.multislider.MultiThumbSliderUi.Thumb;
26  
27  @SuppressWarnings("javadoc")
28  public class MultiThumbSliderDemo extends JPanel
29  {
30      private static final long serialVersionUID = 1L;
31  
32      public static void main(String[] args)
33      {
34          SwingUtilities.invokeLater(new Runnable()
35          {
36              public void run()
37              {
38                  JFrame frame = new JFrame();
39                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
40                  frame.getContentPane().add(new MultiThumbSliderDemo());
41  
42                  frame.pack();
43                  frame.setLocationRelativeTo(null);
44                  frame.setVisible(true);
45              }
46          });
47      }
48  
49      JComboBox<Collision> collisionComboBox = new JComboBox<Collision>();
50  
51      JComboBox<Class> uiComboBox = new JComboBox<Class>();
52  
53      JComboBox<DefaultMultiThumbSliderUi.Thumb> thumbComboBox = new JComboBox<DefaultMultiThumbSliderUi.Thumb>();
54  
55      JRadioButton overlapOnButton = new JRadioButton("On");
56  
57      JRadioButton overlapOffButton = new JRadioButton("Off");
58  
59      JRadioButton removalOnButton = new JRadioButton("Allowed");
60  
61      JRadioButton removalOffButton = new JRadioButton("Not Allowed");
62  
63      MultiThumbSlider<Character> slider1 = new MultiThumbSlider<Character>(MultiThumbSlider.HORIZONTAL,
64              new float[] {0f, .5f, .75f, 1f}, new Character[] {'A', 'B', 'C', 'D'});
65  
66      MultiThumbSlider<Character> slider2 =
67              new MultiThumbSlider<Character>(MultiThumbSlider.VERTICAL, new float[] {0f, .75f}, new Character[] {'X', 'Y'});
68  
69      MultiThumbSlider<Character> slider3 = new MultiThumbSlider<Character>(MultiThumbSlider.VERTICAL,
70              new float[] {0f, .4f, .75f}, new Character[] {'X', 'Y', 'Z'});
71  
72      MultiThumbSlider<Character> slider4 =
73              new MultiThumbSlider<Character>(MultiThumbSlider.HORIZONTAL, new float[] {0f, .4f}, new Character[] {'X', 'Y'});
74  
75      MultiThumbSlider<?>[] sliders = new MultiThumbSlider<?>[] {this.slider1, this.slider2, this.slider3, this.slider4};
76  
77      public MultiThumbSliderDemo()
78      {
79          setLayout(new GridBagLayout());
80          JPanel controls = new JPanel(new GridBagLayout());
81  
82          GridBagConstraints c = new GridBagConstraints();
83          c.gridx = 0;
84          c.gridy = 0;
85          c.weightx = 1;
86          c.weighty = 0;
87          c.fill = GridBagConstraints.BOTH;
88          c.gridwidth = GridBagConstraints.REMAINDER;
89          add(controls, c);
90  
91          c.gridwidth = 1;
92          c.gridx = 1;
93          c.gridy = 1;
94          c.weightx = 1;
95          c.weighty = 0;
96          c.fill = GridBagConstraints.BOTH;
97          c.insets = new Insets(10, 10, 10, 10);
98          this.slider1.setAutoAdding(false);
99          this.slider1.setPaintTicks(true);
100         this.slider1.setThumbRemovalAllowed(false);
101         add(this.slider1, c);
102 
103         c.gridx = 0;
104         c.gridy = 2;
105         c.weightx = 0;
106         c.weighty = 1;
107         this.slider2.setAutoAdding(false);
108         this.slider2.setPaintTicks(true);
109         this.slider2.setThumbRemovalAllowed(false);
110         add(this.slider2, c);
111 
112         c.gridx = 2;
113         c.gridy = 2;
114         this.slider3.setAutoAdding(false);
115         this.slider3.setInverted(true);
116         this.slider3.setThumbRemovalAllowed(false);
117         add(this.slider3, c);
118 
119         c.gridx = 1;
120         c.gridy = 3;
121         c.weightx = 1;
122         c.weighty = 0;
123         this.slider4.setAutoAdding(false);
124         this.slider4.setInverted(true);
125         this.slider4.setThumbRemovalAllowed(false);
126         add(this.slider4, c);
127 
128         setBackground(Color.white);
129         setOpaque(true);
130 
131         c = new GridBagConstraints();
132         c.gridx = 0;
133         c.gridy = 0;
134         c.anchor = GridBagConstraints.EAST;
135         c.insets = new Insets(3, 3, 3, 3);
136         controls.add(new JLabel("Collision Policy:"), c);
137         c.gridy++;
138         controls.add(new JLabel("Thumb Shape:"), c);
139         c.gridy++;
140         controls.add(new JLabel("Thumb Overlap:"), c);
141         c.gridy++;
142         controls.add(new JLabel("Removal:"), c);
143         c.gridy++;
144         controls.add(new JLabel("UI:"), c);
145 
146         c.gridy = 0;
147         c.gridx = 1;
148         c.anchor = GridBagConstraints.WEST;
149         controls.add(this.collisionComboBox, c);
150         c.gridy++;
151         controls.add(this.thumbComboBox, c);
152         c.gridy++;
153         controls.add(wrap(this.overlapOnButton, this.overlapOffButton), c);
154         c.gridy++;
155         controls.add(wrap(this.removalOnButton, this.removalOffButton), c);
156         c.gridy++;
157         controls.add(this.uiComboBox, c);
158 
159         ButtonGroup group1 = new ButtonGroup();
160         group1.add(this.overlapOnButton);
161         group1.add(this.overlapOffButton);
162 
163         ButtonGroup group2 = new ButtonGroup();
164         group2.add(this.removalOnButton);
165         group2.add(this.removalOffButton);
166 
167         this.overlapOffButton.setSelected(true);
168         this.removalOffButton.setSelected(true);
169 
170         ActionListener overlapActionListener = new ActionListener()
171         {
172             @Override
173             public void actionPerformed(ActionEvent e)
174             {
175                 for (MultiThumbSlider<?> s : MultiThumbSliderDemo.this.sliders)
176                 {
177                     s.setThumbOverlap(MultiThumbSliderDemo.this.overlapOnButton.isSelected());
178                 }
179             }
180         };
181         this.overlapOnButton.addActionListener(overlapActionListener);
182         this.overlapOffButton.addActionListener(overlapActionListener);
183 
184         ActionListener removalActionListener = new ActionListener()
185         {
186             @Override
187             public void actionPerformed(ActionEvent e)
188             {
189                 for (MultiThumbSlider<?> s : MultiThumbSliderDemo.this.sliders)
190                 {
191                     s.setThumbRemovalAllowed(MultiThumbSliderDemo.this.removalOnButton.isSelected());
192                 }
193             }
194         };
195         this.removalOnButton.addActionListener(removalActionListener);
196         this.removalOffButton.addActionListener(removalActionListener);
197 
198         for (Collision collision : Collision.values())
199         {
200             this.collisionComboBox.addItem(collision);
201         }
202         ActionListener collisionActionListener = new ActionListener()
203         {
204             @Override
205             public void actionPerformed(ActionEvent e)
206             {
207                 for (MultiThumbSlider<?> s : MultiThumbSliderDemo.this.sliders)
208                 {
209                     s.setCollisionPolicy((Collision) MultiThumbSliderDemo.this.collisionComboBox.getSelectedItem());
210                 }
211             }
212         };
213         this.collisionComboBox.addActionListener(collisionActionListener);
214 
215         for (Thumb t : Thumb.values())
216         {
217             this.thumbComboBox.addItem(t);
218         }
219         ActionListener thumbActionListener = new ActionListener()
220         {
221             @Override
222             public void actionPerformed(ActionEvent e)
223             {
224                 for (MultiThumbSlider<?> s : MultiThumbSliderDemo.this.sliders)
225                 {
226                     s.putClientProperty(MultiThumbSliderUi.THUMB_SHAPE_PROPERTY,
227                             (Thumb) MultiThumbSliderDemo.this.thumbComboBox.getSelectedItem());
228                 }
229             }
230         };
231         this.thumbComboBox.addActionListener(thumbActionListener);
232 
233         thumbActionListener.actionPerformed(null);
234         collisionActionListener.actionPerformed(null);
235         overlapActionListener.actionPerformed(null);
236         removalActionListener.actionPerformed(null);
237         controls.setOpaque(false);
238 
239         // optional console output
240         for (MultiThumbSlider<?> s : this.sliders)
241         {
242             s.addChangeListener(new ChangeListener()
243             {
244                 @Override
245                 public void stateChanged(ChangeEvent e)
246                 {
247                     MultiThumbSlider<?> s = (MultiThumbSlider<?>) e.getSource();
248                     float[] pos = s.getThumbPositions();
249                     Object[] values = s.getValues();
250                     for (int a = 0; a < pos.length; a++)
251                     {
252                         System.out.println("thumb[" + a + "]: " + pos[a] + " -> " + values[a]);
253                     }
254                 }
255             });
256         }
257 
258         this.uiComboBox.addActionListener(new ActionListener()
259         {
260             @Override
261             public void actionPerformed(ActionEvent e)
262             {
263                 try
264                 {
265                     for (MultiThumbSlider<?> s : MultiThumbSliderDemo.this.sliders)
266                     {
267                         Class t = (Class) MultiThumbSliderDemo.this.uiComboBox.getSelectedItem();
268                         Constructor constructor = t.getConstructor(new Class[] {MultiThumbSlider.class});
269                         s.setUI((MultiThumbSliderUi) constructor.newInstance(new Object[] {s}));
270                     }
271                     Window w = SwingUtilities.getWindowAncestor(MultiThumbSliderDemo.this);
272                     if (w != null)
273                         w.pack();
274                 }
275                 catch (Throwable t)
276                 {
277                     t.printStackTrace();
278                 }
279             }
280         });
281         this.uiComboBox.addItem(AquaMultiThumbSliderUi.class);
282         this.uiComboBox.addItem(DefaultMultiThumbSliderUi.class);
283         this.uiComboBox.addItem(VistaMultiThumbSliderUI.class);
284 
285     }
286 
287     private JPanel wrap(JComponent... list)
288     {
289         JPanel p = new JPanel(new FlowLayout());
290         p.setOpaque(false);
291         for (JComponent c : list)
292         {
293             p.add(c);
294         }
295         return p;
296     }
297 }