1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui.components;
21
22 import java.awt.Container;
23 import java.awt.Window;
24 import java.awt.event.MouseAdapter;
25 import java.awt.event.MouseEvent;
26 import java.awt.event.WindowAdapter;
27 import java.awt.event.WindowEvent;
28
29 import javax.swing.JComponent;
30 import javax.swing.JDialog;
31 import javax.swing.JFrame;
32 import javax.swing.JLabel;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public final class InterceptorPane extends JComponent
56 {
57 private static final long serialVersionUID = 1L;
58
59
60 protected Window window = null;
61
62
63 protected boolean ignoreOwned = false;
64
65
66 private ClickListener clickListener = null;
67
68
69 private FocusMonitor focusMonitor = null;
70
71 protected final class ClickListener extends MouseAdapter
72 {
73 private final void ignoreEvent(MouseEvent e)
74 {
75 e.consume();
76 setVisible(false);
77 }
78
79
80
81
82
83 public final void mouseClicked(MouseEvent e)
84 {
85 ignoreEvent(e);
86 }
87
88
89
90
91
92 public final void mousePressed(MouseEvent e)
93 {
94 ignoreEvent(e);
95 }
96 }
97
98
99
100
101
102
103
104
105
106 protected final class FocusMonitor extends WindowAdapter
107 {
108
109
110
111
112
113
114
115 public final void windowDeactivated(WindowEvent e)
116 {
117 if (ignoreOwned
118 && (e.getOppositeWindow().getOwner() == getWindow())) {
119 return;
120 }
121
122 setSize(getWindow().getSize());
123 setVisible(true);
124 }
125
126
127
128
129
130
131
132
133 public final void windowClosing(WindowEvent e)
134 {
135 uninstall();
136 }
137 }
138
139
140
141
142
143
144
145 public InterceptorPane(Container container)
146 {
147 this(container, false);
148 }
149
150
151
152
153
154
155
156
157 public InterceptorPane(Container container, boolean ignoreOwnedWindows)
158 {
159 super();
160 initialize(container);
161 ignoreOwned = ignoreOwnedWindows;
162 }
163
164
165
166
167
168
169
170
171
172
173 private void initialize(Container container)
174 {
175 boolean initialized = false;
176
177 if (container instanceof JFrame) {
178 window = (JFrame)container;
179 ((JFrame)window).setGlassPane(this);
180
181 initialized = true;
182 }
183
184 if (container instanceof JDialog) {
185 window = (JDialog)container;
186 ((JDialog)window).setGlassPane(this);
187
188 initialized = true;
189 }
190
191 if (!initialized) {
192 throw new UnsupportedOperationException("Window class "
193 + container.getClass().getName()
194 + " does not support glass panes.");
195 }
196
197 clickListener = new ClickListener();
198 addMouseListener(clickListener);
199
200
201
202 focusMonitor = new FocusMonitor();
203 window.addWindowListener(focusMonitor);
204 }
205
206
207
208
209
210
211 public void uninstall()
212 {
213 if (window == null) {
214 return;
215 }
216
217 window.removeMouseListener(clickListener);
218
219 window.removeWindowListener(focusMonitor);
220
221 if (window instanceof JFrame) {
222 ((JFrame)window).setGlassPane(new JLabel());
223 }
224
225 if (window instanceof JDialog) {
226 ((JDialog)window).setGlassPane(new JLabel());
227 }
228
229 window = null;
230 clickListener = null;
231 focusMonitor = null;
232 }
233
234
235
236
237
238
239 public Window getWindow()
240 {
241 return window;
242 }
243 }
244
245