Draw tool tips at a most-reasonable position, and updated to use

ToolTipProvider interface for Node tool tips.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@317 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Walter Korman
2001-08-28 23:50:45 +00:00
parent 00efd9960a
commit b882283cd3
3 changed files with 83 additions and 38 deletions
+22 -15
View File
@@ -1,11 +1,12 @@
// //
// $Id: Node.java,v 1.4 2001/08/27 21:38:41 shaper Exp $ // $Id: Node.java,v 1.5 2001/08/28 23:50:45 shaper Exp $
package com.threerings.nodemap; package com.threerings.nodemap;
import java.awt.*; import java.awt.*;
import java.util.*; import java.util.*;
import com.samskivert.swing.ToolTipProvider;
import com.samskivert.swing.util.SwingUtil; import com.samskivert.swing.util.SwingUtil;
/** /**
@@ -13,7 +14,7 @@ import com.samskivert.swing.util.SwingUtil;
* *
* @see NodeMap * @see NodeMap
*/ */
public abstract class Node public abstract class Node implements ToolTipProvider
{ {
/** /**
* Construct a node with the given attributes. * Construct a node with the given attributes.
@@ -144,26 +145,32 @@ public abstract class Node
* name. * name.
* *
* @param g the graphics context. * @param g the graphics context.
* @param x the x-position at which the tip should be drawn.
* @param y the y-position at which the tip should be drawn.
*/ */
public void paintToolTip (Graphics g) public void paintToolTip (Graphics g, int x, int y)
{ {
// calculate tool tip position and dimensions Dimension d = getToolTipSize(g);
// draw the yellow box
g.setColor(Color.yellow);
g.fillRect(x, y, d.width, d.height);
g.setColor(Color.black);
g.drawRect(x, y, d.width, d.height);
// draw the node name
SwingUtil.drawStringCentered(g, _name, x, y, d.width, d.height);
}
public Dimension getToolTipSize (Graphics g)
{
// calculate tool tip dimensions
FontMetrics fm = g.getFontMetrics(g.getFont()); FontMetrics fm = g.getFontMetrics(g.getFont());
int wid = fm.stringWidth(_name) + 10; int wid = fm.stringWidth(_name) + 10;
int hei = fm.getAscent() + 4; int hei = fm.getAscent() + 4;
int xpos = _x + ((_width - wid) / 2); return new Dimension(wid, hei);
int ypos = _y + ((_height - hei) / 2);
// draw the yellow box
g.setColor(Color.yellow);
g.fillRect(xpos, ypos, wid, hei);
g.setColor(Color.black);
g.drawRect(xpos, ypos, wid, hei);
// draw the node name
SwingUtil.drawStringCentered(g, _name, xpos, ypos, wid, hei);
} }
/** /**
+51 -18
View File
@@ -1,5 +1,5 @@
// //
// $Id: NodeMap.java,v 1.3 2001/08/23 23:44:12 shaper Exp $ // $Id: NodeMap.java,v 1.4 2001/08/28 23:50:45 shaper Exp $
package com.threerings.nodemap; package com.threerings.nodemap;
@@ -8,6 +8,8 @@ import java.util.*;
import javax.swing.JComponent; import javax.swing.JComponent;
import com.samskivert.swing.ToolTipManager; import com.samskivert.swing.ToolTipManager;
import com.samskivert.swing.ToolTipProvider;
import com.samskivert.swing.util.ToolTipUtil;
/** /**
* The node map class represents a directed graph of nodes comprised * The node map class represents a directed graph of nodes comprised
@@ -106,8 +108,13 @@ public class NodeMap
} }
// draw the tool tip, if any // draw the tool tip, if any
if (_tip != null) { if (_tipper != null) {
_tip.paintToolTip(g); // determine proper tool tip placement
Point pos = ToolTipUtil.getTipPosition(
_tipx, _tipy, _tipper.getToolTipSize(g), getBounds());
// paint away
_tipper.paintToolTip(g, pos.x, pos.y);
} }
// restore original origin // restore original origin
@@ -182,6 +189,15 @@ public class NodeMap
return new Dimension(_maxx - _minx, _maxy - _miny); return new Dimension(_maxx - _minx, _maxy - _miny);
} }
/**
* Return the bounding rectangle of the node map.
*/
public Rectangle getBounds ()
{
Dimension size = getSize();
return new Rectangle(_minx, _miny, size.width, size.height);
}
/** /**
* Handle mouse-moved events passed on by the containing panel. * Handle mouse-moved events passed on by the containing panel.
* Inform any affected nodes of mouse-entered and mouse-exited * Inform any affected nodes of mouse-entered and mouse-exited
@@ -194,15 +210,15 @@ public class NodeMap
{ {
Node enternode = null; Node enternode = null;
// tell the tip manager that the mouse moved
if (_tipmgr != null) {
_tipmgr.handleMouseMoved();
}
// translate coordinates to node map origin // translate coordinates to node map origin
x += _minx; x += _minx;
y += _miny; y += _miny;
// tell the tip manager that the mouse moved
if (_tipmgr != null) {
_tipmgr.handleMouseMoved(x, y);
}
// check whether we've entered a node // check whether we've entered a node
int size = _nodes.size(); int size = _nodes.size();
for (int ii = 0; ii < size; ii++) { for (int ii = 0; ii < size; ii++) {
@@ -225,7 +241,7 @@ public class NodeMap
_lastnode.handleMouseExited(); _lastnode.handleMouseExited();
if (_tipmgr != null) { if (_tipmgr != null) {
_tipmgr.handleMouseExited(_lastnode); _tipmgr.handleMouseExited();
} }
} }
@@ -234,7 +250,7 @@ public class NodeMap
enternode.handleMouseEntered(); enternode.handleMouseEntered();
if (_tipmgr != null) { if (_tipmgr != null) {
_tipmgr.handleMouseEntered(enternode); _tipmgr.handleMouseEntered(enternode, x, y);
} }
} }
@@ -278,14 +294,28 @@ public class NodeMap
} }
/** /**
* Set the node whose tool tip should be displayed when the node * Handle mouse-exited events passed on by the containing panel.
* map is rendered. * Inform the tool tip manager that we've exited any node we may
* * have been within.
* @param node the tool tip node.
*/ */
public void setToolTipNode (Node node) public void handleMouseExited ()
{ {
_tip = node; handleMouseMoved(-1, -1);
}
/**
* Set the tool tip provider for display when the node map is
* rendered.
*
* @param tipper the tool tip provider
* @param x the last mouse x-position.
* @param y the last mouse y-position.
*/
public void setToolTipProvider (ToolTipProvider tipper, int x, int y)
{
_tipper = tipper;
_tipx = x;
_tipy = y;
} }
/** /**
@@ -299,8 +329,11 @@ public class NodeMap
_tipmgr = tipmgr; _tipmgr = tipmgr;
} }
/** The node whose tool tip is currently displayed. */ /** The current tool tip provider. */
protected Node _tip; protected ToolTipProvider _tipper;
/** The mouse position for calculating tool tip display position. */
protected int _tipx, _tipy;
/** The tool tip manager. */ /** The tool tip manager. */
protected ToolTipManager _tipmgr; protected ToolTipManager _tipmgr;
@@ -1,5 +1,5 @@
// //
// $Id: NodeMapPanel.java,v 1.2 2001/08/23 00:22:30 shaper Exp $ // $Id: NodeMapPanel.java,v 1.3 2001/08/28 23:50:45 shaper Exp $
package com.threerings.nodemap; package com.threerings.nodemap;
@@ -9,6 +9,7 @@ import javax.swing.*;
import com.samskivert.swing.ToolTipManager; import com.samskivert.swing.ToolTipManager;
import com.samskivert.swing.ToolTipObserver; import com.samskivert.swing.ToolTipObserver;
import com.samskivert.swing.ToolTipProvider;
/** /**
* The node map panel handles display of a node map and passes Swing * The node map panel handles display of a node map and passes Swing
@@ -40,15 +41,15 @@ public class NodeMapPanel extends JPanel
return _map.getSize(); return _map.getSize();
} }
public void showToolTip (Object target) public void showToolTip (ToolTipProvider tipper, int x, int y)
{ {
_map.setToolTipNode((Node)target); _map.setToolTipProvider(tipper, x, y);
repaint(); repaint();
} }
public void hideToolTip () public void hideToolTip ()
{ {
showToolTip(null); showToolTip(null, -1, -1);
} }
public JComponent getComponent () public JComponent getComponent ()
@@ -63,10 +64,14 @@ public class NodeMapPanel extends JPanel
_map.handleMouseClicked(e.getX(), e.getY()); _map.handleMouseClicked(e.getX(), e.getY());
} }
public void mouseExited (MouseEvent e)
{
_map.handleMouseExited();
}
public void mousePressed (MouseEvent e) { } public void mousePressed (MouseEvent e) { }
public void mouseReleased (MouseEvent e) { } public void mouseReleased (MouseEvent e) { }
public void mouseEntered (MouseEvent e) { } public void mouseEntered (MouseEvent e) { }
public void mouseExited (MouseEvent e) { }
/** MouseMotionListener interface methods */ /** MouseMotionListener interface methods */