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 AlignArea extends Align {
048: public static final int INSIDE = 1;
049: public static final int OUTSIDE = 2;
050:
051: private int mode = INSIDE;
052: private int sx = 0;
053: private int sy = 0;
054: private Dimension size = new Dimension(0, 0);
055: private Insets insets = new Insets(0, 0, 0, 0);
056: private boolean isValid = false;
057: private Rectangle rect;
058:
059: public void setAlign(int a) {
060: if (getAlign() == a)
061: return;
062: super .setAlign(a);
063: invalidate();
064: }
065:
066: public void setSize(Dimension d) {
067: if (d != null && size != null) {
068: if (d.width == size.width && d.height == size.height)
069: return;
070: }
071:
072: invalidate();
073: if (d == null)
074: size = null;
075: else
076: size = new Dimension(d.width, d.height);
077: }
078:
079: public Dimension getSize() {
080: if (size == null)
081: return null;
082: return new Dimension(size.width, size.height);
083: }
084:
085: public void setSizeAlignObj(Dimension d) {
086: if (sx == d.width && sy == d.height)
087: return;
088: invalidate();
089: sx = d.width;
090: sy = d.height;
091: }
092:
093: public Dimension getSizeAlignObj() {
094: return new Dimension(sx, sy);
095: }
096:
097: public void setInsets(Insets i) {
098: if (i != null && i.top == insets.top && i.left == insets.left
099: && i.right == insets.right && i.bottom == insets.bottom)
100: return;
101:
102: invalidate();
103: if (i == null)
104: insets = null;
105: else
106: insets = new Insets(i.top, i.left, i.bottom, i.right);
107: }
108:
109: public Insets getInsets() {
110: return insets;
111: }
112:
113: public void setMode(int m) {
114: if (mode == m)
115: return;
116: invalidate();
117: mode = m;
118: }
119:
120: public int getMode() {
121: return mode;
122: }
123:
124: public Rectangle getAlignRectangle() {
125: if (isValid()) {
126: if (rect == null)
127: return null;
128: return new Rectangle(rect.x, rect.y, rect.width,
129: rect.height);
130: }
131:
132: recalc();
133: Dimension s = getSize();
134:
135: s.width -= (insets.left + insets.right);
136: s.height -= (insets.top + insets.bottom);
137:
138: int wx = getWidth(sx, size);
139: int wy = getHeight(sy, size);
140: int xx = size.width - wx;
141: int yy = size.height - wy;
142: int a = getAlign();
143: Rectangle r = new Rectangle(xx / 2, yy / 2, wx, wy);
144:
145: if ((a & LEFT) > 0)
146: r.x = insets.left;
147: else if ((a & RIGHT) > 0)
148: r.x = xx - insets.right;
149:
150: if ((a & TOP) > 0)
151: r.y = insets.top;
152: else if ((a & BOTTOM) > 0)
153: r.y = yy - insets.bottom;
154:
155: rect = r;
156: validate();
157: return r;
158: }
159:
160: public boolean isBelongArea(int x, int y) {
161: return isBelongArea(this , x, y);
162: }
163:
164: protected int getWidth(int s, Dimension size) {
165: return s;
166: }
167:
168: protected int getHeight(int s, Dimension size) {
169: return s;
170: }
171:
172: protected void validate() {
173: isValid = true;
174: }
175:
176: protected void recalc() {
177: }
178:
179: public void invalidate() {
180: isValid = false;
181: }
182:
183: public boolean isValid() {
184: return isValid;
185: }
186:
187: public static boolean isBelongArea(AlignArea a, int x, int y) {
188: Rectangle r = a.getAlignRectangle();
189: if (r == null)
190: return false;
191:
192: switch (a.getMode()) {
193: case AlignArea.INSIDE:
194: return r.inside(x, y);
195: case AlignArea.OUTSIDE:
196: return !r.inside(x, y);
197: }
198: return false;
199: }
200: }
|