BasicSliderUI.java [plain text]
package javax.swing.plaf.basic;
import java.awt.Color;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Dictionary;
import java.util.Enumeration;
import javax.swing.BoundedRangeModel;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.MouseInputAdapter;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.SliderUI;
public class BasicSliderUI extends SliderUI
{
public class ChangeHandler implements ChangeListener
{
public void stateChanged(ChangeEvent e)
{
calculateThumbLocation();
slider.repaint();
}
}
protected class ComponentHandler extends ComponentAdapter
{
public void componentResized(ComponentEvent e)
{
calculateGeometry();
slider.revalidate();
slider.repaint();
}
}
public class FocusHandler implements FocusListener
{
public void focusGained(FocusEvent e)
{
}
public void focusLost(FocusEvent e)
{
}
}
public class PropertyChangeHandler implements PropertyChangeListener
{
public void propertyChange(PropertyChangeEvent e)
{
if (e.getPropertyName().equals("orientation"))
recalculateIfOrientationChanged();
else if (e.getPropertyName().equals("model"))
{
BoundedRangeModel oldModel = (BoundedRangeModel) e.getOldValue();
oldModel.removeChangeListener(changeListener);
slider.getModel().addChangeListener(changeListener);
calculateThumbLocation();
}
slider.repaint();
}
}
public class ScrollListener implements ActionListener
{
private transient int direction;
private transient boolean block;
public ScrollListener()
{
direction = POSITIVE_SCROLL;
block = false;
}
public ScrollListener(int dir, boolean block)
{
direction = dir;
this.block = block;
}
public void actionPerformed(ActionEvent e)
{
if (! trackListener.shouldScroll(direction))
{
scrollTimer.stop();
return;
}
if (block)
scrollByBlock(direction);
else
scrollByUnit(direction);
}
public void setDirection(int direction)
{
this.direction = direction;
}
public void setScrollByBlock(boolean block)
{
this.block = block;
}
}
public class TrackListener extends MouseInputAdapter
{
protected int currentMouseX;
protected int currentMouseY;
protected int offset;
public void mouseDragged(MouseEvent e)
{
currentMouseX = e.getX();
currentMouseY = e.getY();
if (slider.getValueIsAdjusting())
{
int value;
if (slider.getOrientation() == JSlider.HORIZONTAL)
value = valueForXPosition(currentMouseX) - offset;
else
value = valueForYPosition(currentMouseY) - offset;
slider.setValue(value);
}
}
public void mouseMoved(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
currentMouseX = e.getX();
currentMouseY = e.getY();
int value;
if (slider.getOrientation() == JSlider.HORIZONTAL)
value = valueForXPosition(currentMouseX);
else
value = valueForYPosition(currentMouseY);
if (slider.getSnapToTicks())
value = findClosestTick(value);
if (! thumbRect.contains(e.getPoint()))
{
if (value > slider.getValue())
scrollDueToClickInTrack(POSITIVE_SCROLL);
else
scrollDueToClickInTrack(NEGATIVE_SCROLL);
}
else
{
slider.setValueIsAdjusting(true);
offset = value - slider.getValue();
}
}
public void mouseReleased(MouseEvent e)
{
currentMouseX = e.getX();
currentMouseY = e.getY();
if (slider.getValueIsAdjusting())
{
slider.setValueIsAdjusting(false);
if (slider.getSnapToTicks())
slider.setValue(findClosestTick(slider.getValue()));
}
if (scrollTimer != null)
scrollTimer.stop();
}
public boolean shouldScroll(int direction)
{
int value;
if (slider.getOrientation() == JSlider.HORIZONTAL)
value = valueForXPosition(currentMouseX);
else
value = valueForYPosition(currentMouseY);
if (direction == POSITIVE_SCROLL)
return (value > slider.getValue());
else
return (value < slider.getValue());
}
}
private transient int thumbHeight;
private transient int thumbWidth;
private transient int tickHeight;
protected ChangeListener changeListener;
protected PropertyChangeListener propertyChangeListener;
protected ScrollListener scrollListener;
protected ComponentListener componentListener;
protected FocusListener focusListener;
protected TrackListener trackListener;
protected Insets focusInsets;
protected Insets insetCache;
protected Rectangle contentRect;
protected Rectangle focusRect;
protected Rectangle thumbRect;
protected Rectangle tickRect;
protected Rectangle labelRect;
protected Rectangle trackRect;
public static final int MAX_SCROLL = 2;
public static final int MIN_SCROLL = -2;
public static final int NEGATIVE_SCROLL = -1;
public static final int POSITIVE_SCROLL = 1;
protected int trackBuffer;
protected boolean leftToRightCache;
protected Timer scrollTimer;
protected JSlider slider;
private transient Color shadowColor;
private transient Color highlightColor;
private transient Color focusColor;
public BasicSliderUI(JSlider b)
{
super();
}
protected Color getShadowColor()
{
return shadowColor;
}
protected Color getHighlightColor()
{
return highlightColor;
}
protected Color getFocusColor()
{
return focusColor;
}
public static ComponentUI createUI(JComponent b)
{
return new BasicSliderUI((JSlider) b);
}
public void installUI(JComponent c)
{
super.installUI(c);
if (c instanceof JSlider)
{
slider = (JSlider) c;
focusRect = new Rectangle();
contentRect = new Rectangle();
thumbRect = new Rectangle();
trackRect = new Rectangle();
tickRect = new Rectangle();
labelRect = new Rectangle();
insetCache = slider.getInsets();
leftToRightCache = ! slider.getInverted();
scrollTimer = new Timer(200, null);
scrollTimer.setRepeats(true);
installDefaults(slider);
installListeners(slider);
installKeyboardActions(slider);
calculateFocusRect();
calculateContentRect();
calculateThumbSize();
calculateTrackBuffer();
calculateTrackRect();
calculateThumbLocation();
calculateTickRect();
calculateLabelRect();
}
}
public void uninstallUI(JComponent c)
{
super.uninstallUI(c);
uninstallKeyboardActions(slider);
uninstallListeners(slider);
scrollTimer = null;
focusRect = null;
contentRect = null;
thumbRect = null;
trackRect = null;
tickRect = null;
labelRect = null;
focusInsets = null;
}
protected void installDefaults(JSlider slider)
{
UIDefaults defaults = UIManager.getLookAndFeelDefaults();
slider.setForeground(defaults.getColor("Slider.foreground"));
slider.setBackground(defaults.getColor("Slider.background"));
shadowColor = defaults.getColor("Slider.shadow");
highlightColor = defaults.getColor("Slider.highlight");
focusColor = defaults.getColor("Slider.focus");
slider.setBorder(defaults.getBorder("Slider.border"));
slider.setOpaque(true);
thumbHeight = defaults.getInt("Slider.thumbHeight");
thumbWidth = defaults.getInt("Slider.thumbWidth");
tickHeight = defaults.getInt("Slider.tickHeight");
focusInsets = defaults.getInsets("Slider.focusInsets");
}
protected TrackListener createTrackListener(JSlider slider)
{
return new TrackListener();
}
protected ChangeListener createChangeListener(JSlider slider)
{
return new ChangeHandler();
}
protected ComponentListener createComponentListener(JSlider slider)
{
return new ComponentHandler();
}
protected FocusListener createFocusListener(JSlider slider)
{
return new FocusHandler();
}
protected ScrollListener createScrollListener(JSlider slider)
{
return new ScrollListener();
}
protected PropertyChangeListener createPropertyChangeListener(JSlider slider)
{
return new PropertyChangeHandler();
}
protected void installListeners(JSlider slider)
{
propertyChangeListener = createPropertyChangeListener(slider);
componentListener = createComponentListener(slider);
trackListener = createTrackListener(slider);
focusListener = createFocusListener(slider);
changeListener = createChangeListener(slider);
scrollListener = createScrollListener(slider);
slider.addPropertyChangeListener(propertyChangeListener);
slider.addComponentListener(componentListener);
slider.addMouseListener(trackListener);
slider.addMouseMotionListener(trackListener);
slider.addFocusListener(focusListener);
slider.getModel().addChangeListener(changeListener);
scrollTimer.addActionListener(scrollListener);
}
protected void uninstallListeners(JSlider slider)
{
slider.removePropertyChangeListener(propertyChangeListener);
slider.removeComponentListener(componentListener);
slider.removeMouseListener(trackListener);
slider.removeMouseMotionListener(trackListener);
slider.removeFocusListener(focusListener);
slider.getModel().removeChangeListener(changeListener);
scrollTimer.removeActionListener(scrollListener);
propertyChangeListener = null;
componentListener = null;
trackListener = null;
focusListener = null;
changeListener = null;
scrollListener = null;
}
protected void installKeyboardActions(JSlider slider)
{
}
protected void uninstallKeyboardActions(JSlider slider)
{
}
public Dimension getPreferredHorizontalSize()
{
Insets insets = slider.getInsets();
int width = getWidthOfWidestLabel() * (slider.getLabelTable() == null ? 0
: slider.getLabelTable()
.size());
if (width < 200)
width = 200;
width += insets.left + insets.right + focusInsets.left + focusInsets.right;
int height = thumbHeight;
if (slider.getPaintTicks() && slider.getMajorTickSpacing() > 0
|| slider.getMinorTickSpacing() > 0)
height += tickHeight;
if (slider.getPaintLabels())
height += getHeightOfTallestLabel();
height += insets.top + insets.bottom + focusInsets.top
+ focusInsets.bottom;
return new Dimension(width, height);
}
public Dimension getPreferredVerticalSize()
{
Insets insets = slider.getInsets();
int height = getHeightOfTallestLabel() * (slider.getLabelTable() == null
? 0 : slider.getLabelTable()
.size());
if (height < 200)
height = 200;
height += insets.top + insets.bottom + focusInsets.top
+ focusInsets.bottom;
int width = thumbHeight;
if (slider.getPaintTicks() && slider.getMajorTickSpacing() > 0
|| slider.getMinorTickSpacing() > 0)
width += tickHeight;
if (slider.getPaintLabels())
width += getWidthOfWidestLabel();
width += insets.left + insets.right + focusInsets.left + focusInsets.right;
return new Dimension(width, height);
}
public Dimension getMinimumHorizontalSize()
{
return getPreferredHorizontalSize();
}
public Dimension getMinimumVerticalSize()
{
return getPreferredVerticalSize();
}
public Dimension getPreferredSize(JComponent c)
{
if (slider.getOrientation() == JSlider.HORIZONTAL)
return getPreferredHorizontalSize();
else
return getPreferredVerticalSize();
}
public Dimension getMinimumSize(JComponent c)
{
if (slider.getOrientation() == JSlider.HORIZONTAL)
return getPreferredHorizontalSize();
else
return getPreferredVerticalSize();
}
public Dimension getMaximumSize(JComponent c)
{
if (slider.getOrientation() == JSlider.HORIZONTAL)
return getPreferredHorizontalSize();
else
return getPreferredVerticalSize();
}
protected void calculateGeometry()
{
calculateFocusRect();
calculateContentRect();
calculateThumbSize();
calculateTrackBuffer();
calculateTrackRect();
calculateTickRect();
calculateLabelRect();
calculateThumbLocation();
}
protected void calculateFocusRect()
{
insetCache = slider.getInsets();
focusRect = SwingUtilities.calculateInnerArea(slider, focusRect);
if (focusRect.width < 0)
focusRect.width = 0;
if (focusRect.height < 0)
focusRect.height = 0;
}
protected void calculateThumbSize()
{
if (slider.getOrientation() == JSlider.HORIZONTAL)
{
if (thumbWidth > contentRect.width)
thumbRect.width = contentRect.width / 4;
else
thumbRect.width = thumbWidth;
if (thumbHeight > contentRect.height)
thumbRect.height = contentRect.height;
else
thumbRect.height = thumbHeight;
}
else
{
if (thumbWidth > contentRect.height)
thumbRect.height = contentRect.height / 4;
else
thumbRect.height = thumbWidth;
if (thumbHeight > contentRect.width)
thumbRect.width = contentRect.width;
else
thumbRect.width = thumbHeight;
}
}
protected void calculateContentRect()
{
contentRect.x = focusRect.x + focusInsets.left;
contentRect.y = focusRect.y + focusInsets.top;
contentRect.width = focusRect.width - focusInsets.left - focusInsets.right;
contentRect.height = focusRect.height - focusInsets.top
- focusInsets.bottom;
if (contentRect.width < 0)
contentRect.width = 0;
if (contentRect.height < 0)
contentRect.height = 0;
}
protected void calculateThumbLocation()
{
int value = slider.getValue();
if (slider.getOrientation() == JSlider.HORIZONTAL)
{
thumbRect.x = xPositionForValue(value) - thumbRect.width / 2;
thumbRect.y = contentRect.y;
}
else
{
thumbRect.x = contentRect.x;
thumbRect.y = yPositionForValue(value) - thumbRect.height / 2;
}
}
protected void calculateTrackBuffer()
{
if (slider.getOrientation() == JSlider.HORIZONTAL)
trackBuffer = thumbRect.width;
else
trackBuffer = thumbRect.height;
}
protected Dimension getThumbSize()
{
return thumbRect.getSize();
}
protected void calculateTrackRect()
{
if (slider.getOrientation() == JSlider.HORIZONTAL)
{
trackRect.x = contentRect.x + trackBuffer;
trackRect.y = contentRect.y;
trackRect.width = contentRect.width - 2 * trackBuffer;
trackRect.height = thumbRect.height;
}
else
{
trackRect.x = contentRect.x;
trackRect.y = contentRect.y + trackBuffer;
trackRect.width = thumbRect.width;
trackRect.height = contentRect.height - 2 * trackBuffer;
}
}
protected int getTickLength()
{
return tickHeight;
}
protected void calculateTickRect()
{
if (slider.getOrientation() == JSlider.HORIZONTAL)
{
tickRect.x = trackRect.x;
tickRect.y = trackRect.y + trackRect.height;
tickRect.width = trackRect.width;
tickRect.height = getTickLength();
if (tickRect.y + tickRect.height > contentRect.y + contentRect.height)
tickRect.height = contentRect.y + contentRect.height - tickRect.y;
}
else
{
tickRect.x = trackRect.x + trackRect.width;
tickRect.y = trackRect.y;
tickRect.width = getTickLength();
tickRect.height = trackRect.height;
if (tickRect.x + tickRect.width > contentRect.x + contentRect.width)
tickRect.width = contentRect.x + contentRect.width - tickRect.x;
}
}
protected void calculateLabelRect()
{
if (slider.getOrientation() == JSlider.HORIZONTAL)
{
labelRect.x = contentRect.x;
labelRect.y = tickRect.y + tickRect.height;
labelRect.width = contentRect.width;
labelRect.height = contentRect.height - labelRect.y;
}
else
{
labelRect.x = tickRect.x + tickRect.width;
labelRect.y = contentRect.y;
labelRect.width = contentRect.width - labelRect.x;
labelRect.height = contentRect.height;
}
}
protected int getWidthOfWidestLabel()
{
int widest = 0;
Component label;
if (slider.getLabelTable() == null)
return 0;
Dimension pref;
for (Enumeration list = slider.getLabelTable().elements();
list.hasMoreElements();)
{
Object comp = list.nextElement();
if (! (comp instanceof Component))
continue;
label = (Component) comp;
pref = label.getPreferredSize();
if (pref != null && pref.width > widest)
widest = pref.width;
}
return widest;
}
protected int getHeightOfTallestLabel()
{
int tallest = 0;
Component label;
if (slider.getLabelTable() == null)
return 0;
Dimension pref;
for (Enumeration list = slider.getLabelTable().elements();
list.hasMoreElements();)
{
Object comp = list.nextElement();
if (! (comp instanceof Component))
continue;
label = (Component) comp;
pref = label.getPreferredSize();
if (pref != null && pref.height > tallest)
tallest = pref.height;
}
return tallest;
}
protected int getWidthOfHighValueLabel()
{
Component highValueLabel = getHighestValueLabel();
if (highValueLabel != null)
return highValueLabel.getWidth();
else
return 0;
}
protected int getWidthOfLowValueLabel()
{
Component lowValueLabel = getLowestValueLabel();
if (lowValueLabel != null)
return lowValueLabel.getWidth();
else
return 0;
}
protected int getHeightOfHighValueLabel()
{
Component highValueLabel = getHighestValueLabel();
if (highValueLabel != null)
return highValueLabel.getHeight();
else
return 0;
}
protected int getHeightOfLowValueLabel()
{
Component lowValueLabel = getLowestValueLabel();
if (lowValueLabel != null)
return lowValueLabel.getHeight();
else
return 0;
}
protected boolean drawInverted()
{
return ! (slider.getInverted() ^ leftToRightCache);
}
protected Component getLowestValueLabel()
{
Integer key = new Integer(Integer.MAX_VALUE);
Integer tmpKey;
Dictionary labelTable = slider.getLabelTable();
if (labelTable == null)
return null;
for (Enumeration list = labelTable.keys(); list.hasMoreElements();)
{
Object value = list.nextElement();
if (! (value instanceof Integer))
continue;
tmpKey = (Integer) value;
if (tmpKey.intValue() < key.intValue())
key = tmpKey;
}
Object comp = labelTable.get(key);
if (! (comp instanceof Component))
return null;
return (Component) comp;
}
protected Component getHighestValueLabel()
{
Integer key = new Integer(Integer.MIN_VALUE);
Integer tmpKey;
Dictionary labelTable = slider.getLabelTable();
if (labelTable == null)
return null;
for (Enumeration list = labelTable.keys(); list.hasMoreElements();)
{
Object value = list.nextElement();
if (! (value instanceof Integer))
continue;
tmpKey = (Integer) value;
if (tmpKey.intValue() > key.intValue())
key = tmpKey;
}
Object comp = labelTable.get(key);
if (! (comp instanceof Component))
return null;
return (Component) comp;
}
public void paint(Graphics g, JComponent c)
{
leftToRightCache = slider.getComponentOrientation() != ComponentOrientation.RIGHT_TO_LEFT;
calculateThumbLocation();
if (slider.getPaintTrack())
paintTrack(g);
if (slider.getPaintTicks())
paintTicks(g);
if (slider.getPaintLabels())
paintLabels(g);
paintThumb(g);
}
protected void recalculateIfInsetsChanged()
{
calculateFocusRect();
calculateContentRect();
calculateThumbSize();
calculateTrackBuffer();
calculateTrackRect();
calculateThumbLocation();
calculateTickRect();
calculateLabelRect();
}
protected void recalculateIfOrientationChanged()
{
calculateThumbSize();
calculateTrackBuffer();
calculateTrackRect();
calculateThumbLocation();
calculateTickRect();
calculateLabelRect();
}
public void paintFocus(Graphics g)
{
Color saved_color = g.getColor();
g.setColor(getFocusColor());
g.drawRect(focusRect.x, focusRect.y, focusRect.width, focusRect.height);
g.setColor(saved_color);
}
public void paintTrack(Graphics g)
{
Color saved_color = g.getColor();
int width;
int height;
Point a = new Point(trackRect.x, trackRect.y);
Point b = new Point(a);
Point c = new Point(a);
Point d = new Point(a);
Polygon high;
Polygon shadow;
if (slider.getOrientation() == JSlider.HORIZONTAL)
{
width = trackRect.width;
height = (thumbRect.height / 4 == 0) ? 1 : thumbRect.height / 4;
a.translate(0, (trackRect.height / 2) - (height / 2));
b.translate(0, (trackRect.height / 2) + (height / 2));
c.translate(trackRect.width, (trackRect.height / 2) + (height / 2));
d.translate(trackRect.width, (trackRect.height / 2) - (height / 2));
}
else
{
width = (thumbRect.width / 4 == 0) ? 1 : thumbRect.width / 4;
height = trackRect.height;
a.translate((trackRect.width / 2) - (width / 2), 0);
b.translate((trackRect.width / 2) - (width / 2), trackRect.height);
c.translate((trackRect.width / 2) + (width / 2), trackRect.height);
d.translate((trackRect.width / 2) + (width / 2), 0);
}
g.setColor(Color.GRAY);
g.fillRect(a.x, a.y, width, height);
g.setColor(getHighlightColor());
g.drawLine(b.x, b.y, c.x, c.y);
g.drawLine(c.x, c.y, d.x, d.y);
g.setColor(getShadowColor());
g.drawLine(b.x, b.y, a.x, a.y);
g.drawLine(a.x, a.y, d.x, d.y);
g.setColor(saved_color);
}
public void paintTicks(Graphics g)
{
int max = slider.getMaximum();
int min = slider.getMinimum();
int majorSpace = slider.getMajorTickSpacing();
int minorSpace = slider.getMinorTickSpacing();
if (majorSpace > 0)
{
if (slider.getOrientation() == JSlider.HORIZONTAL)
{
double loc = tickRect.x;
double increment = (max == min) ? 0
: majorSpace * (double) tickRect.width / (max
- min);
if (drawInverted())
{
loc += tickRect.width;
increment *= -1;
}
for (int i = min; i <= max; i += majorSpace)
{
paintMajorTickForHorizSlider(g, tickRect, (int) loc);
loc += increment;
}
}
else
{
double loc = tickRect.height + tickRect.y;
double increment = (max == min) ? 0
: -majorSpace * (double) tickRect.height / (max
- min);
if (drawInverted())
{
loc = tickRect.y;
increment *= -1;
}
for (int i = min; i <= max; i += majorSpace)
{
paintMajorTickForVertSlider(g, tickRect, (int) loc);
loc += increment;
}
}
}
if (minorSpace > 0)
{
if (slider.getOrientation() == JSlider.HORIZONTAL)
{
double loc = tickRect.x;
double increment = (max == min) ? 0
: minorSpace * (double) tickRect.width / (max
- min);
if (drawInverted())
{
loc += tickRect.width;
increment *= -1;
}
for (int i = min; i <= max; i += minorSpace)
{
paintMinorTickForHorizSlider(g, tickRect, (int) loc);
loc += increment;
}
}
else
{
double loc = tickRect.height + tickRect.y;
double increment = (max == min) ? 0
: -minorSpace * (double) tickRect.height / (max
- min);
if (drawInverted())
{
loc = tickRect.y;
increment *= -1;
}
for (int i = min; i <= max; i += minorSpace)
{
paintMinorTickForVertSlider(g, tickRect, (int) loc);
loc += increment;
}
}
}
}
protected void paintMinorTickForHorizSlider(Graphics g,
Rectangle tickBounds, int x)
{
int y = tickRect.y + tickRect.height / 4;
Color saved = g.getColor();
g.setColor(Color.BLACK);
g.drawLine(x, y, x, y + tickRect.height / 4);
g.setColor(saved);
}
protected void paintMajorTickForHorizSlider(Graphics g,
Rectangle tickBounds, int x)
{
int y = tickRect.y + tickRect.height / 4;
Color saved = g.getColor();
g.setColor(Color.BLACK);
g.drawLine(x, y, x, y + tickRect.height / 2);
g.setColor(saved);
}
protected void paintMinorTickForVertSlider(Graphics g, Rectangle tickBounds,
int y)
{
int x = tickRect.x + tickRect.width / 4;
Color saved = g.getColor();
g.setColor(Color.BLACK);
g.drawLine(x, y, x + tickRect.width / 4, y);
g.setColor(saved);
}
protected void paintMajorTickForVertSlider(Graphics g, Rectangle tickBounds,
int y)
{
int x = tickRect.x + tickRect.width / 4;
Color saved = g.getColor();
g.setColor(Color.BLACK);
g.drawLine(x, y, x + tickRect.width / 2, y);
g.setColor(saved);
}
public void paintLabels(Graphics g)
{
if (slider.getLabelTable() != null)
{
Dictionary table = slider.getLabelTable();
Integer tmpKey;
Object key;
Object element;
Component label;
if (slider.getOrientation() == JSlider.HORIZONTAL)
{
for (Enumeration list = table.keys(); list.hasMoreElements();)
{
key = list.nextElement();
if (! (key instanceof Integer))
continue;
tmpKey = (Integer) key;
element = table.get(tmpKey);
if (! (element instanceof JLabel))
continue;
label = (Component) element;
paintHorizontalLabel(g, tmpKey.intValue(), label);
}
}
else
{
for (Enumeration list = table.keys(); list.hasMoreElements();)
{
key = list.nextElement();
if (! (key instanceof Integer))
continue;
tmpKey = (Integer) key;
element = table.get(tmpKey);
if (! (element instanceof JLabel))
continue;
label = (Component) element;
paintVerticalLabel(g, tmpKey.intValue(), label);
}
}
}
}
protected void paintHorizontalLabel(Graphics g, int value, Component label)
{
Dimension dim = label.getPreferredSize();
int w = (int) dim.getWidth();
int h = (int) dim.getHeight();
int max = slider.getMaximum();
int min = slider.getMinimum();
if (value > max || value < min)
return;
int xpos = xPositionForValue(value) - w / 2;
int ypos = labelRect.y;
if (xpos < 0)
xpos = 0;
if (xpos + w > labelRect.x + labelRect.width)
w = labelRect.x + labelRect.width - xpos;
if (h > labelRect.height)
h = labelRect.height;
label.setBounds(xpos, ypos, w, h);
javax.swing.SwingUtilities.paintComponent(g, label, null, label.getBounds());
}
protected void paintVerticalLabel(Graphics g, int value, Component label)
{
Dimension dim = label.getPreferredSize();
int w = (int) dim.getWidth();
int h = (int) dim.getHeight();
int max = slider.getMaximum();
int min = slider.getMinimum();
if (value > max || value < min)
return;
int xpos = labelRect.x;
int ypos = yPositionForValue(value) - h / 2;
if (ypos < 0)
ypos = 0;
if (ypos + h > labelRect.y + labelRect.height)
h = labelRect.y + labelRect.height - ypos;
if (w > labelRect.width)
w = labelRect.width;
label.setBounds(xpos, ypos, w, h);
javax.swing.SwingUtilities.paintComponent(g, label, null, label.getBounds());
}
public void paintThumb(Graphics g)
{
Color saved_color = g.getColor();
Polygon thumb = new Polygon();
Point a = new Point(thumbRect.x, thumbRect.y);
Point b = new Point(a);
Point c = new Point(a);
Point d = new Point(a);
Point e = new Point(a);
Polygon bright;
Polygon dark;
Polygon all;
int turnPoint;
if (slider.getOrientation() == JSlider.HORIZONTAL)
{
turnPoint = thumbRect.height * 3 / 4;
b.translate(thumbRect.width, 0);
c.translate(thumbRect.width, turnPoint);
d.translate(thumbRect.width / 2, thumbRect.height);
e.translate(0, turnPoint);
bright = new Polygon(new int[] { b.x, a.x, e.x, d.x },
new int[] { b.y, a.y, e.y, d.y }, 4);
dark = new Polygon(new int[] { b.x, c.x, d.x },
new int[] { b.y, c.y, d.y }, 3);
all = new Polygon(new int[] { a.x + 1, b.x, c.x, d.x, e.x + 1 },
new int[] { a.y + 1, b.y + 1, c.y, d.y + 1, e.y }, 5);
}
else
{
turnPoint = thumbRect.width * 3 / 4;
b.translate(turnPoint, 0);
c.translate(thumbRect.width, thumbRect.height / 2);
d.translate(turnPoint, thumbRect.height);
e.translate(0, thumbRect.height);
bright = new Polygon(new int[] { c.x, b.x, a.x, e.x },
new int[] { c.y, b.y, a.y, e.y }, 4);
dark = new Polygon(new int[] { c.x, d.x, e.x + 1 },
new int[] { c.y, d.y, e.y }, 3);
all = new Polygon(new int[] { a.x + 1, b.x, c.x - 1, d.x, e.x + 1 },
new int[] { a.y + 1, b.y + 1, c.y, d.y, e.y }, 5);
}
g.setColor(Color.WHITE);
g.drawPolyline(bright.xpoints, bright.ypoints, bright.npoints);
g.setColor(Color.BLACK);
g.drawPolyline(dark.xpoints, dark.ypoints, dark.npoints);
g.setColor(Color.GRAY);
g.fillPolygon(all);
g.setColor(saved_color);
}
public void setThumbLocation(int x, int y)
{
thumbRect.x = x;
thumbRect.y = y;
}
public void scrollByBlock(int direction)
{
int unit = direction * (slider.getMaximum() - slider.getMinimum()) / 10;
int moveTo = slider.getValue() + unit;
if (slider.getSnapToTicks())
moveTo = findClosestTick(moveTo);
slider.setValue(moveTo);
}
public void scrollByUnit(int direction)
{
int moveTo = slider.getValue() + direction;
if (slider.getSnapToTicks())
moveTo = findClosestTick(moveTo);
slider.setValue(moveTo);
}
protected void scrollDueToClickInTrack(int dir)
{
scrollTimer.stop();
scrollListener.setDirection(dir);
scrollListener.setScrollByBlock(true);
scrollTimer.start();
}
protected int xPositionForValue(int value)
{
int min = slider.getMinimum();
int max = slider.getMaximum();
int extent = slider.getExtent();
int len = trackRect.width;
int xPos = (max == min) ? 0 : (value - min) * len / (max - min);
if (! drawInverted())
xPos += trackRect.x;
else
{
xPos = trackRect.width - xPos;
xPos += trackRect.x;
}
return xPos;
}
protected int yPositionForValue(int value)
{
int min = slider.getMinimum();
int max = slider.getMaximum();
int extent = slider.getExtent();
int len = trackRect.height;
int yPos = (max == min) ? 0 : (value - min) * len / (max - min);
if (! drawInverted())
{
yPos = trackRect.height - yPos;
yPos += trackRect.y;
}
else
yPos += trackRect.y;
return yPos;
}
public int valueForYPosition(int yPos)
{
int min = slider.getMinimum();
int max = slider.getMaximum();
int len = trackRect.height;
int value;
if (len == 0)
return ((max - min) / 2);
if (! drawInverted())
value = ((len - (yPos - trackRect.y)) * (max - min) / len + min);
else
value = ((yPos - trackRect.y) * (max - min) / len + min);
if (value > max)
value = max;
else if (value < min)
value = min;
return value;
}
public int valueForXPosition(int xPos)
{
int min = slider.getMinimum();
int max = slider.getMaximum();
int len = trackRect.width;
int value;
if (len == 0)
return ((max - min) / 2);
if (! drawInverted())
value = ((xPos - trackRect.x) * (max - min) / len + min);
else
value = ((len - (xPos - trackRect.x)) * (max - min) / len + min);
if (value > max)
value = max;
else if (value < min)
value = min;
return value;
}
private int findClosestTick(int value)
{
int min = slider.getMinimum();
int max = slider.getMaximum();
int majorSpace = slider.getMajorTickSpacing();
int minorSpace = slider.getMinorTickSpacing();
int minor = min - value;
int major = min - value;
if (majorSpace <= 0 && minorSpace <= 0)
return value;
if (majorSpace > 0)
{
int lowerBound = (value - min) / majorSpace;
int majLower = majorSpace * lowerBound + min;
int majHigher = majorSpace * (lowerBound + 1) + min;
if (majHigher <= max && majHigher - value <= value - majLower)
major = majHigher - value;
else
major = majLower - value;
}
if (minorSpace > 0)
{
int lowerBound = value / minorSpace;
int minLower = minorSpace * lowerBound;
int minHigher = minorSpace * (lowerBound + 1);
if (minHigher <= max && minHigher - value <= value - minLower)
minor = minHigher - value;
else
minor = minLower - value;
}
if (Math.abs(minor) > Math.abs(major))
return value + major;
else
return value + minor;
}
}