KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > tabcontrol > SlidingButtonUI


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;
21
22 import java.awt.*;
23 import java.awt.geom.AffineTransform JavaDoc;
24 import javax.swing.AbstractButton JavaDoc;
25 import javax.swing.ButtonModel JavaDoc;
26 import javax.swing.JComponent JavaDoc;
27 import javax.swing.SwingUtilities JavaDoc;
28
29 import javax.swing.plaf.ComponentUI JavaDoc;
30 import javax.swing.plaf.basic.BasicHTML JavaDoc;
31 import javax.swing.plaf.basic.BasicToggleButtonUI JavaDoc;
32 import javax.swing.text.View JavaDoc;
33 import org.netbeans.swing.tabcontrol.SlideBarDataModel;
34 import org.netbeans.swing.tabcontrol.SlidingButton;
35
36 /** Button UI for SlidingButton component that can paint rotated text.
37  * To change appearance, provide a subclass of this class or subclass of ComponentUI
38  * and register it via UIDefaults in standard Swing way.
39  * Typically the methods of interest when
40  * subclassing are <code>paintBackground</code> and <code>paintIconAndText</code>.
41  * <p>
42  * Instances of this class should be stateless, taking all data from model,
43  * such that a single instance can manage any number of buttons.
44  *
45  * @see SlidingButton
46  *
47  * @author Tim Boudreau, Dafe Simonek
48  */

