001: /*
002: * Copyright (C) 2004 Giuseppe MANNA
003: *
004: * This file is part of FreeReportBuilder
005: *
006: * FreeReportBuilder is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package it.frb;
023:
024: import java.awt.*;
025: import java.awt.event.*;
026: import javax.swing.*;
027: import javax.swing.border.*;
028:
029: public class ResizeBorder extends AbstractBorder implements
030: MouseListener, MouseMotionListener {
031: private static final int LEFT_UP_CORNER = 1;
032: private static final int UP_CENTER = 2;
033: private static final int RIGHT_UP_CORNER = 3;
034: private static final int LEFT_CENTER = 4;
035: private static final int RIGHT_CENTER = 5;
036: private static final int LEFT_DOWN_CORNER = 6;
037: private static final int DOWN_CENTER = 7;
038: private static final int RIGHT_DOWN_CORNER = 8;
039:
040: int length = 10;
041: int insets = 5;
042: int pointPressed;
043: boolean resize;
044:
045: JComponent c;
046: Point sourcePoint;
047: Point lastPosition = new Point();
048: Dimension initialDimension;
049:
050: Rectangle leftUpCorner;
051: Rectangle leftDownCorner;
052: Rectangle rightUpCorner;
053: Rectangle rightDownCorner;
054: Rectangle upCenter;
055: Rectangle downCenter;
056: Rectangle leftCenter;
057: Rectangle rightCenter;
058:
059: public ResizeBorder() {
060: super ();
061: }
062:
063: public void paintBorder(Component c, Graphics g, int x, int y,
064: int width, int height) {
065: this .c = (JComponent) c;
066:
067: c.addMouseListener(this );
068: c.addMouseMotionListener(this );
069:
070: g.drawRect(x, y, width - 1, height - 1);
071:
072: g.setColor(Color.blue);
073: //left up
074: g.fillRect(x - insets, y - insets, length, length);
075: //left down
076: g.fillRect(x - insets, (y + height) - insets, length, length);
077: //right up
078: g.fillRect((x + width) - insets, y - insets, length, length);
079: //right down
080: g.fillRect((x + width) - insets, (y + height) - insets, length,
081: length);
082: //Up
083: g.fillRect((x + width) / 2 - insets, y - insets, length / 2,
084: length);
085: //down
086: g.fillRect((x + width) / 2 - insets, (y + height) - insets,
087: length / 2, length);
088: //left center
089: g.fillRect(x - insets, (y + height) / 2 - insets / 2, length,
090: length / 2);
091: //right center
092: g.fillRect((x + width) - insets, (y + height) / 2 - insets / 2,
093: length, length / 2);
094:
095: leftUpCorner = new Rectangle(x - insets, y - insets, length,
096: length);
097: leftDownCorner = new Rectangle(x - insets, (y + height)
098: - insets, length, length);
099: rightUpCorner = new Rectangle((x + width) - insets, y - insets,
100: length, length);
101: rightDownCorner = new Rectangle((x + width) - insets,
102: (y + height) - insets, length, length);
103: upCenter = new Rectangle((x + width) / 2 - insets, y - insets,
104: length / 2, length);
105: downCenter = new Rectangle((x + width) / 2 - insets,
106: (y + height) - insets, length / 2, length);
107: leftCenter = new Rectangle(x - insets, (y + height) / 2
108: - insets / 2, length, length / 2);
109: rightCenter = new Rectangle((x + width) - insets, (y + height)
110: / 2 - insets / 2, length, length / 2);
111: }
112:
113: public void mouseDragged(MouseEvent e) {
114: if (resize) {
115: int incx = sourcePoint.x - c.getLocation().x;
116: int incy = sourcePoint.y - c.getLocation().y;
117:
118: if (pointPressed == LEFT_UP_CORNER) {
119: if (initialDimension.width + incx > 5
120: && initialDimension.height + incy > 5) {
121: c.setLocation(c.getLocation().x, c.getLocation().y);
122: c.setSize(initialDimension.width + incx,
123: initialDimension.height + incy);
124: lastPosition.x = c.getLocation().x;
125: lastPosition.y = c.getLocation().y;
126: } else {
127: c.setLocation(lastPosition.x, lastPosition.y);
128: }
129: } else if (pointPressed == RIGHT_DOWN_CORNER) {
130: if (e.getX() > 5 && e.getY() > 5) {
131: c.setLocation(sourcePoint.x, sourcePoint.y);
132: c.setSize(e.getX(), e.getY());
133: } else {
134: c.setLocation(sourcePoint.x, sourcePoint.y);
135: }
136: } else if (pointPressed == RIGHT_CENTER) {
137: if (e.getX() > 5) {
138: c.setLocation(sourcePoint.x, sourcePoint.y);
139: c.setSize(e.getX(), initialDimension.height);
140: } else {
141: c.setLocation(sourcePoint.x, sourcePoint.y);
142: }
143: } else if (pointPressed == DOWN_CENTER) {
144: if (e.getY() > 5) {
145: c.setLocation(sourcePoint.x, sourcePoint.y);
146: c.setSize(initialDimension.width, e.getY());
147: } else {
148: c.setLocation(sourcePoint.x, sourcePoint.y);
149: }
150: } else if (pointPressed == LEFT_CENTER) {
151: if (initialDimension.width + incx > 5) {
152: c.setLocation(c.getLocation().x, sourcePoint.y);
153: c.setSize(initialDimension.width + incx,
154: initialDimension.height);
155: lastPosition.x = c.getLocation().x;
156: } else {
157: c.setLocation(lastPosition.x, sourcePoint.y);
158: }
159: } else if (pointPressed == UP_CENTER) {
160: if (initialDimension.height + incy > 5) {
161: c.setLocation(sourcePoint.x, c.getLocation().y);
162: c.setSize(initialDimension.width,
163: initialDimension.height + incy);
164: lastPosition.y = c.getLocation().y;
165: } else {
166: c.setLocation(sourcePoint.x, lastPosition.y);
167: }
168: } else if (pointPressed == LEFT_DOWN_CORNER) {
169: if (initialDimension.width + incx > 5 && e.getY() > 5) {
170: c.setLocation(c.getLocation().x, sourcePoint.y);
171: c.setSize(initialDimension.width + incx, e.getY());
172: lastPosition.x = c.getLocation().x;
173: } else {
174: c.setLocation(lastPosition.x, sourcePoint.y);
175: }
176: } else if (pointPressed == RIGHT_UP_CORNER) {
177: if (e.getX() > 5 && initialDimension.height + incy > 5) {
178: c.setLocation(sourcePoint.x, c.getLocation().y);
179: c.setSize(e.getX(), initialDimension.height + incy);
180: lastPosition.y = c.getLocation().y;
181: } else {
182: c.setLocation(sourcePoint.x, lastPosition.y);
183: }
184: }
185: }
186: }
187:
188: public void mouseMoved(MouseEvent e) {
189: setPointer(e);
190: }
191:
192: public void mouseClicked(MouseEvent e) {
193: }
194:
195: public void mouseEntered(MouseEvent e) {
196: }
197:
198: public void mouseExited(MouseEvent e) {
199: }
200:
201: public void mousePressed(MouseEvent e) {
202:
203: if (leftUpCorner.contains(e.getX(), e.getY())) {
204: pointPressed = ResizeBorder.LEFT_UP_CORNER;
205: } else if (leftDownCorner.contains(e.getX(), e.getY())) {
206: pointPressed = ResizeBorder.LEFT_DOWN_CORNER;
207: } else if (rightUpCorner.contains(e.getX(), e.getY())) {
208: pointPressed = ResizeBorder.RIGHT_UP_CORNER;
209: } else if (rightDownCorner.contains(e.getX(), e.getY())) {
210: pointPressed = ResizeBorder.RIGHT_DOWN_CORNER;
211: } else if (upCenter.contains(e.getX(), e.getY())) {
212: pointPressed = ResizeBorder.UP_CENTER;
213: } else if (downCenter.contains(e.getX(), e.getY())) {
214: pointPressed = ResizeBorder.DOWN_CENTER;
215: } else if (leftCenter.contains(e.getX(), e.getY())) {
216: pointPressed = ResizeBorder.LEFT_CENTER;
217: } else if (rightCenter.contains(e.getX(), e.getY())) {
218: pointPressed = ResizeBorder.RIGHT_CENTER;
219: }
220:
221: sourcePoint = c.getLocation();
222: initialDimension = c.getSize();
223: resize = true;
224: }
225:
226: public void mouseReleased(MouseEvent e) {
227: c.removeMouseListener(this );
228: c.removeMouseMotionListener(this );
229: }
230:
231: private void setPointer(MouseEvent e) {
232: Container container = c.getParent();
233:
234: while (!(container instanceof javax.swing.JLayeredPane)) {
235: container = (Container) container.getParent();
236: }
237:
238: if (leftUpCorner.contains(e.getX(), e.getY())) {
239: container.setCursor(Cursor
240: .getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
241: } else if (leftDownCorner.contains(e.getX(), e.getY())) {
242: container.setCursor(Cursor
243: .getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));
244: } else if (rightUpCorner.contains(e.getX(), e.getY())) {
245: container.setCursor(Cursor
246: .getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
247: } else if (rightDownCorner.contains(e.getX(), e.getY())) {
248: container.setCursor(Cursor
249: .getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
250: } else if (upCenter.contains(e.getX(), e.getY())) {
251: container.setCursor(Cursor
252: .getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
253: } else if (downCenter.contains(e.getX(), e.getY())) {
254: container.setCursor(Cursor
255: .getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
256: } else if (leftCenter.contains(e.getX(), e.getY())) {
257: container.setCursor(Cursor
258: .getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
259: } else if (rightCenter.contains(e.getX(), e.getY())) {
260: container.setCursor(Cursor
261: .getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
262: } else {
263: container.setCursor(Cursor
264: .getPredefinedCursor(Cursor.DEFAULT_CURSOR));
265: }
266: }
267: }
|