home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 14 / CDACTUAL.iso / cdactual / demobin / share / os2 / edm2 / 0506 / grinding4.java < prev    next >
Encoding:
Java Source  |  1997-06-01  |  7.6 KB  |  186 lines

  1. package GUITools;
  2. import GUITools.Tooltip;
  3. import java.AWT.*;
  4. import java.AWT.event.*;
  5.  
  6. /**
  7.  * The Hints class was developed to encapsulate the Tooltip class
  8.  * This class draws a yellow label tooltip (or hint/hovering help...).
  9.  * This class takes care of all the tooltip showing and hideing events.
  10.  * This class was developed to run under JDK 1.1.1, with very little assistance
  11.  * from VisualAge for Java, most of the work was done using javac.
  12.  * This class was written by Shai Almog 5/4/97. The sorce is in the public
  13.  * domain and may be modified and used freely. It is not a requirment but
  14.  * I would consider it good manners if you credit me and EDM/2 (www.edm2.com)
  15.  * in applications in which this code was used ;-)
  16. **/
  17. class Hints extends Component implements MouseListener
  18. {
  19.         /**
  20.          * The constructor accepts the folowing parameters:
  21.          * displayArea - The Container where the tooltip will show on, this has to be
  22.          * the same place where the Component is (this is a slight limitaion).
  23.          * hintOwner - The Component to which the tooltip refers.
  24.          * textFont - The font used in the tooltip.
  25.          * hintText - The text of the tooltip.
  26.          * orientation - A constant (one of those defined below) refering to the location
  27.          * of the tooltip relatively to the Component.
  28.         **/
  29.         public Hints(Container displayArea,Component hintOwner,Font textFont,
  30.                      String hintText,int orientation)
  31.         {
  32.                this.orientation = orientation;
  33.                this.hintText = hintText;
  34.                this.hintOwner = hintOwner;
  35.                this.displayArea = displayArea;
  36.                setFont(textFont);
  37.                hintOwner.addMouseListener(this);
  38.         }
  39.  
  40.         public void setBackColor(Color c)
  41.         {
  42.                backColor = c;
  43.         }
  44.  
  45.         public void setTextColor(Color c)
  46.         {
  47.                textColor = c;
  48.         }
  49.  
  50.         public void setFont(Font textFont)
  51.         {
  52.                if (textFont == null) throw (new NullPointerException("Font textFont is null!"));
  53.                this.textFont = textFont;
  54.         }
  55.  
  56.         /**
  57.          * Invoked when the mouse has been clicked on a component.
  58.          * Required by the MouseListener interface.
  59.         **/
  60.         public void mouseClicked(MouseEvent e)
  61.         {
  62.         }
  63.  
  64.         /**
  65.          * Invoked when a mouse button has been pressed on a component.
  66.          * Required by the MouseListener interface.
  67.         **/
  68.         public void mousePressed(MouseEvent e)
  69.         {
  70.         }
  71.  
  72.         /**
  73.          * Invoked when a mouse button has been released on a component.
  74.          * Required by the MouseListener interface.
  75.         **/
  76.         public void mouseReleased(MouseEvent e)
  77.         {
  78.         }
  79.  
  80.         /**
  81.          * Invoked when the mouse enters a component.
  82.          * Required by the MouseListener interface.
  83.         **/
  84.         public void mouseEntered(MouseEvent e)
  85.         {
  86.                calculateXY();
  87.                tooltipInstance = new Tooltip(hintText,textFont,displayArea,x,y);
  88.                tooltipInstance.paint(displayArea.getGraphics());
  89.         }
  90.  
  91.         /**
  92.          * Invoked when the mouse exits the component.
  93.          * Required by the MouseListener interface.
  94.         **/
  95.         public void mouseExited(MouseEvent e)
  96.         {
  97.                tooltipInstance.dispose();
  98.                tooltipInstance = null;
  99.                displayArea.repaint();
  100.         }
  101.  
  102.         /**
  103.          * This method calculates the X and Y where the Toolip should be drawn
  104.          * considering the orientation and the font size.
  105.         **/
  106.         private void calculateXY()
  107.         {
  108.                FontMetrics fontSize = getToolkit().getFontMetrics(textFont);
  109.                int fontWidth = fontSize.stringWidth(hintText);
  110.                int fontHeight = fontSize.getHeight();
  111.                switch(orientation)
  112.                {
  113.                       case TOP_LEFT:
  114.                       {
  115.                               x = hintOwner.getLocation().x - fontWidth;
  116.                               y = hintOwner.getLocation().y - fontHeight;
  117.                               break;
  118.                       }
  119.                       case TOP:
  120.                       {
  121.                               x = hintOwner.getLocation().x - fontWidth;
  122.                               y = hintOwner.getLocation().y;
  123.                               break;
  124.                       }
  125.                       case TOP_RIGHT:
  126.                       {
  127.                               x = hintOwner.getLocation().x + hintOwner.getSize().width;
  128.                               y = hintOwner.getLocation().y - fontHeight;
  129.                               break;
  130.                       }
  131.                       case LEFT:
  132.                       {
  133.                               x = hintOwner.getLocation().x - fontWidth;
  134.                               y = hintOwner.getLocation().y + (hintOwner.getSize().height / 2)
  135.                                            - fontHeight;
  136.                               break;
  137.                       }
  138.                       case RIGHT:
  139.                       {
  140.                                x = hintOwner.getLocation().x + hintOwner.getSize().width;
  141.                                y = hintOwner.getLocation().y + (hintOwner.getSize().height / 2)
  142.                                                - fontHeight;
  143.                                break;
  144.                       }
  145.                       case BOTTOM_RIGHT:
  146.                       {
  147.                                x = hintOwner.getLocation().x + hintOwner.getSize().width;
  148.                                y = hintOwner.getLocation().y + hintOwner.getSize().height;
  149.                                break;
  150.                       }
  151.                       case BOTTOM_LEFT:
  152.                       {
  153.                                x = hintOwner.getLocation().x - fontWidth;
  154.                                y = hintOwner.getLocation().y + hintOwner.getSize().height;
  155.                                break;
  156.                       }
  157.                       case BOTTOM:
  158.                       {
  159.                                x = hintOwner.getLocation().x;
  160.                                y = hintOwner.getLocation().y + hintOwner.getSize().height;
  161.                                break;
  162.                       }
  163.                }
  164.         }
  165.  
  166.         private int x,y; // The x and y location where the hint will be
  167.                          // displayed (relative to the container).
  168.         private Color backColor = Color.yellow,  // The background color of the hint.
  169.                       textColor = Color.black;   // The text color of the hint.
  170.         private Font textFont; // The font of the hint.
  171.         private Tooltip tooltipInstance = null; // The tooltip that will be used.
  172.         private String hintText; // The text of the hint.
  173.         private Container displayArea; // The area containg the hintOwner where
  174.                                        // the label will be displayed.
  175.         private Component hintOwner;   // The Component to which this hint refers
  176.         private int orientation = 0;
  177.         public static final int TOP_LEFT = 1; // These are all the possible
  178.         public static final int TOP = 2;      // orientation valuses.
  179.         public static final int TOP_RIGHT = 3;
  180.         public static final int LEFT = 4;
  181.         public static final int RIGHT = 5;
  182.         public static final int BOTTOM_RIGHT = 6;
  183.         public static final int BOTTOM_LEFT = 7;
  184.         public static final int BOTTOM = 8;
  185. }
  186.