< prev index next >

src/java.desktop/share/classes/javax/swing/plaf/basic/BasicSliderUI.java

Print this page




1668             xPosition -= Math.round( pixelsPerValue * ((double)value - min) );
1669         }
1670 
1671         xPosition = Math.max( trackLeft, xPosition );
1672         xPosition = Math.min( trackRight, xPosition );
1673 
1674         return xPosition;
1675     }
1676 
1677     /**
1678      * Returns the y position for a value.
1679      * @param value the value
1680      * @return the y position for a value
1681      */
1682     protected int yPositionForValue( int value )  {
1683         return yPositionForValue(value, trackRect.y, trackRect.height);
1684     }
1685 
1686     /**
1687      * Returns the y location for the specified value.  No checking is
1688      * done on the arguments.  In particular if <code>trackHeight</code> is
1689      * negative undefined results may occur.
1690      *
1691      * @param value the slider value to get the location for
1692      * @param trackY y-origin of the track
1693      * @param trackHeight the height of the track
1694      * @return the y location for the specified value of the slider
1695      * @since 1.6
1696      */
1697     protected int yPositionForValue(int value, int trackY, int trackHeight) {
1698         int min = slider.getMinimum();
1699         int max = slider.getMaximum();
1700         double valueRange = (double)max - (double)min;
1701         double pixelsPerValue = (double)trackHeight / valueRange;
1702         int trackBottom = trackY + (trackHeight - 1);
1703         int yPosition;
1704 
1705         if ( !drawInverted() ) {
1706             yPosition = trackY;
1707             yPosition += Math.round( pixelsPerValue * ((double)max - value ) );
1708         }


1836                 SwingUtilities.replaceUIInputMap(slider,
1837                     JComponent.WHEN_FOCUSED, km);
1838             } else if (propertyName == "model") {
1839                 ((BoundedRangeModel)e.getOldValue()).removeChangeListener(
1840                     changeListener);
1841                 ((BoundedRangeModel)e.getNewValue()).addChangeListener(
1842                     changeListener);
1843                 calculateThumbLocation();
1844                 slider.repaint();
1845             }
1846         }
1847     }
1848 
1849     /////////////////////////////////////////////////////////////////////////
1850     /// Model Listener Class
1851     /////////////////////////////////////////////////////////////////////////
1852     /**
1853      * Data model listener.
1854      *
1855      * This class should be treated as a &quot;protected&quot; inner class.
1856      * Instantiate it only within subclasses of <code>Foo</code>.
1857      */
1858     public class ChangeHandler implements ChangeListener {
1859         // NOTE: This class exists only for backward compatibility. All
1860         // its functionality has been moved into Handler. If you need to add
1861         // new functionality add it to the Handler, but make sure this
1862         // class calls into the Handler.
1863         public void stateChanged(ChangeEvent e) {
1864             getHandler().stateChanged(e);
1865         }
1866     }
1867 
1868     /////////////////////////////////////////////////////////////////////////
1869     /// Track Listener Class
1870     /////////////////////////////////////////////////////////////////////////
1871     /**
1872      * Track mouse movements.
1873      *
1874      * This class should be treated as a &quot;protected&quot; inner class.
1875      * Instantiate it only within subclasses of <code>Foo</code>.
1876      */
1877     public class TrackListener extends MouseInputAdapter {
1878         /** The offset */
1879         protected transient int offset;
1880         /** Current mouse x. */
1881         protected transient int currentMouseX;
1882         /** Current mouse y. */
1883         protected transient int currentMouseY;
1884 
1885         /**
1886          * {@inheritDoc}
1887          */
1888         public void mouseReleased(MouseEvent e) {
1889             if (!slider.isEnabled()) {
1890                 return;
1891             }
1892 
1893             offset = 0;
1894             scrollTimer.stop();
1895 


2112                 }
2113                 thumbLeft = Math.max(thumbLeft, trackLeft - halfThumbWidth);
2114                 thumbLeft = Math.min(thumbLeft, trackRight - halfThumbWidth);
2115 
2116                 setThumbLocation(thumbLeft, thumbRect.y);
2117 
2118                 thumbMiddle = thumbLeft + halfThumbWidth;
2119                 slider.setValue(valueForXPosition(thumbMiddle));
2120                 break;
2121             }
2122         }
2123 
2124         /** {@inheritDoc} */
2125         public void mouseMoved(MouseEvent e) { }
2126     }
2127 
2128     /**
2129      * Scroll-event listener.
2130      *
2131      * This class should be treated as a &quot;protected&quot; inner class.
2132      * Instantiate it only within subclasses of <code>Foo</code>.
2133      */
2134     public class ScrollListener implements ActionListener {
2135         // changed this class to public to avoid bogus IllegalAccessException
2136         // bug in InternetExplorer browser.  It was protected.  Work around
2137         // for 4109432
2138         int direction = POSITIVE_SCROLL;
2139         boolean useBlockIncrement;
2140 
2141         /**
2142          * Constructs a {@code ScrollListener}
2143          */
2144         public ScrollListener() {
2145             direction = POSITIVE_SCROLL;
2146             useBlockIncrement = true;
2147         }
2148 
2149         /**
2150          * Constructs a {@code ScrollListener}
2151          * @param dir the direction
2152          * @param block whether or not to scroll by block


2173         }
2174 
2175         /** {@inheritDoc} */
2176         public void actionPerformed(ActionEvent e) {
2177             if (useBlockIncrement) {
2178                 scrollByBlock(direction);
2179             }
2180             else {
2181                 scrollByUnit(direction);
2182             }
2183             if (!trackListener.shouldScroll(direction)) {
2184                 ((Timer)e.getSource()).stop();
2185             }
2186         }
2187     }
2188 
2189     /**
2190      * Listener for resizing events.
2191      * <p>
2192      * This class should be treated as a &quot;protected&quot; inner class.
2193      * Instantiate it only within subclasses of <code>Foo</code>.
2194      */
2195     public class ComponentHandler extends ComponentAdapter {
2196         // NOTE: This class exists only for backward compatibility. All
2197         // its functionality has been moved into Handler. If you need to add
2198         // new functionality add it to the Handler, but make sure this
2199         // class calls into the Handler.
2200         public void componentResized(ComponentEvent e)  {
2201             getHandler().componentResized(e);
2202         }
2203     }
2204 
2205     /**
2206      * Focus-change listener.
2207      * <p>
2208      * This class should be treated as a &quot;protected&quot; inner class.
2209      * Instantiate it only within subclasses of <code>Foo</code>.
2210      */
2211     public class FocusHandler implements FocusListener {
2212         // NOTE: This class exists only for backward compatibility. All
2213         // its functionality has been moved into Handler. If you need to add
2214         // new functionality add it to the Handler, but make sure this
2215         // class calls into the Handler.
2216         public void focusGained(FocusEvent e) {
2217             getHandler().focusGained(e);
2218         }
2219 
2220         public void focusLost(FocusEvent e) {
2221             getHandler().focusLost(e);
2222         }
2223     }
2224 
2225     /**
2226      * As of Java 2 platform v1.3 this undocumented class is no longer used.
2227      * The recommended approach to creating bindings is to use a
2228      * combination of an <code>ActionMap</code>, to contain the action,
2229      * and an <code>InputMap</code> to contain the mapping from KeyStroke
2230      * to action description. The InputMap is usually described in the
2231      * LookAndFeel tables.
2232      * <p>
2233      * Please refer to the key bindings specification for further details.
2234      * <p>
2235      * This class should be treated as a &quot;protected&quot; inner class.
2236      * Instantiate it only within subclasses of <code>Foo</code>.
2237      */
2238     @SuppressWarnings("serial") // Superclass is not serializable across versions
2239     public class ActionScroller extends AbstractAction {
2240         // NOTE: This class exists only for backward compatibility. All
2241         // its functionality has been moved into Actions. If you need to add
2242         // new functionality add it to the Actions, but make sure this
2243         // class calls into the Actions.
2244         int dir;
2245         boolean block;
2246         JSlider slider;
2247 
2248         /**
2249          * Constructs an {@code ActionScroller}.
2250          * @param slider a slider
2251          * @param dir the direction
2252          * @param block block scrolling or not
2253          */
2254         public ActionScroller( JSlider slider, int dir, boolean block) {
2255             this.dir = dir;
2256             this.block = block;




1668             xPosition -= Math.round( pixelsPerValue * ((double)value - min) );
1669         }
1670 
1671         xPosition = Math.max( trackLeft, xPosition );
1672         xPosition = Math.min( trackRight, xPosition );
1673 
1674         return xPosition;
1675     }
1676 
1677     /**
1678      * Returns the y position for a value.
1679      * @param value the value
1680      * @return the y position for a value
1681      */
1682     protected int yPositionForValue( int value )  {
1683         return yPositionForValue(value, trackRect.y, trackRect.height);
1684     }
1685 
1686     /**
1687      * Returns the y location for the specified value.  No checking is
1688      * done on the arguments.  In particular if {@code trackHeight} is
1689      * negative undefined results may occur.
1690      *
1691      * @param value the slider value to get the location for
1692      * @param trackY y-origin of the track
1693      * @param trackHeight the height of the track
1694      * @return the y location for the specified value of the slider
1695      * @since 1.6
1696      */
1697     protected int yPositionForValue(int value, int trackY, int trackHeight) {
1698         int min = slider.getMinimum();
1699         int max = slider.getMaximum();
1700         double valueRange = (double)max - (double)min;
1701         double pixelsPerValue = (double)trackHeight / valueRange;
1702         int trackBottom = trackY + (trackHeight - 1);
1703         int yPosition;
1704 
1705         if ( !drawInverted() ) {
1706             yPosition = trackY;
1707             yPosition += Math.round( pixelsPerValue * ((double)max - value ) );
1708         }


1836                 SwingUtilities.replaceUIInputMap(slider,
1837                     JComponent.WHEN_FOCUSED, km);
1838             } else if (propertyName == "model") {
1839                 ((BoundedRangeModel)e.getOldValue()).removeChangeListener(
1840                     changeListener);
1841                 ((BoundedRangeModel)e.getNewValue()).addChangeListener(
1842                     changeListener);
1843                 calculateThumbLocation();
1844                 slider.repaint();
1845             }
1846         }
1847     }
1848 
1849     /////////////////////////////////////////////////////////////////////////
1850     /// Model Listener Class
1851     /////////////////////////////////////////////////////////////////////////
1852     /**
1853      * Data model listener.
1854      *
1855      * This class should be treated as a &quot;protected&quot; inner class.
1856      * Instantiate it only within subclasses of {@code Foo}.
1857      */
1858     public class ChangeHandler implements ChangeListener {
1859         // NOTE: This class exists only for backward compatibility. All
1860         // its functionality has been moved into Handler. If you need to add
1861         // new functionality add it to the Handler, but make sure this
1862         // class calls into the Handler.
1863         public void stateChanged(ChangeEvent e) {
1864             getHandler().stateChanged(e);
1865         }
1866     }
1867 
1868     /////////////////////////////////////////////////////////////////////////
1869     /// Track Listener Class
1870     /////////////////////////////////////////////////////////////////////////
1871     /**
1872      * Track mouse movements.
1873      *
1874      * This class should be treated as a &quot;protected&quot; inner class.
1875      * Instantiate it only within subclasses of {@code Foo}.
1876      */
1877     public class TrackListener extends MouseInputAdapter {
1878         /** The offset */
1879         protected transient int offset;
1880         /** Current mouse x. */
1881         protected transient int currentMouseX;
1882         /** Current mouse y. */
1883         protected transient int currentMouseY;
1884 
1885         /**
1886          * {@inheritDoc}
1887          */
1888         public void mouseReleased(MouseEvent e) {
1889             if (!slider.isEnabled()) {
1890                 return;
1891             }
1892 
1893             offset = 0;
1894             scrollTimer.stop();
1895 


2112                 }
2113                 thumbLeft = Math.max(thumbLeft, trackLeft - halfThumbWidth);
2114                 thumbLeft = Math.min(thumbLeft, trackRight - halfThumbWidth);
2115 
2116                 setThumbLocation(thumbLeft, thumbRect.y);
2117 
2118                 thumbMiddle = thumbLeft + halfThumbWidth;
2119                 slider.setValue(valueForXPosition(thumbMiddle));
2120                 break;
2121             }
2122         }
2123 
2124         /** {@inheritDoc} */
2125         public void mouseMoved(MouseEvent e) { }
2126     }
2127 
2128     /**
2129      * Scroll-event listener.
2130      *
2131      * This class should be treated as a &quot;protected&quot; inner class.
2132      * Instantiate it only within subclasses of {@code Foo}.
2133      */
2134     public class ScrollListener implements ActionListener {
2135         // changed this class to public to avoid bogus IllegalAccessException
2136         // bug in InternetExplorer browser.  It was protected.  Work around
2137         // for 4109432
2138         int direction = POSITIVE_SCROLL;
2139         boolean useBlockIncrement;
2140 
2141         /**
2142          * Constructs a {@code ScrollListener}
2143          */
2144         public ScrollListener() {
2145             direction = POSITIVE_SCROLL;
2146             useBlockIncrement = true;
2147         }
2148 
2149         /**
2150          * Constructs a {@code ScrollListener}
2151          * @param dir the direction
2152          * @param block whether or not to scroll by block


2173         }
2174 
2175         /** {@inheritDoc} */
2176         public void actionPerformed(ActionEvent e) {
2177             if (useBlockIncrement) {
2178                 scrollByBlock(direction);
2179             }
2180             else {
2181                 scrollByUnit(direction);
2182             }
2183             if (!trackListener.shouldScroll(direction)) {
2184                 ((Timer)e.getSource()).stop();
2185             }
2186         }
2187     }
2188 
2189     /**
2190      * Listener for resizing events.
2191      * <p>
2192      * This class should be treated as a &quot;protected&quot; inner class.
2193      * Instantiate it only within subclasses of {@code Foo}.
2194      */
2195     public class ComponentHandler extends ComponentAdapter {
2196         // NOTE: This class exists only for backward compatibility. All
2197         // its functionality has been moved into Handler. If you need to add
2198         // new functionality add it to the Handler, but make sure this
2199         // class calls into the Handler.
2200         public void componentResized(ComponentEvent e)  {
2201             getHandler().componentResized(e);
2202         }
2203     }
2204 
2205     /**
2206      * Focus-change listener.
2207      * <p>
2208      * This class should be treated as a &quot;protected&quot; inner class.
2209      * Instantiate it only within subclasses of {@code Foo}.
2210      */
2211     public class FocusHandler implements FocusListener {
2212         // NOTE: This class exists only for backward compatibility. All
2213         // its functionality has been moved into Handler. If you need to add
2214         // new functionality add it to the Handler, but make sure this
2215         // class calls into the Handler.
2216         public void focusGained(FocusEvent e) {
2217             getHandler().focusGained(e);
2218         }
2219 
2220         public void focusLost(FocusEvent e) {
2221             getHandler().focusLost(e);
2222         }
2223     }
2224 
2225     /**
2226      * As of Java 2 platform v1.3 this undocumented class is no longer used.
2227      * The recommended approach to creating bindings is to use a
2228      * combination of an {@code ActionMap}, to contain the action,
2229      * and an {@code InputMap} to contain the mapping from KeyStroke
2230      * to action description. The InputMap is usually described in the
2231      * LookAndFeel tables.
2232      * <p>
2233      * Please refer to the key bindings specification for further details.
2234      * <p>
2235      * This class should be treated as a &quot;protected&quot; inner class.
2236      * Instantiate it only within subclasses of {@code Foo}.
2237      */
2238     @SuppressWarnings("serial") // Superclass is not serializable across versions
2239     public class ActionScroller extends AbstractAction {
2240         // NOTE: This class exists only for backward compatibility. All
2241         // its functionality has been moved into Actions. If you need to add
2242         // new functionality add it to the Actions, but make sure this
2243         // class calls into the Actions.
2244         int dir;
2245         boolean block;
2246         JSlider slider;
2247 
2248         /**
2249          * Constructs an {@code ActionScroller}.
2250          * @param slider a slider
2251          * @param dir the direction
2252          * @param block block scrolling or not
2253          */
2254         public ActionScroller( JSlider slider, int dir, boolean block) {
2255             this.dir = dir;
2256             this.block = block;


< prev index next >