1 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 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 style; 33 private SynthStyle thumbStyle; 34 private SynthStyle trackStyle; 35 36 private int scrollBarWidth; 37 38 39 public static ComponentUI createUI(JComponent c) { 40 return new SynthScrollBarUI (); 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 oldStyle = style; 57 SynthContext 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 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 getContext(JComponent c) { 124 return getContext(c, getComponentState(c)); 125 } 126 127 private SynthContext getContext(JComponent c, int state) { 128 return SynthContext.getContext(SynthContext .class, c, 129 SynthLookAndFeel.getRegion(c), style, state); 130 } 131 132 private Region 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 getContext(JComponent c, Region region) { 141 return getContext(c, region, getComponentState(c, region)); 142 } 143 144 private SynthContext getContext(JComponent c, Region region, int state) { 145 SynthStyle style = trackStyle; 146 147 if (region == Region.SCROLL_BAR_THUMB) { 148 style = thumbStyle; 149 } 150 return SynthContext.getContext(SynthContext .class, c, region, style, 151 state); 152 } 153 154 private int getComponentState(JComponent c, Region 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 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 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 context = getContext(c); 182 183 paint(context, g); 184 context.dispose(); 185 } 186 187 protected void paint(SynthContext context, Graphics g) { 188 SynthContext 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 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 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 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 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 synthArrowButton = new SynthArrowButton (orientation); 248 synthArrowButton.setName("ScrollBar.button"); 249 return synthArrowButton; 250 } 251 252 protected JButton createIncreaseButton(int orientation) { 253 SynthArrowButton synthArrowButton = new SynthArrowButton (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 )incrButton).setDirection( 269 orient == HORIZONTAL? EAST : SOUTH); 270 ((SynthArrowButton )decrButton).setDirection( 271 orient == HORIZONTAL? WEST : NORTH); 272 } 273 else { 274 ((SynthArrowButton )incrButton).setDirection( 275 orient == HORIZONTAL? WEST : SOUTH); 276 ((SynthArrowButton )decrButton).setDirection( 277 orient == HORIZONTAL ? EAST : NORTH); 278 } 279 } 280 281 public void propertyChange(PropertyChangeEvent e) { 285 String 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 |