001: /*
002: * @(#)ResizableMouseInputAdapter.java 3/4/2005
003: *
004: * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
005: */
006: package com.jidesoft.swing;
007:
008: import javax.swing.*;
009: import javax.swing.event.MouseInputAdapter;
010: import java.awt.*;
011: import java.awt.event.MouseEvent;
012:
013: /**
014: * Mouse input listenr to control the resizing of <code>Resizable</code> component.
015: */
016: public class ResizableMouseInputAdapter extends MouseInputAdapter {
017:
018: /**
019: * Starting bounds of the dragged component.
020: */
021: private static Rectangle _startingBounds;
022:
023: /**
024: * _eventMouseScreenX is the mousePressed location in absolute coordinate system.
025: */
026: private int _eventMouseScreenX;
027: /**
028: * _eventMouseScreenY is the mousePressed location in absolute coordinate system.
029: */
030: private int _eventMouseScreenY;
031:
032: /**
033: * The resize direction.
034: */
035: private int _resizeCorner;
036:
037: protected final static int RESIZE_NONE = 0;
038:
039: private boolean _discardRelease = false;
040:
041: private Resizable _resizable;
042:
043: public ResizableMouseInputAdapter(Resizable resizable) {
044: _resizable = resizable;
045: }
046:
047: private boolean isResizable(int resizeDir) {
048: return _resizable != null
049: && (_resizable.getResizableCorners() & resizeDir) != 0;
050: }
051:
052: @Override
053: public void mousePressed(MouseEvent e) {
054: _resizeCorner = RESIZE_NONE;
055:
056: _startingBounds = _resizable.getComponent().getBounds();
057: if (_resizable.isTopLevel()) {
058: Point location = new Point(_startingBounds.x,
059: _startingBounds.y);
060: SwingUtilities.convertPointToScreen(location, _resizable
061: .getComponent());
062: _startingBounds.x = location.x;
063: _startingBounds.y = location.y;
064: }
065:
066: Point p = new Point(e.getX(), e.getY());
067: SwingUtilities.convertPointToScreen(p, (Component) e
068: .getSource());
069: _eventMouseScreenX = p.x;
070: _eventMouseScreenY = p.y;
071:
072: if (e.getSource() instanceof Resizable.ResizeCorner) {
073: Resizable.ResizeCorner corner = (Resizable.ResizeCorner) e
074: .getSource();
075: _resizeCorner = corner.getCorner();
076: }
077: // resize component
078: else if (e.getSource() == _resizable.getComponent()) {
079: Insets i = _resizable.getComponent().getInsets();
080: if (e.getX() <= i.left) {
081: if (i.top > 0
082: && e.getY() < _resizable.getResizeCornerSize()
083: + i.top) {
084: _resizeCorner = Resizable.UPPER_LEFT;
085: } else if (i.bottom > 0
086: && e.getY() > _resizable.getComponent()
087: .getHeight()
088: - _resizable.getResizeCornerSize()
089: - i.bottom) {
090: _resizeCorner = Resizable.LOWER_LEFT;
091: } else {
092: _resizeCorner = Resizable.LEFT;
093: }
094: } else if (i.right > 0
095: && e.getX() >= _resizable.getComponent().getWidth()
096: - i.right) {
097: if (i.top > 0
098: && e.getY() < _resizable.getResizeCornerSize()
099: + i.top) {
100: _resizeCorner = Resizable.UPPER_RIGHT;
101: } else if (i.bottom > 0
102: && e.getY() > _resizable.getComponent()
103: .getHeight()
104: - _resizable.getResizeCornerSize()
105: - i.bottom) {
106: _resizeCorner = Resizable.LOWER_RIGHT;
107: } else {
108: _resizeCorner = Resizable.RIGHT;
109: }
110: } else if (i.top > 0 && e.getY() <= i.top) {
111: if (i.left > 0
112: && e.getX() < _resizable.getResizeCornerSize()
113: + i.left) {
114: _resizeCorner = Resizable.UPPER_LEFT;
115: } else if (i.right > 0
116: && e.getX() > _resizable.getComponent()
117: .getWidth()
118: - _resizable.getResizeCornerSize()
119: - i.right) {
120: _resizeCorner = Resizable.UPPER_RIGHT;
121: } else {
122: _resizeCorner = Resizable.UPPER;
123: }
124: } else if (i.bottom > 0
125: && e.getY() >= _resizable.getComponent()
126: .getHeight()
127: - i.bottom) {
128: if (i.left > 0
129: && e.getX() < _resizable.getResizeCornerSize()
130: + i.left) {
131: _resizeCorner = Resizable.LOWER_LEFT;
132: } else if (i.right > 0
133: && e.getX() > _resizable.getComponent()
134: .getWidth()
135: - _resizable.getResizeCornerSize()
136: - i.right) {
137: _resizeCorner = Resizable.LOWER_RIGHT;
138: } else {
139: _resizeCorner = Resizable.LOWER;
140: }
141: } else {
142: /* the mouse press happened inside the frame, not in the
143: border */
144: _discardRelease = true;
145: return;
146: }
147: }
148:
149: Cursor s = Cursor.getDefaultCursor();
150: if (isResizable(_resizeCorner)) {
151: switch (_resizeCorner) {
152: case Resizable.LOWER:
153: s = Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR);
154: break;
155: case Resizable.UPPER:
156: s = Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR);
157: break;
158: case Resizable.LEFT:
159: s = Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR);
160: break;
161: case Resizable.RIGHT:
162: s = Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR);
163: break;
164: case Resizable.LOWER_RIGHT:
165: s = Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR);
166: break;
167: case Resizable.LOWER_LEFT:
168: s = Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR);
169: break;
170: case Resizable.UPPER_LEFT:
171: s = Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR);
172: break;
173: case Resizable.UPPER_RIGHT:
174: s = Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR);
175: break;
176: }
177:
178: Container c = _resizable.getComponent()
179: .getTopLevelAncestor();
180:
181: if (c instanceof JFrame) {
182: ((JFrame) c).getGlassPane().setVisible(true);
183: ((JFrame) c).getGlassPane().setCursor(s);
184: } else if (c instanceof JApplet) {
185: ((JApplet) c).getGlassPane().setVisible(true);
186: ((JApplet) c).getGlassPane().setCursor(s);
187: } else if (c instanceof JWindow) {
188: ((JWindow) c).getGlassPane().setVisible(true);
189: ((JWindow) c).getGlassPane().setCursor(s);
190: } else if (c instanceof JDialog) {
191: ((JDialog) c).getGlassPane().setVisible(true);
192: ((JDialog) c).getGlassPane().setCursor(s);
193: }
194:
195: _resizable.beginResizing(_resizeCorner);
196: } else {
197: _resizeCorner = RESIZE_NONE;
198: }
199: }
200:
201: @Override
202: public void mouseDragged(MouseEvent e) {
203: if (_startingBounds == null) {
204: return;
205: }
206:
207: Point p = new Point(e.getX(), e.getY());
208: Component c = (Component) e.getSource();
209: SwingUtilities.convertPointToScreen(p, c);
210:
211: int deltaX = _eventMouseScreenX - p.x;
212: int deltaY = _eventMouseScreenY - p.y;
213:
214: Dimension min = _resizable.getComponent().getMinimumSize();
215: Dimension max = _resizable.getComponent().getMaximumSize();
216:
217: Point point = new Point(_resizable.getComponent().getX(),
218: _resizable.getComponent().getY());
219:
220: if (_resizable.isTopLevel()) {
221: SwingUtilities.convertPointToScreen(point, _resizable
222: .getComponent());
223: }
224:
225: int newX = point.x;
226: int newY = point.y;
227: int newW;
228: int newH;
229:
230: Rectangle parentBounds = _resizable.isTopLevel() ? new Rectangle(
231: Integer.MIN_VALUE, Integer.MIN_VALUE,
232: Integer.MAX_VALUE, Integer.MAX_VALUE)
233: : _resizable.getComponent().getParent().getBounds();
234:
235: switch (_resizeCorner) {
236: case RESIZE_NONE:
237: return;
238: case Resizable.UPPER:
239: if (_startingBounds.height + deltaY < min.height)
240: deltaY = -(_startingBounds.height - min.height);
241: else if (_startingBounds.height + deltaY > max.height)
242: deltaY = max.height - _startingBounds.height;
243: // if (_startingBounds.y - deltaY < 0)
244: // deltaY = _startingBounds.y;
245:
246: newX = _startingBounds.x;
247: newY = _startingBounds.y - deltaY;
248: newW = _startingBounds.width;
249: newH = _startingBounds.height + deltaY;
250: // System.out.println("dragging delta " + deltaY + " newH " + newH);
251: break;
252: case Resizable.UPPER_RIGHT:
253: if (_startingBounds.height + deltaY < min.height)
254: deltaY = -(_startingBounds.height - min.height);
255: else if (_startingBounds.height + deltaY > max.height)
256: deltaY = max.height - _startingBounds.height;
257: // if (_startingBounds.y - deltaY < 0)
258: // deltaY = _startingBounds.y;
259:
260: if (_startingBounds.width - deltaX < min.width)
261: deltaX = _startingBounds.width - min.width;
262: else if (_startingBounds.width - deltaX > max.width)
263: deltaX = -(max.width - _startingBounds.width);
264: if (_startingBounds.x + _startingBounds.width - deltaX > parentBounds.width)
265: deltaX = _startingBounds.x + _startingBounds.width
266: - parentBounds.width;
267:
268: newX = _startingBounds.x;
269: newY = _startingBounds.y - deltaY;
270: newW = _startingBounds.width - deltaX;
271: newH = _startingBounds.height + deltaY;
272: break;
273: case Resizable.RIGHT:
274: if (_startingBounds.width - deltaX < min.width)
275: deltaX = _startingBounds.width - min.width;
276: else if (_startingBounds.width - deltaX > max.width)
277: deltaX = -(max.width - _startingBounds.width);
278: if (_startingBounds.x + _startingBounds.width - deltaX > parentBounds.width)
279: deltaX = _startingBounds.x + _startingBounds.width
280: - parentBounds.width;
281:
282: newW = _startingBounds.width - deltaX;
283: newH = _startingBounds.height;
284: break;
285: case Resizable.LOWER_RIGHT:
286: if (_startingBounds.width - deltaX < min.width)
287: deltaX = _startingBounds.width - min.width;
288: else if (_startingBounds.width - deltaX > max.width)
289: deltaX = -(max.width - _startingBounds.width);
290: if (_startingBounds.x + _startingBounds.width - deltaX > parentBounds.width)
291: deltaX = _startingBounds.x + _startingBounds.width
292: - parentBounds.width;
293:
294: if (_startingBounds.height - deltaY < min.height)
295: deltaY = _startingBounds.height - min.height;
296: else if (_startingBounds.height - deltaY > max.height)
297: deltaY = -(max.height - _startingBounds.height);
298: if (_startingBounds.y + _startingBounds.height - deltaY > parentBounds.height)
299: deltaY = _startingBounds.y + _startingBounds.height
300: - parentBounds.height;
301:
302: newW = _startingBounds.width - deltaX;
303: newH = _startingBounds.height - deltaY;
304: break;
305: case Resizable.LOWER:
306: if (_startingBounds.height - deltaY < min.height)
307: deltaY = _startingBounds.height - min.height;
308: else if (_startingBounds.height - deltaY > max.height)
309: deltaY = -(max.height - _startingBounds.height);
310: if (_startingBounds.y + _startingBounds.height - deltaY > parentBounds.height)
311: deltaY = _startingBounds.y + _startingBounds.height
312: - parentBounds.height;
313:
314: newW = _startingBounds.width;
315: newH = _startingBounds.height - deltaY;
316: break;
317: case Resizable.LOWER_LEFT:
318: if (_startingBounds.height - deltaY < min.height)
319: deltaY = _startingBounds.height - min.height;
320: else if (_startingBounds.height - deltaY > max.height)
321: deltaY = -(max.height - _startingBounds.height);
322: if (_startingBounds.y + _startingBounds.height - deltaY > parentBounds.height)
323: deltaY = _startingBounds.y + _startingBounds.height
324: - parentBounds.height;
325:
326: if (_startingBounds.width + deltaX < min.width)
327: deltaX = -(_startingBounds.width - min.width);
328: else if (_startingBounds.width + deltaX > max.width)
329: deltaX = max.width - _startingBounds.width;
330: // if (_startingBounds.x - deltaX < 0) {
331: // deltaX = _startingBounds.x;
332: // }
333:
334: newX = _startingBounds.x - deltaX;
335: newY = _startingBounds.y;
336: newW = _startingBounds.width + deltaX;
337: newH = _startingBounds.height - deltaY;
338: break;
339: case Resizable.LEFT:
340: if (_startingBounds.width + deltaX < min.width)
341: deltaX = -(_startingBounds.width - min.width);
342: else if (_startingBounds.width + deltaX > max.width)
343: deltaX = max.width - _startingBounds.width;
344: // if (_startingBounds.x - deltaX < 0)
345: // deltaX = _startingBounds.x;
346:
347: newX = _startingBounds.x - deltaX;
348: newY = _startingBounds.y;
349: newW = _startingBounds.width + deltaX;
350: newH = _startingBounds.height;
351: // System.out.println("dragging delta " + deltaX + " newW " + newW);
352: break;
353: case Resizable.UPPER_LEFT:
354: if (_startingBounds.width + deltaX < min.width)
355: deltaX = -(_startingBounds.width - min.width);
356: else if (_startingBounds.width + deltaX > max.width)
357: deltaX = max.width - _startingBounds.width;
358: // if (_startingBounds.x - deltaX < 0)
359: // deltaX = _startingBounds.x;
360:
361: if (_startingBounds.height + deltaY < min.height)
362: deltaY = -(_startingBounds.height - min.height);
363: else if (_startingBounds.height + deltaY > max.height)
364: deltaY = max.height - _startingBounds.height;
365: // if (_startingBounds.y - deltaY < 0) {
366: // deltaY = _startingBounds.y;
367: // }
368:
369: newX = _startingBounds.x - deltaX;
370: newY = _startingBounds.y - deltaY;
371: newW = _startingBounds.width + deltaX;
372: newH = _startingBounds.height + deltaY;
373: break;
374: default:
375: return;
376: }
377:
378: _resizable.resizing(_resizeCorner, newX, newY, newW, newH);
379: }
380:
381: @Override
382: public void mouseReleased(MouseEvent e) {
383: _startingBounds = null;
384:
385: if (_discardRelease) {
386: _discardRelease = false;
387: return;
388: }
389:
390: if (_resizeCorner != RESIZE_NONE) {
391: Container c = _resizable.getComponent()
392: .getTopLevelAncestor();
393: if (c instanceof JFrame) {
394: ((JFrame) _resizable.getComponent()
395: .getTopLevelAncestor()).getGlassPane()
396: .setCursor(Cursor.getDefaultCursor());
397:
398: ((JFrame) _resizable.getComponent()
399: .getTopLevelAncestor()).getGlassPane()
400: .setVisible(false);
401: } else if (c instanceof JApplet) {
402: ((JApplet) c).getGlassPane().setCursor(
403: Cursor.getDefaultCursor());
404: ((JApplet) c).getGlassPane().setVisible(false);
405: } else if (c instanceof JWindow) {
406: ((JWindow) c).getGlassPane().setCursor(
407: Cursor.getDefaultCursor());
408: ((JWindow) c).getGlassPane().setVisible(false);
409: } else if (c instanceof JDialog) {
410: ((JDialog) c).getGlassPane().setCursor(
411: Cursor.getDefaultCursor());
412: ((JDialog) c).getGlassPane().setVisible(false);
413: }
414:
415: _resizable.endResizing(_resizeCorner);
416:
417: _eventMouseScreenX = 0;
418: _eventMouseScreenY = 0;
419: _startingBounds = null;
420: _resizeCorner = RESIZE_NONE;
421: }
422: }
423:
424: /**
425: * mouseMoved is for resize only. When mouse moves over borders and corners, it
426: * will change to differnt cursor to indicate it's resizable.
427: *
428: * @param e mouse event
429: */
430: @Override
431: public void mouseMoved(MouseEvent e) {
432: if (e.getSource() instanceof Resizable.ResizeCorner) {
433: Resizable.ResizeCorner corner = (Resizable.ResizeCorner) e
434: .getSource();
435: switch (corner.getCorner()) {
436: case Resizable.LOWER_RIGHT:
437: corner.setCursor(Cursor
438: .getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
439: return;
440: case Resizable.UPPER_RIGHT:
441: corner.setCursor(Cursor
442: .getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
443: return;
444: case Resizable.LOWER_LEFT:
445: corner.setCursor(Cursor
446: .getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));
447: return;
448: case Resizable.UPPER_LEFT:
449: corner.setCursor(Cursor
450: .getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
451: return;
452: }
453: } else if (e.getSource() == _resizable.getComponent()) {
454: Insets i = _resizable.getComponent().getInsets();
455: if (e.getX() <= i.left) {
456: if (isResizable(Resizable.UPPER_LEFT)
457: && i.top > 0
458: && e.getY() < _resizable.getResizeCornerSize()
459: + i.top)
460: _resizable
461: .getComponent()
462: .setCursor(
463: Cursor
464: .getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
465: else if (isResizable(Resizable.LOWER_LEFT)
466: && i.bottom > 0
467: && e.getY() > _resizable.getComponent()
468: .getHeight()
469: - _resizable.getResizeCornerSize()
470: - i.bottom)
471: _resizable
472: .getComponent()
473: .setCursor(
474: Cursor
475: .getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));
476: else if (isResizable(Resizable.LEFT))
477: _resizable
478: .getComponent()
479: .setCursor(
480: Cursor
481: .getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
482: else
483: _resizable.getComponent().setCursor(
484: Cursor.getDefaultCursor());
485: } else if (e.getX() >= _resizable.getComponent().getWidth()
486: - i.right) {
487: if (isResizable(Resizable.UPPER_RIGHT)
488: && i.top > 0
489: && e.getY() < _resizable.getResizeCornerSize()
490: + i.top)
491: _resizable
492: .getComponent()
493: .setCursor(
494: Cursor
495: .getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
496: else if (isResizable(Resizable.LOWER_LEFT)
497: && i.bottom > 0
498: && e.getY() > _resizable.getComponent()
499: .getHeight()
500: - _resizable.getResizeCornerSize()
501: - i.bottom)
502: _resizable
503: .getComponent()
504: .setCursor(
505: Cursor
506: .getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
507: else if (isResizable(Resizable.RIGHT))
508: _resizable
509: .getComponent()
510: .setCursor(
511: Cursor
512: .getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
513: else
514: _resizable.getComponent().setCursor(
515: Cursor.getDefaultCursor());
516: } else if (e.getY() <= i.top) {
517: if (isResizable(Resizable.UPPER_LEFT)
518: && i.left > 0
519: && e.getX() < _resizable.getResizeCornerSize()
520: + i.left)
521: _resizable
522: .getComponent()
523: .setCursor(
524: Cursor
525: .getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
526: else if (isResizable(Resizable.UPPER_RIGHT)
527: && i.right > 0
528: && e.getX() > _resizable.getComponent()
529: .getWidth()
530: - _resizable.getResizeCornerSize()
531: - i.right)
532: _resizable
533: .getComponent()
534: .setCursor(
535: Cursor
536: .getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
537: else if (isResizable(Resizable.UPPER))
538: _resizable
539: .getComponent()
540: .setCursor(
541: Cursor
542: .getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
543: else
544: _resizable.getComponent().setCursor(
545: Cursor.getDefaultCursor());
546: } else if (e.getY() >= _resizable.getComponent()
547: .getHeight()
548: - i.bottom) {
549: if (isResizable(Resizable.LOWER_LEFT)
550: && i.left > 0
551: && e.getX() < _resizable.getResizeCornerSize()
552: + i.left)
553: _resizable
554: .getComponent()
555: .setCursor(
556: Cursor
557: .getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));
558: else if (isResizable(Resizable.LOWER_RIGHT)
559: && i.right > 0
560: && e.getX() > _resizable.getComponent()
561: .getWidth()
562: - _resizable.getResizeCornerSize()
563: - i.right)
564: _resizable
565: .getComponent()
566: .setCursor(
567: Cursor
568: .getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
569: else if (isResizable(Resizable.LOWER))
570: _resizable
571: .getComponent()
572: .setCursor(
573: Cursor
574: .getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
575: else
576: _resizable.getComponent().setCursor(
577: Cursor.getDefaultCursor());
578: } else
579: _resizable.getComponent().setCursor(
580: Cursor.getDefaultCursor());
581: return;
582: }
583:
584: _resizable.getComponent().setCursor(Cursor.getDefaultCursor());
585: }
586:
587: // implements java.awt.event.MouseListener
588: @Override
589: public void mouseExited(MouseEvent e) {
590: if (e.getSource() instanceof Resizable.ResizeCorner) {
591: Resizable.ResizeCorner corner = (Resizable.ResizeCorner) e
592: .getSource();
593: corner.setCursor(Cursor.getDefaultCursor());
594: } else {
595: _resizable.getComponent().setCursor(
596: Cursor.getDefaultCursor());
597: }
598: }
599: }
|