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: InsetsUtil.java,v 1.14 2005/12/04 13:46:04 jesper Exp $
023: package net.infonode.gui;
024:
025: import net.infonode.util.Direction;
026:
027: import java.awt.*;
028:
029: public class InsetsUtil {
030: public static final Insets EMPTY_INSETS = new Insets(0, 0, 0, 0);
031:
032: private InsetsUtil() {
033: }
034:
035: public static Insets getDiff(Insets source, Insets other) {
036: int top = other.top - source.top;
037: int left = other.left - source.left;
038: int bottom = other.bottom - source.bottom;
039: int right = other.right - source.right;
040: return new Insets(top > 0 ? top : 0, left > 0 ? left : 0,
041: bottom > 0 ? bottom : 0, right > 0 ? right : 0);
042: }
043:
044: public static Insets sub(Insets i1, Insets i2) {
045: return new Insets(i1.top - i2.top, i1.left - i2.left, i1.bottom
046: - i2.bottom, i1.right - i2.right);
047: }
048:
049: public static Insets add(Insets i, Insets i2) {
050: return new Insets(i.top + i2.top, i.left + i2.left, i.bottom
051: + i2.bottom, i.right + i2.right);
052: }
053:
054: public static final Insets flip(Insets insets,
055: boolean horizontalFlip, boolean verticalFlip) {
056: return horizontalFlip ? (verticalFlip ? new Insets(
057: insets.bottom, insets.right, insets.top, insets.left)
058: : new Insets(insets.top, insets.right, insets.bottom,
059: insets.left)) : (verticalFlip ? new Insets(
060: insets.bottom, insets.left, insets.top, insets.right)
061: : insets);
062: }
063:
064: public static final Insets rotate(Direction d, Insets insets) {
065: if (d == Direction.LEFT)
066: return new Insets(insets.bottom, insets.right, insets.top,
067: insets.left);
068: else if (d == Direction.DOWN)
069: return new Insets(insets.left, insets.bottom, insets.right,
070: insets.top);
071: else if (d == Direction.UP)
072: return new Insets(insets.right, insets.top, insets.left,
073: insets.bottom);
074: return (Insets) insets.clone();
075: }
076:
077: public static Insets max(Insets insets1, Insets insets2) {
078: return new Insets(Math.max(insets1.top, insets2.top), Math.max(
079: insets1.left, insets2.left), Math.max(insets1.bottom,
080: insets2.bottom), Math.max(insets1.right, insets2.right));
081: }
082:
083: public static int maxInset(Insets i) {
084: return Math.max(i.top, Math.max(i.bottom, Math.max(i.left,
085: i.right)));
086: }
087:
088: public static int getInset(Insets insets, Direction direction) {
089: return direction == Direction.UP ? insets.top
090: : direction == Direction.LEFT ? insets.left
091: : direction == Direction.DOWN ? insets.bottom
092: : insets.right;
093: }
094:
095: public static Insets setInset(Insets insets, Direction direction,
096: int value) {
097: return direction == Direction.UP ? new Insets(value,
098: insets.left, insets.bottom, insets.right)
099: : direction == Direction.LEFT ? new Insets(insets.top,
100: value, insets.bottom, insets.right)
101: : direction == Direction.DOWN ? new Insets(
102: insets.top, insets.left, value,
103: insets.right) : new Insets(insets.top,
104: insets.left, insets.bottom, value);
105: }
106:
107: public static Insets copy(Insets insets) {
108: return insets == null ? null : new Insets(insets.top,
109: insets.left, insets.bottom, insets.right);
110: }
111:
112: public static void addTo(Insets insets, Insets addition) {
113: insets.top += addition.top;
114: insets.bottom += addition.bottom;
115: insets.left += addition.left;
116: insets.right += addition.right;
117: }
118:
119: public static Insets flipHorizontal(Insets insets) {
120: return new Insets(insets.top, insets.right, insets.bottom,
121: insets.left);
122: }
123:
124: public static Insets flipVertical(Insets insets) {
125: return new Insets(insets.bottom, insets.left, insets.top,
126: insets.right);
127: }
128: }
|