Added NotesDialog MVC. Wired window movement. Added GUIUtil

This commit is contained in:
Jonathan Bernard
2009-12-21 14:06:32 -06:00
parent 5000d266fe
commit 02fc6b5bb7
9 changed files with 212 additions and 5 deletions

View File

@ -0,0 +1,91 @@
package com.jdbernard.timestamper
import java.awt.Point
import java.awt.Rectangle
import java.awt.Toolkit
import java.awt.event.MouseEvent
public class GUIUtil {
static Point calculateWindowMovement(Point currentMousePoint,
Point mousePressRelativeToWindow, Rectangle windowBounds,
Rectangle... snapBoxes) {
// this is the point we will compute as the new upper left
// coordinate of the frame. It needs to be translated some to account
// for the fact that the user can press the mouse anywhere on the
// main panel.
Point currentWindowPoint = (Point) currentMousePoint.clone();
// find out where the new point should be, ignoreing screen bounds
currentWindowPoint.translate((int) -mousePressRelativeToWindow.x,
(int) -mousePressRelativeToWindow.y);
Point finalWindowPoint = (Point) currentWindowPoint.clone();
// snap tests. In the event of a conflict in snaps positions, the
// last snap wins
int wUp = windowBounds.y;
int wDown = windowBounds.y + windowBounds.height;
int wLeft = windowBounds.x;
int wRight = windowBounds.x + windowBounds.width;
// currentTaskLabel.setText("U:" + wUp + " D:" + wDown + " L:" + wLeft
// + " R:" + wRight);
for (Rectangle r : snapBoxes) {
int rUp = r.y;
int rDown = r.y + r.height;
int rLeft = r.x;
int rRight = r.x + r.width;
// check to see if the window is near any of the snap boundaries
// if it is, 'snap' the final point to that boundary.
if (Math.abs(rUp - wUp) < 20) // top of window to top of rect
finalWindowPoint.y = rUp;
else if (Math.abs(rUp - wDown) < 20) // bot of w to top of r
finalWindowPoint.y = rUp - windowBounds.height;
else if (Math.abs(rDown - wUp) < 20) // top of w to bot of r
finalWindowPoint.y = rDown;
else if (Math.abs(rDown - wDown) < 20) // bot of w to bot of r
finalWindowPoint.y = rDown - windowBounds.height;
if (Math.abs(rLeft - wLeft) < 20) // left of w to left of r
finalWindowPoint.x = rLeft;
else if (Math.abs(rLeft - wRight) < 20) // right of w to left of r
finalWindowPoint.x = rLeft - windowBounds.width;
else if (Math.abs(rRight - wLeft) < 20) // left of w to right of r
finalWindowPoint.x = rRight;
else if (Math.abs(rRight - wRight) < 20) // right of w to right of r
finalWindowPoint.x = rRight - windowBounds.width;
}
// this point represents a backwards version of the initial calculation
// to find the finalPoint. It says, based on the current final point,
// assume I moved the frame to that point. Where, relative to that point
// should the mouse be, based on how far it was relative to the point
// when the user pressed it.
Point whereMouseShouldBe = (Point) finalWindowPoint.clone();
whereMouseShouldBe.translate((int) mousePressRelativeToWindow.x,
(int) mousePressRelativeToWindow.y);
// if the actual mouse location is different from the expected location,
// then we know that the snapping behaviour has altered the frames new
// placement. If this alteration is too large (30 px apart in this case)
// then we know the user is trying to pull the frame out of its snapped
// position. In this case, we want to ignore the snap calculations and
// base the new frame location entirely on the current mouse location
if (whereMouseShouldBe.distance(currentMousePoint) > 30) {
finalWindowPoint = (Point) currentMousePoint.clone();
finalWindowPoint.translate((int) -mousePressRelativeToWindow.x,
(int) -mousePressRelativeToWindow.y);
}
return finalWindowPoint;
}
def static componentDragged(frame, MouseEvent evt,
Point mousePressRelativeToFrame, Rectangle... snapBoxes) {
frame.location = calculateWindowMovement(evt.getLocationOnScreen(),
mousePressRelativeToFrame, frame.bounds, snapBoxes)
}
}