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.*;
044: import java.util.*;
045:
046: public class Resizer extends Canvas {
047: private int startx;
048: private int oldrg;
049:
050: private boolean drag = false;
051: private boolean state = false;
052: private boolean _enable;
053:
054: public Resizer() {
055: super ();
056: startx = oldrg = 0;
057: _enable = isEnabled();
058: }
059:
060: public void enable() {
061: enable(true);
062: }
063:
064: public void disable() {
065: enable(false);
066: }
067:
068: public void enable(boolean e) {
069: if (_enable == e)
070: return;
071: super .enable(_enable = e);
072: if (!_enable) {
073: drag = false;
074: startx = oldrg = 0;
075: }
076: repaint();
077: }
078:
079: public void paint(Graphics gr) {
080: if (_enable) {
081: Rectangle r = bounds();
082: int x = 0, y = 0;
083: int w = r.width - 2 * x - 1, h = r.height - 2 * y - 1;
084: gr.setColor(Color.lightGray);
085: gr.fillRect(x, y, x + w, y + h);
086: gr.setColor(Color.white);
087: gr.drawLine(x, y, x + w, y);
088: gr.drawLine(x, y, x, y + h);
089: gr.setColor(Color.black);
090: gr.drawLine(x, y + h, x + w, y + h);
091: gr.drawLine(x + w, y, x + w, y + h);
092: }
093: }
094:
095: private void paintLine(Component c, int x) {
096: for (int i = 0; i < bounds().width; i++, x++)
097: drawVLineOnComponent(c, bounds().y, bounds().height
098: + bounds().y, x, Color.darkGray);
099: }
100:
101: private static void drawVLineOnComponent(Component c, int y1,
102: int y2, int x, Color col) {
103: if (c == null)
104: return;
105: Rectangle d = c.bounds();
106: if (d.height <= y2)
107: y2 = d.height - 1;
108:
109: Component lc = getNextBottomChild(c, y1 + 1, x);
110: int yy1, yy2;
111: while (lc != null && y1 < y2) {
112: Rectangle lr = lc.bounds();
113: yy1 = y1 - lr.y;
114: if (yy1 < 0)
115: yy1 = 0;
116:
117: yy2 = y2 - lr.y;
118: if (yy2 >= lr.height)
119: yy2 = lr.height - 1;
120:
121: int xx = x - lr.x;
122: if (yy2 <= yy1)
123: break;
124:
125: if (lr.y > y1) {
126: Graphics g = c.getGraphics();
127: Color clr = g.getColor();
128: g.setColor(col);
129: g.setXORMode(c.getBackground());
130: g.drawLine(x, y1, x, lr.y);
131: g.setColor(clr);
132: g.setPaintMode();
133: g.dispose();
134: }
135:
136: drawVLineOnComponent(lc, yy1, yy2, xx, col);
137: y1 = (lr.y + lr.height);
138: lc = getNextBottomChild(c, y1, x);
139: }
140:
141: if (y1 < y2) {
142: Graphics g = c.getGraphics();
143: Color clr = g.getColor();
144: g.setColor(col);
145: g.setXORMode(c.getBackground());
146: g.drawLine(x, y1, x, y2);
147: g.setColor(clr);
148: g.setPaintMode();
149: g.dispose();
150: }
151: }
152:
153: private static Component getNextBottomChild(Component parent,
154: int y, int x) {
155: if (!(parent instanceof Container))
156: return null;
157:
158: Component c = getComponentAtFix((Container) parent, x, y); //parent.getComponentAt(x, y);
159: if (c != null && c != parent)
160: return c;
161:
162: Component[] comps = ((Container) parent).getComponents();
163: Component find = null;
164: int fy = Integer.MAX_VALUE;
165: for (int i = 0; i < comps.length; i++) {
166: Rectangle r = comps[i].bounds();
167: if (x < r.x || x > (r.x + r.width))
168: continue;
169:
170: if (r.y < fy && r.y > y) {
171: fy = r.y;
172: find = comps[i];
173: }
174: }
175: return find;
176: }
177:
178: private static Component getComponentAtFix(Container top, int x,
179: int y) {
180: Component[] c = top.getComponents();
181: Vector v = new Vector();
182: for (int i = 0; i < c.length; i++) {
183: if (!c[i].isVisible())
184: continue;
185: Rectangle b = c[i].bounds();
186: if (b.inside(x, y))
187: v.addElement(c[i]);
188: }
189:
190: if (v.size() > 0) {
191: return (Component) v.elementAt(0);
192: }
193: return null;
194: }
195:
196: private void setCursor0(int c) {
197: Component f = this ;
198: while ((f != null) && !(f instanceof Frame))
199: f = f.getParent();
200: if (f instanceof Frame) {
201: ((Frame) f).setCursor(c);
202: }
203: }
204:
205: private void resizeme(int x) {
206: Rectangle r = bounds();
207: ResizeLayout rl = (ResizeLayout) getParent().getLayout();
208: Rectangle rp = getParent().bounds();
209:
210: int pos = r.x + x - startx - r.width;
211: int left = 0;
212: int right = rp.width - 2 * r.width + rp.x;
213:
214: if (pos > right)
215: pos = right;
216: if (pos < left)
217: pos = left;
218:
219: rl.setSeparator(pos, getParent());
220:
221: oldrg = startx = 0;
222: }
223:
224: public boolean mouseEnter(Event ev, int x, int y) {
225: if (_enable) {
226: setCursor0(Frame.E_RESIZE_CURSOR);
227: return true;
228: }
229: return super .mouseEnter(ev, x, y);
230: }
231:
232: public boolean mouseExit(Event ev, int x, int y) {
233: if (_enable) {
234: if (drag) {
235: paintLine(getParent(), oldrg + bounds().x - startx);
236: drag = false;
237: }
238: setCursor0(Frame.DEFAULT_CURSOR);
239: return true;
240: }
241: return super .mouseExit(ev, x, y);
242: }
243:
244: public boolean mouseDown(Event ev, int x, int y) {
245: if (_enable && inside(x, y) && ev.modifiers == 0) {
246: if (drag)
247: return super .mouseDown(ev, x, y);
248: startx = x;
249: oldrg = x;
250: state = drag = true;
251: paintLine(getParent(), oldrg + bounds().x - startx);
252: setCursor0(Frame.E_RESIZE_CURSOR);
253: return true;
254: }
255: return super .mouseDown(ev, x, y);
256: }
257:
258: public boolean mouseUp(Event ev, int x, int y) {
259: if (_enable) {
260: if (state) {
261: paintLine(getParent(), oldrg + bounds().x - startx);
262: resizeme(x);
263: state = drag = false;
264: }
265: return true;
266: }
267: return super .mouseUp(ev, x, y);
268: }
269:
270: public boolean mouseDrag(Event ev, int x, int y) {
271: if (_enable) {
272: if (state) {
273: if (oldrg == x)
274: return super .mouseDrag(ev, x, y);
275: if (drag)
276: paintLine(getParent(), oldrg + bounds().x - startx);
277: else
278: drag = true;
279: paintLine(getParent(), x + bounds().x - startx);
280: oldrg = x;
281: }
282: return true;
283: }
284: return super .mouseDrag(ev, x, y);
285: }
286:
287: public static void debug(String s) {
288: System.out.println(s);
289: }
290: }
|