KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > planetamessenger > ui > JPopupWindow


1 /*
2     =========================================================================
3
4     This module is developed and maintained by PlanetaMessenger.org.
5     Specs, New and updated versions can be found in
6     http://www.planetamessenger.org
7     If you want contact the Team please send a email to Project Manager
8     Leidson Campos Alves Ferreira at leidson@planetamessenger.org
9
10     Copyright (C) since 2001 by PlanetaMessenger.org
11     
12     This library is free software; you can redistribute it and/or
13     modify it under the terms of the GNU Lesser General Public
14     License as published by the Free Software Foundation; either
15     version 2.1 of the License, or (at your option) any later version.
16
17     This library is distributed in the hope that it will be useful,
18     but WITHOUT ANY WARRANTY; without even the implied warranty of
19     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20     Lesser General Public License for more details.
21
22     You should have received a copy of the GNU Lesser General Public
23     License along with this library; if not, write to the Free Software
24     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
26     =========================================================================
27 */

28 /**
29  *
30  * $Id: JPopupWindow.java,v 1.6 2007/02/11 13:52:37 popolony2k Exp $
31  * $Author: popolony2k $
32  * $Name: $
33  * $Revision: 1.6 $
34  * $State: Exp $
35  *
36  */

37
38 package org.planetamessenger.ui;
39
40 import java.awt.*;
41 import javax.swing.*;
42 import javax.swing.border.*;
43 import java.util.*;
44
45
46 public class JPopupWindow extends javax.swing.JWindow JavaDoc implements Runnable JavaDoc {
47
48   static private int ANIMATE_SPEED = 5;
49   static private int WINDOW_INSET = 10;
50   
51   static public final int POSITION_RIGHT_BOTTOM = 0;
52   static public final int POSITION_RIGHT_TOP = 1;
53   static public final int POSITION_LEFT_TOP = 2;
54   static public final int POSITION_LEFT_BOTTOM = 3;
55
56   static private int SHOW_DEFAULT_TIMEOUT = 2000;
57   static public final int SHOW_INFINITE = -1;
58
59   private int nShowTimeout;
60   private int nAnimateSpeed;
61   private int nPopupPosition;
62   private boolean bAsyncMode;
63   private HashMap<JPopupWindowEventListener, JPopupWindowEventListener> listeners;
64
65   
66   /**
67    * Constructor. Initialize all class data.<br>
68    */

69   public JPopupWindow() {
70     super();
71     init();
72   }
73   
74   /**
75    * Constructor. Initialize all class data.<br>
76    * @param parent A parent window for JPopupWindow;
77    */

78   public JPopupWindow( Window parent ) {
79     super( parent );
80     init();
81   }
82   
83   /**
84    * Add a JPopupWindowListener event listener to this<br>
85    * window class.<br>
86    * @param listener The new listener to add;<br>
87    */

88   public void addPopupWindowEventListener( JPopupWindowEventListener listener ) {
89
90     listeners.put( listener, listener );
91   }
92
93   /**
94    * Remove a JPopupWindowListener event listener from this<br>
95    * window class.<br>
96    * @param listener The new listener to remove;<br>
97    */

98   public void removePopupWindowEventListener( JPopupWindowEventListener listener ) {
99
100     listeners.remove( listener );
101   }
102
103   /**
104    * Set the show timeout for dialog.<br>
105    * @param nShowTimeout The Timeout to dialog wait after complete show
106    * before close.
107    * SHOW_INFINITE wait until hidePopup() function is called. This option
108    * is ignored if Operation Mode is synchronous @see setOperationMode();
109    */

110   public void setShowTimeout( int nShowTimeout ) throws Exception JavaDoc {
111    
112     if( !bAsyncMode && ( nShowTimeout == SHOW_INFINITE ) )
113       throw new Exception JavaDoc( "JPopupWindow.setShowTimeout() - SHOW_INFINITE not allowed for synchronous mode" );
114
115     this.nShowTimeout = nShowTimeout;
116   }
117   
118   /**
119    * Get the show timeout for dialog.<br>
120    */

121   public int getShowTimeout() {
122
123     return nShowTimeout;
124   }
125   
126   /**
127    * Set the speed for animating dialog;<br>
128    * @param nAnimateSpeed The new speed for animating dialog;<br>
129    */

130   public void setAnimateSpeed( int nAnimateSpeed ) {
131     
132     this.nAnimateSpeed = nAnimateSpeed;
133   }
134   
135   /**
136    * Return the speed for animating dialog;<br>
137    */

138   public int getAnimateSpeed() {
139    
140     return nAnimateSpeed;
141   }
142   
143   /**
144    * Set the popup position.<br>
145    * @param nPopupPosition The Popup position constant.<br>
146    * Possible values:<br>
147    * POSITION_USER_DEFINED - User defined position provided by setLocation method;<br>
148    * POSITION_RIGHT_BOTTOM - Popup placed in Right bottom corner;<br>
149    * POSITION_RIGHT_TOP - Popup placed in Right top corner;<br>
150    * POSITION_LEFT_TOP - Popup placed in left top corner;<br>;
151    * POSITION_LEFT_BOTTOM - Popup placed in left bottom corner;<br>;
152    * POSITION_CENTER - Popup paced centered in desktop;<br>
153    */

154   public void setPopupPosition( int nPopupPosition ) {
155     
156     this.nPopupPosition = nPopupPosition;
157   }
158
159   /**
160    * Return the popup position.
161    */

162   public int getPopupPosition() {
163    
164     return nPopupPosition;
165   }
166   
167   /**
168    * Set the operation mode for showPopup method.<br>
169    * @param bAsyncMode The new operation mode for showPopup method;<br>
170    * The possible values are:<br>
171    * true Asynchronous mode - This mode doesn't stop caller thread for popup close;<br>
172    * false Synchronous mode - This mode stop caller thread until popup close;<br>
173    */

174   public void setOperationMode( boolean bAsyncMode ) throws Exception JavaDoc {
175    
176     if( !bAsyncMode && ( nShowTimeout == SHOW_INFINITE ) )
177       throw new Exception JavaDoc( "JPopupWindow.setOperationMode() - SHOW_INFINITE not allowed for synchronous mode" );
178
179     this.bAsyncMode = bAsyncMode;
180   }
181   
182   /**
183    * Get the operation mode for showPopup method;<br>
184    */

185   public boolean getOperationMode() {
186    
187     return bAsyncMode;
188   }
189   
190   /**
191    * Set the window border object.<br>
192    * @param border The new border for window object;<br>
193    */

194   public void setBorder( Border border ) {
195     
196     ( ( JComponent ) getContentPane() ).setBorder( border );
197   }
198   
199   /**
200    * Get the window border object.<br>
201    */

202   public Border getBorder() {
203
204     return ( ( JComponent ) getContentPane() ).getBorder();
205   }
206
207   /**
208    * Show the popup message dialog, animating window.<br>
209    */

210   public synchronized void showPopup() {
211
212     Thread JavaDoc popupThread = new Thread JavaDoc( this );
213
214     popupThread.start();
215     
216     if( !bAsyncMode && ( nShowTimeout != SHOW_INFINITE ) )
217       try {
218         wait();
219       } catch( InterruptedException JavaDoc e ) {
220         System.err.println( "JPopupWindow.showPopup() - " + e );
221       }
222   }
223   
224   /**
225    * Hide the popup message dialog, animating window.<br>
226    */

227   public synchronized void hidePopup() throws Exception JavaDoc {
228
229     if( bAsyncMode && ( nShowTimeout == SHOW_INFINITE ) )
230       notify();
231     else
232       throw new Exception JavaDoc( "JPopupWindow.hidePopup() - hidePopup not allowed for synchronous mode and when Timeout differs of SHOW_INFINITE" );
233   }
234
235   /**
236    * Implements the run method of Runnable interface.
237    */

238   public synchronized void run() {
239
240     JPanel contentPane = ( JPanel ) getContentPane();
241     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
242     Dimension dmSize = getSize();
243     Dimension dmPrefSize = new Dimension( dmSize );
244     Insets insets;
245     Point ptPosition;
246     int nSignal;
247
248     setLocation( screenSize.width, screenSize.height + dmSize.height );
249
250     // Set the contentPane preferred size
251
insets = contentPane.getBorder().getBorderInsets( contentPane );
252     dmPrefSize.height-=( insets.top + insets.bottom );
253     contentPane.setPreferredSize( dmPrefSize );
254
255     insets = getInsets();
256     pack();
257     
258     //On Winblows, without alwaysOnTop, the taskbar ends up on top of the message (especially if the taskbar is taller than the default 1 row)
259
setAlwaysOnTop( true );
260     
261     setVisible( true );
262
263     // Set the initial dialog position
264
switch( nPopupPosition ) {
265
266       case POSITION_RIGHT_TOP : setLocation( ( screenSize.width - ( dmSize.width + ( ( insets.left + insets.right ) * 4 ) ) ), -( dmSize.height - WINDOW_INSET ) ); break;
267       
268       case POSITION_LEFT_TOP : setLocation( insets.left + insets.right, -( dmSize.height - WINDOW_INSET ) ); break;
269       
270       case POSITION_LEFT_BOTTOM : setLocation( ( insets.left + insets.right ), screenSize.height - ( insets.top + insets.bottom ) - WINDOW_INSET ); break;
271
272       case POSITION_RIGHT_BOTTOM :
273
274       default: setLocation( ( screenSize.width - ( dmSize.width + ( ( insets.left + insets.right ) * 4 ) ) ), screenSize.height - ( insets.top + insets.bottom ) - WINDOW_INSET );
275     }
276     
277     ptPosition = getLocation();
278     nSignal = ( ( nPopupPosition == POSITION_RIGHT_TOP ) || ( nPopupPosition == POSITION_LEFT_TOP ) ? 1 : -1 );
279     
280     // Open the popup
281
for( int nCount = 0; nCount < dmSize.height; nCount+=nAnimateSpeed, ptPosition.y+=( nSignal * nAnimateSpeed ) ) {
282       setLocation( ptPosition.x, ptPosition.y );
283       
284       try {
285         wait( nAnimateSpeed * 2 );
286       } catch( InterruptedException JavaDoc e ) {
287         System.err.println( "JPopupWindow.run() - " + e );
288       }
289     }
290
291     fireOnShow();
292     
293     if( bAsyncMode && ( nShowTimeout == SHOW_INFINITE ) ) {
294       try {
295         wait();
296       } catch( InterruptedException JavaDoc e ) {
297         System.err.println( "JPopupWindow.run() - " + e );
298       }
299     }
300     else {
301       // Wait for time to show dialog
302
try {
303         Thread.sleep( nShowTimeout );
304       } catch( InterruptedException JavaDoc e ) {
305         System.err.println( "JMOSPopupWindow.run() - " + e );
306       }
307     }
308
309     nSignal = ( ( nPopupPosition == POSITION_RIGHT_TOP ) || ( nPopupPosition == POSITION_LEFT_TOP ) ? -1 : 1 );
310
311     // Close the popup
312
for( int nCount = dmSize.height; nCount > 0; nCount-=nAnimateSpeed, ptPosition.y+=( nSignal * nAnimateSpeed ) ) {
313       setLocation( ptPosition.x, ptPosition.y );
314       
315       try {
316         wait( nAnimateSpeed * 2 );
317       } catch( InterruptedException JavaDoc e ) {
318         System.err.println( "JPopupWindow.run() - " + e );
319       }
320     }
321
322     setAlwaysOnTop( false );
323     setVisible( false );
324     dispose();
325     
326     fireOnHide();
327
328     if( !bAsyncMode )
329       notify();
330   }
331
332   /**
333    * Override the setContentPane method to avoid new content Pane setting by<br>
334    * objects and future childs.<br>
335    * @param contentPane The content pane to set;<br>
336    */

337   public final void setContentPane( Container contentPane ) {
338   
339     // Do nothing
340
}
341   
342   // Private declarations
343
/**
344    * Fire the onShow event for all registered listeners.<br>
345    */

346   private void fireOnShow() {
347
348     HashMap<JPopupWindowEventListener, JPopupWindowEventListener> clone = ( HashMap<JPopupWindowEventListener, JPopupWindowEventListener> ) listeners.clone();
349     Iterator<JPopupWindowEventListener> itItems = clone.values().iterator();
350
351     while( itItems.hasNext() )
352       itItems.next().onShow( this );
353   }
354   
355   /**
356    * Fire the onHide event for all registered listeners.<br>
357    */

358   private void fireOnHide() {
359     
360     HashMap<JPopupWindowEventListener, JPopupWindowEventListener> clone = ( HashMap<JPopupWindowEventListener, JPopupWindowEventListener> ) listeners.clone();
361     Iterator<JPopupWindowEventListener> itItems = clone.values().iterator();
362
363     while( itItems.hasNext() )
364       itItems.next().onHide( this );
365   }
366
367   /**
368    * Initialize all class data.
369    */

370   private void init() {
371
372     JPanel contentPane = ( JPanel ) getContentPane();
373     Border border = new javax.swing.border.EtchedBorder JavaDoc( EtchedBorder.RAISED );
374
375     setVisible( false );
376     contentPane.setBorder( border );
377     
378     nShowTimeout = SHOW_DEFAULT_TIMEOUT;
379     nAnimateSpeed = ANIMATE_SPEED;
380     nPopupPosition = POSITION_RIGHT_BOTTOM;
381     bAsyncMode = true;
382     listeners = new HashMap<JPopupWindowEventListener, JPopupWindowEventListener>();
383   }
384 }
385
386 // JMOSPopupWindow class
387
Popular Tags