home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-01 | 7.6 KB | 186 lines |
- package GUITools;
- import GUITools.Tooltip;
- import java.AWT.*;
- import java.AWT.event.*;
-
- /**
- * The Hints class was developed to encapsulate the Tooltip class
- * This class draws a yellow label tooltip (or hint/hovering help...).
- * This class takes care of all the tooltip showing and hideing events.
- * This class was developed to run under JDK 1.1.1, with very little assistance
- * from VisualAge for Java, most of the work was done using javac.
- * This class was written by Shai Almog 5/4/97. The sorce is in the public
- * domain and may be modified and used freely. It is not a requirment but
- * I would consider it good manners if you credit me and EDM/2 (www.edm2.com)
- * in applications in which this code was used ;-)
- **/
- class Hints extends Component implements MouseListener
- {
- /**
- * The constructor accepts the folowing parameters:
- * displayArea - The Container where the tooltip will show on, this has to be
- * the same place where the Component is (this is a slight limitaion).
- * hintOwner - The Component to which the tooltip refers.
- * textFont - The font used in the tooltip.
- * hintText - The text of the tooltip.
- * orientation - A constant (one of those defined below) refering to the location
- * of the tooltip relatively to the Component.
- **/
- public Hints(Container displayArea,Component hintOwner,Font textFont,
- String hintText,int orientation)
- {
- this.orientation = orientation;
- this.hintText = hintText;
- this.hintOwner = hintOwner;
- this.displayArea = displayArea;
- setFont(textFont);
- hintOwner.addMouseListener(this);
- }
-
- public void setBackColor(Color c)
- {
- backColor = c;
- }
-
- public void setTextColor(Color c)
- {
- textColor = c;
- }
-
- public void setFont(Font textFont)
- {
- if (textFont == null) throw (new NullPointerException("Font textFont is null!"));
- this.textFont = textFont;
- }
-
- /**
- * Invoked when the mouse has been clicked on a component.
- * Required by the MouseListener interface.
- **/
- public void mouseClicked(MouseEvent e)
- {
- }
-
- /**
- * Invoked when a mouse button has been pressed on a component.
- * Required by the MouseListener interface.
- **/
- public void mousePressed(MouseEvent e)
- {
- }
-
- /**
- * Invoked when a mouse button has been released on a component.
- * Required by the MouseListener interface.
- **/
- public void mouseReleased(MouseEvent e)
- {
- }
-
- /**
- * Invoked when the mouse enters a component.
- * Required by the MouseListener interface.
- **/
- public void mouseEntered(MouseEvent e)
- {
- calculateXY();
- tooltipInstance = new Tooltip(hintText,textFont,displayArea,x,y);
- tooltipInstance.paint(displayArea.getGraphics());
- }
-
- /**
- * Invoked when the mouse exits the component.
- * Required by the MouseListener interface.
- **/
- public void mouseExited(MouseEvent e)
- {
- tooltipInstance.dispose();
- tooltipInstance = null;
- displayArea.repaint();
- }
-
- /**
- * This method calculates the X and Y where the Toolip should be drawn
- * considering the orientation and the font size.
- **/
- private void calculateXY()
- {
- FontMetrics fontSize = getToolkit().getFontMetrics(textFont);
- int fontWidth = fontSize.stringWidth(hintText);
- int fontHeight = fontSize.getHeight();
- switch(orientation)
- {
- case TOP_LEFT:
- {
- x = hintOwner.getLocation().x - fontWidth;
- y = hintOwner.getLocation().y - fontHeight;
- break;
- }
- case TOP:
- {
- x = hintOwner.getLocation().x - fontWidth;
- y = hintOwner.getLocation().y;
- break;
- }
- case TOP_RIGHT:
- {
- x = hintOwner.getLocation().x + hintOwner.getSize().width;
- y = hintOwner.getLocation().y - fontHeight;
- break;
- }
- case LEFT:
- {
- x = hintOwner.getLocation().x - fontWidth;
- y = hintOwner.getLocation().y + (hintOwner.getSize().height / 2)
- - fontHeight;
- break;
- }
- case RIGHT:
- {
- x = hintOwner.getLocation().x + hintOwner.getSize().width;
- y = hintOwner.getLocation().y + (hintOwner.getSize().height / 2)
- - fontHeight;
- break;
- }
- case BOTTOM_RIGHT:
- {
- x = hintOwner.getLocation().x + hintOwner.getSize().width;
- y = hintOwner.getLocation().y + hintOwner.getSize().height;
- break;
- }
- case BOTTOM_LEFT:
- {
- x = hintOwner.getLocation().x - fontWidth;
- y = hintOwner.getLocation().y + hintOwner.getSize().height;
- break;
- }
- case BOTTOM:
- {
- x = hintOwner.getLocation().x;
- y = hintOwner.getLocation().y + hintOwner.getSize().height;
- break;
- }
- }
- }
-
- private int x,y; // The x and y location where the hint will be
- // displayed (relative to the container).
- private Color backColor = Color.yellow, // The background color of the hint.
- textColor = Color.black; // The text color of the hint.
- private Font textFont; // The font of the hint.
- private Tooltip tooltipInstance = null; // The tooltip that will be used.
- private String hintText; // The text of the hint.
- private Container displayArea; // The area containg the hintOwner where
- // the label will be displayed.
- private Component hintOwner; // The Component to which this hint refers
- private int orientation = 0;
- public static final int TOP_LEFT = 1; // These are all the possible
- public static final int TOP = 2; // orientation valuses.
- public static final int TOP_RIGHT = 3;
- public static final int LEFT = 4;
- public static final int RIGHT = 5;
- public static final int BOTTOM_RIGHT = 6;
- public static final int BOTTOM_LEFT = 7;
- public static final int BOTTOM = 8;
- }
-