KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > util > ui > JRangeSlider


1 package prefuse.util.ui;
2
3 import java.awt.BasicStroke JavaDoc;
4 import java.awt.Color JavaDoc;
5 import java.awt.Cursor JavaDoc;
6 import java.awt.Dimension JavaDoc;
7 import java.awt.Graphics JavaDoc;
8 import java.awt.Graphics2D JavaDoc;
9 import java.awt.Rectangle JavaDoc;
10 import java.awt.event.KeyEvent JavaDoc;
11 import java.awt.event.KeyListener JavaDoc;
12 import java.awt.event.MouseEvent JavaDoc;
13 import java.awt.event.MouseListener JavaDoc;
14 import java.awt.event.MouseMotionListener JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Iterator JavaDoc;
17
18 import javax.swing.BoundedRangeModel JavaDoc;
19 import javax.swing.DefaultBoundedRangeModel JavaDoc;
20 import javax.swing.JComponent JavaDoc;
21 import javax.swing.event.ChangeEvent JavaDoc;
22 import javax.swing.event.ChangeListener JavaDoc;
23
24 /**
25  * <p>Implements a Swing-based Range slider, which allows the user to enter a
26  * range (minimum and maximum) value.</p>
27  *
28  * @author Ben Bederson
29  * @author Jesse Grosjean
30  * @author Jon Meyer
31  * @author Lance Good
32  * @author jeffrey heer
33  */

