|
Java example source code file (AquaScrollBarUI.java)
This example Java source code file (AquaScrollBarUI.java) is included in the alvinalexander.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
Learn more about this Java project at its project page.
The AquaScrollBarUI.java Java example source code
/*
* Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.apple.laf;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
import javax.swing.event.*;
import javax.swing.plaf.*;
import apple.laf.*;
import apple.laf.JRSUIConstants.*;
import apple.laf.JRSUIState.ScrollBarState;
import com.apple.laf.AquaUtils.RecyclableSingleton;
public class AquaScrollBarUI extends ScrollBarUI {
private static final int kInitialDelay = 300;
private static final int kNormalDelay = 100;
// when we make small and mini scrollbars, this will no longer be a constant
static final int MIN_ARROW_COLLAPSE_SIZE = 64;
// tracking state
protected boolean fIsDragging;
protected Timer fScrollTimer;
protected ScrollListener fScrollListener;
protected TrackListener fTrackListener;
protected Hit fTrackHighlight = Hit.NONE;
protected Hit fMousePart = Hit.NONE; // Which arrow (if any) we moused pressed down in (used by arrow drag tracking)
protected JScrollBar fScrollBar;
protected ModelListener fModelListener;
protected PropertyChangeListener fPropertyChangeListener;
protected final AquaPainter<ScrollBarState> painter = AquaPainter.create(JRSUIStateFactory.getScrollBar());
// Create PLAF
public static ComponentUI createUI(final JComponent c) {
return new AquaScrollBarUI();
}
public AquaScrollBarUI() { }
public void installUI(final JComponent c) {
fScrollBar = (JScrollBar)c;
installListeners();
configureScrollBarColors();
}
public void uninstallUI(final JComponent c) {
uninstallListeners();
fScrollBar = null;
}
protected void configureScrollBarColors() {
LookAndFeel.installColors(fScrollBar, "ScrollBar.background", "ScrollBar.foreground");
}
protected TrackListener createTrackListener() {
return new TrackListener();
}
protected ScrollListener createScrollListener() {
return new ScrollListener();
}
protected void installListeners() {
fTrackListener = createTrackListener();
fModelListener = createModelListener();
fPropertyChangeListener = createPropertyChangeListener();
fScrollBar.addMouseListener(fTrackListener);
fScrollBar.addMouseMotionListener(fTrackListener);
fScrollBar.getModel().addChangeListener(fModelListener);
fScrollBar.addPropertyChangeListener(fPropertyChangeListener);
fScrollListener = createScrollListener();
fScrollTimer = new Timer(kNormalDelay, fScrollListener);
fScrollTimer.setInitialDelay(kInitialDelay); // default InitialDelay?
}
protected void uninstallListeners() {
fScrollTimer.stop();
fScrollTimer = null;
fScrollBar.getModel().removeChangeListener(fModelListener);
fScrollBar.removeMouseListener(fTrackListener);
fScrollBar.removeMouseMotionListener(fTrackListener);
fScrollBar.removePropertyChangeListener(fPropertyChangeListener);
}
protected PropertyChangeListener createPropertyChangeListener() {
return new PropertyChangeHandler();
}
protected ModelListener createModelListener() {
return new ModelListener();
}
protected void syncState(final JComponent c) {
final ScrollBarState scrollBarState = painter.state;
scrollBarState.set(isHorizontal() ? Orientation.HORIZONTAL : Orientation.VERTICAL);
final float trackExtent = fScrollBar.getMaximum() - fScrollBar.getMinimum() - fScrollBar.getModel().getExtent();
if (trackExtent <= 0.0f) {
scrollBarState.set(NothingToScroll.YES);
return;
}
final ScrollBarPart pressedPart = getPressedPart();
scrollBarState.set(pressedPart);
scrollBarState.set(getState(c, pressedPart));
scrollBarState.set(NothingToScroll.NO);
scrollBarState.setValue((fScrollBar.getValue() - fScrollBar.getMinimum()) / trackExtent);
scrollBarState.setThumbStart(getThumbStart());
scrollBarState.setThumbPercent(getThumbPercent());
scrollBarState.set(shouldShowArrows() ? ShowArrows.YES : ShowArrows.NO);
}
public void paint(final Graphics g, final JComponent c) {
syncState(c);
painter.paint(g, c, 0, 0, fScrollBar.getWidth(), fScrollBar.getHeight());
}
protected State getState(final JComponent c, final ScrollBarPart pressedPart) {
if (!AquaFocusHandler.isActive(c)) return State.INACTIVE;
if (!c.isEnabled()) return State.INACTIVE;
if (pressedPart != ScrollBarPart.NONE) return State.PRESSED;
return State.ACTIVE;
}
static final RecyclableSingleton<Map hitToPressedPartMap = new RecyclableSingleton
Other Java examples (source code examples)
Here is a short list of links related to this Java AquaScrollBarUI.java source code file:
|