home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing.plaf.metal;
-
- import com.sun.java.swing.Icon;
- import com.sun.java.swing.JComponent;
- import com.sun.java.swing.JSlider;
- import com.sun.java.swing.UIManager;
- import com.sun.java.swing.plaf.ComponentUI;
- import com.sun.java.swing.plaf.basic.BasicSliderUI;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Rectangle;
- import java.beans.PropertyChangeEvent;
- import java.beans.PropertyChangeListener;
-
- public class MetalSliderUI extends BasicSliderUI implements PropertyChangeListener {
- protected final int TICK_BUFFER = 4;
- protected boolean filledSlider = false;
- protected static Color thumbColor;
- protected static Color highlightColor;
- protected static Color darkShadowColor;
- protected static int trackWidth;
- protected static int tickLength;
- protected static Icon horizThumbIcon;
- protected static Icon vertThumbIcon;
- protected final String SLIDER_FILL = "JSlider.isFilled";
-
- public MetalSliderUI() {
- super((JSlider)null);
- }
-
- public void calculateThumbBounds() {
- int thumbLength = 16;
- if (super.slider.getOrientation() == 0) {
- int thumbX = ((BasicSliderUI)this).xPositionForValue(super.slider.getValue()) - ((BasicSliderUI)this).getThumbRect().width / 2;
- int thumbY = ((BasicSliderUI)this).getScrollTrackRect().y + ((BasicSliderUI)this).getScrollTrackRect().height - thumbLength;
- ((BasicSliderUI)this).setThumbBounds(thumbX, thumbY, 15, thumbLength);
- } else {
- int thumbY = ((BasicSliderUI)this).yPositionForValue(super.slider.getValue()) - ((BasicSliderUI)this).getThumbRect().height / 2;
- int thumbX = ((BasicSliderUI)this).getScrollTrackRect().x + ((BasicSliderUI)this).getScrollTrackRect().width - thumbLength;
- ((BasicSliderUI)this).setThumbBounds(thumbX, thumbY, thumbLength, 15);
- }
-
- }
-
- public static ComponentUI createUI(JComponent c) {
- return new MetalSliderUI();
- }
-
- protected int getThumbOverhang() {
- return 4;
- }
-
- public int getTickSpace() {
- return super.slider.getOrientation() == 0 ? tickLength + 4 + 1 : tickLength + 4 + 3;
- }
-
- protected int getTrackLength() {
- ((BasicSliderUI)this).getFullContentArea();
- return super.slider.getOrientation() == 0 ? ((BasicSliderUI)this).getScrollTrackRect().width - super.trackBuffer * 2 : ((BasicSliderUI)this).getScrollTrackRect().height - super.trackBuffer * 2;
- }
-
- protected int getTrackWidth() {
- return trackWidth;
- }
-
- public void installUI(JComponent c) {
- trackWidth = (Integer)UIManager.get("Slider.trackWidth");
- tickLength = (Integer)UIManager.get("Slider.majorTickLength");
- horizThumbIcon = UIManager.getIcon("Slider.horizontalThumbIcon");
- vertThumbIcon = UIManager.getIcon("Slider.verticalThumbIcon");
- super.installUI(c);
- thumbColor = UIManager.getColor("Slider.thumb");
- highlightColor = UIManager.getColor("Slider.highlight");
- darkShadowColor = UIManager.getColor("Slider.darkShadow");
- super.scrollListener.setScrollByBlock(false);
- c.addPropertyChangeListener(this);
- Object sliderFillProp = c.getClientProperty("JSlider.isFilled");
- if (sliderFillProp != null) {
- this.filledSlider = (Boolean)sliderFillProp;
- }
-
- }
-
- public void paintFocus(Graphics g) {
- if (super.slider.hasFocus()) {
- Rectangle r = super.slider.getBounds();
- r.x = 0;
- r.y = 0;
- if (super.slider.getBorder() != null) {
- r = ((BasicSliderUI)this).getFullContentArea();
- }
-
- g.setColor(((BasicSliderUI)this).getFocusColor());
- g.drawRect(r.x + 1, r.y + 1, r.width - 2, r.height - 2);
- }
-
- }
-
- protected void paintMajorTickForHorizSlider(Graphics g, Rectangle tickBounds, int x) {
- g.setColor((Color)(super.slider.isEnabled() ? super.slider.getForeground() : MetalLookAndFeel.getControlShadow()));
- g.drawLine(x, 4, x, 4 + (tickLength - 1));
- }
-
- protected void paintMajorTickForVertSlider(Graphics g, Rectangle tickBounds, int y) {
- g.setColor((Color)(super.slider.isEnabled() ? super.slider.getForeground() : MetalLookAndFeel.getControlShadow()));
- g.drawLine(4, y, 4 + tickLength, y);
- }
-
- protected void paintMinorTickForHorizSlider(Graphics g, Rectangle tickBounds, int x) {
- g.setColor((Color)(super.slider.isEnabled() ? super.slider.getForeground() : MetalLookAndFeel.getControlShadow()));
- g.drawLine(x, 4, x, 4 + tickLength / 2);
- }
-
- protected void paintMinorTickForVertSlider(Graphics g, Rectangle tickBounds, int y) {
- g.setColor((Color)(super.slider.isEnabled() ? super.slider.getForeground() : MetalLookAndFeel.getControlShadow()));
- g.drawLine(4, y, 4 + tickLength / 2, y);
- }
-
- public void paintThumb(Graphics g) {
- Rectangle knobBounds = ((BasicSliderUI)this).getThumbRect();
- g.translate(knobBounds.x, knobBounds.y);
- if (super.slider.getOrientation() == 0) {
- horizThumbIcon.paintIcon(super.slider, g, 0, 0);
- } else {
- vertThumbIcon.paintIcon(super.slider, g, 0, 0);
- }
-
- g.translate(-knobBounds.x, -knobBounds.y);
- }
-
- public void paintTrack(Graphics g) {
- Rectangle trackBounds = ((BasicSliderUI)this).getScrollTrackRect();
- Color trackColor = (Color)(!super.slider.isEnabled() ? MetalLookAndFeel.getControlShadow() : super.slider.getForeground());
- g.translate(trackBounds.x, trackBounds.y);
- int trackLeft = 0;
- int trackTop = 0;
- int trackRight = 0;
- int trackBottom = 0;
- if (super.slider.getOrientation() == 0) {
- trackLeft = super.trackBuffer;
- trackTop = trackBounds.height - this.getThumbOverhang() - this.getTrackWidth();
- trackRight = trackBounds.width - 1 - super.trackBuffer;
- trackBottom = trackBounds.height - this.getThumbOverhang() - 1;
- if (super.slider.isEnabled()) {
- MetalUtils.drawFlush3DBorder(g, trackLeft, trackTop, this.getTrackLength(), this.getTrackWidth());
- } else {
- Rectangle thumbBounds = ((BasicSliderUI)this).getThumbRect();
- int middleOfThumb = thumbBounds.x + thumbBounds.width / 2;
- g.setColor(trackColor);
- g.drawRect(trackLeft, trackTop, this.getTrackLength(), this.getTrackWidth() - 2);
- if (this.filledSlider) {
- if (super.slider.isEnabled()) {
- g.fillRect(trackLeft, trackTop, middleOfThumb - trackLeft, this.getTrackWidth() - 2);
- } else {
- g.fillRect(middleOfThumb, trackTop, trackRight - middleOfThumb, this.getTrackWidth() - 2);
- }
- }
- }
- } else {
- trackLeft = trackBounds.width - this.getThumbOverhang() - this.getTrackWidth();
- trackTop = super.trackBuffer;
- trackRight = trackBounds.width - this.getThumbOverhang() - 1;
- trackBottom = trackBounds.height - 1 - super.trackBuffer;
- if (super.slider.isEnabled()) {
- MetalUtils.drawFlush3DBorder(g, trackLeft, trackTop, this.getTrackWidth(), this.getTrackLength());
- } else {
- Rectangle thumbBounds = ((BasicSliderUI)this).getThumbRect();
- int middleOfThumb = thumbBounds.y + thumbBounds.height / 2;
- g.setColor(trackColor);
- g.drawRect(trackLeft, trackTop, this.getTrackWidth() - 2, this.getTrackLength());
- if (this.filledSlider) {
- if (super.slider.isEnabled()) {
- g.fillRect(trackLeft, middleOfThumb, this.getTrackWidth() - 2, trackBottom - middleOfThumb);
- } else {
- g.fillRect(trackLeft, trackTop, this.getTrackWidth() - 2, middleOfThumb - trackLeft);
- }
- }
- }
- }
-
- if (!this.filledSlider) {
- if (super.slider.isEnabled()) {
- g.setColor(super.slider.getForeground());
- if (super.slider.getOrientation() == 0) {
- g.fillRect(trackLeft + 2, trackTop + 2, this.getTrackLength() - 4, this.getTrackWidth() - 4);
- g.setColor(MetalLookAndFeel.getPrimaryControl());
- g.drawLine(trackLeft + 1, trackTop + 1, trackLeft + 1, trackBottom - 2);
- g.drawLine(trackLeft + 1, trackTop + 1, trackRight - 2, trackTop + 1);
- g.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
- g.drawLine(trackLeft + 2, trackBottom - 1, trackRight - 1, trackBottom - 1);
- g.drawLine(trackRight - 1, trackTop + 2, trackRight - 1, trackBottom - 1);
- } else {
- g.fillRect(trackLeft + 2, trackTop + 2, this.getTrackWidth() - 4, this.getTrackLength() - 4);
- g.setColor(MetalLookAndFeel.getPrimaryControl());
- g.drawLine(trackLeft + 1, trackTop + 1, trackLeft + 1, trackBottom - 2);
- g.drawLine(trackLeft + 1, trackTop + 1, trackRight - 2, trackTop + 1);
- g.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
- g.drawLine(trackRight - 1, trackTop + 2, trackRight - 1, trackBottom - 1);
- g.drawLine(trackLeft + 2, trackBottom - 1, trackRight - 1, trackBottom - 1);
- }
- }
- } else {
- Color lightColor = MetalLookAndFeel.getPrimaryControl();
- Color darkColor = MetalLookAndFeel.getPrimaryControlDarkShadow();
- g.setColor(super.slider.getForeground());
- if (super.slider.getOrientation() == 0) {
- Rectangle thumbBounds = ((BasicSliderUI)this).getThumbRect();
- int middleOfThumb = thumbBounds.x + thumbBounds.width / 2;
- if (!super.slider.getInverted()) {
- g.fillRect(trackLeft + 2, trackTop + 2, middleOfThumb - (trackLeft + 2), this.getTrackWidth() - 4);
- g.setColor(lightColor);
- g.drawLine(trackLeft + 2, trackTop + 1, middleOfThumb, trackTop + 1);
- g.drawLine(trackLeft + 1, trackTop + 1, trackLeft + 1, trackBottom - 1);
- g.setColor(darkColor);
- g.drawLine(trackLeft + 2, trackBottom - 1, middleOfThumb, trackBottom - 1);
- } else {
- g.fillRect(middleOfThumb, trackTop + 2, trackRight - 1 - middleOfThumb, this.getTrackWidth() - 4);
- g.setColor(lightColor);
- g.drawLine(middleOfThumb, trackTop + 1, trackRight - 2, trackTop + 1);
- g.drawLine(trackRight - 1, trackTop + 1, trackRight - 1, trackBottom - 1);
- g.setColor(darkColor);
- g.drawLine(middleOfThumb, trackBottom - 1, trackRight - 2, trackBottom - 1);
- }
- } else {
- Rectangle thumbBounds = ((BasicSliderUI)this).getThumbRect();
- int middleOfThumb = thumbBounds.y + thumbBounds.height / 2;
- if (!super.slider.getInverted()) {
- g.fillRect(trackLeft + 2, middleOfThumb, this.getTrackWidth() - 4, trackBottom - 1 - middleOfThumb);
- g.setColor(lightColor);
- g.drawLine(trackLeft + 1, middleOfThumb, trackLeft + 1, trackBottom - 2);
- g.drawLine(trackLeft + 1, trackBottom - 1, trackRight - 1, trackBottom - 1);
- g.setColor(darkColor);
- g.drawLine(trackRight - 1, middleOfThumb, trackRight - 1, trackBottom - 2);
- } else {
- g.fillRect(trackLeft + 2, trackTop + 2, this.getTrackWidth() - 4, middleOfThumb - (trackTop + 2));
- g.setColor(lightColor);
- g.drawLine(trackLeft + 1, trackTop + 2, trackLeft + 1, middleOfThumb);
- g.drawLine(trackLeft + 1, trackTop + 1, trackRight - 1, trackTop + 1);
- g.setColor(darkColor);
- g.drawLine(trackRight - 1, trackTop + 2, trackRight - 1, middleOfThumb);
- }
- }
- }
-
- g.translate(-trackBounds.x, -trackBounds.y);
- }
-
- public void propertyChange(PropertyChangeEvent e) {
- super.propertyChange(e);
- String name = e.getPropertyName();
- if (name.equals("JSlider.isFilled")) {
- if (e.getNewValue() != null) {
- this.filledSlider = (Boolean)e.getNewValue();
- } else {
- this.filledSlider = false;
- }
- }
-
- }
-
- protected void scrollDueToClickInTrack(int dir) {
- ((BasicSliderUI)this).scrollByUnit(dir);
- }
-
- public void uninstallUI(JComponent c) {
- c.removePropertyChangeListener(this);
- super.uninstallUI(c);
- }
- }
-