KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > plaf > aqua > PlainAquaToolbarUI


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  * PlainAquaToolbarUI.java
21  *
22  * Created on January 17, 2004, 3:00 AM
23  */

24
25 package org.netbeans.swing.plaf.aqua;
26
27 import org.netbeans.swing.plaf.util.UIUtils;
28
29 import javax.swing.*;
30 import javax.swing.BoxLayout JavaDoc;
31 import javax.swing.border.Border JavaDoc;
32 import javax.swing.plaf.ButtonUI JavaDoc;
33 import javax.swing.plaf.ComponentUI JavaDoc;
34 import javax.swing.plaf.basic.BasicToolBarUI JavaDoc;
35 import java.awt.*;
36 import java.awt.event.ContainerEvent JavaDoc;
37 import java.awt.event.ContainerListener JavaDoc;
38 import java.awt.geom.*;
39 import java.awt.image.BufferedImage JavaDoc;
40 import java.util.HashMap JavaDoc;
41 import java.util.Map JavaDoc;
42
43 /** A ToolbarUI subclass that gets rid of all borders
44  * on buttons and provides a finder-style toolbar look.
45  *
46  * @author Tim Boudreau
47  */

48 public class PlainAquaToolbarUI extends BasicToolBarUI JavaDoc implements ContainerListener JavaDoc {
49     private static final AquaTbBorder aquaborder = new AquaTbBorder();
50 // private static final AquaToolbarLayout layout = new AquaToolbarLayout();
51

52     private static final Color UPPER_GRADIENT_TOP = new Color(255,255,255);
53     private static final Color UPPER_GRADIENT_BOTTOM = new Color (228,230,232);
54     
55     private static final Color LOWER_GRADIENT_TOP = new Color(228,227,215);
56     private static final Color LOWER_GRADIENT_BOTTOM = new Color(249,249,249);
57         
58     
59     /** Creates a new instance of PlainAquaToolbarUI */
60     public PlainAquaToolbarUI() {
61     }
62     
63     public static ComponentUI JavaDoc createUI(JComponent c) {
64         return new PlainAquaToolbarUI();
65     }
66     
67     public void installUI( JComponent c ) {
68         super.installUI(c);
69         //Editor will try install a custom border - just use ours
70
UIManager.put ("Nb.Editor.Toolbar.border", aquaborder);
71         c.setBorder(aquaborder);
72         c.setOpaque(true);
73         c.addContainerListener(this);
74         
75         installButtonUIs (c);
76     }
77     
78     public void uninstallUI (JComponent c) {
79         super.uninstallUI (c);
80         c.setBorder (null);
81         c.removeContainerListener(this);
82     }
83     
84     /** Cache for images - normally only ever two - one for editor toolbar
85      * height and one for main window toolbar height
86      */

87     private static Map JavaDoc<Integer JavaDoc, BufferedImage JavaDoc> icache = new HashMap JavaDoc<Integer JavaDoc, BufferedImage JavaDoc>();
88     
89     private BufferedImage JavaDoc getCacheImage(JComponent c) {
90         BufferedImage JavaDoc img = icache.get(new Integer JavaDoc(c.getHeight()));
91         //Don't make a cache image for very small sizes - we're probably just
92
//initializing, and if not, painting will be cheap enough. Also
93
//ensure that the mid area width is at least a reasonable width for
94
//multiple blits on wide toolbars
95
if (img == null && c.getWidth() > (arcsize * 2) + 24 && c.getHeight() > 12) {
96             img = new BufferedImage JavaDoc (c.getWidth(), c.getHeight(),
97                 BufferedImage.TYPE_INT_ARGB_PRE); //INT_ARGB_PRE is native raster format on mac os
98

99             paintInto (img.getGraphics(), c);
100             icache.put (Integer.valueOf(c.getHeight()), img);
101         }
102         return img;
103     }
104     
105     public void update (Graphics g, JComponent c) {
106         paint (g, c);
107     }
108     
109     public void paint(Graphics g, JComponent c) {
110         if (!c.isOpaque() || c.getWidth() < 4 || c.getHeight() < 4) {
111             return;
112         }
113         BufferedImage JavaDoc img = getCacheImage(c);
114         if (img == null) {
115             paintInto (g, c);
116         } else {
117             //Use a cached image for painting - the geometry ops and
118
//plethora of GradientPaints in aqua toolbars are too expensive
119
//for use on every paint - slows down everything and allocates
120
//an 6 width * height integer rasters for per toolbar per paint
121
paintImage ((Graphics2D) g, img, c);
122         }
123     }
124     
125     /**
126      * Paint a cached image of the toolbar. Paints the left edge and
127      * content, if necessary, repeats the interior section until the
128      * width less-the-edges is full, then paints the end cap.
129      */

130     private void paintImage (Graphics2D g2d, BufferedImage JavaDoc img, JComponent c) {
131         int w = c.getWidth();
132         int h = c.getHeight();
133         int imgw = img.getWidth();
134         AffineTransform nullTransform = AffineTransform.getTranslateInstance(0,0);
135         if (w == imgw) {
136             //Perfect fit, we're done
137
g2d.drawRenderedImage (img, nullTransform);
138         } else if (w > imgw) {
139             //Wider than the image - loop and blit the interior, then draw the
140
//end cap
141
g2d.drawRenderedImage (img, nullTransform);
142             int uncovered = w - (imgw - (arcsize * 2));
143             int x = imgw - arcsize;
144             //Get the mid section of the cached image, less the end caps
145
do {
146                 BufferedImage JavaDoc mid = img.getSubimage (arcsize, 0, Math.min(uncovered, imgw - (arcsize * 2)), h);
147                 //blit it until we've filled all the space we need to
148
g2d.drawRenderedImage (mid, AffineTransform.getTranslateInstance(x, 0));
149                 x += mid.getWidth();
150                 uncovered -= mid.getWidth();
151             } while (x < w - arcsize);
152             BufferedImage JavaDoc rightEdge = img.getSubimage (imgw - arcsize, 0, arcsize, h);
153             g2d.drawRenderedImage(rightEdge, AffineTransform.getTranslateInstance(w - arcsize, 0));
154         } else if (w < imgw) {
155             //Narrower than the image - draw the width we need, then the end cap
156
BufferedImage JavaDoc left = img.getSubimage (0, 0, imgw - arcsize, h);
157             //Don't blit the full image, or we will get white "ears" on the right side
158
g2d.drawRenderedImage(left, nullTransform);
159             BufferedImage JavaDoc right = img.getSubimage (imgw - arcsize, 0, arcsize, h);
160             g2d.drawRenderedImage(right, AffineTransform.getTranslateInstance(w - arcsize, 0));
161         }
162     }
163     
164     private void paintInto (Graphics g, JComponent c) {
165         UIUtils.configureRenderingHints(g);
166         Color temp = g.getColor();
167         Dimension size = c.getSize();
168         
169         Shape s = aquaborder.getInteriorShape(size.width, size.height);
170         Shape clip = g.getClip();
171         if (clip != null) {
172             Area a = new Area(clip);
173             a.intersect(new Area(s));
174             g.setClip (a);
175         } else {
176             g.setClip(s);
177         }
178         
179         Graphics2D g2d = (Graphics2D) g;
180         //g.setColor (Color.ORANGE);
181

182         g2d.setPaint (aquaborder.getUpperPaint(size.width,size.height));
183         g2d.fill (aquaborder.getUpperBevelShape(size.width, size.height));
184         g2d.setPaint (aquaborder.getLowerPaint(size.width,size.height));
185         g2d.fill (aquaborder.getLowerBevelShape(size.width, size.height));
186         
187         
188         g.setClip (clip);
189         g.setColor(temp);
190     }
191     
192     
193     protected Border JavaDoc createRolloverBorder() {
194         return BorderFactory.createEmptyBorder(2,2,2,2);
195     }
196     
197     protected Border JavaDoc createNonRolloverBorder() {
198         return createRolloverBorder();
199     }
200     
201     private Border JavaDoc createNonRolloverToggleBorder() {
202         return createRolloverBorder();
203     }
204     
205     protected void setBorderToRollover(Component c) {
206         if (c instanceof AbstractButton) {
207             ((AbstractButton) c).setBorderPainted(false);
208             ((AbstractButton) c).setBorder(BorderFactory.createEmptyBorder());
209 // ((AbstractButton) c).setContentAreaFilled(false);
210
((AbstractButton) c).setOpaque(false);
211         }
212         if (c instanceof JComponent) {
213             ((JComponent) c).setOpaque(false);
214         }
215     }
216     
217     protected void setBorderToNormal(Component c) {
218         if (c instanceof AbstractButton) {
219             ((AbstractButton) c).setBorderPainted(false);
220 // ((AbstractButton) c).setContentAreaFilled(false);
221
((AbstractButton) c).setOpaque(false);
222         }
223         if (c instanceof JComponent) {
224             ((JComponent) c).setOpaque(false);
225         }
226     }
227     
228     public void setFloating(boolean b, Point p) {
229         //nobody wants this
230
}
231     
232     private void installButtonUI (Component c) {
233         if (c instanceof AbstractButton) {
234             ((AbstractButton) c).setUI(buttonui);
235         }
236         if (c instanceof JComponent) {
237             ((JComponent) c).setOpaque(false);
238         }
239     }
240     
241     private void installButtonUIs (Container parent) {
242         Component[] c = parent.getComponents();
243         for (int i=0; i < c.length; i++) {
244             installButtonUI(c[i]);
245         }
246     }
247     
248     private static final ButtonUI JavaDoc buttonui = new AquaToolBarButtonUI();
249     public void componentAdded(ContainerEvent JavaDoc e) {
250         installButtonUI (e.getChild());
251         Container c = (Container) e.getSource();
252         if ("editorToolbar".equals (c.getName())) {
253             //It's an editor toolbar. Aqua's combo box ui paints outside
254
//of its literal component bounds, and doesn't honor opacity.
255
//Need to ensure the toolbar is tall enough that its border is
256
//not hidden.
257
Dimension min = new Dimension (32, 34);
258             ((JComponent)e.getContainer()).setPreferredSize(min);
259         }
260     }
261     
262     public void componentRemoved(ContainerEvent JavaDoc e) {
263         //do nothing
264
}
265
266     private static final boolean isFinderLook (Component c) {
267         return Boolean.getBoolean ("apple.awt.brushMetalLook");
268     }
269     
270     static int arcsize = 13;
271     static class AquaTbBorder implements Border JavaDoc {
272
273         public Insets getBorderInsets(Component c) {
274             return new Insets (2,4,0,0);
275         }
276         
277         public boolean isBorderOpaque() {
278             return false;
279         }
280         
281         
282         public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
283             UIUtils.configureRenderingHints(g);
284             
285             boolean finderLook = isFinderLook (c);
286             Color col;
287             if (finderLook) {
288                 col = mezi(UIManager.getColor("controlShadow"),
289                     UIManager.getColor("control")); //NOI18N
290
} else {
291                 col = UIManager.getColor("controlShadow");
292             }
293             
294             g.setColor(col);
295             
296             int ytop = y;
297             
298             drawUpper (g, x, y, ytop, w, h);
299
300             g.setColor (mezi (col, UIManager.getColor("control"))); //NOI18N
301
if (finderLook) {
302                 drawUpper (g, x+1, y, ytop+1, w-2, h-1);
303             }
304             
305             if (finderLook) {
306                 col = mezi(UIManager.getColor("controlShadow"),
307                     UIManager.getColor("control")); //NOI18N
308
drawLower (g, x, y, w, h, col, finderLook);
309             } else {
310 // drawLower (g, x, y, w, h, Color.LIGHT_GRAY, finderLook);
311
drawLower (g, x, y-1, w, h, col, finderLook);
312                 g.setColor(new Color(200,200,200));
313                 g.drawLine (x+(arcsize/2)-3, y+h-1, x+w-(arcsize/2), y+h-1);
314             }
315         }
316         
317         private void drawLower (Graphics g, int x, int y, int w, int h, Color col, boolean finderLook) {
318
319             g.setColor(col);
320             g.drawLine(x, y+(arcsize/2), x, y+h-(arcsize / 2));
321             g.drawLine(x+w-1, y+(arcsize/2), x+w-1, y+h-(arcsize / 2));
322
323             if (!finderLook) {
324                 g.setColor(new Color(220,220,220));
325                 g.drawArc (x-1, y+1+h-arcsize, arcsize, arcsize, 180, 90);
326                 g.drawArc ((x+1)+w-(arcsize+1), y+1+h-(arcsize+1), arcsize, arcsize, 270, 90);
327                 g.setColor(col);
328             }
329             
330             g.drawArc (x, y+h-arcsize, arcsize, arcsize, 180, 90);
331             g.drawArc (x+w-(arcsize+1), y+h-(arcsize+1), arcsize, arcsize, 270, 90);
332
333             if (!finderLook) {
334                 
335                 g.setColor (new Color(80,80,80));
336             }
337             g.drawLine (x+(arcsize/2)-3, y+h-1, x+w-(arcsize/2), y+h-1);
338         }
339         
340         private void drawUpper (Graphics g, int x, int y, int ytop, int w, int h) {
341             g.drawArc (x, ytop, arcsize, arcsize, 90, 90);
342
343             g.drawArc (x+w-(arcsize+1), ytop, arcsize, arcsize, 90, -90);
344             
345             g.drawLine(x+(arcsize/2), ytop, x+w-(arcsize/2), ytop);
346         }
347         
348         Paint getUpperPaint (Color top, Color bottom, int w, int h) {
349             GradientPaint result =
350                 UIUtils.getGradientPaint (0, h/4, top, 0, (h/2) + (h/4),
351                 bottom, false);
352             return result;
353         }
354         
355         Paint getLowerPaint (Color top, Color bottom, int w, int h) {
356             GradientPaint result =
357                 UIUtils.getGradientPaint (0, h/2, top, 0, (h/2) + (h/4),
358                 bottom, false);
359             
360             return result;
361         }
362         
363         Paint getUpperPaint (int w, int h) {
364             return getUpperPaint (UPPER_GRADIENT_TOP, UPPER_GRADIENT_BOTTOM, w, h);
365         }
366
367         Paint getLowerPaint (int w, int h) {
368             return getLowerPaint (LOWER_GRADIENT_TOP,
369                 LOWER_GRADIENT_BOTTOM , w, h);
370         }
371         
372         
373         Shape getInteriorShape(int w, int h) {
374             RoundRectangle2D r2d = new RoundRectangle2D.Double(0, 0, w, h, arcsize, arcsize);
375             return r2d;
376         }
377         
378         Shape getUpperBevelShape(int w, int h) {
379             int[] xpoints = new int[] {
380                 0,
381                 0,
382                 h / 2,
383                 w - (h / 4),
384                 w,
385                 w,
386                 0
387             };
388             
389             int[] ypoints = new int[] {
390                 0,
391                 h - (h / 4),
392                 h / 2,
393                 h / 2,
394                 h / 4,
395                 0,
396                 0
397             };
398             Polygon p = new Polygon (xpoints, ypoints, ypoints.length);
399             return p;
400         }
401         
402         Shape getLowerBevelShape(int w, int h) {
403             int[] xpoints = new int[] {
404                 0,
405                 0,
406                 h / 4,
407                 w - (h / 4),
408                 w,
409                 w,
410                 0
411             };
412             
413             int[] ypoints = new int[] {
414                 h,
415                 h - (h / 4),
416                 h / 2,
417                 h / 2,
418                 h / 4,
419                 h,
420                 h
421                 
422             };
423             Polygon p = new Polygon (xpoints, ypoints, ypoints.length);
424             return p;
425         }
426     }
427     
428     private static Color mezi (Color c1, Color c2) {
429         return new Color((c1.getRed() + c2.getRed()) / 2,
430                         (c1.getGreen() + c2.getGreen()) / 2,
431                         (c1.getBlue() + c2.getBlue()) / 2);
432     }
433     
434 }
435
Popular Tags