001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Sergey Burlak
019: * @version $Revision$
020: */package javax.swing.plaf.metal;
021:
022: import java.awt.Color;
023: import java.awt.Dimension;
024: import java.awt.Graphics;
025: import java.awt.Rectangle;
026: import java.beans.PropertyChangeEvent;
027: import java.beans.PropertyChangeListener;
028:
029: import javax.swing.Icon;
030: import javax.swing.JComponent;
031: import javax.swing.JSlider;
032: import javax.swing.UIManager;
033: import javax.swing.plaf.ComponentUI;
034: import javax.swing.plaf.basic.BasicSliderUI;
035:
036: import org.apache.harmony.x.swing.StringConstants;
037:
038: public class MetalSliderUI extends BasicSliderUI {
039: protected class MetalPropertyListener extends PropertyChangeHandler {
040: public void propertyChange(final PropertyChangeEvent e) {
041: String changedProperty = e.getPropertyName();
042: if (StringConstants.COMPONENT_ORIENTATION
043: .equals(changedProperty)) {
044: recalculateIfOrientationChanged();
045: } else if (StringConstants.BORDER_PROPERTY_CHANGED
046: .equals(changedProperty)) {
047: recalculateIfInsetsChanged();
048: }
049: calculateGeometry();
050: slider.revalidate();
051: slider.repaint();
052: }
053: }
054:
055: protected final int TICK_BUFFER = 4;
056: protected final String SLIDER_FILL = "JSlider.isFilled";
057: protected boolean filledSlider;
058: protected static Color thumbColor;
059: protected static Color highlightColor;
060: protected static Color darkShadowColor;
061: protected static int trackWidth;
062: protected static int tickLength;
063: protected static Icon horizThumbIcon;
064: protected static Icon vertThumbIcon;
065:
066: public MetalSliderUI() {
067: super (null);
068: }
069:
070: public static ComponentUI createUI(final JComponent c) {
071: return new MetalSliderUI();
072: }
073:
074: public void installUI(final JComponent c) {
075: slider = (JSlider) c;
076: horizThumbIcon = UIManager
077: .getIcon("Slider.horizontalThumbIcon");
078: vertThumbIcon = UIManager.getIcon("Slider.verticalThumbIcon");
079: thumbRect.setSize(getThumbSize());
080:
081: super .installUI(c);
082:
083: trackWidth = UIManager.getInt("Slider.trackWidth");
084: tickLength = UIManager.getInt("Slider.majorTickLength");
085:
086: thumbColor = UIManager.getColor("Slider.foreground");
087: highlightColor = UIManager.getColor("Slider.highlight");
088: darkShadowColor = UIManager.getColor("Slider.shadow");
089: }
090:
091: protected PropertyChangeListener createPropertyChangeListener(
092: final JSlider slider) {
093: return new MetalPropertyListener();
094: }
095:
096: public void paintThumb(final Graphics g) {
097: Color oldColor = g.getColor();
098: if (slider.isFocusOwner()) {
099: g.setColor(thumbColor);
100: } else {
101: g.setColor(Color.LIGHT_GRAY);
102: }
103: if (slider.getOrientation() == JSlider.HORIZONTAL) {
104: horizThumbIcon.paintIcon(slider, g, thumbRect.x,
105: thumbRect.y);
106: } else {
107: vertThumbIcon
108: .paintIcon(slider, g, thumbRect.x, thumbRect.y);
109: }
110: Color shadow = Color.GRAY;
111: Color highlight = Color.WHITE;
112: Rectangle thumbBounds;
113: if (slider.getOrientation() == JSlider.HORIZONTAL) {
114: thumbBounds = new Rectangle(thumbRect.x + 1, thumbRect.y,
115: thumbRect.width, thumbRect.height / 2);
116: } else {
117: thumbBounds = new Rectangle(thumbRect.x, thumbRect.y + 1,
118: thumbRect.width / 2, thumbRect.height);
119: }
120: MetalBumps.paintBumps(g, thumbBounds, shadow, highlight);
121: g.setColor(oldColor);
122: }
123:
124: public void paintTrack(final Graphics g) {
125: Color oldColor = g.getColor();
126: if (slider.getOrientation() == JSlider.HORIZONTAL) {
127: int x = trackRect.x;
128: int y = thumbRect.y + getThumbSize().height
129: - getThumbOverhang() - trackWidth;
130: int width = getTrackLength();
131: int height = trackWidth;
132: paintTrack(g, x, y, width, height);
133: } else {
134: int x = thumbRect.x + getThumbSize().width
135: - getThumbOverhang() - trackWidth;
136: int y = trackRect.y;
137: int width = trackWidth;
138: int height = getTrackLength();
139: paintTrack(g, x, y, width, height);
140: }
141: g.setColor(oldColor);
142: }
143:
144: public void paintFocus(final Graphics g) {
145: }
146:
147: protected Dimension getThumbSize() {
148: if (slider.getOrientation() == JSlider.HORIZONTAL) {
149: return new Dimension(horizThumbIcon.getIconWidth(),
150: horizThumbIcon.getIconHeight());
151: } else {
152: return new Dimension(vertThumbIcon.getIconWidth(),
153: vertThumbIcon.getIconHeight());
154: }
155: }
156:
157: public int getTickLength() {
158: return slider.getPaintTicks() ? tickLength : 0;
159: }
160:
161: protected int getTrackWidth() {
162: if (slider.getOrientation() == JSlider.HORIZONTAL) {
163: return trackRect.height;
164: } else {
165: return trackRect.width;
166: }
167: }
168:
169: protected int getTrackLength() {
170: if (slider.getOrientation() == JSlider.HORIZONTAL) {
171: return trackRect.width;
172: } else {
173: return trackRect.height;
174: }
175: }
176:
177: protected int getThumbOverhang() {
178: return 4;
179: }
180:
181: protected void scrollDueToClickInTrack(final int dir) {
182: scrollByUnit(dir);
183: }
184:
185: protected void paintMinorTickForHorizSlider(final Graphics g,
186: final Rectangle tickBounds, final int x) {
187: g.setColor(slider.getForeground());
188: super .paintMinorTickForHorizSlider(g, tickBounds, x);
189: }
190:
191: protected void paintMajorTickForHorizSlider(final Graphics g,
192: final Rectangle tickBounds, final int x) {
193: g.setColor(slider.getForeground());
194: super .paintMajorTickForHorizSlider(g, tickBounds, x);
195: }
196:
197: protected void paintMinorTickForVertSlider(final Graphics g,
198: final Rectangle tickBounds, final int y) {
199: g.setColor(slider.getForeground());
200: super .paintMinorTickForVertSlider(g, tickBounds, y);
201: }
202:
203: protected void paintMajorTickForVertSlider(final Graphics g,
204: final Rectangle tickBounds, final int y) {
205: g.setColor(slider.getForeground());
206: super .paintMajorTickForVertSlider(g, tickBounds, y);
207: }
208:
209: private void paintTrack(final Graphics g, final int x, final int y,
210: final int width, final int height) {
211: g.setColor(getShadowColor().brighter());
212: g.drawRect(x + 1, y + 1, width, height);
213: g.setColor(darkShadowColor);
214: g.drawRect(x, y, width, height);
215: g.setColor(highlightColor);
216: g
217: .drawLine(x + 1, y + 1 + height, x + 1 + width, y + 1
218: + height);
219: g.drawLine(x + 1 + width, y + 1, x + 1 + width, y + 1 + height);
220: }
221: }
|