34 public class JRangeSlider extends JComponent JavaDoc
35     implements MouseListener JavaDoc, MouseMotionListener JavaDoc, KeyListener JavaDoc
36 {
37     /*
38      * NOTE: This is a modified version of the original class distributed by
39      * Ben Bederson, Jesse Grosjean, and Jon Meyer as part of an HCIL Tech
40      * Report. It is modified to allow both vertical and horitonal modes.
41      * It also fixes a bug with offset on the buttons. Also fixed a bug with
42      * rendering using (x,y) instead of (0,0) as origin. Also modified to
43      * render arrows as a series of lines rather than as a GeneralPath.
44      * Also modified to fix rounding errors on toLocal and toScreen.
45      *
46      * With inclusion in prefuse, this class has been further modified to use a
47      * bounded range model, support keyboard commands and more extensize
48      * parameterization of rendering/appearance options. Furthermore, a stub
49      * method has been introduced to allow subclasses to perform custom
50      * rendering within the slider through.
51      */

52     
53     final public static int VERTICAL = 0;
54     final public static int HORIZONTAL = 1;
55     final public static int LEFTRIGHT_TOPBOTTOM = 0;
56     final public static int RIGHTLEFT_BOTTOMTOP = 1;
57     
58     final public static int PREFERRED_BREADTH = 16;
59     final public static int PREFERRED_LENGTH = 300;
60     final protected static int ARROW_SZ = 16;
61     final protected static int ARROW_WIDTH = 8;
62     final protected static int ARROW_HEIGHT = 4;
63
64     protected BoundedRangeModel JavaDoc model;
65     protected int orientation;
66     protected int direction;
67     protected boolean empty;
68     protected int increment = 1;
69     protected int minExtent = 0; // min extent, in pixels
70

71     protected ArrayList JavaDoc listeners = new ArrayList JavaDoc();
72     protected ChangeEvent JavaDoc changeEvent = null;
73     protected ChangeListener JavaDoc lstnr;
74  
75     protected Color JavaDoc thumbColor = new Color JavaDoc(150,180,220);
76     
77     // ------------------------------------------------------------------------
78

79     /**
80      * Create a new range slider.
81      *
82      * @param minimum - the minimum value of the range.
83      * @param maximum - the maximum value of the range.
84      * @param lowValue - the current low value shown by the range slider's bar.
85      * @param highValue - the current high value shown by the range slider's bar.
86      * @param orientation - construct a horizontal or vertical slider?
87      */

88     public JRangeSlider(int minimum, int maximum, int lowValue, int highValue, int orientation) {
89         this(new DefaultBoundedRangeModel JavaDoc(lowValue,highValue,minimum,maximum),
90                 orientation,LEFTRIGHT_TOPBOTTOM);
91     }
92
93     /**
94      * Create a new range slider.
95      *
96      * @param minimum - the minimum value of the range.
97      * @param maximum - the maximum value of the range.
98      * @param lowValue - the current low value shown by the range slider's bar.
99      * @param highValue - the current high value shown by the range slider's bar.
100      * @param orientation - construct a horizontal or vertical slider?
101      * @param direction - Is the slider left-to-right/top-to-bottom or right-to-left/bottom-to-top
102      */

103     public JRangeSlider(int minimum, int maximum, int lowValue, int highValue, int orientation, int direction) {
104         this(new DefaultBoundedRangeModel JavaDoc(lowValue, highValue, minimum, maximum),
105                 orientation, direction);
106     }
107     
108     /**
109      * Create a new range slider.
110      *
111      * @param model - a BoundedRangeModel specifying the slider's range
112      * @param orientation - construct a horizontal or vertical slider?
113      * @param direction - Is the slider left-to-right/top-to-bottom or right-to-left/bottom-to-top
114      */

115     public JRangeSlider(BoundedRangeModel JavaDoc model, int orientation, int direction) {
116         super.setFocusable(true);
117         this.model = model;
118         this.orientation = orientation;
119         this.direction = direction;
120         
121         setForeground(Color.LIGHT_GRAY);
122         
123         this.lstnr = createListener();
124         model.addChangeListener(lstnr);
125         
126         addMouseListener(this);
127         addMouseMotionListener(this);
128         addKeyListener(this);
129     }
130     
131     /**
132      * Create a listener to relay change events from the bounded range model.
133      * @return a ChangeListener to relay events from the range model
134      */

135     protected ChangeListener JavaDoc createListener() {
136         return new RangeSliderChangeListener();
137     }
138     
139     /**
140      * Listener that fires a change event when it receives change event from
141      * the slider list model.
142      */

143     protected class RangeSliderChangeListener implements ChangeListener JavaDoc {
144         public void stateChanged(ChangeEvent JavaDoc e) {
145             fireChangeEvent();
146         }
147     }
148     
149     /**
150      * Returns the current "low" value shown by the range slider's bar. The low
151      * value meets the constraint minimum <= lowValue <= highValue <= maximum.
152      */

153     public int getLowValue() {
154         return model.getValue();
155     }
156
157     /**
158      * Sets the low value shown by this range slider. This causes the range slider to be
159      * repainted and a ChangeEvent to be fired.
160      * @param lowValue the low value to use
161      */

162     public void setLowValue(int lowValue) {
163         int e = (model.getValue()-lowValue)+model.getExtent();
164         model.setRangeProperties(lowValue, e,
165             model.getMinimum(), model.getMaximum(), false);
166         model.setValue(lowValue);
167     }
168
169     /**
170      * Returns the current "high" value shown by the range slider's bar. The high
171      * value meets the constraint minimum <= lowValue <= highValue <= maximum.
172      */

173     public int getHighValue() {
174         return model.getValue()+model.getExtent();
175     }
176
177     /**
178      * Sets the high value shown by this range slider. This causes the range slider to be
179      * repainted and a ChangeEvent to be fired.
180      * @param highValue the high value to use
181      */

182     public void setHighValue(int highValue) {
183         model.setExtent(highValue-model.getValue());
184     }
185     
186     /**
187      * Set the slider range span.
188      * @param lowValue the low value of the slider range
189      * @param highValue the high value of the slider range
190      */

191     public void setRange(int lowValue, int highValue) {
192         model.setRangeProperties(lowValue, highValue-lowValue,
193                 model.getMinimum(), model.getMaximum(), false);
194     }
195
196     /**
197      * Gets the minimum possible value for either the low value or the high value.
198      * @return the minimum possible range value
199      */

200     public int getMinimum() {
201         return model.getMinimum();
202     }
203
204     /**
205      * Sets the minimum possible value for either the low value or the high value.
206      * @param minimum the minimum possible range value
207      */

208     public void setMinimum(int minimum) {
209         model.setMinimum(minimum);
210     }
211
212     /**
213      * Gets the maximum possible value for either the low value or the high value.
214      * @return the maximum possible range value
215      */

216     public int getMaximum() {
217         return model.getMaximum();
218     }
219
220     /**
221      * Sets the maximum possible value for either the low value or the high value.
222      * @param maximum the maximum possible range value
223      */

224     public void setMaximum(int maximum) {
225         model.setMaximum(maximum);
226     }
227
228     /**
229      * Sets the minimum extent (difference between low and high values).
230      * This method <strong>does not</strong> change the current state of the
231      * model, but can affect all subsequent interaction.
232      * @param minExtent the minimum extent allowed in subsequent interaction
233      */

234     public void setMinExtent(int minExtent) {
235         this.minExtent = minExtent;
236     }
237     
238     /**
239      * Sets whether this slider is empty.
240      * @param empty true if set to empty, false otherwise
241      */

242     public void setEmpty(boolean empty) {
243         this.empty = empty;
244         repaint();
245     }
246
247     /**
248      * Get the slider thumb color. This is the part of the slider between
249      * the range resize buttons.
250      * @return the slider thumb color
251      */

252     public Color JavaDoc getThumbColor() {
253         return thumbColor;
254     }
255     
256     /**
257      * Set the slider thumb color. This is the part of the slider between
258      * the range resize buttons.
259      * @param thumbColor the slider thumb color
260      */

261     public void setThumbColor(Color JavaDoc thumbColor) {
262         this.thumbColor = thumbColor;
263     }
264     
265     /**
266      * Get the BoundedRangeModel backing this slider.
267      * @return the slider's range model
268      */

269     public BoundedRangeModel JavaDoc getModel() {
270         return model;
271     }
272     
273     /**
274      * Set the BoundedRangeModel backing this slider.
275      * @param brm the slider range model to use
276      */

277     public void setModel(BoundedRangeModel JavaDoc brm) {
278         model.removeChangeListener(lstnr);
279         model = brm;
280         model.addChangeListener(lstnr);
281         repaint();
282     }
283     
284     /**
285      * Registers a listener for ChangeEvents.
286      * @param cl the ChangeListener to add
287      */

288     public void addChangeListener(ChangeListener JavaDoc cl) {
289         if ( !listeners.contains(cl) )
290             listeners.add(cl);
291     }
292
293     /**
294      * Removes a listener for ChangeEvents.
295      * @param cl the ChangeListener to remove
296      */

297     public void removeChangeListener(ChangeListener JavaDoc cl) {
298         listeners.remove(cl);
299     }
300     
301     /**
302      * Fire a change event to all listeners.
303      */

304     protected void fireChangeEvent() {
305         repaint();
306         if ( changeEvent == null )
307             changeEvent = new ChangeEvent JavaDoc(this);
308         Iterator JavaDoc iter = listeners.iterator();
309         while ( iter.hasNext() )
310             ((ChangeListener JavaDoc)iter.next()).stateChanged(changeEvent);
311     }
312
313     /**
314      * @see java.awt.Component#getPreferredSize()
315      */

316     public Dimension JavaDoc getPreferredSize() {
317         if (orientation == VERTICAL) {
318             return new Dimension JavaDoc(PREFERRED_BREADTH, PREFERRED_LENGTH);
319         }
320         else {
321             return new Dimension JavaDoc(PREFERRED_LENGTH, PREFERRED_BREADTH);
322         }
323     }
324
325     // ------------------------------------------------------------------------
326
// Rendering
327

328     /**
329      * Override this method to perform custom painting of the slider trough.
330      * @param g a Graphics2D context for rendering
331      * @param width the width of the slider trough
332      * @param height the height of the slider trough
333      */

334     protected void customPaint(Graphics2D JavaDoc g, int width, int height) {
335         // does nothing in this class
336
// subclasses can override to perform custom painting
337
}
338     
339     /**
340      * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
341      */

342     public void paintComponent(Graphics JavaDoc g) {
343         Rectangle JavaDoc bounds = getBounds();
344         int width = (int)bounds.getWidth() - 1;
345         int height = (int)bounds.getHeight() - 1;
346
347         int min = toScreen(getLowValue());
348         int max = toScreen(getHighValue());
349
350         // Paint the full slider if the slider is marked as empty
351
if (empty) {
352             if (direction == LEFTRIGHT_TOPBOTTOM) {
353                 min = ARROW_SZ;
354                 max = (orientation == VERTICAL) ? height-ARROW_SZ : width-ARROW_SZ;
355             }
356             else {
357                 min = (orientation == VERTICAL) ? height-ARROW_SZ : width-ARROW_SZ;
358                 max = ARROW_SZ;
359             }
360         }
361
362         Graphics2D JavaDoc g2 = (Graphics2D JavaDoc)g;
363         g2.setColor(getBackground());
364         g2.fillRect(0, 0, width, height);
365         g2.setColor(getForeground());
366         g2.drawRect(0, 0, width, height);
367
368         customPaint(g2, width, height);
369         
370         // Draw arrow and thumb backgrounds
371
g2.setStroke(new BasicStroke JavaDoc(1));
372         if (orientation == VERTICAL) {
373             if (direction == LEFTRIGHT_TOPBOTTOM) {
374                 g2.setColor(getForeground());
375                 g2.fillRect(0, min - ARROW_SZ, width, ARROW_SZ-1);
376                 paint3DRectLighting(g2,0,min-ARROW_SZ,width,ARROW_SZ-1);
377             
378                 if ( thumbColor != null ) {
379                     g2.setColor(thumbColor);
380                     g2.fillRect(0, min, width, max - min-1);
381                     paint3DRectLighting(g2,0,min,width,max-min-1);
382                 }
383                 
384                 g2.setColor(getForeground());
385                 g2.fillRect(0, max, width, ARROW_SZ-1);
386                 paint3DRectLighting(g2,0,max,width,ARROW_SZ-1);
387             
388                 // Draw arrows
389
g2.setColor(Color.black);
390                 paintArrow(g2, (width-ARROW_WIDTH) / 2.0, min - ARROW_SZ + (ARROW_SZ-ARROW_HEIGHT) / 2.0, ARROW_WIDTH, ARROW_HEIGHT, true);
391                 paintArrow(g2, (width-ARROW_WIDTH) / 2.0, max + (ARROW_SZ-ARROW_HEIGHT) / 2.0, ARROW_WIDTH, ARROW_HEIGHT, false);
392             }
393             else {
394                 g2.setColor(getForeground());
395                 g2.fillRect(0, min, width, ARROW_SZ-1);
396                 paint3DRectLighting(g2,0,min,width,ARROW_SZ-1);
397             
398                 if ( thumbColor != null ) {
399                     g2.setColor(thumbColor);
400                     g2.fillRect(0, max, width, min-max-1);
401                     paint3DRectLighting(g2,0,max,width,min-max-1);
402                 }
403             
404                 g2.setColor(getForeground());
405                 g2.fillRect(0, max-ARROW_SZ, width, ARROW_SZ-1);
406                 paint3DRectLighting(g2,0,max-ARROW_SZ,width,ARROW_SZ-1);
407             
408                 // Draw arrows
409
g2.setColor(Color.black);
410                 paintArrow(g2, (width-ARROW_WIDTH) / 2.0, min + (ARROW_SZ-ARROW_HEIGHT) / 2.0, ARROW_WIDTH, ARROW_HEIGHT, false);
411                 paintArrow(g2, (width-ARROW_WIDTH) / 2.0, max - ARROW_SZ + (ARROW_SZ-ARROW_HEIGHT) / 2.0, ARROW_WIDTH, ARROW_HEIGHT, true);
412             }
413         }
414         else {
415             if (direction == LEFTRIGHT_TOPBOTTOM) {
416                 g2.setColor(getForeground());
417                 g2.fillRect(min - ARROW_SZ, 0, ARROW_SZ-1, height);
418                 paint3DRectLighting(g2,min-ARROW_SZ,0,ARROW_SZ-1,height);
419             
420                 if ( thumbColor != null ) {
421                     g2.setColor(thumbColor);
422                     g2.fillRect(min, 0, max - min - 1, height);
423                     paint3DRectLighting(g2,min,0,max-min-1,height);
424                 }
425
426                 g2.setColor(getForeground());
427                 g2.fillRect(max, 0, ARROW_SZ-1, height);
428                 paint3DRectLighting(g2,max,0,ARROW_SZ-1,height);
429             
430                 // Draw arrows
431
g2.setColor(Color.black);
432                 paintArrow(g2, min - ARROW_SZ + (ARROW_SZ-ARROW_HEIGHT) / 2.0, (height-ARROW_WIDTH) / 2.0, ARROW_HEIGHT, ARROW_WIDTH, true);
433                 paintArrow(g2, max + (ARROW_SZ-ARROW_HEIGHT) / 2.0, (height-ARROW_WIDTH) / 2.0, ARROW_HEIGHT, ARROW_WIDTH, false);
434             }
435             else {
436                 g2.setColor(getForeground());
437                 g2.fillRect(min, 0, ARROW_SZ - 1, height);
438                 paint3DRectLighting(g2,min,0,ARROW_SZ-1,height);
439                 
440                 if ( thumbColor != null ) {
441                     g2.setColor(thumbColor);
442                     g2.fillRect(max, 0, min - max - 1, height);
443                     paint3DRectLighting(g2,max,0,min-max-1,height);
444                 }
445                 
446                 g2.setColor(getForeground());
447                 g2.fillRect(max-ARROW_SZ, 0, ARROW_SZ-1, height);
448                 paint3DRectLighting(g2,max-ARROW_SZ,0,ARROW_SZ-1,height);
449             
450                 // Draw arrows
451
g2.setColor(Color.black);
452                 paintArrow(g2, min + (ARROW_SZ-ARROW_HEIGHT) / 2.0, (height-ARROW_WIDTH) / 2.0, ARROW_HEIGHT, ARROW_WIDTH, true);
453                 paintArrow(g2, max - ARROW_SZ + (ARROW_SZ-ARROW_HEIGHT) / 2.0, (height-ARROW_WIDTH) / 2.0, ARROW_HEIGHT, ARROW_WIDTH, false);
454             }
455         }
456     }
457
458     /**
459      * This draws an arrow as a series of lines within the specified box.
460      * The last boolean specifies whether the point should be at the
461      * right/bottom or left/top.
462      */

463     protected void paintArrow(Graphics2D JavaDoc g2, double x, double y, int w, int h,
464                               boolean topDown)
465     {
466         int intX = (int)(x+0.5);
467         int intY = (int)(y+0.5);
468         
469         if (orientation == VERTICAL) {
470             if (w % 2 == 0) {
471                 w = w - 1;
472             }
473             
474             if (topDown) {
475                 for(int i=0; i<(w/2+1); i++) {
476                     g2.drawLine(intX+i,intY+i,intX+w-i-1,intY+i);
477                 }
478             }
479             else {
480                 for(int i=0; i<(w/2+1); i++) {
481                     g2.drawLine(intX+w/2-i,intY+i,intX+w-w/2+i-1,intY+i);
482                 }
483             }
484         }
485         else {
486             if (h % 2 == 0) {
487                 h = h - 1;
488             }
489                         
490             if (topDown) {
491                 for(int i=0; i<(h/2+1); i++) {
492                     g2.drawLine(intX+i,intY+i,intX+i,intY+h-i-1);
493                 }
494             }
495             else {
496                 for(int i=0; i<(h/2+1); i++) {
497                     g2.drawLine(intX+i,intY+h/2-i,intX+i,intY+h-h/2+i-1);
498                 }
499             }
500         }
501     }
502     
503     /**
504      * Adds Windows2K type 3D lighting effects
505      */

506     protected void paint3DRectLighting(Graphics2D JavaDoc g2, int x, int y,
507                                        int width, int height)
508     {
509         g2.setColor(Color.white);
510         g2.drawLine(x+1,y+1,x+1,y+height-1);
511         g2.drawLine(x+1,y+1,x+width-1,y+1);
512         g2.setColor(Color.gray);
513         g2.drawLine(x+1,y+height-1,x+width-1,y+height-1);
514         g2.drawLine(x+width-1,y+1,x+width-1,y+height-1);
515         g2.setColor(Color.darkGray);
516         g2.drawLine(x,y+height,x+width,y+height);
517         g2.drawLine(x+width,y,x+width,y+height);
518     }
519
520     /**
521      * Converts from screen coordinates to a range value.
522      */

523     protected int toLocal(int xOrY) {
524         Dimension JavaDoc sz = getSize();
525         int min = getMinimum();
526         double scale;
527         if (orientation == VERTICAL) {
528             scale = (sz.height - (2 * ARROW_SZ)) / (double) (getMaximum() - min);
529         }
530         else {
531             scale = (sz.width - (2 * ARROW_SZ)) / (double) (getMaximum() - min);
532         }
533
534         if (direction == LEFTRIGHT_TOPBOTTOM) {
535             return (int) (((xOrY - ARROW_SZ) / scale) + min + 0.5);
536         }
537         else {
538             if (orientation == VERTICAL) {
539                 return (int) ((sz.height - xOrY - ARROW_SZ) / scale + min + 0.5);
540             }
541             else {
542                 return (int) ((sz.width - xOrY - ARROW_SZ) / scale + min + 0.5);
543             }
544         }
545     }
546
547     /**
548      * Converts from a range value to screen coordinates.
549      */

550     protected int toScreen(int xOrY) {
551         Dimension JavaDoc sz = getSize();
552         int min = getMinimum();
553         double scale;
554         if (orientation == VERTICAL) {
555             scale = (sz.height - (2 * ARROW_SZ)) / (double) (getMaximum() - min);
556         }
557         else {
558             scale = (sz.width - (2 * ARROW_SZ)) / (double) (getMaximum() - min);
559         }
560
561         // If the direction is left/right_top/bottom then we subtract the min and multiply times scale
562
// Otherwise, we have to invert the number by subtracting the value from the height
563
if (direction == LEFTRIGHT_TOPBOTTOM) {
564             return (int)(ARROW_SZ + ((xOrY - min) * scale) + 0.5);
565         }
566         else {
567             if (orientation == VERTICAL) {
568                 return (int)(sz.height-(xOrY - min) * scale - ARROW_SZ + 0.5);
569             }
570             else {
571                 return (int)(sz.width-(xOrY - min) * scale - ARROW_SZ + 0.5);
572             }
573         }
574     }
575
576     /**
577      * Converts from a range value to screen coordinates.
578      */

579     protected double toScreenDouble(int xOrY) {
580         Dimension JavaDoc sz = getSize();
581         int min = getMinimum();
582         double scale;
583         if (orientation == VERTICAL) {
584             scale = (sz.height - (2 * ARROW_SZ)) / (double) (getMaximum()+1 - min);
585         }
586         else {
587             scale = (sz.width - (2 * ARROW_SZ)) / (double) (getMaximum()+1 - min);
588         }
589
590         // If the direction is left/right_top/bottom then we subtract the min and multiply times scale
591
// Otherwise, we have to invert the number by subtracting the value from the height
592
if (direction == LEFTRIGHT_TOPBOTTOM) {
593             return ARROW_SZ + ((xOrY - min) * scale);
594         }
595         else {
596             if (orientation == VERTICAL) {
597                 return sz.height-(xOrY - min) * scale - ARROW_SZ;
598             }
599             else {
600                 return sz.width-(xOrY - min) * scale - ARROW_SZ;
601             }
602         }
603     }
604
605     
606     // ------------------------------------------------------------------------
607
// Event Handling
608

609     static final int PICK_NONE = 0;
610     static final int PICK_LEFT_OR_TOP = 1;
611     static final int PICK_THUMB = 2;
612     static final int PICK_RIGHT_OR_BOTTOM = 3;
613     int pick;
614     int pickOffsetLow;
615     int pickOffsetHigh;
616     int mouse;
617
618     private int pickHandle(int xOrY) {
619         int min = toScreen(getLowValue());
620         int max = toScreen(getHighValue());
621         int pick = PICK_NONE;
622         
623         if (direction == LEFTRIGHT_TOPBOTTOM) {
624             if ((xOrY > (min - ARROW_SZ)) && (xOrY < min)) {
625                 pick = PICK_LEFT_OR_TOP;
626             } else if ((xOrY >= min) && (xOrY <= max)) {
627                 pick = PICK_THUMB;
628             } else if ((xOrY > max) && (xOrY < (max + ARROW_SZ))) {
629                 pick = PICK_RIGHT_OR_BOTTOM;
630             }
631         }
632         else {
633             if ((xOrY > min) && (xOrY < (min + ARROW_SZ))) {
634                 pick = PICK_LEFT_OR_TOP;
635             } else if ((xOrY <= min) && (xOrY >= max)) {
636                 pick = PICK_THUMB;
637             } else if ((xOrY > (max - ARROW_SZ) && (xOrY < max))) {
638                 pick = PICK_RIGHT_OR_BOTTOM;
639             }
640         }
641         
642         return pick;
643     }
644
645     private void offset(int dxOrDy) {
646         model.setValue(model.getValue()+dxOrDy);
647     }
648
649     /**
650      * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
651      */

652     public void mousePressed(MouseEvent JavaDoc e) {
653         if (orientation == VERTICAL) {
654             pick = pickHandle(e.getY());
655             pickOffsetLow = e.getY() - toScreen(getLowValue());
656             pickOffsetHigh = e.getY() - toScreen(getHighValue());
657             mouse = e.getY();
658         }
659         else {
660             pick = pickHandle(e.getX());
661             pickOffsetLow = e.getX() - toScreen(getLowValue());
662             pickOffsetHigh = e.getX() - toScreen(getHighValue());
663             mouse = e.getX();
664         }
665         repaint();
666     }
667
668     /**
669      * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
670      */

671     public void mouseDragged(MouseEvent JavaDoc e) {
672         requestFocus();
673         int value = (orientation == VERTICAL) ? e.getY() : e.getX();
674         
675         int minimum = getMinimum();
676         int maximum = getMaximum();
677         int lowValue = getLowValue();
678         int highValue = getHighValue();
679         
680         switch (pick) {
681             case PICK_LEFT_OR_TOP:
682                 int low = toLocal(value-pickOffsetLow);
683             
684                 if (low < minimum) {
685                     low = minimum;
686                 }
687                 if (low > maximum) {
688                     low = maximum;
689                 }
690                 if (low > highValue-minExtent) {
691                     low = highValue-minExtent;
692                 }
693                 setLowValue(low);
694                 break;
695
696             case PICK_RIGHT_OR_BOTTOM:
697                 int high = toLocal(value-pickOffsetHigh);
698                 
699                 if (high < minimum) {
700                     high = minimum;
701                 }
702                 if (high > maximum) {
703                     high = maximum;
704                 }
705                 if (high < lowValue+minExtent) {
706                     high = lowValue+minExtent;
707                 }
708                 setHighValue(high);
709                 break;
710
711             case PICK_THUMB:
712                 int dxOrDy = toLocal(value - pickOffsetLow) - lowValue;
713                 if ((dxOrDy < 0) && ((lowValue + dxOrDy) < minimum)) {
714                     dxOrDy = minimum - lowValue;
715                 }
716                 if ((dxOrDy > 0) && ((highValue + dxOrDy) > maximum)) {
717                     dxOrDy = maximum - highValue;
718                 }
719                 if (dxOrDy != 0) {
720                     offset(dxOrDy);
721                 }
722                 break;
723         }
724     }
725
726     /**
727      * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
728      */

729     public void mouseReleased(MouseEvent JavaDoc e) {
730         pick = PICK_NONE;
731         repaint();
732     }
733
734     /**
735      * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
736      */

737     public void mouseMoved(MouseEvent JavaDoc e) {
738         if (orientation == VERTICAL) {
739             switch (pickHandle(e.getY())) {
740                 case PICK_LEFT_OR_TOP:
741                     setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
742                     break;
743                 case PICK_RIGHT_OR_BOTTOM:
744                     setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
745                     break;
746                 case PICK_THUMB:
747                     setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
748                     break;
749                 case PICK_NONE :
750                     setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
751                     break;
752             }
753         }
754         else {
755             switch (pickHandle(e.getX())) {
756                 case PICK_LEFT_OR_TOP:
757                     setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
758                     break;
759                 case PICK_RIGHT_OR_BOTTOM:
760                     setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
761                     break;
762                 case PICK_THUMB:
763                     setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
764                     break;
765                 case PICK_NONE :
766                     setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
767                     break;
768             }
769         }
770     }
771
772     /**
773      * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
774      */

775     public void mouseClicked(MouseEvent JavaDoc e) {
776     }
777     /**
778      * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
779      */

780     public void mouseEntered(MouseEvent JavaDoc e) {
781     }
782     /**
783      * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
784      */

785     public void mouseExited(MouseEvent JavaDoc e) {
786     }
787
788     private void grow(int increment) {
789         model.setRangeProperties(model.getValue()-increment,
790             model.getExtent()+2*increment,
791             model.getMinimum(), model.getMaximum(), false);
792     }
793     
794     /**
795      * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
796      */

797     public void keyPressed(KeyEvent JavaDoc e) {
798         int kc = e.getKeyCode();
799         boolean v = (orientation == VERTICAL);
800         boolean d = (kc == KeyEvent.VK_DOWN);
801         boolean u = (kc == KeyEvent.VK_UP);
802         boolean l = (kc == KeyEvent.VK_LEFT);
803         boolean r = (kc == KeyEvent.VK_RIGHT);
804         
805         int minimum = getMinimum();
806         int maximum = getMaximum();
807         int lowValue = getLowValue();
808         int highValue = getHighValue();
809         
810         if ( v&&r || !v&&u ) {
811             if ( lowValue-increment >= minimum &&
812                  highValue+increment <= maximum ) {
813                 grow(increment);
814             }
815         } else if ( v&&l || !v&&d ) {
816             if ( highValue-lowValue >= 2*increment ) {
817                 grow(-1*increment);
818             }
819         } else if ( v&&d || !v&&l ) {
820             if ( lowValue-increment >= minimum ) {
821                 offset(-increment);
822             }
823         } else if ( v&&u || !v&&r ) {
824             if ( highValue+increment <= maximum ) {
825                 offset(increment);
826             }
827         }
828     }
829     
830     /**
831      * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
832      */

833     public void keyReleased(KeyEvent JavaDoc e) {
834     }
835     /**
836      * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
837      */

838     public void keyTyped(KeyEvent JavaDoc e) {
839     }
840     
841 } // end of class JRangeSlider
842
Popular Tags