Source Code Cross Referenced for BasicSliderUI.java in  » Apache-Harmony-Java-SE » javax-package » javax » swing » plaf » basic » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » javax package » javax.swing.plaf.basic 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
0003:         *  contributor license agreements.  See the NOTICE file distributed with
0004:         *  this work for additional information regarding copyright ownership.
0005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
0006:         *  (the "License"); you may not use this file except in compliance with
0007:         *  the License.  You may obtain a copy of the License at
0008:         *
0009:         *     http://www.apache.org/licenses/LICENSE-2.0
0010:         *
0011:         *  Unless required by applicable law or agreed to in writing, software
0012:         *  distributed under the License is distributed on an "AS IS" BASIS,
0013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014:         *  See the License for the specific language governing permissions and
0015:         *  limitations under the License.
0016:         */
0017:        package javax.swing.plaf.basic;
0018:
0019:        import java.awt.Color;
0020:        import java.awt.Component;
0021:        import java.awt.Dimension;
0022:        import java.awt.Graphics;
0023:        import java.awt.IllegalComponentStateException;
0024:        import java.awt.Insets;
0025:        import java.awt.Point;
0026:        import java.awt.Rectangle;
0027:        import java.awt.event.ActionEvent;
0028:        import java.awt.event.ActionListener;
0029:        import java.awt.event.ComponentAdapter;
0030:        import java.awt.event.ComponentEvent;
0031:        import java.awt.event.ComponentListener;
0032:        import java.awt.event.FocusEvent;
0033:        import java.awt.event.FocusListener;
0034:        import java.awt.event.MouseEvent;
0035:        import java.beans.PropertyChangeEvent;
0036:        import java.beans.PropertyChangeListener;
0037:        import java.util.Dictionary;
0038:        import java.util.Enumeration;
0039:
0040:        import javax.swing.AbstractAction;
0041:        import javax.swing.Action;
0042:        import javax.swing.JComponent;
0043:        import javax.swing.JSlider;
0044:        import javax.swing.LookAndFeel;
0045:        import javax.swing.SwingUtilities;
0046:        import javax.swing.Timer;
0047:        import javax.swing.UIManager;
0048:        import javax.swing.event.ChangeEvent;
0049:        import javax.swing.event.ChangeListener;
0050:        import javax.swing.event.MouseInputAdapter;
0051:        import javax.swing.plaf.ComponentUI;
0052:        import javax.swing.plaf.SliderUI;
0053:
0054:        import org.apache.harmony.luni.util.NotImplementedException;
0055:        import org.apache.harmony.x.swing.StringConstants;
0056:        import org.apache.harmony.x.swing.Utilities;
0057:
0058:        import org.apache.harmony.x.swing.internal.nls.Messages;
0059:
0060:        /**
0061:         * <b>Note:</b> <code>serialVersionUID</code> fields are added as
0062:         * a performance optimization only but not as a guarantee of correct
0063:         * deserialization.
0064:         */
0065:        public class BasicSliderUI extends SliderUI {
0066:
0067:            /**
0068:             * This class isn't used since 1.3 and not implemented here (see
0069:             * HARMONY-4523 for details - the methods doesn't throw new
0070:             * NotImplementedException according to backward compatibility)
0071:             */
0072:            @SuppressWarnings("unused")
0073:            public class ActionScroller extends AbstractAction {
0074:                private static final long serialVersionUID = -3454576988589353120L;
0075:
0076:                public ActionScroller(JSlider slider, int dir, boolean block)
0077:                        throws NotImplementedException {
0078:                    // Not implemented.
0079:                }
0080:
0081:                public void actionPerformed(ActionEvent e)
0082:                        throws NotImplementedException {
0083:                    throw new NotImplementedException();
0084:                }
0085:
0086:                @Override
0087:                public boolean isEnabled() {
0088:                    return enabled;
0089:                }
0090:            }
0091:
0092:            public class ChangeHandler implements  ChangeListener {
0093:                public void stateChanged(final ChangeEvent e) {
0094:                    calculateThumbLocation();
0095:                    setThumbLocation(thumbRect.x, thumbRect.y);
0096:                    Rectangle repaintRect = new Rectangle();
0097:                    if (slider.getOrientation() == JSlider.HORIZONTAL) {
0098:                        repaintRect.setBounds(contentRect.x, trackRect.y,
0099:                                contentRect.width, trackRect.height);
0100:                    } else {
0101:                        repaintRect.setBounds(trackRect.x, contentRect.y,
0102:                                trackRect.width, contentRect.height);
0103:                    }
0104:                    slider.repaint(repaintRect);
0105:                }
0106:            }
0107:
0108:            public class ComponentHandler extends ComponentAdapter {
0109:                @Override
0110:                public void componentResized(final ComponentEvent e) {
0111:                    calculateGeometry();
0112:                    slider.repaint();
0113:                }
0114:            }
0115:
0116:            public class FocusHandler implements  FocusListener {
0117:                public void focusGained(final FocusEvent e) {
0118:                    slider.repaint();
0119:                }
0120:
0121:                public void focusLost(final FocusEvent e) {
0122:                    slider.repaint();
0123:                }
0124:            }
0125:
0126:            public class PropertyChangeHandler implements 
0127:                    PropertyChangeListener {
0128:                public void propertyChange(final PropertyChangeEvent e) {
0129:                    String changedProperty = e.getPropertyName();
0130:                    if (StringConstants.COMPONENT_ORIENTATION
0131:                            .equals(changedProperty)) {
0132:                        recalculateIfOrientationChanged();
0133:                    } else if (StringConstants.BORDER_PROPERTY_CHANGED
0134:                            .equals(changedProperty)) {
0135:                        recalculateIfInsetsChanged();
0136:                    }
0137:                    calculateGeometry();
0138:                    slider.revalidate();
0139:                    slider.repaint();
0140:                }
0141:            }
0142:
0143:            public class ScrollListener implements  ActionListener {
0144:                private int dir;
0145:                private boolean block;
0146:
0147:                public ScrollListener() {
0148:                    this (POSITIVE_SCROLL, false);
0149:                }
0150:
0151:                public ScrollListener(final int dir, final boolean block) {
0152:                    this .dir = dir;
0153:                    this .block = block;
0154:                }
0155:
0156:                public void setDirection(final int direction) {
0157:                    this .dir = direction;
0158:                }
0159:
0160:                public void setScrollByBlock(final boolean block) {
0161:                    this .block = block;
0162:                }
0163:
0164:                public void actionPerformed(final ActionEvent e) {
0165:                    if (block) {
0166:                        scrollByBlock(dir);
0167:                    } else {
0168:                        scrollByUnit(dir);
0169:                    }
0170:                    slider.repaint();
0171:                }
0172:            }
0173:
0174:            public class TrackListener extends MouseInputAdapter {
0175:                protected transient int offset;
0176:                protected transient int currentMouseX;
0177:                protected transient int currentMouseY;
0178:
0179:                private Timer trackTimer;
0180:                private boolean inThumb;
0181:                private int diffX;
0182:                private int diffY;
0183:                private Point mousePoint = new Point();
0184:
0185:                public TrackListener() {
0186:
0187:                    trackTimer = new Timer(150, new ActionListener() {
0188:
0189:                        public void actionPerformed(final ActionEvent e) {
0190:
0191:                            Point current = new Point(thumbRect.x, thumbRect.y);
0192:                            Point next = new Point(currentMouseX, currentMouseY);
0193:                            int dir = calculateDirection(current, next);
0194:                            //Changed in H-4480
0195:                            if (shouldScroll(dir)) {
0196:                                scrollDueToClickInTrack(dir);
0197:                            }
0198:
0199:                        }
0200:                    });
0201:                }
0202:
0203:                @Override
0204:                public void mouseReleased(final MouseEvent e) {
0205:                    if (trackTimer.isRunning()) {
0206:                        trackTimer.stop();
0207:                    }
0208:                    offset = 0;
0209:
0210:                    if (slider.getSnapToTicks() && isDragging) {
0211:                        isDragging = false;
0212:
0213:                        int value = getNearestVisibleValue();
0214:                        slider.setValue(value);
0215:
0216:                        if (slider.getOrientation() == JSlider.HORIZONTAL) {
0217:                            setThumbLocation(xPositionForValue(value),
0218:                                    thumbRect.y);
0219:                        } else {
0220:                            setThumbLocation(thumbRect.x,
0221:                                    yPositionForValue(value));
0222:                        }
0223:                        calculateThumbLocation();
0224:                    }
0225:
0226:                    slider.setValueIsAdjusting(false);
0227:                    isDragging = false;
0228:                    slider.repaint();
0229:                }
0230:
0231:                @Override
0232:                public void mousePressed(final MouseEvent e) {
0233:                    if (!slider.isEnabled()) {
0234:                        return;
0235:                    }
0236:
0237:                    if (!slider.isFocusOwner()) {
0238:                        slider.requestFocus();
0239:                    }
0240:                    currentMouseX = e.getX();
0241:                    currentMouseY = e.getY();
0242:                    diffX = currentMouseX - thumbRect.x;
0243:                    diffY = currentMouseY - thumbRect.y;
0244:                    mousePoint = e.getPoint();
0245:
0246:                    inThumb = thumbRect.contains(currentMouseX, currentMouseY);
0247:                    if (!inThumb && SwingUtilities.isLeftMouseButton(e)) {
0248:                        Point currentPoint = new Point(thumbRect.x, thumbRect.y);
0249:                        scrollDueToClickInTrack(calculateDirection(
0250:                                currentPoint, e.getPoint()));
0251:                        trackTimer.start();
0252:
0253:                        slider.getModel().setValueIsAdjusting(true);
0254:                    }
0255:                }
0256:
0257:                public boolean shouldScroll(final int direction) {
0258:                    // The class has been unused in TrackListener before H4480
0259:                    // Now the behaviour has been changed and this method used in timer
0260:                    if (slider.getOrientation() == JSlider.HORIZONTAL) {
0261:
0262:                        if (direction == POSITIVE_SCROLL) {
0263:
0264:                            return mousePoint.x
0265:                                    - (thumbRect.x + computeIncrement() + getThumbSize().width) > 1;
0266:                        }
0267:
0268:                        if (direction == NEGATIVE_SCROLL) {
0269:
0270:                            return mousePoint.x
0271:                                    - (thumbRect.x - computeIncrement()) < -1;
0272:                        }
0273:
0274:                    } else {
0275:
0276:                        if (direction == POSITIVE_SCROLL) {
0277:
0278:                            return mousePoint.y
0279:                                    - (thumbRect.y + computeIncrement() + getThumbSize().height / 2) > 1;
0280:                        }
0281:
0282:                        if (direction == NEGATIVE_SCROLL) {
0283:
0284:                            return mousePoint.y
0285:                                    - (thumbRect.y + computeIncrement() + getThumbSize().height / 2) < -1;
0286:                        }
0287:                    }
0288:                    return false;
0289:                }
0290:
0291:                @Override
0292:                public void mouseDragged(final MouseEvent e) {
0293:                    mousePoint = e.getPoint();
0294:                    if (inThumb && SwingUtilities.isLeftMouseButton(e)) {
0295:                        isDragging = true;
0296:                        slider.getModel().setValueIsAdjusting(true);
0297:
0298:                        Rectangle repaintRect = new Rectangle();
0299:                        if (slider.getOrientation() == JSlider.HORIZONTAL) {
0300:                            int newX = e.getX() - diffX;
0301:                            newX = checkXEdgeCondition(newX);
0302:                            setThumbLocation(newX, thumbRect.y);
0303:                            slider.setValue(valueForXPosition(newX
0304:                                    + getThumbSize().width / 2));
0305:
0306:                            repaintRect.setBounds(contentRect.x, trackRect.y,
0307:                                    contentRect.width, trackRect.height);
0308:                            offset = newX - (currentMouseX - diffX);
0309:                        } else {
0310:                            int newY = e.getY() - diffY;
0311:                            newY = checkYEdgeCondition(newY);
0312:                            setThumbLocation(thumbRect.x, newY);
0313:                            slider.setValue(valueForYPosition(newY
0314:                                    + getThumbSize().height / 2));
0315:
0316:                            repaintRect.setBounds(trackRect.x, contentRect.y,
0317:                                    trackRect.width, contentRect.height);
0318:                            offset = newY - (currentMouseY - diffY);
0319:                        }
0320:                        slider.repaint(repaintRect);
0321:                    }
0322:
0323:                    if (trackTimer.isRunning()
0324:                            && trackRect.contains(e.getX(), e.getY())) {
0325:                        currentMouseX = e.getX();
0326:                        currentMouseY = e.getY();
0327:                    }
0328:                }
0329:
0330:                @Override
0331:                public void mouseMoved(final MouseEvent e) {
0332:                }
0333:
0334:                private int checkXEdgeCondition(final int newX) {
0335:                    int result = newX;
0336:                    if (!drawInverted()
0337:                            ^ !slider.getComponentOrientation().isLeftToRight()) {
0338:                        if (newX + getThumbSize().width / 2 < xPositionForValue(slider
0339:                                .getMinimum())) {
0340:                            result = xPositionForValue(slider.getMinimum())
0341:                                    - getThumbSize().width / 2;
0342:                        }
0343:                        if (newX + getThumbSize().width / 2 > xPositionForValue(slider
0344:                                .getMaximum())) {
0345:                            result = xPositionForValue(slider.getMaximum())
0346:                                    - getThumbSize().width / 2;
0347:                        }
0348:                    } else {
0349:                        if (newX + getThumbSize().width / 2 > xPositionForValue(slider
0350:                                .getMinimum())) {
0351:                            result = xPositionForValue(slider.getMinimum())
0352:                                    - getThumbSize().width / 2;
0353:                        }
0354:                        if (newX + getThumbSize().width / 2 < xPositionForValue(slider
0355:                                .getMaximum())) {
0356:                            result = xPositionForValue(slider.getMaximum())
0357:                                    - getThumbSize().width / 2;
0358:                        }
0359:                    }
0360:                    return result;
0361:                }
0362:
0363:                private int checkYEdgeCondition(final int newY) {
0364:                    int result = newY;
0365:                    if (!drawInverted()
0366:                            ^ (!slider.getComponentOrientation()
0367:                                    .isLeftToRight() && slider.getOrientation() == JSlider.HORIZONTAL)) {
0368:                        if (newY + getThumbSize().height / 2 > yPositionForValue(slider
0369:                                .getMinimum())) {
0370:                            result = yPositionForValue(slider.getMinimum())
0371:                                    - getThumbSize().height / 2;
0372:                        }
0373:                        if (newY + getThumbSize().height / 2 < yPositionForValue(slider
0374:                                .getMaximum())) {
0375:                            result = yPositionForValue(slider.getMaximum())
0376:                                    - getThumbSize().height / 2;
0377:                        }
0378:                    } else {
0379:                        if (newY + getThumbSize().height / 2 < yPositionForValue(slider
0380:                                .getMinimum())) {
0381:                            result = yPositionForValue(slider.getMinimum())
0382:                                    - getThumbSize().height / 2;
0383:                        }
0384:                        if (newY + getThumbSize().height / 2 > yPositionForValue(slider
0385:                                .getMaximum())) {
0386:                            result = yPositionForValue(slider.getMaximum())
0387:                                    - getThumbSize().height / 2;
0388:                        }
0389:                    }
0390:                    return result;
0391:                }
0392:
0393:                private int calculateDirection(final Point current,
0394:                        final Point next) {
0395:                    if (slider.getOrientation() == JSlider.HORIZONTAL) {
0396:                        if (slider.getComponentOrientation().isLeftToRight()) {
0397:                            return (next.x > current.x) ? 1 : -1;
0398:                        } else {
0399:                            return (next.x > current.x) ? -1 : 1;
0400:                        }
0401:                    } else {
0402:                        return (next.y < current.y) ? 1 : -1;
0403:                    }
0404:                }
0405:
0406:                private int getNearestVisibleValue() {
0407:                    int tickSpacing = calculateTickSpacing();
0408:                    int value = slider.getValue();
0409:                    int result = slider.getMinimum();
0410:                    while (result < value) {
0411:                        result += tickSpacing;
0412:                    }
0413:                    result = (result - value < value - (result - tickSpacing)) ? result
0414:                            : result - tickSpacing;
0415:
0416:                    return result;
0417:                }
0418:            }
0419:
0420:            public static final int POSITIVE_SCROLL = 1;
0421:            public static final int NEGATIVE_SCROLL = -1;
0422:            public static final int MIN_SCROLL = -2;
0423:            public static final int MAX_SCROLL = 2;
0424:
0425:            protected Insets focusInsets = new Insets(0, 0, 0, 0);
0426:            protected Timer scrollTimer;
0427:            protected JSlider slider;
0428:            protected Insets insetCache;
0429:            protected boolean leftToRightCache;
0430:            protected Rectangle focusRect;
0431:            protected Rectangle contentRect;
0432:            protected Rectangle labelRect;
0433:            protected Rectangle tickRect;
0434:            protected Rectangle trackRect;
0435:            protected Rectangle thumbRect;
0436:            protected int trackBuffer;
0437:            protected TrackListener trackListener;
0438:            protected ChangeListener changeListener;
0439:            protected ComponentListener componentListener;
0440:            protected FocusListener focusListener;
0441:            protected ScrollListener scrollListener;
0442:            protected PropertyChangeListener propertyChangeListener;
0443:
0444:            private Color shadowColor;
0445:            private Color highlightColor;
0446:            private Color focusColor;
0447:
0448:            private boolean isDragging;
0449:
0450:            private ChangeListener changeHandler;
0451:            private ComponentListener componentHandler;
0452:            private FocusListener focusHandler;
0453:            private PropertyChangeListener propertyChangeHandler;
0454:
0455:            private static final int THUMB_WIDTH = 11;
0456:            private static final int THUMB_HEIGHT = 20;
0457:            private static final int TRACK_SIZE = 2;
0458:            private static final int DEFAULT_SLIDER_SIZE = 200;
0459:            private static final int DEFAULT_SLIDER_MIN_SIZE = 36;
0460:            private static final int TICK_LENGTH = 8;
0461:            private static final int UNIT_INCREMENT = 1;
0462:
0463:            public BasicSliderUI(final JSlider slider) {
0464:                focusRect = new Rectangle();
0465:                contentRect = new Rectangle();
0466:                labelRect = new Rectangle();
0467:                tickRect = new Rectangle();
0468:                trackRect = new Rectangle();
0469:                thumbRect = new Rectangle();
0470:            }
0471:
0472:            protected Color getShadowColor() {
0473:                return shadowColor;
0474:            }
0475:
0476:            protected Color getHighlightColor() {
0477:                return highlightColor;
0478:            }
0479:
0480:            protected Color getFocusColor() {
0481:                return focusColor;
0482:            }
0483:
0484:            protected boolean isDragging() {
0485:                return isDragging;
0486:            }
0487:
0488:            public static ComponentUI createUI(final JComponent c) {
0489:                return new BasicSliderUI((JSlider) c);
0490:            }
0491:
0492:            @Override
0493:            public void installUI(final JComponent c) {
0494:                slider = (JSlider) c;
0495:
0496:                installDefaults(slider);
0497:                installListeners(slider);
0498:                installKeyboardActions(slider);
0499:
0500:                calculateGeometry();
0501:            }
0502:
0503:            @Override
0504:            public void uninstallUI(final JComponent c) {
0505:                if (c != slider) {
0506:                    throw new IllegalComponentStateException(
0507:                            Messages
0508:                                    .getString(
0509:                                            "swing.0E", new Object[] { this , c, slider })); //$NON-NLS-1$
0510:                }
0511:                uninstallListeners(slider);
0512:                uninstallKeyboardActions(slider);
0513:            }
0514:
0515:            protected void installDefaults(final JSlider slider) {
0516:                LookAndFeel.installColors(slider, "Slider.background",
0517:                        "Slider.foreground");
0518:
0519:                shadowColor = UIManager.getColor("Slider.shadow");
0520:                highlightColor = UIManager.getColor("Slider.highlight");
0521:                focusColor = UIManager.getColor("Slider.focus");
0522:
0523:                focusInsets = UIManager.getInsets("Slider.focusInsets");
0524:            }
0525:
0526:            protected TrackListener createTrackListener(final JSlider slider) {
0527:                return new TrackListener();
0528:            }
0529:
0530:            protected ChangeListener createChangeListener(final JSlider slider) {
0531:                if (changeHandler == null) {
0532:                    changeHandler = new ChangeHandler();
0533:                }
0534:                return changeHandler;
0535:            }
0536:
0537:            protected ComponentListener createComponentListener(
0538:                    final JSlider slider) {
0539:                if (componentHandler == null) {
0540:                    componentHandler = new ComponentHandler();
0541:                }
0542:                return componentHandler;
0543:            }
0544:
0545:            protected FocusListener createFocusListener(final JSlider slider) {
0546:                if (focusHandler == null) {
0547:                    focusHandler = new FocusHandler();
0548:                }
0549:                return focusHandler;
0550:            }
0551:
0552:            protected ScrollListener createScrollListener(final JSlider slider) {
0553:                return slider != null ? new ScrollListener(slider
0554:                        .getOrientation(), slider.getSnapToTicks())
0555:                        : new ScrollListener();
0556:            }
0557:
0558:            protected PropertyChangeListener createPropertyChangeListener(
0559:                    final JSlider slider) {
0560:                if (propertyChangeHandler == null) {
0561:                    propertyChangeHandler = new PropertyChangeHandler();
0562:                }
0563:                return propertyChangeHandler;
0564:            }
0565:
0566:            protected void installListeners(final JSlider slider) {
0567:                changeListener = new ChangeHandler();
0568:                slider.getModel().addChangeListener(changeListener);
0569:
0570:                componentListener = createComponentListener(slider);
0571:                slider.addComponentListener(componentListener);
0572:
0573:                focusListener = createFocusListener(slider);
0574:                slider.addFocusListener(focusListener);
0575:
0576:                propertyChangeListener = createPropertyChangeListener(slider);
0577:                slider.addPropertyChangeListener(propertyChangeListener);
0578:
0579:                trackListener = createTrackListener(slider);
0580:                slider.addMouseListener(trackListener);
0581:                slider.addMouseMotionListener(trackListener);
0582:
0583:                scrollListener = createScrollListener(slider);
0584:                scrollTimer = new Timer(150, scrollListener);
0585:            }
0586:
0587:            protected void uninstallListeners(final JSlider slider) {
0588:                slider.getModel().removeChangeListener(changeListener);
0589:                changeListener = null;
0590:
0591:                slider.removeComponentListener(componentListener);
0592:                componentListener = null;
0593:
0594:                slider.removeFocusListener(focusListener);
0595:                focusListener = null;
0596:
0597:                slider.removePropertyChangeListener(propertyChangeListener);
0598:                propertyChangeListener = null;
0599:
0600:                slider.removeMouseListener(trackListener);
0601:                slider.removeMouseMotionListener(trackListener);
0602:                trackListener = null;
0603:
0604:                scrollListener = null;
0605:                scrollTimer = null;
0606:            }
0607:
0608:            protected void installKeyboardActions(final JSlider slider) {
0609:                Utilities.installKeyboardActions(slider,
0610:                        JComponent.WHEN_FOCUSED, "Slider.focusInputMap",
0611:                        "Slider.focusInputMap.RightToLeft");
0612:
0613:                slider.getActionMap().put("positiveUnitIncrement",
0614:                        newPositiveUnitIncrementAction());
0615:                slider.getActionMap().put("positiveBlockIncrement",
0616:                        newPositiveBlockIncrementAction());
0617:                slider.getActionMap().put("negativeUnitIncrement",
0618:                        newNegativeUnitIncrementAction());
0619:                slider.getActionMap().put("negativeBlockIncrement",
0620:                        newNegativeBlockIncrementAction());
0621:                slider.getActionMap().put("minScroll", newMinScrollAction());
0622:                slider.getActionMap().put("maxScroll", newMaxScrollAction());
0623:            }
0624:
0625:            protected void uninstallKeyboardActions(final JSlider slider) {
0626:                Utilities.uninstallKeyboardActions(slider,
0627:                        JComponent.WHEN_FOCUSED);
0628:            }
0629:
0630:            public Dimension getPreferredHorizontalSize() {
0631:                Insets insets = slider.getInsets();
0632:                int trackHeight = trackRect.height > 0 ? trackRect.height : 0;
0633:                int tickHeight = tickRect.height > 0 ? tickRect.height : 0;
0634:                int labelHeight = labelRect.height > 0 ? labelRect.height : 0;
0635:                int height = trackHeight + tickHeight + labelHeight
0636:                        + insets.top + insets.bottom + focusInsets.top
0637:                        + focusInsets.bottom;
0638:                return new Dimension(DEFAULT_SLIDER_SIZE, height);
0639:            }
0640:
0641:            public Dimension getPreferredVerticalSize() {
0642:                Insets insets = slider.getInsets();
0643:                int trackWidth = trackRect.width > 0 ? trackRect.width : 0;
0644:                int tickWidth = tickRect.width > 0 ? tickRect.width : 0;
0645:                int labelWidth = labelRect.width > 0 ? labelRect.width : 0;
0646:                int width = trackWidth + tickWidth + labelWidth + insets.left
0647:                        + insets.right + focusInsets.left + focusInsets.right;
0648:                return new Dimension(width, DEFAULT_SLIDER_SIZE);
0649:            }
0650:
0651:            public Dimension getMinimumHorizontalSize() {
0652:                Insets insets = slider.getInsets();
0653:                int trackHeight = trackRect.height > 0 ? trackRect.height : 0;
0654:                int tickHeight = tickRect.height > 0 ? tickRect.height : 0;
0655:                int labelHeight = labelRect.height > 0 ? labelRect.height : 0;
0656:                int height = trackHeight + tickHeight + labelHeight
0657:                        + insets.top + insets.bottom + focusInsets.top
0658:                        + focusInsets.bottom;
0659:                return new Dimension(DEFAULT_SLIDER_MIN_SIZE, height);
0660:            }
0661:
0662:            public Dimension getMinimumVerticalSize() {
0663:                Insets insets = slider.getInsets();
0664:                int trackWidth = trackRect.width > 0 ? trackRect.width : 0;
0665:                int tickWidth = tickRect.width > 0 ? tickRect.width : 0;
0666:                int labelWidth = labelRect.width > 0 ? labelRect.width : 0;
0667:                int width = trackWidth + tickWidth + labelWidth + insets.left
0668:                        + insets.right + focusInsets.left + focusInsets.right;
0669:                return new Dimension(width, DEFAULT_SLIDER_MIN_SIZE);
0670:            }
0671:
0672:            @Override
0673:            public Dimension getPreferredSize(final JComponent c) {
0674:                if (slider.getOrientation() == JSlider.HORIZONTAL) {
0675:                    return getPreferredHorizontalSize();
0676:                } else {
0677:                    return getPreferredVerticalSize();
0678:                }
0679:            }
0680:
0681:            @Override
0682:            public Dimension getMinimumSize(final JComponent c) {
0683:                if (slider.getOrientation() == JSlider.HORIZONTAL) {
0684:                    return getMinimumHorizontalSize();
0685:                } else {
0686:                    return getMinimumVerticalSize();
0687:                }
0688:            }
0689:
0690:            @Override
0691:            public Dimension getMaximumSize(final JComponent c) {
0692:                if (slider.getOrientation() == JSlider.HORIZONTAL) {
0693:                    return new Dimension(Short.MAX_VALUE,
0694:                            getPreferredHorizontalSize().height);
0695:                } else {
0696:                    return new Dimension(getPreferredHorizontalSize().width,
0697:                            Short.MAX_VALUE);
0698:                }
0699:            }
0700:
0701:            protected void calculateGeometry() {
0702:                calculateFocusRect();
0703:                calculateContentRect();
0704:                calculateThumbSize();
0705:                calculateTrackBuffer();
0706:                calculateTrackRect();
0707:                calculateTickRect();
0708:                calculateLabelRect();
0709:                calculateThumbLocation();
0710:            }
0711:
0712:            protected void calculateFocusRect() {
0713:                Insets insets = slider.getInsets();
0714:                int x = insets.left;
0715:                int y = insets.top;
0716:                int width = slider.getWidth() - insets.left - insets.right;
0717:                int height = slider.getHeight() - insets.top - insets.bottom;
0718:
0719:                focusRect.setBounds(x, y, width, height);
0720:            }
0721:
0722:            protected void calculateThumbSize() {
0723:                int width = (slider.getOrientation() == JSlider.HORIZONTAL) ? THUMB_WIDTH
0724:                        : THUMB_HEIGHT;
0725:                int height = (slider.getOrientation() == JSlider.HORIZONTAL) ? THUMB_HEIGHT
0726:                        : THUMB_WIDTH;
0727:                thumbRect.setSize(width, height);
0728:            }
0729:
0730:            protected void calculateContentRect() {
0731:                int x = focusRect.x + focusInsets.left;
0732:                int y = focusRect.y + focusInsets.top;
0733:                int width = focusRect.width - focusInsets.left
0734:                        - focusInsets.right;
0735:                int height = focusRect.height - focusInsets.top
0736:                        - focusInsets.bottom;
0737:
0738:                contentRect.setBounds(x, y, width, height);
0739:            }
0740:
0741:            protected void calculateThumbLocation() {
0742:                if (isDragging) {
0743:                    return;
0744:                }
0745:                if (slider.getOrientation() == JSlider.HORIZONTAL) {
0746:                    int x = xPositionForValue(slider.getValue());
0747:                    int y = trackRect.y;
0748:                    x -= getThumbSize().width / 2;
0749:                    thumbRect.setLocation(x, y);
0750:                } else {
0751:                    int x = trackRect.x;
0752:                    int y = yPositionForValue(slider.getValue());
0753:                    y -= getThumbSize().height / 2;
0754:                    thumbRect.setLocation(x, y);
0755:                }
0756:            }
0757:
0758:            protected void calculateTrackBuffer() {
0759:                if (slider.getPaintLabels()) {
0760:                    if ((slider.getOrientation() == JSlider.HORIZONTAL)) {
0761:                        int widthOfHighValueLabel = getWidthOfHighValueLabel();
0762:                        int widthOfLowValueLabel = getWidthOfLowValueLabel();
0763:                        trackBuffer = widthOfHighValueLabel > widthOfLowValueLabel ? widthOfHighValueLabel / 2
0764:                                : widthOfLowValueLabel / 2;
0765:                    } else {
0766:                        int heightOfHighValueLabel = getHeightOfHighValueLabel();
0767:                        int heightOfLowValueLabel = getHeightOfLowValueLabel();
0768:                        trackBuffer = heightOfHighValueLabel > heightOfLowValueLabel ? heightOfHighValueLabel / 2
0769:                                : heightOfLowValueLabel / 2;
0770:                    }
0771:                } else {
0772:                    trackBuffer = 0;
0773:                }
0774:            }
0775:
0776:            protected void calculateTrackRect() {
0777:
0778:                if (slider.getOrientation() == JSlider.HORIZONTAL) {
0779:                    int width = contentRect.width - trackBuffer * 2
0780:                            - getThumbSize().width;
0781:                    int x = contentRect.x + trackBuffer + getThumbSize().width
0782:                            / 2;
0783:                    int y = contentRect.y
0784:                            + (contentRect.height - getThumbSize().height
0785:                                    + labelRect.height + tickRect.height) / 2;
0786:
0787:                    trackRect.setBounds(x, y, width, getThumbSize().height);
0788:
0789:                } else {
0790:                    int height = contentRect.height - trackBuffer * 2
0791:                            - getThumbSize().height;
0792:                    int sizeToCenter = (getThumbSize().width + labelRect.width + tickRect.width) / 2;
0793:                    int y = contentRect.y + trackBuffer + getThumbSize().height
0794:                            / 2;
0795:                    int x;
0796:
0797:                    if (slider.getComponentOrientation().isLeftToRight()) {
0798:                        x = contentRect.x + contentRect.width / 2
0799:                                - sizeToCenter;
0800:
0801:                    } else {
0802:                        x = contentRect.x + contentRect.width / 2
0803:                                + sizeToCenter - getThumbSize().width;
0804:                    }
0805:
0806:                    trackRect.setBounds(x, y, getThumbSize().width, height);
0807:                }
0808:            }
0809:
0810:            protected int getTickLength() {
0811:                return slider.getPaintTicks() ? TICK_LENGTH : 0;
0812:            }
0813:
0814:            protected void calculateTickRect() {
0815:                if (slider.getOrientation() == JSlider.HORIZONTAL) {
0816:                    int x = trackRect.x;
0817:                    int y = trackRect.y + trackRect.height;
0818:                    int width = trackRect.width;
0819:                    int height = getTickLength();
0820:                    tickRect.setBounds(x, y, width, height);
0821:                } else {
0822:                    int x;
0823:                    if (slider.getComponentOrientation().isLeftToRight()) {
0824:                        x = trackRect.x + trackRect.width;
0825:                    } else {
0826:                        x = trackRect.x - getTickLength();
0827:                    }
0828:                    int y = trackRect.y;
0829:                    int width = getTickLength();
0830:                    int height = trackRect.height;
0831:                    tickRect.setBounds(x, y, width, height);
0832:                }
0833:            }
0834:
0835:            protected void calculateLabelRect() {
0836:                if (slider.getOrientation() == JSlider.HORIZONTAL) {
0837:                    int x = contentRect.x;
0838:                    int y = tickRect.y + tickRect.height;
0839:                    int width = contentRect.width;
0840:                    int height = slider.getPaintLabels() ? getHeightOfTallestLabel()
0841:                            : 0;
0842:                    labelRect.setBounds(x, y, width, height);
0843:                } else {
0844:                    int x;
0845:                    int width = slider.getPaintLabels() ? getWidthOfWidestLabel()
0846:                            : 0;
0847:                    int height = contentRect.height;
0848:                    if (slider.getComponentOrientation().isLeftToRight()) {
0849:                        x = tickRect.x + tickRect.width;
0850:                    } else {
0851:                        x = tickRect.x - width;
0852:                    }
0853:                    int y = contentRect.y;
0854:                    labelRect.setBounds(x, y, width, height);
0855:                }
0856:            }
0857:
0858:            protected Dimension getThumbSize() {
0859:                return thumbRect.getSize();
0860:            }
0861:
0862:            protected int getWidthOfWidestLabel() {
0863:                Dictionary table = slider.getLabelTable();
0864:                if (table == null) {
0865:                    return 0;
0866:                }
0867:                Enumeration keys = table.keys();
0868:                int result = ((Component) table.get(keys.nextElement()))
0869:                        .getWidth();
0870:                while (keys.hasMoreElements()) {
0871:                    Component label = ((Component) table
0872:                            .get(keys.nextElement()));
0873:                    if (label.getWidth() > result) {
0874:                        result = label.getWidth();
0875:                    }
0876:                }
0877:
0878:                return result;
0879:            }
0880:
0881:            protected int getHeightOfTallestLabel() {
0882:                Dictionary table = slider.getLabelTable();
0883:                if (table == null) {
0884:                    return 0;
0885:                }
0886:                Enumeration keys = table.keys();
0887:                int result = ((Component) table.get(keys.nextElement()))
0888:                        .getHeight();
0889:                while (keys.hasMoreElements()) {
0890:                    Component label = ((Component) table
0891:                            .get(keys.nextElement()));
0892:                    if (label.getHeight() > result) {
0893:                        result = label.getHeight();
0894:                    }
0895:                }
0896:
0897:                return result;
0898:            }
0899:
0900:            protected int getWidthOfHighValueLabel() {
0901:                Component label = getHighestValueLabel();
0902:
0903:                return label == null ? 0 : label.getWidth();
0904:            }
0905:
0906:            protected int getWidthOfLowValueLabel() {
0907:                Component label = getLowestValueLabel();
0908:
0909:                return label == null ? 0 : label.getWidth();
0910:            }
0911:
0912:            protected int getHeightOfHighValueLabel() {
0913:                Component label = getHighestValueLabel();
0914:
0915:                return label == null ? 0 : label.getHeight();
0916:            }
0917:
0918:            protected int getHeightOfLowValueLabel() {
0919:                Component label = getLowestValueLabel();
0920:
0921:                return label == null ? 0 : label.getHeight();
0922:            }
0923:
0924:            protected Component getLowestValueLabel() {
0925:                Dictionary table = slider.getLabelTable();
0926:                if (table == null) {
0927:                    return null;
0928:                }
0929:                Enumeration keys = table.keys();
0930:                Integer lastKey = (Integer) keys.nextElement();
0931:                Component result = (Component) table.get(lastKey);
0932:                while (keys.hasMoreElements()) {
0933:                    Integer el = (Integer) keys.nextElement();
0934:                    if (el.intValue() < lastKey.intValue()) {
0935:                        lastKey = el;
0936:                        result = (Component) table.get(lastKey);
0937:                    }
0938:                }
0939:
0940:                return result;
0941:            }
0942:
0943:            protected Component getHighestValueLabel() {
0944:                Dictionary table = slider.getLabelTable();
0945:                if (table == null) {
0946:                    return null;
0947:                }
0948:                Enumeration keys = table.keys();
0949:                Integer lastKey = (Integer) keys.nextElement();
0950:                Component result = (Component) table.get(lastKey);
0951:                while (keys.hasMoreElements()) {
0952:                    Integer el = (Integer) keys.nextElement();
0953:                    if (el.intValue() > lastKey.intValue()) {
0954:                        lastKey = el;
0955:                        result = (Component) table.get(lastKey);
0956:                    }
0957:                }
0958:
0959:                return result;
0960:            }
0961:
0962:            @Override
0963:            public void paint(final Graphics g, final JComponent c) {
0964:                Color oldColor = g.getColor();
0965:
0966:                g.setColor(slider.getBackground());
0967:                g.fillRect(0, 0, slider.getWidth(), slider.getHeight());
0968:
0969:                if (slider.isFocusOwner()) {
0970:                    paintFocus(g);
0971:                }
0972:                if (slider.getPaintTrack()) {
0973:                    paintTrack(g);
0974:                }
0975:                if (slider.getPaintTicks()) {
0976:                    paintTicks(g);
0977:                }
0978:                if (slider.getPaintLabels()) {
0979:                    paintLabels(g);
0980:                }
0981:                paintThumb(g);
0982:
0983:                g.setColor(oldColor);
0984:            }
0985:
0986:            protected void recalculateIfInsetsChanged() {
0987:                calculateGeometry();
0988:            }
0989:
0990:            protected void recalculateIfOrientationChanged() {
0991:                calculateGeometry();
0992:                uninstallKeyboardActions(slider);
0993:                installKeyboardActions(slider);
0994:            }
0995:
0996:            protected boolean drawInverted() {
0997:                return slider.getInverted();
0998:            }
0999:
1000:            public void paintFocus(final Graphics g) {
1001:                Color oldColor = g.getColor();
1002:                g.setColor(focusColor);
1003:                g.drawRect(focusRect.x, focusRect.y, focusRect.width - 1,
1004:                        focusRect.height - 1);
1005:                g.setColor(oldColor);
1006:            }
1007:
1008:            public void paintTrack(final Graphics g) {
1009:                if (slider.getOrientation() == JSlider.HORIZONTAL) {
1010:                    Utilities.draw3DRect(g, trackRect.x, trackRect.y
1011:                            + (trackRect.height - TRACK_SIZE) / 2,
1012:                            trackRect.width, TRACK_SIZE, Color.DARK_GRAY,
1013:                            Color.WHITE, false);
1014:                } else {
1015:                    Utilities.draw3DRect(g, trackRect.x
1016:                            + (trackRect.width - TRACK_SIZE) / 2, trackRect.y,
1017:                            TRACK_SIZE, trackRect.height, Color.DARK_GRAY,
1018:                            Color.WHITE, false);
1019:                }
1020:            }
1021:
1022:            public void paintTicks(final Graphics g) {
1023:                Color oldColor = g.getColor();
1024:                g.setColor(Color.BLACK);
1025:                if (slider.getOrientation() == JSlider.HORIZONTAL) {
1026:                    if (slider.getMajorTickSpacing() != 0) {
1027:                        int value = slider.getMinimum();
1028:                        while (value <= slider.getMaximum()) {
1029:                            paintMajorTickForHorizSlider(g, tickRect,
1030:                                    xPositionForValue(value));
1031:                            value += slider.getMajorTickSpacing();
1032:                        }
1033:                    }
1034:                    if (slider.getMinorTickSpacing() != 0) {
1035:                        int value = slider.getMinimum();
1036:                        while (value <= slider.getMaximum()) {
1037:                            paintMinorTickForHorizSlider(g, tickRect,
1038:                                    xPositionForValue(value));
1039:                            value += slider.getMinorTickSpacing();
1040:                        }
1041:                    }
1042:                } else {
1043:                    if (slider.getMajorTickSpacing() != 0) {
1044:                        int value = slider.getMinimum();
1045:                        while (value <= slider.getMaximum()) {
1046:                            paintMajorTickForVertSlider(g, tickRect,
1047:                                    yPositionForValue(value));
1048:                            value += slider.getMajorTickSpacing();
1049:                        }
1050:                    }
1051:                    if (slider.getMinorTickSpacing() != 0) {
1052:                        int value = slider.getMinimum();
1053:                        while (value <= slider.getMaximum()) {
1054:                            paintMinorTickForVertSlider(g, tickRect,
1055:                                    yPositionForValue(value));
1056:                            value += slider.getMinorTickSpacing();
1057:                        }
1058:                    }
1059:                }
1060:                g.setColor(oldColor);
1061:            }
1062:
1063:            protected void paintMinorTickForHorizSlider(final Graphics g,
1064:                    final Rectangle tickBounds, final int x) {
1065:                g.drawLine(x, tickBounds.y, x, tickBounds.y + tickBounds.height
1066:                        / 2);
1067:            }
1068:
1069:            protected void paintMajorTickForHorizSlider(final Graphics g,
1070:                    final Rectangle tickBounds, final int x) {
1071:                g
1072:                        .drawLine(x, tickBounds.y, x, tickBounds.y
1073:                                + tickBounds.height);
1074:            }
1075:
1076:            protected void paintMinorTickForVertSlider(final Graphics g,
1077:                    final Rectangle tickBounds, final int y) {
1078:                int diff = slider.getComponentOrientation().isLeftToRight() ? 0
1079:                        : tickBounds.width / 2;
1080:                g.drawLine(tickBounds.x + diff, y, tickBounds.x
1081:                        + tickBounds.width / 2 + diff, y);
1082:            }
1083:
1084:            protected void paintMajorTickForVertSlider(final Graphics g,
1085:                    final Rectangle tickBounds, final int y) {
1086:                g.drawLine(tickBounds.x, y, tickBounds.x + tickBounds.width, y);
1087:            }
1088:
1089:            public void paintLabels(final Graphics g) {
1090:                if (slider.getMajorTickSpacing() == 0) {
1091:                    return;
1092:                }
1093:                Dictionary labelTable = slider.getLabelTable();
1094:                if (labelTable == null) {
1095:                    return;
1096:                }
1097:                if (slider.getOrientation() == JSlider.HORIZONTAL) {
1098:                    int value = slider.getMinimum();
1099:                    while (value <= slider.getMaximum()) {
1100:                        Component label = (Component) labelTable
1101:                                .get(new Integer(value));
1102:                        if (label != null) {
1103:                            paintHorizontalLabel(g, value, label);
1104:                        }
1105:                        value += slider.getMajorTickSpacing();
1106:                    }
1107:                } else {
1108:                    int value = slider.getMinimum();
1109:                    while (value <= slider.getMaximum()) {
1110:                        Component label = (Component) labelTable
1111:                                .get(new Integer(value));
1112:                        if (label != null) {
1113:                            paintVerticalLabel(g, value, label);
1114:                        }
1115:                        value += slider.getMajorTickSpacing();
1116:                    }
1117:                }
1118:            }
1119:
1120:            protected void paintHorizontalLabel(final Graphics g,
1121:                    final int value, final Component label) {
1122:                int x = xPositionForValue(value) - label.getWidth() / 2;
1123:                ;
1124:                int y = labelRect.y;
1125:                g.translate(x, y);
1126:
1127:                label.paint(g);
1128:
1129:                g.translate(-x, -y);
1130:            }
1131:
1132:            protected void paintVerticalLabel(final Graphics g,
1133:                    final int value, final Component label) {
1134:                int x;
1135:                if (slider.getComponentOrientation().isLeftToRight()) {
1136:                    x = labelRect.x;
1137:                } else {
1138:                    x = labelRect.x + labelRect.width - label.getWidth();
1139:                }
1140:                int y = yPositionForValue(value) - label.getHeight() / 2;
1141:                g.translate(x, y);
1142:
1143:                label.paint(g);
1144:
1145:                g.translate(-x, -y);
1146:            }
1147:
1148:            public void paintThumb(final Graphics g) {
1149:                if (slider.getPaintTicks()) {
1150:                    Color oldColor = g.getColor();
1151:                    g.setColor(slider.getBackground());
1152:                    g.fillRect(thumbRect.x, thumbRect.y, getThumbSize().width,
1153:                            getThumbSize().height);
1154:                    g.setColor(oldColor);
1155:                    paintThumbWithPointer(g);
1156:                } else {
1157:                    Color oldColor = g.getColor();
1158:                    g.setColor(slider.getBackground());
1159:                    g.fillRect(thumbRect.x, thumbRect.y, getThumbSize().width,
1160:                            getThumbSize().height);
1161:                    g.setColor(oldColor);
1162:                    g.fillRect(thumbRect.x, thumbRect.y, getThumbSize().width,
1163:                            getThumbSize().height);
1164:                    Utilities.draw3DRect(g, thumbRect.x, thumbRect.y,
1165:                            getThumbSize().width, getThumbSize().height,
1166:                            Color.GRAY, Color.WHITE, true);
1167:                }
1168:            }
1169:
1170:            public void setThumbLocation(final int x, final int y) {
1171:                thumbRect.setLocation(x, y);
1172:            }
1173:
1174:            int computeIncrement() {
1175:
1176:                if (slider.getMajorTickSpacing() != 0) {
1177:
1178:                    return slider.getMajorTickSpacing();
1179:
1180:                } else {
1181:
1182:                    int increment = (slider.getMaximum() - slider.getMinimum()) / 10;
1183:                    if (increment <= 0) {
1184:                        increment = 1;
1185:                    }
1186:                    return increment;
1187:                }
1188:            }
1189:
1190:            public void scrollByBlock(final int direction) {
1191:                //Changed in H-4480
1192:                scrollByIncrement(direction, computeIncrement());
1193:            }
1194:
1195:            public void scrollByUnit(final int direction) {
1196:                scrollByIncrement(direction, UNIT_INCREMENT);
1197:            }
1198:
1199:            protected void scrollDueToClickInTrack(final int dir) {
1200:                scrollByBlock(dir);
1201:            }
1202:
1203:            protected int xPositionForValue(final int value) {
1204:                // Changed according to JIRA 4445
1205:                double valueToSizeRatio = (double) value
1206:                        / (double) (slider.getMaximum() - slider.getMinimum());
1207:
1208:                if ((drawInverted() ^ !slider.getComponentOrientation()
1209:                        .isLeftToRight())) {
1210:                    return (int) (trackRect.x + trackRect.width - (trackRect.width * valueToSizeRatio));
1211:                } else {
1212:                    return (int) (trackRect.x + trackRect.width
1213:                            * valueToSizeRatio);
1214:                }
1215:
1216:            }
1217:
1218:            protected int yPositionForValue(final int value) {
1219:                // Changed according to JIRA 4445
1220:                double valueToSizeRatio = (double) value
1221:                        / (double) (slider.getMaximum() - slider.getMinimum());
1222:
1223:                if ((drawInverted() ^ (!slider.getComponentOrientation()
1224:                        .isLeftToRight() && slider.getOrientation() == JSlider.HORIZONTAL))) {
1225:                    return (int) (trackRect.y + trackRect.height
1226:                            * valueToSizeRatio);
1227:                } else {
1228:                    return (int) (trackRect.y + trackRect.height - (trackRect.height * valueToSizeRatio));
1229:                }
1230:            }
1231:
1232:            public int valueForYPosition(final int yPos) {
1233:                int size = slider.getMaximum() - slider.getMinimum();
1234:                int intervalSize = trackRect.height / size;
1235:
1236:                int result = drawInverted()
1237:                        ^ (!slider.getComponentOrientation().isLeftToRight() && slider
1238:                                .getOrientation() == JSlider.HORIZONTAL) ? slider
1239:                        .getMinimum()
1240:                        + (yPos - trackRect.y + intervalSize / 2)
1241:                        * size
1242:                        / trackRect.height
1243:                        : slider.getMinimum()
1244:                                + (trackRect.y + trackRect.height - yPos + intervalSize / 2)
1245:                                * size / trackRect.height;
1246:                result = (result < slider.getMinimum()) ? slider.getMinimum()
1247:                        : result;
1248:                result = (result > slider.getMaximum()) ? slider.getMaximum()
1249:                        : result;
1250:
1251:                return result;
1252:            }
1253:
1254:            public int valueForXPosition(final int xPos) {
1255:                int size = slider.getMaximum() - slider.getMinimum();
1256:                int intervalSize = trackRect.width / size;
1257:
1258:                int result = drawInverted()
1259:                        ^ !slider.getComponentOrientation().isLeftToRight() ? slider
1260:                        .getMinimum()
1261:                        + (trackRect.x + trackRect.width - xPos + intervalSize / 2)
1262:                        * size / trackRect.width
1263:                        : slider.getMinimum()
1264:                                + (xPos - trackRect.x + intervalSize / 2)
1265:                                * size / trackRect.width;
1266:                result = (result < slider.getMinimum()) ? slider.getMinimum()
1267:                        : result;
1268:                result = (result > slider.getMaximum()) ? slider.getMaximum()
1269:                        : result;
1270:
1271:                return result;
1272:            }
1273:
1274:            private Action newMaxScrollAction() {
1275:                return new AbstractAction() {
1276:                    private static final long serialVersionUID = -3822301141065864044L;
1277:
1278:                    public void actionPerformed(final ActionEvent e) {
1279:                        if (drawInverted()) {
1280:                            slider.setValue(slider.getMinimum());
1281:                        } else {
1282:                            slider.setValue(slider.getMaximum());
1283:                        }
1284:                        slider.repaint();
1285:                    }
1286:                };
1287:            }
1288:
1289:            private Action newMinScrollAction() {
1290:                return new AbstractAction() {
1291:                    private static final long serialVersionUID = 703565386507416752L;
1292:
1293:                    public void actionPerformed(final ActionEvent e) {
1294:                        if (drawInverted()) {
1295:                            slider.setValue(slider.getMaximum());
1296:                        } else {
1297:                            slider.setValue(slider.getMinimum());
1298:                        }
1299:                        slider.repaint();
1300:                    }
1301:                };
1302:            }
1303:
1304:            private Action newNegativeBlockIncrementAction() {
1305:                return new AbstractAction() {
1306:                    private static final long serialVersionUID = 7818668169396841520L;
1307:
1308:                    public void actionPerformed(final ActionEvent e) {
1309:                        scrollByBlock(NEGATIVE_SCROLL);
1310:                    }
1311:                };
1312:            }
1313:
1314:            private Action newNegativeUnitIncrementAction() {
1315:                return new AbstractAction() {
1316:                    private static final long serialVersionUID = -4366059737366026435L;
1317:
1318:                    public void actionPerformed(final ActionEvent e) {
1319:                        scrollByUnit(NEGATIVE_SCROLL);
1320:                    }
1321:                };
1322:            }
1323:
1324:            private Action newPositiveBlockIncrementAction() {
1325:                return new AbstractAction() {
1326:                    private static final long serialVersionUID = -5999323396935662487L;
1327:
1328:                    public void actionPerformed(final ActionEvent e) {
1329:                        scrollByBlock(POSITIVE_SCROLL);
1330:                    }
1331:                };
1332:            }
1333:
1334:            private Action newPositiveUnitIncrementAction() {
1335:                return new AbstractAction() {
1336:                    private static final long serialVersionUID = 5166413389559469128L;
1337:
1338:                    public void actionPerformed(final ActionEvent e) {
1339:                        scrollByUnit(POSITIVE_SCROLL);
1340:                    }
1341:                };
1342:            }
1343:
1344:            private void scrollByIncrement(final int direction,
1345:                    final int increment) {
1346:                int dir = direction;
1347:                if (drawInverted()) {
1348:                    dir = dir * (-1);
1349:                }
1350:                if (dir >= POSITIVE_SCROLL) {
1351:                    slider.setValue(slider.getValue() + increment);
1352:                } else {
1353:                    slider.setValue(slider.getValue() - increment);
1354:                }
1355:            }
1356:
1357:            private void paintThumbWithPointer(final Graphics g) {
1358:                int x = thumbRect.x;
1359:                int y = thumbRect.y;
1360:                Color shadow = Color.gray;
1361:                Color highlight = Color.white;
1362:
1363:                final Color oldColor = g.getColor();
1364:                final Color topLeft = highlight;
1365:                final Color bottomRight = shadow;
1366:                final int bottom = y + getThumbSize().height - 1;
1367:                final int right = x + getThumbSize().width - 1;
1368:                g.setColor(topLeft);
1369:                if (slider.getOrientation() == JSlider.HORIZONTAL) {
1370:                    g.drawLine(x, y, right, y);
1371:                    g.drawLine(right, y, right, bottom - getThumbSize().height
1372:                            / 4);
1373:                    g.drawLine(right, bottom - getThumbSize().height / 4, x
1374:                            + getThumbSize().width / 2, bottom);
1375:                } else {
1376:                    g.drawLine(x, y, x, bottom);
1377:                    g.drawLine(x, y, right - getThumbSize().width / 4, y);
1378:                    g.drawLine(right - getThumbSize().width / 4, y, right, y
1379:                            + getThumbSize().height / 2);
1380:                }
1381:                g.setColor(bottomRight);
1382:                if (slider.getOrientation() == JSlider.HORIZONTAL) {
1383:                    g.drawLine(x, y, x, bottom - getThumbSize().height / 4);
1384:                    g.drawLine(x, bottom - getThumbSize().height / 4, x
1385:                            + getThumbSize().width / 2, bottom);
1386:                } else {
1387:                    g.drawLine(x, bottom, right - getThumbSize().width / 4,
1388:                            bottom);
1389:                    g.drawLine(right - getThumbSize().width / 4, bottom, right,
1390:                            y + getThumbSize().height / 2);
1391:                }
1392:                g.setColor(oldColor);
1393:            }
1394:
1395:            private int calculateTickSpacing() {
1396:                if (slider.getMinorTickSpacing() != 0
1397:                        && slider.getMajorTickSpacing() != 0) {
1398:                    return slider.getMinorTickSpacing();
1399:                } else if (slider.getMajorTickSpacing() != 0) {
1400:                    return slider.getMajorTickSpacing();
1401:                } else {
1402:                    return 1;
1403:                }
1404:            }
1405:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.