1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
package net.wotonomy.ui.swing.util; |
20 |
|
|
21 |
|
import java.awt.Component; |
22 |
|
import java.awt.Container; |
23 |
|
import java.awt.Image; |
24 |
|
import java.awt.Rectangle; |
25 |
|
import java.awt.Toolkit; |
26 |
|
import java.awt.event.ActionEvent; |
27 |
|
import java.awt.event.ActionListener; |
28 |
|
import java.net.URL; |
29 |
|
|
30 |
|
import javax.swing.ImageIcon; |
31 |
|
import javax.swing.JComponent; |
32 |
|
import javax.swing.JLabel; |
33 |
|
import javax.swing.JPanel; |
34 |
|
import javax.swing.JRootPane; |
35 |
|
import javax.swing.SwingUtilities; |
36 |
|
import javax.swing.Timer; |
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
public class ComponentHighlighter implements ActionListener |
47 |
|
{ |
48 |
|
|
49 |
|
JRootPane rootPane; |
50 |
|
JComponent component; |
51 |
|
Component oldGlassPane; |
52 |
|
JLabel imageLabel; |
53 |
|
Timer timer; |
54 |
|
JPanel glassPane; |
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
0 |
public ComponentHighlighter( JComponent aComponent, URL aURL ) |
62 |
0 |
{ |
63 |
0 |
if ( aURL == null ) return; |
64 |
0 |
init( aComponent, Toolkit.getDefaultToolkit().getImage( aURL ) ); |
65 |
0 |
} |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
0 |
public ComponentHighlighter( JComponent aComponent, Image anImage ) |
74 |
0 |
{ |
75 |
0 |
init( aComponent, anImage ); |
76 |
0 |
} |
77 |
|
|
78 |
|
protected void init( JComponent aComponent, Image anImage ) |
79 |
|
{ |
80 |
0 |
if ( ( aComponent == null ) || ( anImage == null ) ) return; |
81 |
|
|
82 |
0 |
component = aComponent; |
83 |
0 |
rootPane = SwingUtilities.getRootPane( component ); |
84 |
0 |
oldGlassPane = rootPane.getGlassPane(); |
85 |
|
|
86 |
0 |
glassPane = new JPanel(); |
87 |
0 |
rootPane.setGlassPane( glassPane ); |
88 |
0 |
glassPane.setVisible( true ); |
89 |
0 |
glassPane.setOpaque( false ); |
90 |
0 |
glassPane.setLayout( null ); |
91 |
|
|
92 |
0 |
ImageIcon icon = new ImageIcon( anImage ); |
93 |
|
|
94 |
0 |
imageLabel = new JLabel(); |
95 |
0 |
imageLabel.setIconTextGap( 0 ); |
96 |
0 |
imageLabel.setIcon( icon ); |
97 |
0 |
imageLabel.setSize( icon.getIconWidth(), icon.getIconHeight() ); |
98 |
0 |
glassPane.add( imageLabel ); |
99 |
|
|
100 |
0 |
Rectangle bounds = component.getBounds(); |
101 |
0 |
if ( component.getParent() instanceof Component ) |
102 |
|
{ |
103 |
0 |
bounds = SwingUtilities.convertRectangle( (Container) component.getParent(), |
104 |
0 |
bounds, rootPane.getContentPane() ); |
105 |
|
} |
106 |
0 |
imageLabel.setLocation( |
107 |
0 |
bounds.x, bounds.y + bounds.height - imageLabel.getBounds().height ); |
108 |
|
|
109 |
0 |
glassPane.revalidate(); |
110 |
0 |
glassPane.repaint(); |
111 |
|
|
112 |
0 |
component.transferFocus(); |
113 |
|
|
114 |
0 |
timer = new Timer( 80, this ); |
115 |
0 |
timer.setRepeats( true ); |
116 |
0 |
timer.start(); |
117 |
0 |
} |
118 |
|
|
119 |
|
public void actionPerformed( ActionEvent evt ) |
120 |
|
{ |
121 |
0 |
Rectangle bounds = imageLabel.getBounds(); |
122 |
0 |
Rectangle target = component.getBounds(); |
123 |
0 |
if ( component.getParent() instanceof Component ) |
124 |
|
{ |
125 |
0 |
target = SwingUtilities.convertRectangle( (Container) component.getParent(), |
126 |
0 |
target, rootPane.getContentPane() ); |
127 |
|
} |
128 |
|
|
129 |
0 |
if ( bounds.x + bounds.width > target.x + target.width ) |
130 |
|
{ |
131 |
0 |
timer.stop(); |
132 |
0 |
rootPane.setGlassPane( oldGlassPane ); |
133 |
0 |
component.requestFocus(); |
134 |
0 |
return; |
135 |
|
} |
136 |
|
|
137 |
|
|
138 |
0 |
imageLabel.setLocation( |
139 |
0 |
bounds.x + Math.max( bounds.width / 12, 1 ), bounds.y ); |
140 |
0 |
imageLabel.repaint(); |
141 |
0 |
} |
142 |
|
} |
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
|