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: ShapedUtil.java,v 1.5 2005/02/16 11:28:13 jesper Exp $
023: package net.infonode.gui.shaped;
024:
025: import net.infonode.gui.InsetsUtil;
026: import net.infonode.gui.RectangleUtil;
027: import net.infonode.gui.shaped.panel.ShapedPanel;
028: import net.infonode.util.Direction;
029:
030: import java.awt.*;
031:
032: /**
033: * @author $Author: jesper $
034: * @version $Revision: 1.5 $
035: */
036: public class ShapedUtil {
037: private ShapedUtil() {
038: }
039:
040: public static Direction getDirection(Component c) {
041: return c instanceof ShapedPanel ? ((ShapedPanel) c)
042: .getDirection() : Direction.RIGHT;
043: }
044:
045: public static Insets transformInsets(Component c, Insets insets) {
046: return InsetsUtil
047: .rotate(getDirection(c), flipInsets(c, insets));
048: }
049:
050: public static Insets flipInsets(Component c, Insets i) {
051: if (c instanceof ShapedPanel) {
052: if (((ShapedPanel) c).isHorizontalFlip())
053: i = InsetsUtil.flipHorizontal(i);
054: if (((ShapedPanel) c).isVerticalFlip())
055: i = InsetsUtil.flipVertical(i);
056: }
057:
058: return i;
059: }
060:
061: public static void rotateCW(Polygon polygon, int height) {
062: for (int i = 0; i < polygon.npoints; i++) {
063: int tmp = polygon.ypoints[i];
064: polygon.ypoints[i] = polygon.xpoints[i];
065: polygon.xpoints[i] = height - 1 - tmp;
066: }
067: }
068:
069: public static void rotate(Polygon polygon, Direction d, int width,
070: int height) {
071: if (d == Direction.UP) {
072: rotateCW(polygon, height);
073: rotateCW(polygon, width);
074: rotateCW(polygon, height);
075: } else if (d == Direction.LEFT) {
076: rotateCW(polygon, height);
077: rotateCW(polygon, width);
078: } else if (d == Direction.DOWN) {
079: rotateCW(polygon, height);
080: }
081: }
082:
083: public static Rectangle transform(Component c, Rectangle rect) {
084: if (c instanceof ShapedPanel) {
085: ShapedPanel sp = (ShapedPanel) c;
086: return RectangleUtil.transform(rect, sp.getDirection(), sp
087: .isHorizontalFlip(), sp.isVerticalFlip(), c
088: .getWidth(), c.getHeight());
089: } else
090: return rect;
091: }
092:
093: public static Dimension transform(Component c, Dimension dim) {
094: return getDirection(c).isHorizontal() ? dim : new Dimension(
095: dim.height, dim.width);
096: }
097:
098: public static int getWidth(Component c, int width, int height) {
099: return getDirection(c).isHorizontal() ? width : height;
100: }
101:
102: public static int getHeight(Component c, int width, int height) {
103: return getDirection(c).isHorizontal() ? height : width;
104: }
105:
106: }
|