KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > tabcontrol > plaf > SlidingTabDisplayerButtonUI


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 package org.netbeans.swing.tabcontrol.plaf;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.FontMetrics JavaDoc;
25 import java.awt.Graphics JavaDoc;
26 import java.awt.Graphics2D JavaDoc;
27 import java.awt.Insets JavaDoc;
28 import java.awt.geom.AffineTransform JavaDoc;
29 import javax.swing.AbstractButton JavaDoc;
30 import javax.swing.BorderFactory JavaDoc;
31 import javax.swing.Icon JavaDoc;
32 import javax.swing.JComponent JavaDoc;
33 import javax.swing.plaf.ComponentUI JavaDoc;
34 import javax.swing.plaf.basic.BasicToggleButtonUI JavaDoc;
35 import org.netbeans.swing.tabcontrol.TabDisplayer;
36 import org.openide.awt.HtmlRenderer;
37
38 /**
39  * Button UI that can paint rotated text, which is used by {@link BasicSlidingTabDisplayerUI}.
40  * Uses the lightweight HTML renderer for good performance when rendering HTML strings. It is
41  * intended as a UI for {@link BasicSlidingTabDisplayerUI.IndexButton}. Provide a subclass
42  * of this class via UIDefaults to change the painting behavior or appearance of the tab buttons in
43  * "sliding" style tabbed controls. Typically the only method of interest when
44  * subclassing is {@link #paintBackground}.
45  * <p>
46  * As with its superclass {@link BasicToggleButtonUI}, instances of this class should be stateless,
47  * such that a single instance can manage any number of buttons.
48  *
49  * @see BasicSlidingTabDisplayerUI
50  * @see org.netbeans.swing.tabcontrol.TabbedContainer#TYPE_SLIDING
51  * @author Tim Boudreau
52  */

