1 19 20 21 26 package org.netbeans.swing.scrollbars.impl; 27 28 import com.sun.java.swing.plaf.windows.WindowsScrollBarUI; 29 import org.netbeans.swing.scrollbars.spi.Mark; 30 import org.netbeans.swing.scrollbars.spi.MarkingModel; 31 32 import javax.swing.*; 33 import javax.swing.event.ChangeListener ; 34 import java.awt.*; 35 import java.awt.event.ComponentEvent ; 36 import java.awt.event.ComponentListener ; 37 import java.awt.event.MouseEvent ; 38 import java.beans.PropertyChangeEvent ; 39 import java.beans.PropertyChangeListener ; 40 import java.util.Enumeration ; 41 import java.util.HashMap ; 42 import java.util.Iterator ; 43 import java.util.Map ; 44 45 51 public class WindowsMarkedScrollBarUI extends WindowsScrollBarUI implements PropertyChangeListener , ChangeListener { 52 private static final int minsize = 4; 53 54 private int gutterSize=9; 55 private MarkingModel model; 56 57 61 public WindowsMarkedScrollBarUI(MarkingModel model) { 62 this.model = model; 63 model.addChangeListener(this); 64 updateUI(); 65 } 66 67 public void updateUI () { 68 if ((scrollbar != null) && scrollbar.isVisible()) { 69 if (scrollbar.getOrientation() == JScrollBar.VERTICAL) { 70 layoutVScrollbar (scrollbar); 71 } else { 72 layoutHScrollbar (scrollbar); 73 } 74 } 75 } 76 77 public Dimension getPreferredSize(JComponent c) { 78 Dimension result = super.getPreferredSize(c); 79 if (scrollbar.getOrientation() == JScrollBar.VERTICAL) { 80 result.width += gutterSize / 2; 81 } else { 82 result.height += gutterSize / 2; 83 } 84 return result; 85 } 86 87 public void installUI(JComponent c) { 88 updateUI(); 89 super.installUI(c); 90 UIManager.addPropertyChangeListener(this); 91 c.addComponentListener ((ComponentListener ) trackListener); 92 } 93 94 public void uninstallUI(JComponent c) { 95 super.uninstallUI(c); 96 UIManager.removePropertyChangeListener(this); 97 c.removeComponentListener ((ComponentListener ) trackListener); 98 } 99 100 102 public final void propertyChange (PropertyChangeEvent pce) { 103 updateUI(); 104 } 105 106 protected void layoutVScrollbar(JScrollBar sb) { 107 super.layoutVScrollbar (sb); 108 trackRect.width -=gutterSize; 109 thumbRect.width -=gutterSize; 110 invalidateMarks(); 111 } 112 113 protected void layoutHScrollbar(JScrollBar sb) { 114 super.layoutVScrollbar (sb); 115 trackRect.height -=gutterSize; 116 thumbRect.height -=gutterSize; 117 invalidateMarks(); 118 } 119 120 public void paint(Graphics g, JComponent c) { 121 paintTrack(g, c, getTrackBounds()); 122 paintThumb(g, c, getThumbBounds()); 123 paintMarks(g); 124 if (scrollbar.getOrientation() == JScrollBar.VERTICAL) { 125 g.setColor(UIManager.getColor("controlDkShadow")); g.drawLine(c.getWidth()-2, trackRect.y, c.getWidth()-2, trackRect.y+trackRect.height); 127 g.setColor(UIManager.getColor("controlHighlight")); g.drawLine(c.getWidth()-1, trackRect.y, c.getWidth()-1, trackRect.y+trackRect.height); 129 } else { 130 g.setColor(UIManager.getColor("controlDkShadow")); g.drawLine(trackRect.x, 2, trackRect.x+trackRect.width, 2); 132 g.setColor(UIManager.getColor("controlHighlight")); g.drawLine(trackRect.x, 1, trackRect.x+trackRect.width, 1); 134 } 135 } 136 137 private void paintMarks (Graphics g) { 138 Map map = getRectsToMarks(); 142 for (Iterator i = map.keySet().iterator(); i.hasNext();) { 143 Rectangle mrect = (Rectangle) i.next(); 144 Mark mark = (Mark) map.get(mrect); 145 paintMark (mark, g, mrect); 146 } 147 } 148 149 private boolean invalid = true; 150 private void invalidateMarks() { 151 invalid = true; 152 scrollbar.repaint(); 153 } 154 155 private Map getRectsToMarks() { 156 if (invalid) { 157 buildRects(); 158 } 159 return rectsToMarks; 160 } 161 162 private void buildRects() { 163 rectsToMarks.clear(); 164 if (model.size() == 0) { 165 return; 166 } 167 boolean vertical = scrollbar.getOrientation() == JScrollBar.VERTICAL; 168 int markRange = model.getMaxMarkLocation(); 169 170 Rectangle r = getMarksRect(); 171 for (Enumeration e = model.getMarks(); e.hasMoreElements();) { 172 Mark mark = (Mark) e.nextElement(); 173 Rectangle curr = new Rectangle(); 174 if (vertical) { 175 r.y = translate (mark.getStart(), markRange); 176 r.height = Math.max(minsize, translate (mark.getLength(), markRange)); 177 r.width = gutterSize; 178 curr.setBounds(r.x, r.y, r.width, r.height); 179 rectsToMarks.put (curr, mark); 180 } else { 181 r.x = translate (mark.getStart(), markRange); 182 r.width = Math.max(minsize, translate (mark.getLength(), markRange)); 183 r.height = gutterSize; 184 curr.setBounds(r.x, r.y, r.width, r.height); 185 rectsToMarks.put (curr, mark); 186 } 187 } 188 invalid = false; 189 } 190 191 private HashMap rectsToMarks = new HashMap (); 192 193 197 public Mark markAtPoint (Point p) { 198 Rectangle rect = getMarksRect(); 199 if (!(rect.contains(p))) return null; 200 java.util.Iterator i = rectsToMarks.keySet().iterator(); 201 while (i.hasNext()) { 202 Rectangle r = (Rectangle) i.next(); 203 if (r.contains(p)) return (Mark) rectsToMarks.get(r); 204 } 205 return null; 206 } 207 208 215 protected final int translate (int loc, int max) { 216 int range = getVisibleRange(); 217 double factor = range / max; 218 double pos = factor * loc; 219 return Math.round(Math.round(pos)); 220 } 221 222 227 protected final int getVisibleRange () { 228 Rectangle mrect = getMarksRect(); 229 return (scrollbar.getOrientation() == JScrollBar.VERTICAL) ? 230 mrect.height : mrect.width; 231 } 232 233 236 protected final Rectangle getMarksRect () { 237 Rectangle result; 238 if (scrollbar.getOrientation() == JScrollBar.VERTICAL) { 239 result = new Rectangle (trackRect.x + trackRect.width + 1, trackRect.y, 240 trackRect.width +gutterSize, trackRect.height); 241 } else { 242 result = new Rectangle (trackRect.x, trackRect.y + trackRect.height + 1, 243 trackRect.width, trackRect.height +gutterSize); 244 } 245 return result; 246 } 247 248 protected TrackListener createTrackListener() { 249 return new MarkAndTrackListener (); 250 } 251 252 private int translateToScrollbarModel (int i) { 253 BoundedRangeModel mod = scrollbar.getModel(); 254 int min = mod.getMinimum(); 255 int max = mod.getMaximum(); 256 int mrange = max - min; 257 258 double factor = mrange / model.getMaxMarkLocation(); 259 double pos = (i * factor) + min; 260 return Math.round(Math.round(pos)); 261 } 262 263 private void goToMark (Mark m) { 264 Rectangle r = getMarksRect(); 265 int loc = translate (m.getStart(), r.height); 266 scrollbar.setValue(translateToScrollbarModel (loc)); 267 m.select(); 268 } 269 270 public void stateChanged(javax.swing.event.ChangeEvent e) { 271 invalidateMarks(); 272 } 273 274 protected class MarkAndTrackListener extends TrackListener implements ComponentListener { 275 public void mousePressed (MouseEvent e) 276 { 277 if (scrollbar.getValueIsAdjusting ()) { 278 super.mousePressed (e); 279 return; 280 } 281 if (SwingUtilities.isRightMouseButton(e) || 282 SwingUtilities.isMiddleMouseButton(e) || !scrollbar.isEnabled()) { 283 return; 284 } 285 286 Point p = e.getPoint(); 287 Rectangle r = getMarksRect(); 288 289 if (r.contains (p)) { 290 Mark m = markAtPoint (p); 291 if (m != null) { 292 goToMark (m); 293 } 294 } else { 295 super.mousePressed(e); 296 } 297 } 298 299 private boolean notInMarksRect (MouseEvent e) { 300 Point p = e.getPoint(); 301 Rectangle r = getMarksRect(); 302 return !(r.contains (p)); 303 } 304 305 public void mouseReleased (MouseEvent e) { 306 if (scrollbar.getValueIsAdjusting () || notInMarksRect (e)) { 307 super.mouseReleased (e); 308 } 309 } 310 311 public void mouseDragged (MouseEvent e) { 312 if (scrollbar.getValueIsAdjusting () || notInMarksRect (e)) { 313 super.mouseDragged (e); 314 } 315 } 316 String oldtext; 317 public void mouseMoved (MouseEvent e) { 318 if (scrollbar.getValueIsAdjusting () || notInMarksRect (e)) { 319 super.mouseMoved (e); 320 } else { 321 Point p = e.getPoint(); 322 Rectangle r = getMarksRect(); 323 324 if (r.contains (p)) { 325 Mark m = markAtPoint (p); 326 if (m != null) { 327 scrollbar.setToolTipText (m.getText()); 328 } else { 329 scrollbar.setToolTipText (null); 330 } 331 } 332 } 333 } 334 335 338 public void componentResized(ComponentEvent e) { 339 invalidateMarks(); 340 } 341 342 345 public void componentMoved(ComponentEvent e) { 346 347 } 348 349 352 public void componentShown(ComponentEvent e) { 353 invalidateMarks(); 354 } 355 356 359 public void componentHidden(ComponentEvent e) { 360 rectsToMarks.clear(); 361 invalid = true; 362 } 363 } 364 365 public void paintMarkMetal(Mark mark, Graphics g, Rectangle r) { 366 Color c = (Color) mark.get ("color"); if (c == null) { 368 c = UIManager.getColor("windowText"); } 370 g.setColor (c); 371 g.fillRect (r.x-1, r.y, r.width+1, r.height-1); 372 g.setColor (c.darker()); 373 g.drawRect (r.x-1, r.y, r.width-2, r.height-1); 374 } 375 376 377 378 public void paintMark(Mark mark, Graphics g, Rectangle r) { 379 if (getClass() == MetalMarkedScrollBarUI.class) { 380 paintMarkMetal (mark, g, r); 381 } else { 382 paintMarkWindows (mark, g, r); 383 } 384 } 385 386 private static final int overhang = 4; 387 public void paintMarkWindows (Mark mark, Graphics graphics, Rectangle rect) { 388 Color c = (Color) mark.get ("color"); if (c == null) { 390 c = UIManager.getColor("windowText"); } 392 Paint p = new GradientPaint(rect.x+rect.width-1, rect.y+3, c, 393 rect.x-overhang, rect.y+3, new Color (c.getRed(),c.getGreen(),c.getBlue(),35)); 394 ((Graphics2D)graphics).setPaint(p); 395 396 graphics.fillRect (rect.x-overhang, rect.y, rect.width+4, rect.height); 397 398 Color mColor = c; 399 400 c = UIManager.getColor ("controlHighlight"); p = new GradientPaint(rect.x+rect.width-1, rect.y+3, c, 402 rect.x-overhang, rect.y+3, new Color (c.getRed(),c.getGreen(),c.getBlue(),0)); 403 404 ((Graphics2D)graphics).setPaint(p); 405 406 graphics.drawLine (rect.x - overhang, rect.y, rect.x + rect.width, rect.y); 407 graphics.drawLine (rect.x - overhang, rect.y, rect.x - overhang, rect.y + rect.height); 408 409 420 421 427 428 433 434 p = new GradientPaint(rect.x+rect.width-1, rect.y+3, c.darker(), 435 rect.x-overhang, rect.y+3, UIManager.getColor("controlShadow")); 436 ((Graphics2D)graphics).setPaint(p); 437 438 442 } 443 } 444 | Popular Tags |