1 28 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 implements Runnable { 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 69 public JPopupWindow() { 70 super(); 71 init(); 72 } 73 74 78 public JPopupWindow( Window parent ) { 79 super( parent ); 80 init(); 81 } 82 83 88 public void addPopupWindowEventListener( JPopupWindowEventListener listener ) { 89 90 listeners.put( listener, listener ); 91 } 92 93 98 public void removePopupWindowEventListener( JPopupWindowEventListener listener ) { 99 100 listeners.remove( listener ); 101 } 102 103 110 public void setShowTimeout( int nShowTimeout ) throws Exception { 111 112 if( !bAsyncMode && ( nShowTimeout == SHOW_INFINITE ) ) 113 throw new Exception ( "JPopupWindow.setShowTimeout() - SHOW_INFINITE not allowed for synchronous mode" ); 114 115 this.nShowTimeout = nShowTimeout; 116 } 117 118 121 public int getShowTimeout() { 122 123 return nShowTimeout; 124 } 125 126 130 public void setAnimateSpeed( int nAnimateSpeed ) { 131 132 this.nAnimateSpeed = nAnimateSpeed; 133 } 134 135 138 public int getAnimateSpeed() { 139 140 return nAnimateSpeed; 141 } 142 143 154 public void setPopupPosition( int nPopupPosition ) { 155 156 this.nPopupPosition = nPopupPosition; 157 } 158 159 162 public int getPopupPosition() { 163 164 return nPopupPosition; 165 } 166 167 174 public void setOperationMode( boolean bAsyncMode ) throws Exception { 175 176 if( !bAsyncMode && ( nShowTimeout == SHOW_INFINITE ) ) 177 throw new Exception ( "JPopupWindow.setOperationMode() - SHOW_INFINITE not allowed for synchronous mode" ); 178 179 this.bAsyncMode = bAsyncMode; 180 } 181 182 185 public boolean getOperationMode() { 186 187 return bAsyncMode; 188 } 189 190 194 public void setBorder( Border border ) { 195 196 ( ( JComponent ) getContentPane() ).setBorder( border ); 197 } 198 199 202 public Border getBorder() { 203 204 return ( ( JComponent ) getContentPane() ).getBorder(); 205 } 206 207 210 public synchronized void showPopup() { 211 212 Thread popupThread = new Thread ( this ); 213 214 popupThread.start(); 215 216 if( !bAsyncMode && ( nShowTimeout != SHOW_INFINITE ) ) 217 try { 218 wait(); 219 } catch( InterruptedException e ) { 220 System.err.println( "JPopupWindow.showPopup() - " + e ); 221 } 222 } 223 224 227 public synchronized void hidePopup() throws Exception { 228 229 if( bAsyncMode && ( nShowTimeout == SHOW_INFINITE ) ) 230 notify(); 231 else 232 throw new Exception ( "JPopupWindow.hidePopup() - hidePopup not allowed for synchronous mode and when Timeout differs of SHOW_INFINITE" ); 233 } 234 235 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 insets = contentPane.getBorder().getBorderInsets( contentPane ); 252 dmPrefSize.height-=( insets.top + insets.bottom ); 253 contentPane.setPreferredSize( dmPrefSize ); 254 255 insets = getInsets(); 256 pack(); 257 258 setAlwaysOnTop( true ); 260 261 setVisible( true ); 262 263 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 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 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 e ) { 297 System.err.println( "JPopupWindow.run() - " + e ); 298 } 299 } 300 else { 301 try { 303 Thread.sleep( nShowTimeout ); 304 } catch( InterruptedException 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 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 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 337 public final void setContentPane( Container contentPane ) { 338 339 } 341 342 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 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 370 private void init() { 371 372 JPanel contentPane = ( JPanel ) getContentPane(); 373 Border border = new javax.swing.border.EtchedBorder ( 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 | Popular Tags |