KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > scrollbars > impl > WindowsMarkedScrollBarUI


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 /*
22  * MarkedScrollbarUI.java
23  *
24  * Created on March 5, 2003, 4:32 PM
25  */

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 JavaDoc;
34 import java.awt.*;
35 import java.awt.event.ComponentEvent JavaDoc;
36 import java.awt.event.ComponentListener JavaDoc;
37 import java.awt.event.MouseEvent JavaDoc;
38 import java.beans.PropertyChangeEvent JavaDoc;
39 import java.beans.PropertyChangeListener JavaDoc;
40 import java.util.Enumeration JavaDoc;
41 import java.util.HashMap JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.util.Map JavaDoc;
44
45 /** Scrollbar UI that can take a MarkingModel and visually render
46  * rectangles for and respond to the marks alongside the scrollbar.
47  *
48  * @version 1.1
49  * @author Tim Boudreau
50  */

51 public class WindowsMarkedScrollBarUI extends WindowsScrollBarUI implements PropertyChangeListener JavaDoc, ChangeListener JavaDoc {
52     private static final int minsize = 4;
53
54     private int gutterSize=9;
55     private MarkingModel model;
56
57     /** Creates a new instance of MarkedScrollbarUI
58      * @param model An instance of MarkingModel which will provide marks for it to render alongside
59      * the scrollbar.
60      */

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 JavaDoc) trackListener);
92     }
93
94     public void uninstallUI(JComponent c) {
95         super.uninstallUI(c);
96         UIManager.removePropertyChangeListener(this);
97         c.removeComponentListener ((ComponentListener JavaDoc) trackListener);
98     }
99
100     /** Listens to the UIManager for changes, so the PLAF can be changed
101      * on the fly. Will call <code>updateUI()</code> in the case of a change. */

102     public final void propertyChange (PropertyChangeEvent JavaDoc 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")); //NOI18N
126
g.drawLine(c.getWidth()-2, trackRect.y, c.getWidth()-2, trackRect.y+trackRect.height);
127             g.setColor(UIManager.getColor("controlHighlight")); //NOI18N
128
g.drawLine(c.getWidth()-1, trackRect.y, c.getWidth()-1, trackRect.y+trackRect.height);
129         } else {
130             g.setColor(UIManager.getColor("controlDkShadow")); //NOI18N
131
g.drawLine(trackRect.x, 2, trackRect.x+trackRect.width, 2);
132             g.setColor(UIManager.getColor("controlHighlight")); //NOI18N
133
g.drawLine(trackRect.x, 1, trackRect.x+trackRect.width, 1);
134         }
135     }
136
137     private void paintMarks (Graphics g) {
138 // Rectangle r = new Rectangle ((scrollbar.getWidth() + 2) - gutterSize, 20, gutterSize -1, scrollbar.getHeight() - 40);
139
// g.setColor (scrollbar.getBackground());
140
// g.fillRect (r.x, r.y, r.width+1, r.height);
141
Map JavaDoc map = getRectsToMarks();
142         for (Iterator JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc rectsToMarks = new HashMap JavaDoc();
192
193     /** Find the mark at a given point in the component's coordinate space
194      * @return The Mark object at that point, or null
195      * @param p A point in the scrollbar's coordinate space
196      */

197     public Mark markAtPoint (Point p) {
198         Rectangle rect = getMarksRect();
199         if (!(rect.contains(p))) return null;
200         java.util.Iterator JavaDoc 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     /** Translate a location as given by a call to <code>someMark.getStart()</code>
209      * into the coordinate space of the scrollbar.
210      * @param loc The location given by the Mark object
211      * @param max The maximum, given by the model
212      * @return The coordinate in the space of the scrollbar, as a position along the
213      * scrollbar's resizable axis.
214      */

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     /** Get the total number of possible thumb positions on the resizable axis of the
223      * scrollbar - the track height or width, depending on the scrollbar's
224      * orientation
225      * @return The total
226      */

227     protected final int getVisibleRange () {
228         Rectangle mrect = getMarksRect();
229         return (scrollbar.getOrientation() == JScrollBar.VERTICAL) ?
230             mrect.height : mrect.width;
231     }
232
233     /** Get the rectangle within which marks can be displayed.
234      * @return The rectangle
235      */

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 JavaDoc e) {
271         invalidateMarks();
272     }
273
274     protected class MarkAndTrackListener extends TrackListener implements ComponentListener JavaDoc {
275         public void mousePressed (MouseEvent JavaDoc 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 JavaDoc e) {
300             Point p = e.getPoint();
301             Rectangle r = getMarksRect();
302             return !(r.contains (p));
303         }
304
305         public void mouseReleased (MouseEvent JavaDoc e) {
306             if (scrollbar.getValueIsAdjusting () || notInMarksRect (e)) {
307                 super.mouseReleased (e);
308             }
309         }
310
311         public void mouseDragged (MouseEvent JavaDoc e) {
312             if (scrollbar.getValueIsAdjusting () || notInMarksRect (e)) {
313                 super.mouseDragged (e);
314             }
315         }
316         String JavaDoc oldtext;
317         public void mouseMoved (MouseEvent JavaDoc 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         /**
336          * Invoked when the component's size changes.
337          */

338         public void componentResized(ComponentEvent JavaDoc e) {
339             invalidateMarks();
340         }
341
342         /**
343          * Invoked when the component's position changes.
344          */

345         public void componentMoved(ComponentEvent JavaDoc e) {
346
347         }
348
349         /**
350          * Invoked when the component has been made visible.
351          */

352         public void componentShown(ComponentEvent JavaDoc e) {
353             invalidateMarks();
354         }
355
356         /**
357          * Invoked when the component has been made invisible.
358          */

359         public void componentHidden(ComponentEvent JavaDoc 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"); //NOI18N
367
if (c == null) {
368              c = UIManager.getColor("windowText"); //NOI18N
369
}
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"); //NOI18N
389
if (c == null) {
390             c = UIManager.getColor("windowText"); //NOI18N
391
}
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"); //NOI18N
401
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         /*
410         c = mColor.darker();
411         p = new GradientPaint(rect.x+rect.width-1, rect.y+3, c,
412             rect.x-overhang, rect.y+3, new Color (c.getRed(),c.getGreen(),c.getBlue(),10));
413
414
415         ((Graphics2D)graphics).setPaint(p);
416
417         graphics.drawLine (rect.x - overhang, rect.y + rect.height, rect.x + rect.width, rect.y + rect.height);
418         graphics.drawLine (rect.x + rect.width - 4, rect.y + rect.height, rect.x + rect.width - 4, rect.y + 1);
419           */

420
421 /* graphics.setColor(c.darker());
422         graphics.drawLine(rect.x+rect.width, rect.y, rect.x+rect.width, rect.y+rect.height-1);
423         graphics.drawLine(rect.x+rect.width-1, rect.y, rect.x+rect.width-1, rect.y+rect.height-1);
424         graphics.drawLine(rect.x+rect.width-2, rect.y, rect.x+rect.width-2, rect.y+rect.height-1);
425         graphics.drawLine(rect.x+rect.width-3, rect.y, rect.x+rect.width-3, rect.y+rect.height-1);
426         */

427
428 /*
429         graphics.setColor(UIManager.getColor("controlLtHighlight"));
430         graphics.drawLine(rect.x-overhang, rect.y,
431             rect.x+rect.width+overhang, rect.y);
432             */

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         /*
439         graphics.drawLine(rect.x-overhang, rect.y+rect.height,
440             rect.x+rect.width+overhang, rect.y+rect.height);
441             */

442     }
443 }
444
Popular Tags