001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: RotatableLabelUI.java,v 1.11 2005/12/04 13:46:04 jesper Exp $
023: package net.infonode.gui;
024:
025: import net.infonode.util.Direction;
026:
027: import javax.swing.*;
028: import javax.swing.plaf.basic.BasicLabelUI;
029: import java.awt.*;
030: import java.awt.geom.AffineTransform;
031:
032: public class RotatableLabelUI extends BasicLabelUI {
033: // Optimization
034: private static Rectangle paintIconR = new Rectangle();
035: private static Rectangle paintTextR = new Rectangle();
036: private static Rectangle paintViewR = new Rectangle();
037:
038: private Direction direction;
039: private boolean mirror;
040:
041: public RotatableLabelUI(Direction direction) {
042: this (direction, false);
043: }
044:
045: public RotatableLabelUI(Direction direction, boolean mirror) {
046: this .direction = direction;
047: this .mirror = mirror;
048: }
049:
050: public Direction getDirection() {
051: return direction;
052: }
053:
054: public void setDirection(Direction direction) {
055: this .direction = direction;
056: }
057:
058: public boolean isMirror() {
059: return mirror;
060: }
061:
062: public void setMirror(boolean mirror) {
063: this .mirror = mirror;
064: }
065:
066: public void paint(Graphics g, JComponent c) {
067: JLabel label = (JLabel) c;
068: String text = label.getText();
069: Icon icon = (label.isEnabled()) ? label.getIcon() : label
070: .getDisabledIcon();
071:
072: if (icon == null && text == null)
073: return;
074:
075: FontMetrics fm = g.getFontMetrics();
076: Insets insets = c.getInsets();
077:
078: paintViewR.x = insets.left;
079: paintViewR.y = insets.top;
080:
081: if (direction.isHorizontal()) {
082: paintViewR.height = c.getHeight()
083: - (insets.top + insets.bottom);
084: paintViewR.width = c.getWidth()
085: - (insets.left + insets.right);
086: } else {
087: paintViewR.height = c.getWidth()
088: - (insets.top + insets.bottom);
089: paintViewR.width = c.getHeight()
090: - (insets.left + insets.right);
091: }
092:
093: paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
094: paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;
095:
096: String clippedText = layoutCL(label, fm, text, icon,
097: paintViewR, paintIconR, paintTextR);
098:
099: Graphics2D g2 = (Graphics2D) g;
100: AffineTransform tr = g2.getTransform();
101:
102: int m = mirror ? -1 : 1;
103: g2
104: .transform(direction == Direction.RIGHT ? new AffineTransform(
105: 1, 0, 0, m, 0, mirror ? c.getHeight() : 0)
106: : direction == Direction.DOWN ? new AffineTransform(
107: 0, 1, -m, 0, mirror ? 0 : c.getWidth(),
108: 0)
109: : direction == Direction.LEFT ? new AffineTransform(
110: -1, 0, 0, -m, c.getWidth(),
111: mirror ? 0 : c.getHeight())
112: : new AffineTransform(0, -1, m,
113: 0,
114: mirror ? c.getWidth()
115: : 0, c
116: .getHeight()));
117:
118: if (icon != null) {
119: icon.paintIcon(c, g, paintIconR.x, paintIconR.y);
120: }
121:
122: if (text != null) {
123: int textX = paintTextR.x;
124: int textY = paintTextR.y + fm.getAscent();
125:
126: if (label.isEnabled()) {
127: paintEnabledText(label, g, clippedText, textX, textY);
128: } else {
129: paintDisabledText(label, g, clippedText, textX, textY);
130: }
131: }
132:
133: g2.setTransform(tr);
134: }
135: }
|