1   /*
2    * Copyright (c) 2003 Sun Microsystems, Inc. All  Rights Reserved.
3    * 
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions
6    * are met:
7    * 
8    * -Redistributions of source code must retain the above copyright
9    *  notice, this list of conditions and the following disclaimer.
10   * 
11   * -Redistribution in binary form must reproduct the above copyright
12   *  notice, this list of conditions and the following disclaimer in
13   *  the documentation and/or other materials provided with the distribution.
14   * 
15   * Neither the name of Sun Microsystems, Inc. or the names of contributors
16   * may be used to endorse or promote products derived from this software
17   * without specific prior written permission.
18   * 
19   * This software is provided "AS IS," without a warranty of any kind. ALL
20   * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
21   * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
22   * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
23   * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
24   * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
25   * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
26   * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
27   * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
28   * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
29   * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
30   * 
31   * You acknowledge that Software is not designed, licensed or intended for
32   * use in the design, construction, operation or maintenance of any nuclear
33   * facility.
34   */
35  
36  /*
37   * @(#)ToolTipDemo.java 1.6 03/01/23
38   */
39  
40  
41  import javax.swing.*;
42  import javax.swing.event.*;
43  import javax.swing.text.*;
44  import javax.swing.border.*;
45  import javax.swing.colorchooser.*;
46  import javax.swing.filechooser.*;
47  import javax.accessibility.*;
48  
49  import java.awt.*;
50  import java.awt.event.*;
51  import java.beans.*;
52  import java.util.*;
53  import java.io.*;
54  import java.applet.*;
55  import java.net.*;
56  
57  /**
58   * ToolTip Demo
59   *
60   * @version 1.6 01/23/03
61   * @author Jeff Dinkins
62   */
63  public class ToolTipDemo extends DemoModule {
64  
65      /**
66       * main method allows us to run as a standalone demo.
67       */
68      public static void main(String[] args) {
69      ToolTipDemo demo = new ToolTipDemo(null);
70      demo.mainImpl();
71      }
72  
73      /**
74       * ToolTipDemo Constructor
75       */
76      public ToolTipDemo(SwingSet2 swingset) {
77      // Set the title for this demo, and an icon used to represent this
78      // demo inside the SwingSet2 app.
79      super(swingset, "ToolTipDemo", "toolbar/ToolTip.gif");
80  
81      // Set the layout manager.
82      JPanel p = getDemoPanel();
83      p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
84      p.setBackground(Color.white);
85  
86      // Create a Cow to put in the center of the panel.
87      Cow cow = new Cow();
88      cow.getAccessibleContext().setAccessibleName(getString("ToolTipDemo.accessible_cow"));
89  
90      // Set the tooltip text. Note, for fun, we also set more tooltip text
91      // descriptions for the cow down below in the Cow.contains() method.
92      cow.setToolTipText(getString("ToolTipDemo.cow"));
93  
94      // Add the cow midway down the panel
95      p.add(Box.createRigidArea(new Dimension(1, 150)));
96      p.add(cow);
97      }
98  
99  
100     class Cow extends JLabel {
101     Polygon cowgon = new Polygon();
102     
103     public Cow() {
104         super(createImageIcon("tooltip/cow.gif", getString("ToolTipDemo.bessie")));
105         setAlignmentX(CENTER_ALIGNMENT);
106 
107         // Set polygon points that define the outline of the cow.
108         cowgon.addPoint(3,20);    cowgon.addPoint(44,4);
109         cowgon.addPoint(79,15);   cowgon.addPoint(130,11);
110         cowgon.addPoint(252,5);   cowgon.addPoint(181,17);
111         cowgon.addPoint(301,45);  cowgon.addPoint(292,214);
112         cowgon.addPoint(269,209); cowgon.addPoint(266,142);
113         cowgon.addPoint(250,161); cowgon.addPoint(235,218);
114         cowgon.addPoint(203,206); cowgon.addPoint(215,137);
115         cowgon.addPoint(195,142); cowgon.addPoint(143,132);
116         cowgon.addPoint(133,189); cowgon.addPoint(160,200);
117         cowgon.addPoint(97,196);  cowgon.addPoint(107,182);
118         cowgon.addPoint(118,185); cowgon.addPoint(110,144);
119         cowgon.addPoint(59,77);   cowgon.addPoint(30,82);
120         cowgon.addPoint(30,35);   cowgon.addPoint(15,36);
121     }
122     
123     boolean moo = false;
124     boolean milk = false;
125     boolean tail = false;
126 
127     // Use the contains method to set the tooltip text depending
128     // on where the mouse is over the cow.
129     public boolean contains(int x, int y) {
130         if(!cowgon.contains(new Point(x, y))) {
131         return false;
132         }
133         
134         if((x > 30) && (x < 60) && (y > 60) && (y < 85)) {
135         if(!moo) {
136             setToolTipText("<html><center><font color=blue size=+2>" +
137                    getString("ToolTipDemo.moo") + "</font></center></html>");
138             moo = true;
139             milk = false;
140             tail = false;
141         }
142         } else if((x > 150) && (x < 260) && (y > 90) && (y < 145)) {
143         if(!milk) {
144             setToolTipText("<html><center><font face=AvantGarde size=+1 color=white>" +
145                    getString("ToolTipDemo.got_milk") + "</font></center></html>");
146             milk = true;
147             moo = false;
148             tail = false;
149         }
150         } else if((x > 280) && (x < 300) && (y > 20) && (y < 175)) {
151         if(!tail) {
152             setToolTipText("<html><em><b>" + getString("ToolTipDemo.tail") + "</b></em></html>");
153             tail = true;
154             moo = false;
155             milk = false;
156         }
157         } else if(moo || milk || tail) {
158         setToolTipText(getString("ToolTipDemo.tooltip_features"));
159         moo = false;
160         tail = false;
161         milk = false;
162         }
163 
164         return true;
165     }
166     }
167     
168 }
169