001: /* ====================================================================
002: * The JRefactory License, Version 1.0
003: *
004: * Copyright (c) 2001 JRefactory. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by the
021: * JRefactory (http://www.sourceforge.org/projects/jrefactory)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. The names "JRefactory" must not be used to endorse or promote
026: * products derived from this software without prior written
027: * permission. For written permission, please contact seguin@acm.org.
028: *
029: * 5. Products derived from this software may not be called "JRefactory",
030: * nor may "JRefactory" appear in their name, without prior written
031: * permission of Chris Seguin.
032: *
033: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
034: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
035: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
036: * DISCLAIMED. IN NO EVENT SHALL THE CHRIS SEGUIN OR
037: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
038: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
039: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
040: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
041: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
042: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
043: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
044: * SUCH DAMAGE.
045: * ====================================================================
046: *
047: * This software consists of voluntary contributions made by many
048: * individuals on behalf of JRefactory. For more information on
049: * JRefactory, please see
050: * <http://www.sourceforge.org/projects/jrefactory>.
051: */
052: package org.acm.seguin.uml.line;
053:
054: import java.awt.Dimension;
055: import java.awt.LayoutManager;
056: import java.awt.Point;
057: import java.awt.Rectangle;
058: import javax.swing.JPanel;
059:
060: /**
061: * Panel that can be scaled and shifted
062: *
063: *@author Chris Seguin
064: *@author <a href="JRefactory@ladyshot.demon.co.uk">Mike Atkinson</a>
065: *@version $Id: ScalablePanel.java,v 1.4 2003/07/29 20:51:57 mikeatkinson Exp $
066: *@created September 12, 2001
067: */
068: public abstract class ScalablePanel extends JPanel {
069: private boolean inShapeChange;
070: private boolean inScaling = false;
071:
072: private double scale = 1.0;
073:
074: private int absoluteWidth = -1;
075: private int absoluteHeight = -1;
076: private int absoluteX = -1;
077: private int absoluteY = -1;
078:
079: /**
080: * Constructor for the ScalablePanel object
081: */
082: public ScalablePanel() {
083: init();
084: }
085:
086: /**
087: * Constructor for the ScalablePanel object
088: *
089: *@param doubleBuffered Description of Parameter
090: */
091: public ScalablePanel(boolean doubleBuffered) {
092: super (doubleBuffered);
093: init();
094: }
095:
096: /**
097: * Constructor for the ScalablePanel object
098: *
099: *@param layout Description of Parameter
100: */
101: public ScalablePanel(LayoutManager layout) {
102: super (layout);
103: init();
104: }
105:
106: /**
107: * Constructor for the ScalablePanel object
108: *
109: *@param layout Description of Parameter
110: *@param doubleBuffered Description of Parameter
111: */
112: public ScalablePanel(LayoutManager layout, boolean doubleBuffered) {
113: super (layout, doubleBuffered);
114: init();
115: }
116:
117: /**
118: * Sets the Location attribute of the ScalablePanel object
119: *
120: *@param x The new Location value
121: *@param y The new Location value
122: */
123: public void setLocation(int x, int y) {
124: if (inShapeChange) {
125: super .setLocation(x, y);
126: } else {
127: inShapeChange = true;
128: absoluteX = x;
129: absoluteY = y;
130: super .setLocation(scaleInteger(x), scaleInteger(y));
131: inShapeChange = false;
132: }
133: }
134:
135: /**
136: * Sets the Location attribute of the ScalablePanel object
137: *
138: *@param pt The new Location value
139: */
140: public void setLocation(Point pt) {
141: if (inShapeChange) {
142: super .setLocation(pt);
143: } else {
144: inShapeChange = true;
145: absoluteX = pt.x;
146: absoluteY = pt.y;
147: super .setLocation(scaleInteger(pt.x), scaleInteger(pt.y));
148: inShapeChange = false;
149: }
150: }
151:
152: /**
153: * Sets the Size attribute of the ScalablePanel object
154: *
155: *@param w The new Size value
156: *@param h The new Size value
157: */
158: public void setSize(int w, int h) {
159: if (inShapeChange) {
160: super .setSize(w, h);
161: } else {
162: inShapeChange = true;
163: absoluteWidth = w;
164: absoluteHeight = h;
165: super .setSize(scaleInteger(w), scaleInteger(h));
166: inShapeChange = false;
167: }
168: }
169:
170: /**
171: * Sets the Size attribute of the ScalablePanel object
172: *
173: *@param dim The new Size value
174: */
175: public void setSize(Dimension dim) {
176: if (inShapeChange) {
177: super .setSize(dim);
178: } else {
179: inShapeChange = true;
180: absoluteWidth = dim.width;
181: absoluteHeight = dim.height;
182: super .setSize(scaleInteger(dim.width),
183: scaleInteger(dim.height));
184: inShapeChange = false;
185: }
186: }
187:
188: /**
189: * Sets the Bounds attribute of the ScalablePanel object
190: *
191: *@param x The new Bounds value
192: *@param y The new Bounds value
193: *@param w The new Bounds value
194: *@param h The new Bounds value
195: */
196: public void setBounds(int x, int y, int w, int h) {
197: if (inShapeChange) {
198: super .setBounds(x, y, w, h);
199: } else {
200: inShapeChange = true;
201: absoluteX = x;
202: absoluteY = y;
203: absoluteWidth = w;
204: absoluteHeight = h;
205: super .setBounds(scaleInteger(x), scaleInteger(y),
206: scaleInteger(h), scaleInteger(h));
207: inShapeChange = false;
208: }
209: }
210:
211: /**
212: * Sets the Bounds attribute of the ScalablePanel object
213: *
214: *@param rect The new Bounds value
215: */
216: public void setBounds(Rectangle rect) {
217: if (inShapeChange) {
218: super .setBounds(rect);
219: } else {
220: inShapeChange = true;
221: absoluteX = rect.x;
222: absoluteY = rect.y;
223: absoluteWidth = rect.width;
224: absoluteHeight = rect.height;
225: super
226: .setBounds(scaleInteger(rect.x),
227: scaleInteger(rect.y),
228: scaleInteger(rect.width),
229: scaleInteger(rect.height));
230: inShapeChange = false;
231: }
232: }
233:
234: /**
235: * This method moves the class diagram around on the screen
236: *
237: *@param x the x coordinate (scaled value)
238: *@param y the y coordinate (scaled value)
239: */
240: public void shift(int x, int y) {
241: Point pt = getLocation();
242: inShapeChange = true;
243: absoluteX = Math.max(0, unscaleInteger(x + pt.x));
244: absoluteY = Math.max(0, unscaleInteger(y + pt.y));
245: setLocation(Math.max(0, x + pt.x), Math.max(0, y + pt.y));
246: inShapeChange = false;
247: }
248:
249: /**
250: * Scales the image
251: *
252: *@param value the amount to scale
253: */
254: public void scale(double value) {
255: if (Math.abs(scale - value) > 0.001) {
256: Rectangle rect = getUnscaledBounds();
257:
258: scale = value;
259:
260: inScaling = true;
261: setBounds(rect);
262: inScaling = false;
263: }
264: }
265:
266: /**
267: * Invokes old version of setLocation
268: *
269: *@param x Description of Parameter
270: *@param y Description of Parameter
271: */
272: public void move(int x, int y) {
273: if (inShapeChange) {
274: super .move(x, y);
275: } else {
276: inShapeChange = true;
277: absoluteX = x;
278: absoluteY = y;
279: super .move(scaleInteger(x), scaleInteger(y));
280: inShapeChange = false;
281: }
282: }
283:
284: /**
285: * Description of the Method
286: *
287: *@param w Description of Parameter
288: *@param h Description of Parameter
289: */
290: public void resize(int w, int h) {
291: if (inShapeChange) {
292: super .resize(w, h);
293: } else {
294: inShapeChange = true;
295: absoluteWidth = w;
296: absoluteHeight = h;
297: super .resize(scaleInteger(w), scaleInteger(h));
298: inShapeChange = false;
299: }
300: }
301:
302: /**
303: * Description of the Method
304: *
305: *@param dim Description of Parameter
306: */
307: public void resize(Dimension dim) {
308: if (inShapeChange) {
309: super .resize(dim);
310: } else {
311: inShapeChange = true;
312: absoluteWidth = dim.width;
313: absoluteHeight = dim.height;
314: super .resize(scaleInteger(dim.width),
315: scaleInteger(dim.height));
316: inShapeChange = false;
317: }
318: }
319:
320: /**
321: * Description of the Method
322: *
323: *@param x Description of Parameter
324: *@param y Description of Parameter
325: *@param w Description of Parameter
326: *@param h Description of Parameter
327: */
328: public void reshape(int x, int y, int w, int h) {
329: if (inShapeChange) {
330: super .reshape(x, y, w, h);
331: } else {
332: inShapeChange = true;
333: absoluteX = x;
334: absoluteY = y;
335: absoluteWidth = w;
336: absoluteHeight = h;
337: super .reshape(scaleInteger(x), scaleInteger(y),
338: scaleInteger(h), scaleInteger(h));
339: inShapeChange = false;
340: }
341: }
342:
343: /**
344: * Return the scaling factor
345: *
346: *@return the scaling factor
347: */
348: protected double getScale() {
349: return scale;
350: }
351:
352: /**
353: * Scale the integer
354: *
355: *@param value the value to be converted
356: *@return the scaled version
357: */
358: protected int scaleInteger(int value) {
359: return (int) Math.round(1.0 + scale * value);
360: }
361:
362: /**
363: * Get the bounds without scaling factors
364: *
365: *@return the rectangle containing the boundaries
366: */
367: public Rectangle getUnscaledBounds() {
368: return new Rectangle(absoluteX, absoluteY, absoluteWidth,
369: absoluteHeight);
370: }
371:
372: /**
373: * Computes the location without the scaling factor
374: *
375: *@return the unscaled location
376: */
377: public Point getUnscaledLocation() {
378: return new Point(absoluteX, absoluteY);
379: }
380:
381: /**
382: * Inverse of the scaleInteger operation
383: *
384: *@param value the input value
385: *@return the result of the unscape operation
386: */
387: protected int unscaleInteger(int value) {
388: return (int) (value / scale);
389: }
390:
391: /**
392: * Initialize the valuse
393: */
394: private void init() {
395: inShapeChange = false;
396: scale = 1.0;
397: }
398: }
|