-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathCHoverScrollPane.java
More file actions
674 lines (630 loc) · 23.7 KB
/
Copy pathCHoverScrollPane.java
File metadata and controls
674 lines (630 loc) · 23.7 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the MIT License https://raw.github.com/mit-cml/app-inventor/master/mitlicense.txt
package openblocks.codeblockutil;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.BoundedRangeModel;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.ScrollPaneConstants;
import openblocks.codeblockutil.CScrollPane.ScrollPolicy;
/**
* The CHoverScrollPane is a swing-compatible widget that
* allows clients of this CScrollPane to control the
* width of the thumb, the color of the thumb, and
* and the color of the track. Like all swing-compatible
* scroll panes, the CScrollPane wraps a viewport and must
* change the viewing space (also known as the visible rectangle)
* of the scroll pane when users attempts to scroll
* with the mouse, wheel, or key board.
*
* This scroll pane is unique in that it hover INSIDE the viewing
* space rather than exist outside the viewing space. By default,
* the track is transparent to help users see parts of the viewport
* covered by the track.
*/
public class CHoverScrollPane extends CScrollPane implements KeyListener{
private static final long serialVersionUID = 328149080214L;
private static Color defaultTrackColor = new Color(50,50,50,175);
private int SCROLLINGUNIT =3;
private HoverVerticalBar verticalbar;
private HoverHorizontalBar horizontalbar;
private JScrollPane scrollviewport;
private ScrollPolicy vpolicy;
private ScrollPolicy hpolicy;
private int thumbWidth;
/**
* Constructs a custom CHoverScrollPane with the view port set to "view",
* with both scroll bar policies set to "ALWAYS" (see
* javax.swing.JScrollPane for a description on the use of
* scroll bar policies). Thumb will have girth of 10 and an interior
* color of black, hovering above a grayed-out transparant background.
*
* @param view
*
* @requires view != null
* @effects constructs a CScrollPane as described in method overview
*/
public CHoverScrollPane(JComponent view){
this(view, ScrollPolicy.VERTICAL_BAR_ALWAYS,
ScrollPolicy.HORIZONTAL_BAR_ALWAYS);
}
/**
* Constructs a custom CHoverScrollPane with the view port set to "view",
* and correponding vertical and horizontal bar policies (see
* javax.swing.JScrollPane for a description on the use of
* scroll bar policies). Thumb will have girth of 10 and an interior
* color of black, hovering above a grayed-out transparent background.
*
* @param view
* @param verticalPolicy
* @param horizontalPolicy
*
* @requires view != null
* @effects constructs a CScrollPane as described in method overview
*/
public CHoverScrollPane(JComponent view,ScrollPolicy verticalPolicy,ScrollPolicy horizontalPolicy){
this(view, verticalPolicy,
horizontalPolicy,
10, Color.darkGray, null);
}
/**
* Constructs a custom CHoverScrollPane with the view port set to "view",
* with both scroll bar policies set to "ALWAYS" (see
* javax.swing.JScrollPane for a description on the use of
* scroll bar policies). Thumb will have girth of equal to
* thumbWidth and an interior color equal to thumbColor.
*
* @param view
*
* @requires view != null
* @effects Constructs a CScrollPane as described in method overview.
* If thumbColor is null, then the deafault Color.black value
* will be used. If trackColor is null, then the
* default Color.white value will be used.
*/
public CHoverScrollPane(JComponent view,int thumbWidth,Color thumbColor,Color trackColor){
this(view, ScrollPolicy.VERTICAL_BAR_ALWAYS,
ScrollPolicy.HORIZONTAL_BAR_ALWAYS,
thumbWidth, thumbColor, trackColor);
}
/**
* Constructs a custom CHoverScrollPane with the view port set to "view",
* with correponding vertical and horizontal bar policies (see
* javax.swing.JScrollPane for a description on the use of
* scroll bar policies). The thumb will have a girth equal to
* "thumbWidth" and an interior color of thumbColor. The background
* underneath the thumb will have a color equal to thumbBackground.
*
* @param view - the viewport
* @param verticalPolicy - the vertical scroll bar policy
* @param horizontalPolicy - the horizontal scroll bar policy
* @param thumbWidth - the width of the vertical scroll bar in pixels and
* the height of the horiztontal scroll bar in pixels
* @param thumbColor - the interior color of the thumb
* @param trackColor - the backgorund color under the thumb
*
* @requires view != null
* @effects Creates a JScrollPane that displays the view component
* in a viewport whose view position can be controlled with
* a pair of scrollbars.
* -If the scrollbar policies are null, then it will use the default
* "ALWAYS" policy. That is, the scroll bars will always show.
* -If the thumbWidth is null or less than 0, then the scroll bars
* will not show.
* -If thumbColor is null, then thumbs will dedault on Color.black.
* -If trackColor is null, then the default grayed-out transparent color
* will be used as the background color.
*/
public CHoverScrollPane(
JComponent view,
ScrollPolicy verticalPolicy,
ScrollPolicy horizontalPolicy,
int thumbWidth,
Color thumbColor,
Color trackColor){
//////////////////////////////////////
// INITIALIZE COMPONENTS
super();
this.setLayout(null);
this.vpolicy = verticalPolicy;
this.hpolicy = horizontalPolicy;
this.thumbWidth = thumbWidth;
this.setOpaque(true);
scrollviewport = new JScrollPane(view,
ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER){
private static final long serialVersionUID = 328149080215L;
protected boolean processKeyBinding(KeyStroke ks, KeyEvent e,
int condition, boolean pressed){
switch(e.getKeyCode()){
case KeyEvent.VK_UP:
return false;
case KeyEvent.VK_DOWN:
return false;
case KeyEvent.VK_LEFT:
return false;
case KeyEvent.VK_RIGHT:
return false;
case KeyEvent.VK_TAB:
System.out.println("bl.enter tab");
return false;
default:
return super.processKeyBinding(ks, e, condition, pressed);
}
}
};
scrollviewport.setBorder(null);
scrollviewport.setWheelScrollingEnabled(true);
if(thumbWidth<0) thumbWidth = 0;
if (thumbColor == null ) thumbColor = Color.black;
if(trackColor == null) trackColor = defaultTrackColor;
verticalbar = new HoverVerticalBar(thumbWidth, thumbColor, trackColor,
scrollviewport.getVerticalScrollBar().getModel(), verticalPolicy);
horizontalbar = new HoverHorizontalBar(thumbWidth, thumbColor, trackColor,
scrollviewport.getHorizontalScrollBar().getModel());
/////////////////////////////////////////
// SET LISTENERS
view.addKeyListener(this);
scrollviewport.addMouseWheelListener(this);
//////////////////////////////////
// SET LAYOUT
add(scrollviewport, JLayeredPane.DEFAULT_LAYER);
if(verticalPolicy.equals(ScrollPolicy.VERTICAL_BAR_ALWAYS) || verticalPolicy .equals(ScrollPolicy. VERTICAL_BAR_AS_NEEDED)){
add(verticalbar, JLayeredPane.PALETTE_LAYER);
}
if(horizontalPolicy .equals(ScrollPolicy. HORIZONTAL_BAR_ALWAYS) || horizontalPolicy .equals(ScrollPolicy. HORIZONTAL_BAR_AS_NEEDED)){
add(horizontalbar, JLayeredPane.PALETTE_LAYER);
}
this.addComponentListener(new ComponentAdapter(){
public void componentResized(ComponentEvent e){
repositionComponents();
}
});
this.repositionComponents();
this.revalidate();
}
public void repositionComponents(){
scrollviewport.setBounds(0,0,this.getWidth(), this.getHeight());
if(this.vpolicy .equals(ScrollPolicy. VERTICAL_BAR_ALWAYS) || this.vpolicy .equals(ScrollPolicy. VERTICAL_BAR_AS_NEEDED)){
verticalbar.setBounds(this.getWidth()-this.thumbWidth,7,
this.thumbWidth, this.getHeight()-this.thumbWidth-7);
}else{
verticalbar.setBounds(this.getWidth()-this.thumbWidth,7,
0,0);
}
if(this.hpolicy .equals(ScrollPolicy. HORIZONTAL_BAR_ALWAYS)){
horizontalbar.setBounds(7,this.getHeight()-this.thumbWidth,
this.getWidth()-this.thumbWidth-7, this.thumbWidth);
}else if(this.hpolicy .equals(ScrollPolicy. HORIZONTAL_BAR_AS_NEEDED)){
if(this.getWidth()<this.scrollviewport.getViewport().getView().getWidth()-2){
horizontalbar.setBounds(7,this.getHeight()-this.thumbWidth,
this.getWidth()-this.thumbWidth-7, this.thumbWidth);
}else{
horizontalbar.setBounds(7,this.getHeight()-this.thumbWidth,
0,0);
}
}
this.revalidate();
}
/**
* @override CScrollPane.getVerticalModel
*/
public BoundedRangeModel getVerticalModel(){
return scrollviewport.getVerticalScrollBar().getModel();
}
/**
* @override CScrollPane.getHorizontalModel
*/
public BoundedRangeModel getHorizontalModel(){
return scrollviewport.getHorizontalScrollBar().getModel();
}
/**
* The subtractions of getX and getY compensate for a mistake somewhere else
* (tbd) that doesn't account for the width of the factory panel.
*/
@Override
public void scrollRectToVisible(Rectangle contentRect){
Rectangle r = new Rectangle(contentRect);
r.x -= getX();
r.y -= getY();
scrollviewport.getViewport().scrollRectToVisible(r);
}
/**
* @override CScrollPane.setScrollingUnit
*/
public void setScrollingUnit(int x){
this.SCROLLINGUNIT = x;
this.verticalbar.setScrollingUnit(x);
}
/**
* @override CScrollPane.mouseWheelMoved
* TODO: This is duplicate code (In Hover, Glass, and Tackless ScollPanes)
* For MACs only: Horizontal scroll events are delivered
* to JScrollPanes as Shift+ScrollWheel events AUTOMATICALLY,
* since there is no horizontal scrolling API in Java.
* Horizontal scrolling mouse events will now move the
* content view horizontally, along with Shift key modifier events.
* For WINDOWs: Manually press Shift while scrolling to scroll horizantally
*/
public void mouseWheelMoved(MouseWheelEvent e) {
if(e.isShiftDown()){
scrollviewport.getHorizontalScrollBar().getModel().setValue(
scrollviewport.getHorizontalScrollBar().getModel().getValue()+
e.getUnitsToScroll()*e.getScrollAmount()*SCROLLINGUNIT);
horizontalbar.repaint();
}else {
scrollviewport.getVerticalScrollBar().getModel().setValue(
scrollviewport.getVerticalScrollBar().getModel().getValue()+
e.getUnitsToScroll()*e.getScrollAmount()*SCROLLINGUNIT);
verticalbar.repaint();
}
}
/**
* KeyListeners: Should repaint the scrollbar
* everytime the user presses a key
*/
public void keyPressed(KeyEvent e){
verticalbar.repaint();
horizontalbar.repaint();
}
/**
* KeyListeners: Should repaint the scrollbar
* everytime the user presses a key
*/
public void keyReleased(KeyEvent e){
verticalbar.repaint();
horizontalbar.repaint();
}
/**
* KeyListeners: Should repaint the scrollbar
* everytime the user presses a key
*/
public void keyTyped(KeyEvent e) {
verticalbar.repaint();
horizontalbar.repaint();
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new BorderLayout());
f.setSize(400, 400);
JTextArea b = new JTextArea(20,30);
final CHoverScrollPane c = new CHoverScrollPane(
b,
ScrollPolicy.VERTICAL_BAR_ALWAYS,
ScrollPolicy.HORIZONTAL_BAR_ALWAYS,
20,
Color.red,
Color.blue);
f.getContentPane().add(c);
f.setVisible(true);
f.addComponentListener(new ComponentAdapter(){
public void componentResized(ComponentEvent e){
c.repositionComponents();
}
});
}
}
/**
* A VerticalBar is a vertical scroll bar that operates in conjuction with
* the PARALLEL bouding model range passed as an argument in the
* constructor. Any changes to this scrollbar's thumb position
* should perform the same parallel changes to the bounding model range.
*
*
*/
class HoverVerticalBar extends JPanel implements MouseListener, MouseMotionListener, MouseWheelListener{
private static final long serialVersionUID = 328149080216L;
/**
* Bounding model that this vertical scroll bar will change when the thumb
* is moved. This bar also collects information from the bounding
* model (including the value, maximum, minimum, and extent) in order
* to render the corect size and location of the thumb.
*/
private final BoundedRangeModel modelrange;
/** The thumb color of this vertical scroll bar*/
private final Color thumbColor;
/** The track color of this vertical scroll bar */
private final Color trackColor;
/** Rendering hints of the thumb border */
private final RenderingHints renderingHints;
/** Last location of the mouse press */
private int pressLocation;
/** Amount by which the mouse wheel scrolls */
private int SCROLLINGUNIT = 3;
/** Vertical Scroll Bar Policy of this */
private ScrollPolicy vpolicy;
/**
*
* @param barwidth - the final HEIGHT on the thumb
* @param thumbColor - the final color of the thumb's interior
* @param trackColor - the final color of trh track
* @param modelrange - the mutating view ranges to control
* and be controlled by this HorizontalBar
*
* @requires barwidth != null && thumbColor != null && modelrange != null
* @effects Constructs this to have a thumb of barwidth in size
* with a thumb color set to thummbColor.
*/
public HoverVerticalBar(int barwidth, Color thumbColor, Color trackColor, BoundedRangeModel modelrange, ScrollPolicy verticalPolicy){
this.vpolicy = verticalPolicy;
this.trackColor = trackColor;
this.modelrange=modelrange;
this.thumbColor=thumbColor;
this.renderingHints = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
this.pressLocation = 0;
this.setOpaque(false);
this.addMouseMotionListener(this);
this.addMouseListener(this);
this.addMouseWheelListener(this);
}
/**
* paints scrollbar
*/
public void paint(Graphics g){
//paint super
super.paint(g);
//set up values
double viewValue = modelToView(modelrange.getValue());
double viewExtent = modelToView(modelrange.getExtent());
int vWidth;
if(!vpolicy.equals(ScrollPolicy.VERTICAL_BAR_AS_NEEDED) || (modelrange.getMaximum()>this.getHeight()+7+this.getWidth())){
vWidth = this.getWidth()/2;
Graphics2D g2 = (Graphics2D)g;
g2.addRenderingHints(renderingHints);
g2.translate(vWidth/2, 0);
g2.setColor(trackColor);
g2.fillRoundRect(0,0,vWidth, this.getHeight(), vWidth, vWidth);
g2.setColor(new Color(150,150,150));
g2.drawRoundRect(0,0,vWidth, this.getHeight()-1, vWidth, vWidth);
if(viewValue<this.getHeight()-0.5f*this.getWidth()){
g2.translate(0,viewValue);
}else{
g2.translate(0, this.getHeight()-0.5f*this.getWidth());
}
g2.setPaint(new GradientPaint(
0,0,this.thumbColor,
this.getWidth()+10,0, Color.black, true));
g2.fillRoundRect(0,0,vWidth, (int)viewExtent, vWidth, vWidth);
g2.setColor(new Color(250,250,250,100));
g2.drawRoundRect(0,0,vWidth, (int)viewExtent, vWidth, vWidth);
}
}
/**
* returns true iff the thumb contains the specified point
* @return true if the thumb contains the specified point
* @overrides contains(x,y) from JComponent
* @param x the x coordinate of the point
* @param y the y coordinate of the point
*/
public boolean contains(int x, int y) {
return x > this.getWidth()*.25 && x < this.getWidth()*.75;
}
/**
* @param view - view value to tranform
*
* @requires view != null
* @return a tranformed value from view coordinates to model coordinates
*/
private double viewToModel(int view){
return view*modelrange.getMaximum()/this.getHeight();
}
/**
* @param model - model value to tranform
*
* @requires model != null
* @return a tranformed value from model coordinates to view coordinates
*/
private double modelToView(int model){
return model*this.getHeight()/(double)modelrange.getMaximum();
}
/**
* MouseListener: Should either scroll by some drag distance
* or, if the user presses outside the thumb, it should jump
* directly to the location of the mouse press and THEN
* scroll by some drag distance.
*/
public void mousePressed(MouseEvent e) {
double viewValue = modelToView(modelrange.getValue());
double viewExtent = modelToView(modelrange.getExtent());
if (e.getY()<viewValue || e.getY()>(viewExtent+viewValue)){
this.pressLocation = (int)(viewExtent/2);
modelrange.setValue((int)viewToModel(e.getY())-modelrange.getExtent()/2);
this.repaint();
}else{
this.pressLocation=e.getY()-(int)viewValue;
}
}
/**
* Drag scroll bar by same drag distance as mouse drag
*/
public void mouseDragged(MouseEvent e){
modelrange.setValue((int)viewToModel(e.getY()-this.pressLocation));
this.repaint();
}
/**
* Drops the thumb
*/
public void mouseReleased(MouseEvent e) {
this.pressLocation = 0;
}
public void mouseMoved(MouseEvent e){}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
/**
* Translate the viewport by same amount of wheel scroll
*/
public void mouseWheelMoved(MouseWheelEvent e) {
modelrange.setValue(modelrange.getValue()+e.getUnitsToScroll()*e.getScrollAmount()*SCROLLINGUNIT);
this.repaint();
}
public void setScrollingUnit(int x){
this.SCROLLINGUNIT=x;
}
}
/**
* A HorizontalBar is a vertical scroll bar that operates in conjuction with
* the PARALLEL bouding model range passed as an argument in the
* constructor. Any changes to this scrollbar's thumb position
* should perform the same parallel changes to the bounding model range.
*
*
*/
class HoverHorizontalBar extends JPanel implements MouseListener, MouseMotionListener{
private static final long serialVersionUID = 328149080217L;
/**
* Bounding model that this horizontal scroll bar will change when the thumb
* is moved. This bar also collects information from the bounding
* model (including the value, maximum, minimum, and extent) in order
* to render the corect size and location of the thumb.
*/
private final BoundedRangeModel modelrange;
/** The thumb color of this vertical scroll bar*/
private final Color thumbColor;
/** The track color os thif vertical scroll bar */
private final Color trackColor;
/** Rendering hints of the thumb border */
private final RenderingHints renderingHints;
/** First Location of a mouse press */
private int pressLocation;
/**
*
* @param barwidth - the final HEIGHT on the thumb
* @param thumbColor - the final color of the thumb's interior
* @param trackColor - the final color of the track
* @param modelrange - the mutating view ranges to control
* and be controlled by this HorizontalBar
*
* @requires barwidth != null && thumbColor != null && modelrange != null
* @effects Constructs this to have a thumb of barwidth in size
* with a thumb color set to thummbColor.
*/
public HoverHorizontalBar(int barwidth, Color thumbColor, Color trackColor, BoundedRangeModel modelrange){
this.modelrange=modelrange;
this.thumbColor=thumbColor;
this.trackColor = trackColor;
this.renderingHints = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
this.pressLocation = 0;
this.setOpaque(false);
this.addMouseMotionListener(this);
this.addMouseListener(this);
}
/**
* paints scrollbar
*/
public void paint(Graphics g){
//paint super
super.paint(g);
//set up values
double viewValue = modelToView(modelrange.getValue());
double viewExtent = modelToView(modelrange.getExtent());
int vHeight = this.getHeight()/2;
Graphics2D g2 = (Graphics2D)g;
g2.addRenderingHints(renderingHints);
g2.translate(0, vHeight/2);
g2.setColor(trackColor);
g2.fillRoundRect(0,0,this.getWidth(), vHeight, vHeight, vHeight);
g2.setColor(new Color(150,150,150));
g2.drawRoundRect(0,0,this.getWidth()-1, vHeight, vHeight, vHeight);
if(viewValue<this.getWidth()-0.5f*this.getHeight()){
g2.translate(viewValue, 0);
}else{
g2.translate(this.getWidth()-0.5f*this.getHeight(), 0);
}
g2.setPaint(new GradientPaint(
0,0,this.thumbColor,
0, this.getHeight()+10, Color.black, true));
g2.fillRoundRect(0,0,(int)viewExtent, vHeight, vHeight, vHeight);
g2.setColor(new Color(250,250,250,100));
g2.drawRoundRect(0,0,(int)viewExtent,vHeight, vHeight, vHeight);
}
/**
* returns true iff the thumb contains the specified point
* @return true if the thumb contains the specified point
* @overrides contains(x,y) from JComponent
* @param x the x coordinate of the point
* @param y the y coordinate of the point
*/
public boolean contains(int x, int y) {
return y > this.getHeight()*.25 && y < this.getHeight()*.75;
}
/**
* @param view - view value to tranform
*
* @requires view != null
* @return a tranformed value from view coordinates to model coordinates
*/
private double viewToModel(int view){
return view*modelrange.getMaximum()/this.getWidth();
}
/**
* @param model - model value to tranform
*
* @requires model != null
* @return a tranformed value from model coordinates to view coordinates
*/
private double modelToView(int model){
return model*this.getWidth()/(double)modelrange.getMaximum();
}
/**
* MouseListener: Should either scroll by some drag distance
* or, if the user presses outside the thumb, it should jump
* directly to the location of the mouse press and THEN
* scroll by some drag distance.
*/
public void mousePressed(MouseEvent e) {
double viewValue = modelToView(modelrange.getValue());
double viewExtent = modelToView(modelrange.getExtent());
if (e.getX()<viewValue || e.getX()>(viewExtent+viewValue)){
this.pressLocation = (int)(viewExtent/2);
modelrange.setValue((int)viewToModel(e.getX())-modelrange.getExtent()/2);
this.repaint();
}else{
this.pressLocation=e.getX()-(int)viewValue;
}
}
/**
* Drag scroll bar by same drag distance as mouse drag
*/
public void mouseDragged(MouseEvent e){
modelrange.setValue((int)viewToModel(e.getX()-this.pressLocation));
this.repaint();
}
/**
* Drops the thumb
*/
public void mouseReleased(MouseEvent e) {
this.pressLocation = 0;
}
public void mouseMoved(MouseEvent e){}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
}