001: /*
002: * Copyright (c) 2005, romain guy (romain.guy@jext.org) and craig wickesser (craig@codecraig.com)
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
006: *
007: * * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
008: * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
009: * * Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
010: *
011: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
012: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
013: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
014: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
015: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
016: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
017: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
018: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
019: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
020: * POSSIBILITY OF SUCH DAMAGE.
021: */
022:
023: package net.java.swingfx.rubberband.rubberbands;
024:
025: import java.awt.Rectangle;
026: import java.awt.event.MouseEvent;
027:
028: import javax.swing.event.MouseInputAdapter;
029:
030: import net.java.swingfx.rubberband.canvas.RubberBandCanvas;
031:
032: /**
033: * An abstract implementation of {@link RubberBand} which handles
034: * the basic drawing/setup of the rubber band.
035: *
036: * @author rwickesser
037: * @since 1.0
038: * $Revision: 1.2 $
039: */
040: public abstract class AbstractRubberBand extends MouseInputAdapter
041: implements RubberBand {
042: /** the canvas where the rubber band will be drawn onto */
043: protected RubberBandCanvas canvas;
044:
045: /** maintains the size and location of the rubber band */
046: protected Rectangle rubberband;
047:
048: /** stores the x coordinate of the mouse pressed event */
049: private int pressX;
050: /** stores the y coordinate of the mouse pressed event */
051: private int pressY;
052:
053: /**
054: * if <code>true</code> then the rubber band will disappear
055: * after the mouse is released, otherwise the rubber band stays
056: * visible until a new rubber band is drawn. Subclasses
057: * should override
058: */
059: private boolean hideOnRelease;
060:
061: /**
062: * Creates a new <code>RubberBand</code> and sets the canvas
063: *
064: * @param canvas the <code>RubberBandCanvas</code> on which the rubber band
065: * will be drawn
066: *
067: * @see #setCanvas(RubberBandCanvas)
068: */
069: public AbstractRubberBand(RubberBandCanvas canvas) {
070: this .canvas = canvas;
071: init();
072: }
073:
074: /**
075: * Initializes the rubber band
076: */
077: private void init() {
078: rubberband = new Rectangle();
079: setHideOnRelease(true);
080:
081: if (canvas != null) {
082: canvas.setRubberBand(this );
083: addMouseListeners();
084: }
085: }
086:
087: /* (non-Javadoc)
088: * @see gui.rubberband.RubberBand#addMouseListeners()
089: */
090: public void addMouseListeners() {
091: canvas.getCanvas().addMouseListener(this );
092: canvas.getCanvas().addMouseMotionListener(this );
093: }
094:
095: /* (non-Javadoc)
096: * @see net.java.swingfx.rubberband.RubberBand#getBounds()
097: */
098: public Rectangle getBounds() {
099: return rubberband.getBounds();
100: }
101:
102: /* (non-Javadoc)
103: * @see gui.rubberband.RubberBand#setCanvas(RubberBandCanvas)
104: */
105: public void setCanvas(RubberBandCanvas canvas) {
106: this .canvas = canvas;
107: this .canvas.setRubberBand(this );
108: addMouseListeners();
109: }
110:
111: /* (non-Javadoc)
112: * @see javax.swing.event.MouseInputAdapter#mouseDragged(java.awt.event.MouseEvent)
113: */
114: public void mouseDragged(MouseEvent e) {
115: updateRubberBand(e);
116:
117: int x = e.getX();
118: int y = e.getY();
119: int w = 0;
120: int h = 0;
121:
122: // adjust x and width
123: if (pressX < x) {
124: int tmp = x;
125: x = pressX;
126: w = tmp - x;
127: } else {
128: w = pressX - x;
129: }
130:
131: // adjust y and height
132: if (pressY < y) {
133: int tmp = y;
134: y = pressY;
135: h = tmp - y;
136: } else {
137: h = pressY - y;
138: }
139:
140: // update rubber band size and location
141: update(x, y, w, h);
142:
143: // repaint the canvas so the rubber band is updated visually
144: updateCanvas();
145: }
146:
147: /* (non-Javadoc)
148: * @see javax.swing.event.MouseInputAdapter#mousePressed(java.awt.event.MouseEvent)
149: */
150: public void mousePressed(MouseEvent e) {
151: startRubberBand(e);
152: pressX = e.getX();
153: pressY = e.getY();
154:
155: // update rubber band size and location
156: update(pressX, pressY, 0, 0);
157: }
158:
159: /* (non-Javadoc)
160: * @see javax.swing.event.MouseInputAdapter#mouseReleased(java.awt.event.MouseEvent)
161: */
162: public void mouseReleased(MouseEvent e) {
163: stopRubberBand(e);
164:
165: // erase the rubber band if hideOnRelease is true
166: if (isHideOnRelease()) {
167: // update rubber band size and location
168: update(-1, -1, 0, 0);
169:
170: // repaint the canvas so the rubber band disappears
171: updateCanvas();
172: }
173: }
174:
175: /**
176: * Makes a call to the canvas' repaint method
177: */
178: private void updateCanvas() {
179: canvas.getCanvas().repaint();
180: }
181:
182: /**
183: * Sets whether the rubber band should disappear when the mouse
184: * is released or not
185: *
186: * @param hideOnRelease if <code>true</code> the rubber band will
187: * disappear when the mouse is released, if
188: * <code>false</code> the rubber band will remain
189: * visible until a new rubber band is created
190: */
191: protected void setHideOnRelease(boolean hideOnRelease) {
192: this .hideOnRelease = hideOnRelease;
193: }
194:
195: /**
196: * Returns whether or not the rubber band should disappear
197: * after the mouse is released
198: *
199: * @return <code>true</code> if the rubber band should
200: * disappear when the mouse is released, else if
201: * <code>false</code> the rubber band should remain
202: * visible until a new rubber band is created
203: */
204: protected boolean isHideOnRelease() {
205: return hideOnRelease;
206: }
207: }
|