KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > uicapture > v1 > VirtualWindowUI


1 /*
2  * @(#)VirtualWindowUI.java
3  *
4  * Copyright (C) 2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.uicapture.v1;
28
29 import java.awt.Color JavaDoc;
30 import java.awt.Point JavaDoc;
31 import java.awt.Window JavaDoc;
32 import java.awt.Graphics JavaDoc;
33 import java.awt.Rectangle JavaDoc;
34 import java.awt.Image JavaDoc;
35 import java.awt.event.FocusListener JavaDoc;
36 import java.awt.event.FocusEvent JavaDoc;
37
38
39
40 /**
41  * This UI part of the VirtualWindow framework is in charge of intercepting
42  * all Window events related to drawing, in order to keep the invisible
43  * facade.
44  * <P>
45  * There is a problem with the transparency right now. I'm investigating
46  * a solution. The final (and unwanted) solution is to capture the background
47  * behind the Window, and display that on {@link #paint( Graphics )} calls.
48  * <P>
49  * WARNING: if the screen size is to resize, then this will not work correctly.
50  *
51  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
52  * @since Jan 4, 2002
53  * @version Mar 13, 2002
54  */

55 public class VirtualWindowUI extends Window JavaDoc implements Runnable JavaDoc, FocusListener JavaDoc
56 {
57     //-------------------------------------------------------------------------
58
// Private fields
59

60     /**
61      * @serial
62      */

63     private boolean enableGlass = false;
64     
65     // Thread-based variables
66
// Synchronization must be used with care!
67
private transient Thread JavaDoc runner = null;
68     private transient Object JavaDoc syncObj = new Object JavaDoc();
69     private transient boolean doRun = true;
70     
71     private transient Image JavaDoc background = null;
72     
73     
74     private static final Color JavaDoc INVISIBLE = new Color JavaDoc( 0, 0, 0, 0 );
75     private static final int SLEEP_LENGTH = 100;
76     
77     
78     
79     //-------------------------------------------------------------------------
80
// Constructors
81

82     
83     /**
84      * Creates a disabled window with no parent.
85      */

86     public VirtualWindowUI( Window JavaDoc owner )
87     {
88         super( owner );
89         
90         // setup Window variables
91
// setFocusableWindow( true );
92
setBackground( INVISIBLE );
93         setForeground( INVISIBLE );
94
95         // start the stay-focused thread
96
/*
97         this.runner = new Thread( this, "UI Capture always-focused thread" );
98         this.runner.setDaemon( true );
99         this.runner.setPriority(
100             (Thread.NORM_PRIORITY - Thread.MIN_PRIORITY) / 2 );
101         this.runner.start();
102         */

103         
104         // get the window registered in the UI.
105
setSize( 0, 0 );
106         super.show();
107         setGlassEnabled( false );
108     }
109     
110     
111     
112     
113
114
115     //-------------------------------------------------------------------------
116
// Public methods
117

118     
119     /**
120      * Sets the inner state for displaying the glass pane. If the pane is
121      * enabled, then the glass pane will attempt to maximize itself and
122      * keep itself on the foreground at all costs.
123      * <P>
124      * This should be the only method where the inner <tt>enableGlass</tt>
125      * field is manipulated. Any inner testing against this variable
126      * needs to be synchronized.
127      *
128      * @param on <tt>true</tt> if the glass pane is enabled (active and
129      * intercepting events), or <tt>false</tt> if is disabled.
130      */

131     public void setGlassEnabled( boolean on )
132     {
133         synchronized( this.syncObj )
134         {
135             this.enableGlass = on;
136         
137             if (on)
138             {
139                 this.syncObj.notifyAll();
140                 maximize();
141             }
142         }
143     }
144     
145     
146     /**
147      * Retrieves the current glass enabled state.
148      *
149      * @return <tt>true</tt> if the glass pane is enabled (active and
150      * intercepting events), or <tt>false</tt> if is disabled.
151      */

152     public boolean isGlassEnabled()
153     {
154         return this.enableGlass;
155     }
156     
157     
158     /**
159      * Enlarge the window to the size of the screen.
160      */

161     public void maximize()
162     {
163         synchronized( this.syncObj )
164         {
165             if (this.enableGlass)
166             {
167                 Rectangle JavaDoc rect = getCoveredScreen();
168                 setLocation( rect.getLocation() );
169                 setSize( rect.getSize() );
170             }
171         }
172     }
173     
174     
175     /**
176      * Retrieve the size of the covered window.
177      */

178     public Rectangle JavaDoc getCoveredScreen()
179     {
180         return new Rectangle JavaDoc(
181             new Point JavaDoc( 0, 0 ),
182             getToolkit().getScreenSize() );
183     }
184     
185     
186     /**
187      * Update the background image. This may set the background to
188      * <tt>null</tt>.
189      */

190     public void setBackground( Image JavaDoc img )
191     {
192         synchronized( this.syncObj )
193         {
194             this.background = img;
195         }
196     }
197     
198     
199     //-------------------------------------------------------------------------
200
// Inherited methods
201

202     
203     /**
204      *
205      */

206     public void show()
207     {
208         setGlassEnabled( true );
209         
210         super.show();
211         toFront();
212     }
213     
214     
215     /**
216      *
217      */

218     public void hide()
219     {
220         setGlassEnabled( false );
221         toBack();
222         super.hide();
223     }
224     
225     
226     /**
227      *
228      */

229     public void dispose()
230     {
231         if (this.runner != null)
232         {
233             this.doRun = false;
234             synchronized( this.syncObj )
235             {
236                 setGlassEnabled( false );
237                 this.syncObj.notifyAll();
238             }
239             
240             // wait for thread to die.
241
try
242             {
243                 this.runner.join( 1000 );
244                 this.runner = null;
245             }
246             catch (InterruptedException JavaDoc ie)
247             {
248                 // !!!
249
// ignore for now.
250
}
251             this.background = null;
252         }
253         
254         super.dispose();
255     }
256     
257     
258     /**
259      *
260      */

261     public void update( Graphics JavaDoc g )
262     {
263         // do nothing
264
}
265     
266     
267     /**
268      *
269      */

270     public void paint( Graphics JavaDoc g )
271     {
272         // Fill the background with the transparent color.
273
synchronized( this.syncObj )
274         {
275             if (this.background == null)
276             {
277                 // g.setColor( INVISIBLE );
278
g.fillRect( 0, 0, getWidth(), getHeight() );
279             }
280             else
281             {
282                 g.drawImage( this.background, 0, 0, this );
283             }
284         }
285     }
286     
287     
288     /**
289      *
290     public void repaint()
291     {
292         // do nothing
293     }
294      */

295     
296     
297     //-------------------------------------------------------------------------
298
// Event methods
299

300     
301     
302     /**
303      * Thread runner to keep the window in the front.
304      */

305     public void run()
306     {
307         while (this.doRun)
308         {
309             boolean doToFront = false;
310             try
311             {
312                 synchronized( this.syncObj )
313                 {
314                     if (this.enableGlass == false)
315                     {
316                         // pause
317
this.syncObj.wait();
318                     }
319                     else
320                     {
321                         doToFront = true;
322                     }
323                 }
324                 if (doToFront)
325                 {
326                     // send to the foreground
327
toFront();
328                     
329                     // sleep for a bit
330
Thread.sleep( SLEEP_LENGTH );
331                 }
332             }
333             catch (InterruptedException JavaDoc ie)
334             {
335                 // *must* be something important!
336
break;
337             }
338         }
339     }
340     
341     
342     /**
343      *
344      */

345     public void focusGained( FocusEvent JavaDoc fe )
346     {
347         // do nothing
348
}
349     
350     
351     /**
352      *
353      */

354     public void focusLost( FocusEvent JavaDoc fe )
355     {
356         // bring us back to the front.
357
synchronized( this.syncObj )
358         {
359             if (this.enableGlass)
360             {
361                 toFront();
362             }
363         }
364     }
365     
366     
367     
368     //-------------------------------------------------------------------------
369
// Protected methods
370

371     
372     
373     /**
374      * @exception Throwable thrown if the super throws anything.
375      */

376     protected void finalize() throws Throwable JavaDoc
377     {
378         if (this.runner != null)
379         {
380             dispose();
381         }
382         
383         super.finalize();
384     }
385     
386 }
387
388
Popular Tags