KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > synth > SynthScrollBarUI


1 /*
2  * @(#)SynthScrollBarUI.java 1.28 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.plaf.synth;
9
10 import java.awt.*;
11 import java.awt.event.*;
12
13 import java.beans.*;
14
15 import javax.swing.*;
16 import javax.swing.event.*;
17 import javax.swing.plaf.*;
18 import javax.swing.plaf.basic.*;
19 import sun.swing.plaf.synth.SynthUI;
20
21
22 /**
23  * Synth's ScrollBarUI.
24  *
25  * @version 1.28, 12/19/03
26  * @author Scott Violet
27  */

28 class SynthScrollBarUI extends BasicScrollBarUI implements
29                                     PropertyChangeListener, SynthUI {
30     private static final Insets tmpInsets = new Insets(0, 0, 0, 0);
31
32     private SynthStyle JavaDoc style;
33     private SynthStyle JavaDoc thumbStyle;
34     private SynthStyle JavaDoc trackStyle;
35
36     private int scrollBarWidth;
37
38
39     public static ComponentUI createUI(JComponent c) {
40         return new SynthScrollBarUI JavaDoc();
41     }
42
43     protected void installDefaults() {
44     trackHighlight = NO_HIGHLIGHT;
45         if (scrollbar.getLayout() == null ||
46                      (scrollbar.getLayout() instanceof UIResource)) {
47             scrollbar.setLayout(this);
48         }
49         updateStyle(scrollbar);
50     }
51
52     protected void configureScrollBarColors() {
53     }
54
55     private void updateStyle(JScrollBar c) {
56         SynthStyle JavaDoc oldStyle = style;
57         SynthContext JavaDoc context = getContext(c, ENABLED);
58         style = SynthLookAndFeel.updateStyle(context, this);
59         if (style != oldStyle) {
60             Insets insets = c.getInsets();
61             scrollBarWidth = style.getInt(context,"ScrollBar.thumbHeight", 14);
62             minimumThumbSize = new Dimension();
63             if (c.getOrientation() == JScrollBar.VERTICAL) {
64                     minimumThumbSize.width = scrollBarWidth;
65                     minimumThumbSize.height = 7;
66                     scrollBarWidth += insets.left + insets.right;
67             } else {
68                     minimumThumbSize.width = 7;
69                     minimumThumbSize.height = scrollBarWidth;
70                     scrollBarWidth += insets.top + insets.bottom;
71             }
72             maximumThumbSize = (Dimension)style.get(context,
73                         "ScrollBar.maximumThumbSize");
74             if (maximumThumbSize == null) {
75                 maximumThumbSize = new Dimension(4096, 4097);
76             }
77             if (oldStyle != null) {
78                 uninstallKeyboardActions();
79                 installKeyboardActions();
80             }
81         }
82         context.dispose();
83
84         context = getContext(c, Region.SCROLL_BAR_TRACK, ENABLED);
85         trackStyle = SynthLookAndFeel.updateStyle(context, this);
86         context.dispose();
87
88         context = getContext(c, Region.SCROLL_BAR_THUMB, ENABLED);
89         thumbStyle = SynthLookAndFeel.updateStyle(context, this);
90         context.dispose();
91     }
92
93     protected void installListeners() {
94         super.installListeners();
95         scrollbar.addPropertyChangeListener(this);
96     }
97
98     protected void uninstallListeners() {
99         super.uninstallListeners();
100         scrollbar.removePropertyChangeListener(this);
101     }
102
103     protected void uninstallDefaults(){
104         SynthContext JavaDoc context = getContext(scrollbar, ENABLED);
105         style.uninstallDefaults(context);
106         context.dispose();
107         style = null;
108
109         context = getContext(scrollbar, Region.SCROLL_BAR_TRACK, ENABLED);
110         trackStyle.uninstallDefaults(context);
111         context.dispose();
112         trackStyle = null;
113
114         context = getContext(scrollbar, Region.SCROLL_BAR_THUMB, ENABLED);
115         thumbStyle.uninstallDefaults(context);
116         context.dispose();
117         thumbStyle = null;
118
119         super.uninstallDefaults();
120     }
121
122
123     public SynthContext JavaDoc getContext(JComponent c) {
124         return getContext(c, getComponentState(c));
125     }
126
127     private SynthContext JavaDoc getContext(JComponent c, int state) {
128         return SynthContext.getContext(SynthContext JavaDoc.class, c,
129                     SynthLookAndFeel.getRegion(c), style, state);
130     }
131
132     private Region JavaDoc getRegion(JComponent c) {
133         return SynthLookAndFeel.getRegion(c);
134     }
135
136     private int getComponentState(JComponent c) {
137         return SynthLookAndFeel.getComponentState(c);
138     }
139
140     private SynthContext JavaDoc getContext(JComponent c, Region JavaDoc region) {
141         return getContext(c, region, getComponentState(c, region));
142     }
143
144     private SynthContext JavaDoc getContext(JComponent c, Region JavaDoc region, int state) {
145         SynthStyle JavaDoc style = trackStyle;
146
147         if (region == Region.SCROLL_BAR_THUMB) {
148             style = thumbStyle;
149         }
150         return SynthContext.getContext(SynthContext JavaDoc.class, c, region, style,
151                                        state);
152     }
153
154     private int getComponentState(JComponent c, Region JavaDoc region) {
155         if (region == Region.SCROLL_BAR_THUMB && isThumbRollover() &&
156                                                  c.isEnabled()) {
157             return MOUSE_OVER;
158         }
159         return SynthLookAndFeel.getComponentState(c);
160     }
161
162     public boolean getSupportsAbsolutePositioning() {
163         SynthContext JavaDoc context = getContext(scrollbar);
164         boolean value = style.getBoolean(context,
165                       "ScrollBar.allowsAbsolutePositioning", false);
166         context.dispose();
167     return value;
168     }
169
170     public void update(Graphics g, JComponent c) {
171         SynthContext JavaDoc context = getContext(c);
172
173         SynthLookAndFeel.update(context, g);
174         context.getPainter().paintScrollBarBackground(context,
175                           g, 0, 0, c.getWidth(), c.getHeight());
176         paint(context, g);
177         context.dispose();
178     }
179
180     public void paint(Graphics g, JComponent c) {
181         SynthContext JavaDoc context = getContext(c);
182
183         paint(context, g);
184         context.dispose();
185     }
186
187     protected void paint(SynthContext JavaDoc context, Graphics g) {
188         SynthContext JavaDoc subcontext = getContext(scrollbar,
189                                              Region.SCROLL_BAR_TRACK);
190     paintTrack(subcontext, g, getTrackBounds());
191         subcontext.dispose();
192
193         subcontext = getContext(scrollbar, Region.SCROLL_BAR_THUMB);
194     paintThumb(subcontext, g, getThumbBounds());
195         subcontext.dispose();
196     }
197
198     public void paintBorder(SynthContext JavaDoc context, Graphics g, int x,
199                             int y, int w, int h) {
200         context.getPainter().paintScrollBarBorder(context, g, x, y, w, h);
201     }
202
203     protected void paintTrack(SynthContext JavaDoc ss, Graphics g,
204                               Rectangle trackBounds) {
205         SynthLookAndFeel.updateSubregion(ss, g, trackBounds);
206         ss.getPainter().paintScrollBarTrackBackground(ss, g, trackBounds.x,
207                         trackBounds.y, trackBounds.width, trackBounds.height);
208         ss.getPainter().paintScrollBarTrackBorder(ss, g, trackBounds.x,
209                         trackBounds.y, trackBounds.width, trackBounds.height);
210     }
211
212     protected void paintThumb(SynthContext JavaDoc ss, Graphics g,
213                               Rectangle thumbBounds) {
214         int orientation = scrollbar.getOrientation();
215         ss.getPainter().paintScrollBarThumbBackground(ss, g, thumbBounds.x,
216                         thumbBounds.y, thumbBounds.width, thumbBounds.height,
217                         orientation);
218         ss.getPainter().paintScrollBarThumbBorder(ss, g, thumbBounds.x,
219                         thumbBounds.y, thumbBounds.width, thumbBounds.height,
220                         orientation);
221     }
222
223     /**
224      * A vertical scrollbar's preferred width is the maximum of
225      * preferred widths of the (non <code>null</code>)
226      * increment/decrement buttons,
227      * and the minimum width of the thumb. The preferred height is the
228      * sum of the preferred heights of the same parts. The basis for
229      * the preferred size of a horizontal scrollbar is similar.
230      * <p>
231      * The <code>preferredSize</code> is only computed once, subsequent
232      * calls to this method just return a cached size.
233      *
234      * @param c the <code>JScrollBar</code> that's delegating this method to us
235      * @return the preferred size of a Basic JScrollBar
236      * @see #getMaximumSize
237      * @see #getMinimumSize
238      */

239     public Dimension getPreferredSize(JComponent c) {
240     return (scrollbar.getOrientation() == JScrollBar.VERTICAL)
241         ? new Dimension(scrollBarWidth, 48)
242         : new Dimension(48, scrollBarWidth);
243     }
244
245
246     protected JButton createDecreaseButton(int orientation) {
247         SynthArrowButton JavaDoc synthArrowButton = new SynthArrowButton JavaDoc(orientation);
248         synthArrowButton.setName("ScrollBar.button");
249         return synthArrowButton;
250     }
251
252     protected JButton createIncreaseButton(int orientation) {
253         SynthArrowButton JavaDoc synthArrowButton = new SynthArrowButton JavaDoc(orientation);
254         synthArrowButton.setName("ScrollBar.button");
255         return synthArrowButton;
256     }
257
258     protected void setThumbRollover(boolean active) {
259         if (isThumbRollover() != active) {
260             scrollbar.repaint(getThumbBounds());
261             super.setThumbRollover(active);
262         }
263     }
264
265     private void updateButtonDirections() {
266         int orient = scrollbar.getOrientation();
267         if (scrollbar.getComponentOrientation().isLeftToRight()) {
268             ((SynthArrowButton JavaDoc)incrButton).setDirection(
269                         orient == HORIZONTAL? EAST : SOUTH);
270             ((SynthArrowButton JavaDoc)decrButton).setDirection(
271                         orient == HORIZONTAL? WEST : NORTH);
272         }
273         else {
274             ((SynthArrowButton JavaDoc)incrButton).setDirection(
275                         orient == HORIZONTAL? WEST : SOUTH);
276             ((SynthArrowButton JavaDoc)decrButton).setDirection(
277                         orient == HORIZONTAL ? EAST : NORTH);
278         }
279     }
280
281     //
282
// PropertyChangeListener
283
//
284
public void propertyChange(PropertyChangeEvent e) {
285         String JavaDoc propertyName = e.getPropertyName();
286
287         if (SynthLookAndFeel.shouldUpdateStyle(e)) {
288             updateStyle((JScrollBar)e.getSource());
289         }
290         else if ("orientation" == propertyName) {
291             updateButtonDirections();
292         }
293         else if ("componentOrientation" == propertyName) {
294             updateButtonDirections();
295     }
296     }
297 }
298
Popular Tags