001: /*
002: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
007: *
008: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
009: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
010: * the license for the specific language governing your rights and limitations.
011: *
012: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
013: */
014: package org.pentaho.reportdesigner.lib.client.components.docking;
015:
016: import org.jetbrains.annotations.NotNull;
017:
018: import javax.swing.*;
019: import java.awt.*;
020: import java.awt.font.FontRenderContext;
021: import java.awt.font.GlyphVector;
022: import java.awt.font.LineMetrics;
023: import java.awt.geom.AffineTransform;
024:
025: /**
026: * User: Martin
027: * Date: 01.04.2005
028: * Time: 09:52:21
029: */
030: public class RotateTextIcon implements Icon {
031: public static final int NONE = 0;
032: public static final int CW = 1;
033: public static final int CCW = 2;
034:
035: private int rotate;
036: @NotNull
037: private Font font;
038: @NotNull
039: private GlyphVector glyphs;
040: private float width;
041: private float height;
042: private float ascent;
043:
044: public RotateTextIcon(int rotate, @NotNull
045: Font font, @NotNull
046: String text) {
047: this .rotate = rotate;
048: this .font = font;
049:
050: FontRenderContext fontRenderContext = new FontRenderContext(
051: null, false, false);
052: glyphs = font.createGlyphVector(fontRenderContext, text);
053: width = (int) glyphs.getLogicalBounds().getWidth() + 4;
054: //height = (int)glyphs.getLogicalBounds().getHeight();
055:
056: LineMetrics lineMetrics = font.getLineMetrics(text,
057: fontRenderContext);
058: ascent = lineMetrics.getAscent();
059: height = (int) lineMetrics.getHeight();
060: }
061:
062: public int getIconWidth() {
063: return (int) (rotate == RotateTextIcon.CW
064: || rotate == RotateTextIcon.CCW ? height : width);
065: }
066:
067: public int getIconHeight() {
068: return (int) (rotate == RotateTextIcon.CW
069: || rotate == RotateTextIcon.CCW ? width : height);
070: }
071:
072: public void paintIcon(@NotNull
073: Component c, @NotNull
074: Graphics g, int x, int y) {
075: Graphics2D g2d = (Graphics2D) g;
076: g2d.setFont(font);
077: AffineTransform oldTransform = g2d.getTransform();
078: RenderingHints oldHints = g2d.getRenderingHints();
079:
080: g2d.setColor(c.getForeground());
081:
082: if (rotate == RotateTextIcon.NONE) {
083: g2d.drawGlyphVector(glyphs, x + 2, y + ascent);
084: } else if (rotate == RotateTextIcon.CW) {
085: AffineTransform trans = new AffineTransform();
086: trans.concatenate(oldTransform);
087: trans.translate(x, y + 2);
088: trans.rotate(Math.PI / 2, height / 2, width / 2);
089: g2d.setTransform(trans);
090: g2d.drawGlyphVector(glyphs, (height - width) / 2,
091: (width - height) / 2 + ascent);
092: } else if (rotate == RotateTextIcon.CCW) {
093: AffineTransform trans = new AffineTransform();
094: trans.concatenate(oldTransform);
095: trans.translate(x, y - 2);
096: trans.rotate(Math.PI * 3 / 2, height / 2, width / 2);
097: g2d.setTransform(trans);
098: g2d.drawGlyphVector(glyphs, (height - width) / 2,
099: (width - height) / 2 + ascent);
100: }
101:
102: g2d.setTransform(oldTransform);
103: g2d.setRenderingHints(oldHints);
104: }
105:
106: }
|