49 public class SlidingButtonUI extends BasicToggleButtonUI JavaDoc {
50
51     //TODO - just a temporary solution to have some default impl..
52
private static final BasicToggleButtonUI JavaDoc INSTANCE = new SlidingButtonUI();
53     
54     /** Private, no need for outer classes to instantiate */
55     protected SlidingButtonUI() {
56     }
57     
58     public static ComponentUI JavaDoc createUI(JComponent JavaDoc c) {
59         return INSTANCE;
60     }
61     
62     protected String JavaDoc getPropertyPrefix() {
63         //TODO -define own prefix?
64
return "ToggleButton.";
65     }
66
67     public void paint(Graphics g, JComponent JavaDoc c) {
68        AbstractButton JavaDoc b = (AbstractButton JavaDoc) c;
69         ButtonModel JavaDoc model = b.getModel();
70         Dimension size = b.getSize();
71         FontMetrics fm = g.getFontMetrics();
72         Insets i = c.getInsets();
73         Rectangle viewRect = new Rectangle(size);
74         Rectangle iconRect = new Rectangle();
75         Rectangle textRect = new Rectangle();
76         Font f = c.getFont();
77         g.setFont(f);
78         
79         Rectangle rotatedViewRect;
80         Rectangle rotatedIconRect = new Rectangle();
81         Rectangle rotatedTextRect = new Rectangle();
82         SlidingButton slide = (SlidingButton)b;
83         Graphics2D g2d = (Graphics2D)g;
84         int orientation = slide.getOrientation();
85         if (orientation == SlideBarDataModel.SOUTH) {
86             rotatedViewRect = new Rectangle(0, 0, viewRect.width, viewRect.height);
87         } else {
88             rotatedViewRect = new Rectangle(0, 0, viewRect.height, viewRect.width);
89         }
90         
91         // layout the text and icon
92
String JavaDoc text = SwingUtilities.layoutCompoundLabel(
93             c, fm, b.getText(), b.getIcon(),
94             b.getVerticalAlignment(), b.getHorizontalAlignment(),
95             b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
96             rotatedViewRect, rotatedIconRect, rotatedTextRect,
97         b.getText() == null ? 0 : b.getIconTextGap());
98         
99         if (orientation == SlideBarDataModel.SOUTH) {
100             iconRect = new Rectangle(viewRect.x + rotatedIconRect.x, viewRect.y + rotatedIconRect.y,
101                                      rotatedIconRect.width, rotatedIconRect.height);
102             textRect = new Rectangle(viewRect.x + rotatedTextRect.x, viewRect.y + rotatedTextRect.y,
103                                      rotatedTextRect.width, rotatedTextRect.height);
104         }
105         if (orientation == SlideBarDataModel.WEST) {
106             iconRect = new Rectangle(viewRect.x + rotatedIconRect.y,
107                                      viewRect.y + viewRect.height - rotatedIconRect.x - rotatedIconRect.width,
108                                      rotatedIconRect.height,
109                                      rotatedIconRect.width);
110             textRect = new Rectangle(viewRect.x + rotatedTextRect.y,
111                                      viewRect.y + viewRect.height - rotatedTextRect.y - rotatedTextRect.width,
112                                      rotatedTextRect.height,
113                                      rotatedTextRect.width);
114         }
115         if (orientation == SlideBarDataModel.EAST) {
116             iconRect = new Rectangle(viewRect.x + viewRect.width - rotatedIconRect.y - rotatedIconRect.height,
117                                      viewRect.y + rotatedIconRect.x,
118                                      rotatedIconRect.height,
119                                      rotatedIconRect.width);
120             textRect = new Rectangle(viewRect.x + viewRect.width - rotatedTextRect.y - rotatedTextRect.height,
121                                      viewRect.y + rotatedTextRect.x,
122                                      rotatedTextRect.height,
123                                      rotatedTextRect.width);
124         }
125
126         g.setColor(b.getBackground());
127
128         if (model.isArmed() && model.isPressed() || model.isSelected() || model.isRollover()) {
129             paintButtonPressed(g,b);
130     } else if (b.isOpaque()) {
131             paintBackground(g2d, b);
132     }
133     
134         
135         
136         // Paint the Icon
137
if(b.getIcon() != null) {
138             paintIcon(g, b, iconRect);
139         }
140     
141         // Draw the Text
142
if(text != null && !text.equals("")) {
143             
144             
145             AffineTransform JavaDoc saveTr = g2d.getTransform();
146             if (orientation != SlideBarDataModel.SOUTH) {
147                 if (orientation == SlideBarDataModel.WEST) {
148                     // rotate 90 degrees counterclockwise for WEST orientation
149
g2d.rotate( -Math.PI / 2 );
150                     g2d.translate(-c.getHeight(), 0 );
151                 } else {
152                     // rotate 90 degrees clockwise for EAST orientation
153
g2d.rotate( Math.PI / 2 );
154                     g2d.translate( 0, - c.getWidth() );
155                 }
156             }
157             
158             
159             View JavaDoc v = (View JavaDoc) c.getClientProperty(BasicHTML.propertyKey);
160             if (v != null) {
161                 v.paint(g, rotatedTextRect);
162             } else {
163                 paintText(g, b, rotatedTextRect, text);
164             }
165             
166             
167             // restore transformation
168
g2d.setTransform(saveTr);
169         }
170         
171         // draw the dashed focus line.
172
if (b.isFocusPainted() && b.hasFocus()) {
173         paintFocus(g, b, viewRect, textRect, iconRect);
174         }
175  
176     }
177     
178     protected void paintBackground(Graphics2D g, AbstractButton JavaDoc b) {
179         Dimension size = b.getSize();
180         
181         Insets insets = b.getInsets();
182         Insets margin = b.getMargin();
183         
184         g.fillRect(insets.left - margin.left,
185             insets.top - margin.top,
186         size.width - (insets.left-margin.left) - (insets.right - margin.right),
187         size.height - (insets.top-margin.top) - (insets.bottom - margin.bottom));
188     }
189
190     
191     
192     public Dimension getMinimumSize(JComponent JavaDoc c) {
193         return getPreferredSize(c);
194     }
195
196     public Dimension getPreferredSize(JComponent JavaDoc c) {
197         SlidingButton b = (SlidingButton) c;
198         Dimension prefSize = super.getPreferredSize(c);
199         int orientation = b.getOrientation();
200         
201         if (orientation == SlideBarDataModel.SOUTH) {
202             return prefSize;
203         }
204         else {
205             return new Dimension(prefSize.height, prefSize.width);
206         }
207     }
208
209     public Dimension getMaximumSize(JComponent JavaDoc c) {
210         return getPreferredSize(c);
211     }
212 }
213
Popular Tags