53 public class SlidingTabDisplayerButtonUI extends BasicToggleButtonUI JavaDoc {
54     private static final SlidingTabDisplayerButtonUI INSTANCE = new SlidingTabDisplayerButtonUI();
55     /** Creates a new instance of SlidingTabDisplayerButtonUI */
56     private SlidingTabDisplayerButtonUI() {
57     }
58     
59     public static ComponentUI JavaDoc createUI(JComponent JavaDoc c) {
60         return INSTANCE;
61     }
62
63     /** Overridden to not install keyboard actions (the buttons aren't focusable
64      * anyway) and not invoke the overhead of BasicHTML */

65     public void installUI(JComponent JavaDoc c) {
66         installDefaults((AbstractButton JavaDoc) c);
67         installListeners((AbstractButton JavaDoc) c);
68         installBorder((AbstractButton JavaDoc) c);
69     }
70     
71     /** Install a border on the button */
72     protected void installBorder (AbstractButton JavaDoc b) {
73         b.setBorder (BorderFactory.createEtchedBorder());
74     }
75     
76     /** Overridden to not uninstall keyboard actions (the buttons aren't focusable
77      * anyway) and not invoke the overhead of BasicHTML */

78     public void uninstallUI(JComponent JavaDoc c) {
79         uninstallListeners((AbstractButton JavaDoc) c);
80         uninstallDefaults((AbstractButton JavaDoc) c);
81     }
82     
83     /** Overridden to not call super.installDefaults() and only set the button
84      * to be non-focusable */

85     public void installDefaults (AbstractButton JavaDoc b) {
86         b.setFocusable (false);
87     }
88     
89     public Dimension JavaDoc getMinimumSize(JComponent JavaDoc c) {
90         return getPreferredSize(c);
91     }
92
93     public Dimension JavaDoc getPreferredSize(JComponent JavaDoc c) {
94         return null; //Layout manager handles this anyway, nobody should ask for it
95
}
96
97     public Dimension JavaDoc getMaximumSize(JComponent JavaDoc c) {
98         return getPreferredSize(c);
99     }
100     
101     /** Provides the painting logic. Note that this does not call any of the
102      * painting methods of BasicToggleButtonUI */

103     public final void paint(Graphics JavaDoc g, JComponent JavaDoc c) {
104         
105         BasicSlidingTabDisplayerUI.IndexButton b =
106             (BasicSlidingTabDisplayerUI.IndexButton) c;
107         
108         Graphics2D JavaDoc g2d = (Graphics2D JavaDoc) g;
109         
110         paintBackground (g2d, b);
111         
112         Object JavaDoc orientation = b.getOrientation();
113         
114         AffineTransform JavaDoc tr = g2d.getTransform();
115         if (orientation == TabDisplayer.ORIENTATION_EAST) {
116              g2d.rotate( Math.PI / 2 );
117              g2d.translate( 0, - c.getWidth() );
118         } else if (orientation == TabDisplayer.ORIENTATION_WEST) {
119              g2d.rotate(-Math.PI / 2);
120              g2d.translate(-c.getHeight(), 0);
121         }
122
123         paintIconAndText (g2d, b, orientation);
124         g2d.setTransform (tr);
125     }
126     
127     /** Paints the tab background */
128     protected void paintBackground (Graphics2D JavaDoc g, BasicSlidingTabDisplayerUI.IndexButton b) {
129         Color JavaDoc c = b.isSelected() ? Color.ORANGE : b.getBackground();
130         g.setColor (c);
131         g.fillRect (0, 0, b.getWidth(), b.getHeight());
132     }
133     
134     /** Paints the icon and text using the HTML mini renderer */
135     protected final void paintIconAndText (Graphics2D JavaDoc g, BasicSlidingTabDisplayerUI.IndexButton b, Object JavaDoc orientation) {
136         FontMetrics JavaDoc fm = g.getFontMetrics(b.getFont());
137         Insets JavaDoc ins = b.getInsets();
138         
139         boolean flip = orientation == TabDisplayer.ORIENTATION_EAST ||
140             orientation == TabDisplayer.ORIENTATION_WEST;
141         
142         int txtX = flip ? ins.top : ins.left;
143         
144         int txtY = orientation == TabDisplayer.ORIENTATION_EAST ? ins.right :
145             orientation == TabDisplayer.ORIENTATION_WEST ? ins.left : ins.top;
146             
147         int txtW = flip ? b.getHeight() - (ins.top + ins.bottom):
148             b.getWidth() - (ins.left + ins.right);
149         
150         int iconX = txtX;
151         int iconY = txtY;
152         
153         int txtH = fm.getHeight();
154         txtY += fm.getMaxAscent();
155         
156         Icon JavaDoc icon = b.getIcon();
157         
158         int iconH = icon.getIconHeight();
159         int iconW = icon.getIconWidth();
160
161         int workingHeight;
162         if (flip) {
163             workingHeight = b.getWidth() - (ins.left + ins.right);
164         } else {
165             workingHeight = b.getHeight() - (ins.top + ins.bottom);
166         }
167         txtY += (workingHeight / 2) - (txtH / 2);
168         iconY += (workingHeight / 2) - (iconH / 2);
169         
170         if (icon != null && iconW > 0 && iconH > 0) {
171             txtX += iconW + b.getIconTextGap();
172             icon.paintIcon (b, g, iconX, iconY);
173             txtW -= iconH + b.getIconTextGap();
174         }
175         
176         HtmlRenderer.renderString(b.getText(), g, txtX, txtY, txtW, txtH, b.getFont(),
177               b.getForeground(), HtmlRenderer.STYLE_TRUNCATE, true);
178     }
179
180     private static SlidingTabDisplayerButtonUI AQUA_INSTANCE = null;
181     
182     /** Aqua ui for sliding buttons. This class is public so it can be
183      * instantiated by UIManager, but is of no interest as API. */

184     public static final class Aqua extends SlidingTabDisplayerButtonUI {
185         public static ComponentUI JavaDoc createUI(JComponent JavaDoc c) {
186             if (AQUA_INSTANCE == null) {
187                 AQUA_INSTANCE = new Aqua();
188             }
189             return AQUA_INSTANCE;
190         }
191
192         protected void installBorder (AbstractButton JavaDoc b) {
193             b.setBorder (BorderFactory.createEmptyBorder (5,2,2,2));
194         }
195
196         protected void paintBackground (Graphics2D JavaDoc g, BasicSlidingTabDisplayerUI.IndexButton b) {
197             GenericGlowingChiclet chic = GenericGlowingChiclet.INSTANCE;
198             int state = 0;
199             state |= b.isSelected() ? chic.STATE_SELECTED : 0;
200             state |= b.getModel().isPressed() ? chic.STATE_PRESSED : 0;
201             state |= b.isActive() ? chic.STATE_ACTIVE : 0;
202
203             chic.setState(state);
204             chic.setArcs(0.2f, 0.2f, 0.2f, 0.2f);
205             chic.setBounds (0, 1, b.getWidth(), b.getHeight());
206             chic.setAllowVertical(true);
207             chic.draw(g);
208             chic.setAllowVertical(false);
209         }
210     }
211 }
212
Popular Tags