From 5451957b54258030dba3ee2d3cbbd8dabdc1e47f Mon Sep 17 00:00:00 2001 From: shaper Date: Fri, 14 Jun 2002 07:55:11 +0000 Subject: [PATCH] Renamed getToolTipPosition() to the more generally-useful fitRectInRect() and moved to SwingUtil. git-svn-id: https://samskivert.googlecode.com/svn/trunk@777 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../com/samskivert/swing/util/SwingUtil.java | 38 ++++++++++- .../samskivert/swing/util/ToolTipUtil.java | 67 ------------------- 2 files changed, 37 insertions(+), 68 deletions(-) delete mode 100644 projects/samskivert/src/java/com/samskivert/swing/util/ToolTipUtil.java diff --git a/projects/samskivert/src/java/com/samskivert/swing/util/SwingUtil.java b/projects/samskivert/src/java/com/samskivert/swing/util/SwingUtil.java index 2cfd754b..834de2ef 100644 --- a/projects/samskivert/src/java/com/samskivert/swing/util/SwingUtil.java +++ b/projects/samskivert/src/java/com/samskivert/swing/util/SwingUtil.java @@ -1,5 +1,5 @@ // -// $Id: SwingUtil.java,v 1.9 2002/06/05 23:15:26 ray Exp $ +// $Id: SwingUtil.java,v 1.10 2002/06/14 07:55:11 shaper Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Michael Bayne @@ -78,6 +78,42 @@ public class SwingUtil g.drawString(str, xpos, ypos); } + /** + * Returns the most reasonable position for the specified rectangle to + * be placed at so as to maximize its containment by the specified + * bounding rectangle while still placing it as near its original + * coordinates as possible. + * + * @param rect the rectangle to be positioned. + * @param bounds the containing rectangle. + */ + public static Point fitRectInRect ( + Rectangle rect, Rectangle bounds) + { + // make sure left edge is within bounds + Rectangle erect = new Rectangle(rect); + if (erect.x < bounds.x) { + erect.x = bounds.x; + } + + // make sure top edge is within bounds + if (erect.y < bounds.y) { + erect.y = bounds.y; + } + + // do our best to fit entire rectangle into bounds horizontally + if ((erect.x + erect.width) > (bounds.x + bounds.width)) { + erect.x = (bounds.x + bounds.width) - erect.width; + } + + // do our best to fit entire rect into bounds vertically + if ((erect.y + erect.height) > (bounds.y + bounds.height)) { + erect.y = (bounds.y + bounds.height) - erect.height; + } + + return new Point(erect.x, erect.y); + } + /** * Return a polygon representing the rectangle defined by the * specified upper left coordinate and the supplied dimensions. diff --git a/projects/samskivert/src/java/com/samskivert/swing/util/ToolTipUtil.java b/projects/samskivert/src/java/com/samskivert/swing/util/ToolTipUtil.java deleted file mode 100644 index 37c0c360..00000000 --- a/projects/samskivert/src/java/com/samskivert/swing/util/ToolTipUtil.java +++ /dev/null @@ -1,67 +0,0 @@ -// -// $Id: ToolTipUtil.java,v 1.2 2001/12/14 18:58:29 shaper Exp $ -// -// samskivert library - useful routines for java programs -// Copyright (C) 2001 Walter Korman -// -// This library is free software; you can redistribute it and/or modify it -// under the terms of the GNU Lesser General Public License as published -// by the Free Software Foundation; either version 2.1 of the License, or -// (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - -package com.samskivert.swing.util; - -import java.awt.*; - -/** - * Miscellaneous useful tool tip related utility functions. - */ -public class ToolTipUtil -{ - /** - * Returns an eminently reasonable position at which the tool tip - * may be displayed so as to maximize its visibility while still - * placing it as near the relevant mouse position as is feasible. - * - * @param x the mouse x-position associated with the tool tip. - * @param y the mouse y-position associated with the tool tip. - * @param tip the tool tip dimensions. - * @param bounds the tool tip container's bounding rectangle. - */ - public static Point getTipPosition ( - int x, int y, Dimension tip, Rectangle bounds) - { - Rectangle tiprect = new Rectangle(x, y, tip.width, tip.height); - - // make sure left edge of tip is within bounds - if (tiprect.x < bounds.x) { - tiprect.x = bounds.x; - } - - // make sure top edge of tip is within bounds - if (tiprect.y < bounds.y) { - tiprect.y = bounds.y; - } - - // do our best to fit entire tip into bounds horizontally - if ((tiprect.x + tiprect.width) > (bounds.x + bounds.width)) { - tiprect.x = (bounds.x + bounds.width) - tiprect.width; - } - - // do our best to fit entire tip into bounds vertically - if ((tiprect.y + tiprect.height) > (bounds.y + bounds.height)) { - tiprect.y = (bounds.y + bounds.height) - tiprect.height; - } - - return new Point(tiprect.x, tiprect.y); - } -}