001: /**
002: * Caption: Zaval Java Resource Editor
003: * $Revision: 0.37 $
004: * $Date: 2002/03/28 9:24:42 $
005: *
006: * @author: Victor Krapivin
007: * @version: 1.3
008: *
009: * Zaval JRC Editor is a visual editor which allows you to manipulate
010: * localization strings for all Java based software with appropriate
011: * support embedded.
012: *
013: * For more info on this product read Zaval Java Resource Editor User's Guide
014: * (It comes within this package).
015: * The latest product version is always available from the product's homepage:
016: * http://www.zaval.org/products/jrc-editor/
017: * and from the SourceForge:
018: * http://sourceforge.net/projects/zaval0002/
019: *
020: * Contacts:
021: * Support : support@zaval.org
022: * Change Requests : change-request@zaval.org
023: * Feedback : feedback@zaval.org
024: * Other : info@zaval.org
025: *
026: * Copyright (C) 2001-2002 Zaval Creative Engineering Group (http://www.zaval.org)
027: *
028: * This program is free software; you can redistribute it and/or
029: * modify it under the terms of the GNU General Public License
030: * (version 2) as published by the Free Software Foundation.
031: *
032: * This program is distributed in the hope that it will be useful,
033: * but WITHOUT ANY WARRANTY; without even the implied warranty of
034: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
035: * GNU General Public License for more details.
036: *
037: * You should have received a copy of the GNU General Public License
038: * along with this program; if not, write to the Free Software
039: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
040: *
041: */package org.zaval.awt;
042:
043: import java.awt.Insets;
044: import java.awt.Dimension;
045: import java.awt.Rectangle;
046:
047: public class AlignHelper {
048: public static Insets align2insets(int a) {
049: Insets i = new Insets(-1, -1, -1, -1);
050: i.top = ((a & Align.TOP) > 0) ? 1 : 0;
051: i.left = ((a & Align.LEFT) > 0) ? 1 : 0;
052: i.bottom = ((a & Align.BOTTOM) > 0) ? 1 : 0;
053: i.right = ((a & Align.RIGHT) > 0) ? 1 : 0;
054: if (!check(i))
055: return null;
056: return i;
057: }
058:
059: public static int insets2align(Insets i) {
060: if (i == null || !check(i))
061: return -1;
062: int a = 0;
063: a |= ((i.top > 0) ? Align.TOP : 0);
064: a |= ((i.left > 0) ? Align.LEFT : 0);
065: a |= ((i.bottom > 0) ? Align.BOTTOM : 0);
066: a |= ((i.right > 0) ? Align.RIGHT : 0);
067: return a;
068: }
069:
070: public static String align2str(int a) {
071: String r = null;
072: switch (a) {
073: case Align.TOP:
074: r = Align.STR_TOP;
075: break;
076: case Align.BOTTOM:
077: r = Align.STR_BOTTOM;
078: break;
079: case Align.LEFT:
080: r = Align.STR_LEFT;
081: break;
082: case Align.RIGHT:
083: r = Align.STR_RIGHT;
084: break;
085: case Align.TLEFT:
086: r = Align.STR_TLEFT;
087: break;
088: case Align.TRIGHT:
089: r = Align.STR_TRIGHT;
090: break;
091: case Align.BRIGHT:
092: r = Align.STR_BRIGHT;
093: break;
094: case Align.BLEFT:
095: r = Align.STR_BLEFT;
096: break;
097: case Align.CENTER:
098: r = Align.STR_CENTER;
099: break;
100: }
101: return r;
102: }
103:
104: protected static boolean check(Insets i) {
105: if ((i.top > 0 && i.bottom > 0) || (i.left > 0 && i.right > 0))
106: return false;
107:
108: if (i.top < 0 || i.bottom < 0 || i.left < 0 || i.right < 0)
109: return false;
110: return true;
111: }
112:
113: public static boolean isBelongArea(AlignArea a, int x, int y) {
114: Rectangle r = a.getAlignRectangle();
115: if (r == null)
116: return false;
117:
118: switch (a.getMode()) {
119: case AlignArea.INSIDE:
120: return r.inside(x, y);
121: case AlignArea.OUTSIDE:
122: return !r.inside(x, y);
123: }
124: return false;
125: }
126:
127: public static Insets getPointAlignInsets(AlignArea a, int x, int y) {
128: if (!isBelongArea(a, x, y))
129: return null;
130:
131: if (a.getMode() == AlignArea.INSIDE)
132: return a.getAlignInsets();
133: Rectangle r = a.getAlignRectangle();
134: int maxx = r.x + r.width;
135: int maxy = r.y + r.height;
136: Insets code = new Insets(0, 0, 0, 0);
137:
138: if (x > maxx)
139: code.right++;
140: else {
141: if (x < r.x)
142: code.left++;
143: }
144:
145: if (y > maxy)
146: code.bottom++;
147: else {
148: if (y < r.y)
149: code.top++;
150: }
151:
152: return code;
153: }
154: }
|