38 |
|
|
39 |
package javax.swing.plaf.metal; |
package javax.swing.plaf.metal; |
40 |
|
|
41 |
|
import java.awt.Color; |
42 |
import java.awt.Dimension; |
import java.awt.Dimension; |
43 |
import java.awt.Graphics; |
import java.awt.Graphics; |
44 |
import java.awt.Rectangle; |
import java.awt.Rectangle; |
45 |
import java.util.HashMap; |
import java.beans.PropertyChangeEvent; |
46 |
|
import java.beans.PropertyChangeListener; |
47 |
|
|
48 |
|
import javax.swing.JButton; |
49 |
import javax.swing.JComponent; |
import javax.swing.JComponent; |
50 |
|
import javax.swing.JScrollBar; |
51 |
import javax.swing.UIDefaults; |
import javax.swing.UIDefaults; |
52 |
import javax.swing.UIManager; |
import javax.swing.UIManager; |
53 |
import javax.swing.plaf.ComponentUI; |
import javax.swing.plaf.ComponentUI; |
54 |
import javax.swing.plaf.basic.BasicScrollBarUI; |
import javax.swing.plaf.basic.BasicScrollBarUI; |
55 |
|
|
56 |
|
/** |
57 |
|
* A UI delegate for the {@link JScrollBar} component. |
58 |
|
*/ |
59 |
public class MetalScrollBarUI |
public class MetalScrollBarUI |
60 |
extends BasicScrollBarUI |
extends BasicScrollBarUI |
61 |
{ |
{ |
62 |
|
|
63 |
|
/** |
64 |
|
* A property change handler for the UI delegate that monitors for |
65 |
|
* changes to the "JScrollBar.isFreeStanding" property, and updates |
66 |
|
* the buttons and track rendering as appropriate. |
67 |
|
*/ |
68 |
|
class MetalScrollBarPropertyChangeHandler |
69 |
|
extends BasicScrollBarUI.PropertyChangeHandler |
70 |
|
{ |
71 |
|
/** |
72 |
|
* Creates a new handler. |
73 |
|
* |
74 |
|
* @see #createPropertyChangeListener() |
75 |
|
*/ |
76 |
|
public MetalScrollBarPropertyChangeHandler() |
77 |
|
{ |
78 |
|
} |
79 |
|
|
80 |
|
/** |
81 |
|
* Handles a property change event. If the event name is |
82 |
|
* <code>JSlider.isFreeStanding</code>, this method updates the |
83 |
|
* delegate, otherwise the event is passed up to the super class. |
84 |
|
* |
85 |
|
* @param e the property change event. |
86 |
|
*/ |
87 |
|
public void propertyChange(PropertyChangeEvent e) |
88 |
|
{ |
89 |
|
if (e.getPropertyName().equals(FREE_STANDING_PROP)) |
90 |
|
{ |
91 |
|
Boolean prop = (Boolean) e.getNewValue(); |
92 |
|
isFreeStanding = (prop == null ? true : prop.booleanValue()); |
93 |
|
increaseButton.setFreeStanding(isFreeStanding); |
94 |
|
decreaseButton.setFreeStanding(isFreeStanding); |
95 |
|
} |
96 |
|
} |
97 |
|
} |
98 |
|
|
99 |
|
/** The name for the 'free standing' property. */ |
100 |
|
public static final String FREE_STANDING_PROP = "JScrollBar.isFreeStanding"; |
101 |
|
|
102 |
/** The minimum thumb size */ |
/** The minimum thumb size */ |
103 |
private static final Dimension MIN_THUMB_SIZE = new Dimension(18, 18); |
private static final Dimension MIN_THUMB_SIZE = new Dimension(17, 17); |
|
|
|
|
// FIXME: maybe replace by a Map of instances when this becomes stateful |
|
|
/** The shared UI instance for JScrollBars. */ |
|
|
private static HashMap instances = null; |
|
104 |
|
|
105 |
|
/** The button that increases the value in the scroll bar. */ |
106 |
|
protected MetalScrollButton increaseButton; |
107 |
|
|
108 |
|
/** The button that decreases the value in the scroll bar. */ |
109 |
|
protected MetalScrollButton decreaseButton; |
110 |
|
|
111 |
|
/** The scroll bar width. */ |
112 |
|
protected int scrollBarWidth; |
113 |
|
|
114 |
|
/** |
115 |
|
* A flag that indicates whether the scroll bar is "free standing", which |
116 |
|
* means it has complete borders and can be used anywhere in the UI. A |
117 |
|
* scroll bar which is not free standing has borders missing from one |
118 |
|
* side, and relies on being part of another container with its own borders |
119 |
|
* to look right visually. */ |
120 |
|
protected boolean isFreeStanding; |
121 |
|
|
122 |
/** |
/** |
123 |
* Constructs a new instance of MetalScrollBarUI. |
* Constructs a new instance of MetalScrollBarUI. |
124 |
*/ |
*/ |
136 |
*/ |
*/ |
137 |
public static ComponentUI createUI(JComponent component) |
public static ComponentUI createUI(JComponent component) |
138 |
{ |
{ |
139 |
if (instances == null) |
return new MetalScrollBarUI(); |
140 |
instances = new HashMap(); |
} |
141 |
|
|
142 |
|
/** |
143 |
|
* Installs the defaults. |
144 |
|
*/ |
145 |
|
protected void installDefaults() |
146 |
|
{ |
147 |
|
// need to initialise isFreeStanding before calling the super class, |
148 |
|
// so that the value is set when createIncreaseButton() and |
149 |
|
// createDecreaseButton() are called (unless there is somewhere earlier |
150 |
|
// that we can do this). |
151 |
|
Boolean prop = (Boolean) scrollbar.getClientProperty(FREE_STANDING_PROP); |
152 |
|
isFreeStanding = (prop == null ? true : prop.booleanValue()); |
153 |
|
super.installDefaults(); |
154 |
|
} |
155 |
|
|
156 |
|
/** |
157 |
|
* Creates a property change listener for the delegate to use. This |
158 |
|
* overrides the method to provide a custom listener for the |
159 |
|
* {@link MetalLookAndFeel} that can handle the |
160 |
|
* <code>JScrollBar.isFreeStanding</code> property. |
161 |
|
* |
162 |
|
* @return A property change listener. |
163 |
|
*/ |
164 |
|
protected PropertyChangeListener createPropertyChangeListener() |
165 |
|
{ |
166 |
|
return new MetalScrollBarPropertyChangeHandler(); |
167 |
|
} |
168 |
|
|
169 |
|
/** |
170 |
|
* Creates a new button to use as the control at the lower end of the |
171 |
|
* {@link JScrollBar}. |
172 |
|
* |
173 |
|
* @param orientation the orientation of the button ({@link #NORTH}, |
174 |
|
* {@link #SOUTH}, {@link #EAST} or {@link #WEST}). |
175 |
|
* |
176 |
|
* @return The button. |
177 |
|
*/ |
178 |
|
protected JButton createDecreaseButton(int orientation) |
179 |
|
{ |
180 |
|
UIDefaults defaults = UIManager.getLookAndFeelDefaults(); |
181 |
|
scrollBarWidth = defaults.getInt("ScrollBar.width"); |
182 |
|
return new MetalScrollButton(orientation, scrollBarWidth, isFreeStanding); |
183 |
|
} |
184 |
|
|
185 |
Object o = instances.get(component); |
/** |
186 |
MetalScrollBarUI instance; |
* Creates a new button to use as the control at the upper end of the |
187 |
if (o == null) |
* {@link JScrollBar}. |
188 |
|
* |
189 |
|
* @param orientation the orientation of the button ({@link #NORTH}, |
190 |
|
* {@link #SOUTH}, {@link #EAST} or {@link #WEST}). |
191 |
|
* |
192 |
|
* @return The button. |
193 |
|
*/ |
194 |
|
protected JButton createIncreaseButton(int orientation) |
195 |
|
{ |
196 |
|
UIDefaults defaults = UIManager.getLookAndFeelDefaults(); |
197 |
|
scrollBarWidth = defaults.getInt("ScrollBar.width"); |
198 |
|
return new MetalScrollButton(orientation, scrollBarWidth, isFreeStanding); |
199 |
|
} |
200 |
|
|
201 |
|
/** |
202 |
|
* Paints the track for the scrollbar. |
203 |
|
* |
204 |
|
* @param g the graphics device. |
205 |
|
* @param c the component. |
206 |
|
* @param trackBounds the track bounds. |
207 |
|
*/ |
208 |
|
protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) |
209 |
|
{ |
210 |
|
g.setColor(MetalLookAndFeel.getControl()); |
211 |
|
g.fillRect(trackBounds.x, trackBounds.y, trackBounds.width, |
212 |
|
trackBounds.height); |
213 |
|
if (scrollbar.getOrientation() == HORIZONTAL) |
214 |
|
paintTrackHorizontal(g, c, trackBounds.x, trackBounds.y, |
215 |
|
trackBounds.width, trackBounds.height); |
216 |
|
else |
217 |
|
paintTrackVertical(g, c, trackBounds.x, trackBounds.y, |
218 |
|
trackBounds.width, trackBounds.height); |
219 |
|
|
220 |
|
} |
221 |
|
|
222 |
|
/** |
223 |
|
* Paints the track for a horizontal scrollbar. |
224 |
|
* |
225 |
|
* @param g the graphics device. |
226 |
|
* @param c the component. |
227 |
|
* @param x the x-coordinate for the track bounds. |
228 |
|
* @param y the y-coordinate for the track bounds. |
229 |
|
* @param w the width for the track bounds. |
230 |
|
* @param h the height for the track bounds. |
231 |
|
*/ |
232 |
|
private void paintTrackHorizontal(Graphics g, JComponent c, |
233 |
|
int x, int y, int w, int h) |
234 |
|
{ |
235 |
|
if (c.isEnabled()) |
236 |
{ |
{ |
237 |
instance = new MetalScrollBarUI(); |
g.setColor(MetalLookAndFeel.getControlDarkShadow()); |
238 |
instances.put(component, instance); |
g.drawLine(x, y, x, y + h - 1); |
239 |
|
g.drawLine(x, y, x + w - 1, y); |
240 |
|
g.drawLine(x + w - 1, y, x + w - 1, y + h - 1); |
241 |
|
|
242 |
|
g.setColor(MetalLookAndFeel.getControlShadow()); |
243 |
|
g.drawLine(x + 1, y + 1, x + 1, y + h - 1); |
244 |
|
g.drawLine(x + 1, y + 1, x + w - 2, y + 1); |
245 |
|
|
246 |
|
if (isFreeStanding) |
247 |
|
{ |
248 |
|
g.setColor(MetalLookAndFeel.getControlDarkShadow()); |
249 |
|
g.drawLine(x, y + h - 2, x + w - 1, y + h - 2); |
250 |
|
g.setColor(MetalLookAndFeel.getControlShadow()); |
251 |
|
g.drawLine(x, y + h - 1, x + w - 1, y + h - 1); |
252 |
|
} |
253 |
} |
} |
254 |
else |
else |
255 |
instance = (MetalScrollBarUI) o; |
{ |
256 |
|
g.setColor(MetalLookAndFeel.getControlDisabled()); |
257 |
return instance; |
g.drawRect(x, y, w - 1, h - 1); |
258 |
|
} |
259 |
|
} |
260 |
|
|
261 |
|
/** |
262 |
|
* Paints the track for a vertical scrollbar. |
263 |
|
* |
264 |
|
* @param g the graphics device. |
265 |
|
* @param c the component. |
266 |
|
* @param x the x-coordinate for the track bounds. |
267 |
|
* @param y the y-coordinate for the track bounds. |
268 |
|
* @param w the width for the track bounds. |
269 |
|
* @param h the height for the track bounds. |
270 |
|
*/ |
271 |
|
protected void paintTrackVertical(Graphics g, JComponent c, |
272 |
|
int x, int y, int w, int h) |
273 |
|
{ |
274 |
|
if (c.isEnabled()) |
275 |
|
{ |
276 |
|
g.setColor(MetalLookAndFeel.getControlDarkShadow()); |
277 |
|
g.drawLine(x, y, x, y + h - 1); |
278 |
|
g.drawLine(x, y, x + w - 1, y); |
279 |
|
g.drawLine(x, y + h - 1, x + w - 1, y + h - 1); |
280 |
|
|
281 |
|
g.setColor(MetalLookAndFeel.getControlShadow()); |
282 |
|
g.drawLine(x + 1, y + 1, x + w - 1, y + 1); |
283 |
|
g.drawLine(x + 1, y + 1, x + 1, y + h - 2); |
284 |
|
g.drawLine(x + 1, y + h - 2, x + w - 1, y + h - 2); |
285 |
|
|
286 |
|
if (isFreeStanding) |
287 |
|
{ |
288 |
|
g.setColor(MetalLookAndFeel.getControlDarkShadow()); |
289 |
|
g.drawLine(x + w - 2, y, x + w - 2, y + h - 1); |
290 |
|
g.setColor(MetalLookAndFeel.getControlHighlight()); |
291 |
|
g.drawLine(x + w - 1, y, x + w - 1, y + h - 1); |
292 |
|
} |
293 |
|
} |
294 |
|
else |
295 |
|
{ |
296 |
|
g.setColor(MetalLookAndFeel.getControlDisabled()); |
297 |
|
g.drawRect(x, y, w - 1, h - 1); |
298 |
|
} |
299 |
} |
} |
300 |
|
|
301 |
/** |
/** |
307 |
*/ |
*/ |
308 |
protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) |
protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) |
309 |
{ |
{ |
310 |
|
// a disabled scrollbar has no thumb in the metal look and feel |
311 |
|
if (!c.isEnabled()) |
312 |
|
return; |
313 |
|
|
314 |
// first we fill the background |
// first we fill the background |
315 |
g.setColor(thumbColor); |
g.setColor(thumbColor); |
316 |
g.fillRect(thumbBounds.x, thumbBounds.y, thumbBounds.width, |
g.fillRect(thumbBounds.x, thumbBounds.y, thumbBounds.width, |
317 |
thumbBounds.height); |
thumbBounds.height); |
318 |
|
|
319 |
// draw the outer dark line |
// draw the outer dark line |
320 |
g.setColor(thumbDarkShadowColor); |
int hAdj = 1; |
321 |
g.drawRect(thumbBounds.x, thumbBounds.y, thumbBounds.width - 1, |
int wAdj = 1; |
322 |
thumbBounds.height - 1); |
if (scrollbar.getOrientation() == HORIZONTAL) |
323 |
|
hAdj++; |
324 |
|
else |
325 |
|
wAdj++; |
326 |
|
|
327 |
|
g.setColor(new Color(102, 102, 153)); |
328 |
|
g.drawRect(thumbBounds.x, thumbBounds.y, thumbBounds.width - wAdj, |
329 |
|
thumbBounds.height - hAdj); |
330 |
|
|
331 |
// draw the inner light line |
// draw the inner light line |
332 |
g.setColor(thumbHighlightColor); |
g.setColor(thumbHighlightColor); |
347 |
// draw the pattern |
// draw the pattern |
348 |
MetalUtils.fillMetalPattern(g, thumbBounds.x + 3, thumbBounds.y + 3, |
MetalUtils.fillMetalPattern(g, thumbBounds.x + 3, thumbBounds.y + 3, |
349 |
thumbBounds.width - 6, thumbBounds.height - 6, |
thumbBounds.width - 6, thumbBounds.height - 6, |
350 |
thumbHighlightColor, thumbDarkShadowColor); |
thumbHighlightColor, new Color(102, 102, 153)); |
351 |
} |
} |
352 |
|
|
353 |
/** |
/** |
359 |
{ |
{ |
360 |
return MIN_THUMB_SIZE; |
return MIN_THUMB_SIZE; |
361 |
} |
} |
362 |
|
|
363 |
} |
} |
364 |
|
|