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