KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mc4j > console > swing > ui > VerticalLabelUI


1 /*
2  * Copyright 2002-2004 Greg Hinkle
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.mc4j.console.swing.ui;
18
19 import java.awt.Dimension JavaDoc;
20 import java.awt.FontMetrics JavaDoc;
21 import java.awt.Graphics JavaDoc;
22 import java.awt.Graphics2D JavaDoc;
23 import java.awt.Insets JavaDoc;
24 import java.awt.Rectangle JavaDoc;
25 import java.awt.geom.AffineTransform JavaDoc;
26
27 import javax.swing.Icon JavaDoc;
28 import javax.swing.JComponent JavaDoc;
29 import javax.swing.JLabel JavaDoc;
30 import javax.swing.plaf.basic.BasicLabelUI JavaDoc;
31
32 /**
33  * <p>This label UI is used to render a JLabel in vertical format.</p>
34  *
35  *
36  * @author Greg Hinkle, February 2002
37  * @version $Revision: 480 $($Author: ghinkl $ / $Date: 2004-10-05 01:17:41 -0400 (Tue, 05 Oct 2004) $)
38  */

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