synthsliderui.java

来自「java jdk 1.4的源码」· Java 代码 · 共 1,576 行 · 第 1/4 页

JAVA
1,576
字号
                    propertyName.equals("paintLabels")) {                calculateGeometry();                slider.repaint();            } else if (propertyName.equals("componentOrientation")) {                calculateGeometry();                slider.repaint();                InputMap km = getInputMap(JComponent.WHEN_FOCUSED);                SwingUtilities.replaceUIInputMap(slider,                    JComponent.WHEN_FOCUSED, km);            } else if (propertyName.equals("model")) {                ((BoundedRangeModel)e.getOldValue()).removeChangeListener(                    changeListener);                ((BoundedRangeModel)e.getNewValue()).addChangeListener(                    changeListener);                calculateThumbLocation();                slider.repaint();            }            if (SynthLookAndFeel.shouldUpdateStyle(e)) {                fetchStyle((JSlider)e.getSource());            }        }    }    /////////////////////////////////////////////////////////////////////////    /// Model Listener Class    /////////////////////////////////////////////////////////////////////////            /**     * Data model listener.     *     * This inner class is marked &quot;public&quot; due to a compiler bug.     * This class should be treated as a &quot;protected&quot; inner class.     * Instantiate it only within subclasses of <Foo>.     */    class ChangeHandler implements ChangeListener {        public void stateChanged(ChangeEvent e) {            if (!isDragging) {                calculateThumbLocation();                slider.repaint();            }        }    }    //////////////////////////////////////////////////    /// Track Listener Class    //////////////////////////////////////////////////    /**     * Track mouse movements.     */    protected class TrackListener extends MouseInputAdapter {        protected transient int offset;        protected transient int currentMouseX, currentMouseY;        public void mouseDragged(MouseEvent e) {            SynthScrollBarUI ui;            int thumbMiddle = 0;            if (!slider.isEnabled()) {                return;            }            currentMouseX = e.getX();            currentMouseY = e.getY();            if (!isDragging) {                return;            }            slider.setValueIsAdjusting(true);            switch (slider.getOrientation()) {            case JSlider.VERTICAL:                      int halfThumbHeight = thumbRect.height / 2;                int thumbTop = e.getY() - offset;                int trackTop = trackRect.y;                int trackBottom = trackRect.y + trackRect.height                    - halfThumbHeight - trackBorder;                int vMax = yPositionForValue(slider.getMaximum() -                    slider.getExtent());                if (drawInverted()) {                    trackBottom = vMax;                } else {                    trackTop = vMax;                }                thumbTop = Math.max(thumbTop, trackTop - halfThumbHeight);                thumbTop = Math.min(thumbTop, trackBottom - halfThumbHeight);                setThumbLocation(thumbRect.x, thumbTop);                thumbMiddle = thumbTop + halfThumbHeight;                slider.setValue(valueForYPosition(thumbMiddle));                break;            case JSlider.HORIZONTAL:                int halfThumbWidth = thumbRect.width / 2;                int thumbLeft = e.getX() - offset;                int trackLeft = trackRect.x + halfThumbWidth + trackBorder;                int trackRight = trackRect.x + trackRect.width                    - halfThumbWidth - trackBorder;                int hMax = xPositionForValue(slider.getMaximum() -                    slider.getExtent());                if (drawInverted()) {                    trackLeft = hMax;                } else {                    trackRight = hMax;                }                thumbLeft = Math.max(thumbLeft, trackLeft - halfThumbWidth);                thumbLeft = Math.min(thumbLeft, trackRight - halfThumbWidth);                setThumbLocation(thumbLeft, thumbRect.y);                thumbMiddle = thumbLeft + halfThumbWidth;                slider.setValue(valueForXPosition(thumbMiddle));                break;            default:                return;            }            if (slider.getValueIsAdjusting()) {                setThumbActive(true);            }        }        public void mouseReleased(MouseEvent e) {            if (!slider.isEnabled()) {                return;            }            offset = 0;            scrollTimer.stop();            // This is the way we have to determine snap-to-ticks.  It's            // hard to explain but since ChangeEvents don't give us any idea            // what has changed we don't have a way to stop the thumb bounds            // from being recalculated.  Recalculating the thumb bounds moves            // the thumb over the current value (i.e., snapping to the ticks).            if ( slider.getSnapToTicks() /*|| slider.getSnapToValue()*/ ) {                isDragging = false;                slider.setValueIsAdjusting(false);            }            else {                slider.setValueIsAdjusting(false);                isDragging = false;            }                    updateThumbState(e.getX(), e.getY());            slider.repaint();        }        public void mouseMoved(MouseEvent e) {            updateThumbState(e.getX(), e.getY());        }        /**        * If the mouse is pressed above the "thumb" component        * then reduce the scrollbars value by one page ("page up"),         * otherwise increase it by one page.  If there is no         * thumb then page up if the mouse is in the upper half        * of the track.        */        public void mousePressed(MouseEvent e)                {            if (!slider.isEnabled()) {                return;            }            currentMouseX = e.getX();            currentMouseY = e.getY();            if (slider.isRequestFocusEnabled()) {                slider.requestFocus();            }            // Clicked in the Thumb area?            if (thumbRect.contains(currentMouseX, currentMouseY)) {                switch (slider.getOrientation()) {                case JSlider.VERTICAL:                    offset = currentMouseY - thumbRect.y;                    break;                case JSlider.HORIZONTAL:                    offset = currentMouseX - thumbRect.x;                    break;                }                isDragging = true;                return;            }            isDragging = false;            slider.setValueIsAdjusting(true);            Dimension sbSize = slider.getSize();            int direction = POSITIVE_SCROLL;            switch (slider.getOrientation()) {            case JSlider.VERTICAL:                if (thumbRect.isEmpty()) {                    int scrollbarCenter = sbSize.height / 2;                    if (!drawInverted()) {                        direction = (currentMouseY < scrollbarCenter) ?                            POSITIVE_SCROLL : NEGATIVE_SCROLL;                    }                    else {                        direction = (currentMouseY < scrollbarCenter) ?                            NEGATIVE_SCROLL : POSITIVE_SCROLL;                    }                }                else {                    int thumbY = thumbRect.y;                    if (!drawInverted()) {                        direction = (currentMouseY < thumbY) ?                            POSITIVE_SCROLL : NEGATIVE_SCROLL;                    }                    else {                        direction = (currentMouseY < thumbY) ?                            NEGATIVE_SCROLL : POSITIVE_SCROLL;                    }                }                break;                                case JSlider.HORIZONTAL:                if (thumbRect.isEmpty()) {                    int scrollbarCenter = sbSize.width / 2;                    if (!drawInverted()) {                        direction = (currentMouseX < scrollbarCenter) ?                            NEGATIVE_SCROLL : POSITIVE_SCROLL;                    }                    else {                        direction = (currentMouseX < scrollbarCenter) ?                            POSITIVE_SCROLL : NEGATIVE_SCROLL;                    }                } else {                    int thumbX = thumbRect.x;                    if (!drawInverted()) {                        direction = (currentMouseX < thumbX) ?                            NEGATIVE_SCROLL : POSITIVE_SCROLL;                    }                    else {                        direction = (currentMouseX < thumbX) ?                            POSITIVE_SCROLL : NEGATIVE_SCROLL;                    }                }                break;            }            scrollDueToClickInTrack(direction);            Rectangle r = thumbRect;            if (!r.contains(currentMouseX, currentMouseY)) {                if (shouldScroll(direction)) {                    scrollTimer.stop();                    scrollListener.setDirection(direction);                    scrollTimer.start();                }            }        }        public boolean shouldScroll(int direction) {            Rectangle r = thumbRect;            if (slider.getOrientation() == JSlider.VERTICAL) {                if (drawInverted() ? direction < 0 : direction > 0) {                    if (r.y + r.height  <= currentMouseY) {                        return false;                    }                } else if (r.y >= currentMouseY) {                    return false;                }            } else {                if (drawInverted() ? direction < 0 : direction > 0) {                    if (r.x + r.width  >= currentMouseX) {                        return false;                    }                } else if (r.x <= currentMouseX) {                    return false;                }            }            if (direction >0 && slider.getValue()                    + slider.getExtent() >= slider.getMaximum()) {                return false;            } else if (direction < 0                    && slider.getValue() <= slider.getMinimum()) {                return false;            }            return true;        }    }    /**     * Scroll-event listener.     *     * This inner class is marked &quot;public&quot; due to a compiler bug.     * This class should be treated as a &quot;protected&quot; inner class.     * Instantiate it only within subclasses of <Foo>.     */    class ScrollListener implements ActionListener {        // changed this class to public to avoid bogus IllegalAccessException        // bug i InternetExplorer browser.  It was protected.  Work around        // for 4109432.        int direction = POSITIVE_SCROLL;        boolean useBlockIncrement;        public ScrollListener() {            direction = POSITIVE_SCROLL;            useBlockIncrement = true;        }        public ScrollListener(int dir, boolean block)   {            direction = dir;            useBlockIncrement = block;        }        public void setDirection(int direction) { this.direction = direction; }        public void setScrollByBlock(boolean block) {            this.useBlockIncrement = block;        }        public void actionPerformed(ActionEvent e) {            if (useBlockIncrement) {                scrollByBlock(direction);            } else {                scrollByUnit(direction);            }            if (!trackListener.shouldScroll(direction)) {                ((Timer)e.getSource()).stop();            }        }    }    /**     * Listener for resizing events.     * <p>     * This inner class is marked &quot;public&quot; due to a compiler bug.     * This class should be treated as a &quot;protected&quot; inner class.     * Instantiate it only within subclasses of <Foo>.     */    class ComponentHandler extends ComponentAdapter {        public void componentResized(ComponentEvent e)  {            calculateGeometry();            slider.repaint();        }    }    /**     * Focus-change listener.     * <p>     * This inner class is marked &quot;public&quot; due to a compiler bug.     * This class should be treated as a &quot;protected&quot; inner class.     * Instantiate it only within subclasses of <Foo>.     */    class FocusHandler implements FocusListener {        public void focusGained(FocusEvent e) { slider.repaint();}         public void focusLost(FocusEvent e) { slider.repaint();}    }    static class SharedActionScroller extends AbstractAction {        int dir;        boolean block;        public SharedActionScroller(int dir, boolean block) {            this.dir = dir;            this.block = block;        }        public void actionPerformed(ActionEvent e) {            JSlider slider = (JSlider)e.getSource();            if (dir == NEGATIVE_SCROLL || dir == POSITIVE_SCROLL) {                int realDir = dir;                SynthSliderUI ui = (SynthSliderUI)slider.getUI();                if (slider.getInverted()) {                    realDir = dir == NEGATIVE_SCROLL ?                        POSITIVE_SCROLL : NEGATIVE_SCROLL;                }                                if (block) {                    ui.scrollByBlock(realDir);                } else {                    ui.scrollByUnit(realDir);                }            } else {                if (slider.getInverted()) {                    if (dir == MIN_SCROLL) {                        slider.setValue(slider.getMaximum());                    } else if (dir == MAX_SCROLL) {                        slider.setValue(slider.getMinimum());                    }                } else {                    if (dir == MIN_SCROLL) {                        slider.setValue(slider.getMinimum());                    } else if (dir == MAX_SCROLL) {                        slider.setValue(slider.getMaximum());                    }                }                   }        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?