KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > swing > ui > VerticalLabelUI


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.services.swing.ui;
19
20 import java.awt.Dimension JavaDoc;
21 import java.awt.FontMetrics JavaDoc;
22 import java.awt.Graphics JavaDoc;
23 import java.awt.Graphics2D JavaDoc;
24 import java.awt.Insets JavaDoc;
25 import java.awt.Rectangle JavaDoc;
26 import java.awt.geom.AffineTransform JavaDoc;
27
28 import javax.swing.Icon JavaDoc;
29 import javax.swing.JComponent JavaDoc;
30 import javax.swing.JLabel JavaDoc;
31 import javax.swing.plaf.basic.BasicLabelUI JavaDoc;
32
33 /**
34  * <p>This is the template for Classes.</p>
35  *
36  *
37  * @since carbon 1.0
38  * @author Greg Hinkle, January 2002
39  * @version $Revision: 1.4 $($Author: dvoet $ / $Date: 2003/05/05 21:21:27 $)
40  * @copyright 2002 Sapient
41  */

42 public class VerticalLabelUI extends BasicLabelUI JavaDoc {
43     static {
44         labelUI = new VerticalLabelUI(false);
45     }
46
47     protected boolean clockwise;
48
49
50     public VerticalLabelUI(boolean clockwise) {
51         super();
52         this.clockwise = clockwise;
53     }
54
55
56     public Dimension JavaDoc getPreferredSize(JComponent JavaDoc c) {
57         Dimension JavaDoc dim = super.getPreferredSize(c);
58         return new Dimension JavaDoc( dim.height, dim.width );
59     }
60
61     private static Rectangle JavaDoc paintIconR = new Rectangle JavaDoc();
62     private static Rectangle JavaDoc paintTextR = new Rectangle JavaDoc();
63     private static Rectangle JavaDoc paintViewR = new Rectangle JavaDoc();
64     private static Insets JavaDoc paintViewInsets = new Insets JavaDoc(0, 0, 0, 0);
65
66     public void paint(Graphics JavaDoc g, JComponent JavaDoc c) {
67
68         JLabel JavaDoc label = (JLabel JavaDoc)c;
69         String JavaDoc text = label.getText();
70         Icon JavaDoc icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon();
71
72         if ((icon == null) && (text == null)) {
73             return;
74         }
75
76         FontMetrics JavaDoc fm = g.getFontMetrics();
77         paintViewInsets = c.getInsets(paintViewInsets);
78
79         paintViewR.x = paintViewInsets.left;
80         paintViewR.y = paintViewInsets.top;
81
82         // Use inverted height & width
83
paintViewR.height = c.getWidth() - (paintViewInsets.left + paintViewInsets.right);
84         paintViewR.width = c.getHeight() - (paintViewInsets.top + paintViewInsets.bottom);
85
86         paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
87         paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;
88
89         String JavaDoc clippedText =
90             layoutCL(label, fm, text, icon, paintViewR, paintIconR, paintTextR);
91
92         Graphics2D JavaDoc g2 = (Graphics2D JavaDoc) g;
93         AffineTransform JavaDoc tr = g2.getTransform();
94         if (clockwise) {
95             g2.rotate( Math.PI / 2 );
96             g2.translate( 0, - c.getWidth() );
97         } else {
98             g2.rotate( - Math.PI / 2 );
99             g2.translate( - c.getHeight(), 0 );
100         }
101
102         if (icon != null) {
103             icon.paintIcon(c, g, paintIconR.x, paintIconR.y);
104         }
105
106         if (text != null) {
107             int textX = paintTextR.x;
108             int textY = paintTextR.y + fm.getAscent();
109
110             if (label.isEnabled()) {
111                 paintEnabledText(label, g, clippedText, textX, textY);
112             } else {
113                 paintDisabledText(label, g, clippedText, textX, textY);
114             }
115         }
116
117         g2.setTransform( tr );
118     }
119 }
Popular Tags