KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > FlashingIcon


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core;
21
22 import java.awt.Cursor JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.Point JavaDoc;
25 import java.awt.event.MouseEvent JavaDoc;
26 import java.awt.event.MouseListener JavaDoc;
27 import javax.swing.Icon JavaDoc;
28 import javax.swing.JComponent JavaDoc;
29 import javax.swing.JToolTip JavaDoc;
30 import org.openide.util.RequestProcessor;
31 import org.openide.util.RequestProcessor.Task;
32
33 /**
34  * A flashing icon to provide visual feedback for the user when something
35  * not very important happens in the system.
36  * The icon is flashed for a few seconds and then remains visible for a while longer.
37  *
38  * @author saubrecht
39  */

40 abstract class FlashingIcon extends JComponent JavaDoc implements MouseListener JavaDoc {
41     
42     private static final long STOP_FLASHING_DELAY = 5 * 1000;
43     private static final long DISAPPEAR_DELAY_MILLIS = STOP_FLASHING_DELAY + 30 * 1000;
44     
45     private Icon JavaDoc icon;
46     
47     private boolean keepRunning = false;
48     private boolean isIconVisible = false;
49     private boolean keepFlashing = true;
50     private long startTime = 0;
51     private RequestProcessor rp;
52     private Task timerTask;
53     
54     /**
55      * Creates a new instance of FlashingIcon
56      *
57      * @param icon The icon that will be flashing (blinking)
58      */

59     protected FlashingIcon( Icon JavaDoc icon ) {
60         this.icon = icon;
61         Dimension JavaDoc d = new Dimension JavaDoc( icon.getIconWidth(), icon.getIconHeight() );
62         setMinimumSize( d );
63         setMaximumSize( d );
64         setPreferredSize( d );
65         
66         addMouseListener( this );
67         rp = new RequestProcessor( "Exception Notification Icon" ); //NOI18N
68
}
69
70     /**
71      * Start flashing of the icon. If the icon is already flashing, the timer
72      * is reset.
73      * If the icon is visible but not flashing, it starts flashing again
74      * and the disappear timer is reset.
75      */

76     public void startFlashing() {
77         synchronized( this ) {
78             startTime = System.currentTimeMillis();
79             isIconVisible = !isIconVisible;
80             keepRunning = true;
81             keepFlashing = true;
82             if( null == timerTask ) {
83                 timerTask = rp.create( new Timer() );
84             }
85             timerTask.run();
86         }
87         repaint();
88     }
89     
90     /**
91      * Stop the flashing and hide the icon.
92      */

93     public void disappear() {
94         synchronized( this ) {
95             keepRunning = false;
96             isIconVisible = false;
97             keepFlashing = false;
98             if( null != timerTask )
99                 timerTask.cancel();
100             timerTask = null;
101             setToolTipText( null );
102         }
103         repaint();
104     }
105     
106     /**
107      * Stop flashing of the icon. The icon remains visible and active (listens
108      * for mouse clicks and displays tooltip) until the disappear timer expires.
109      */

110     public void stopFlashing() {
111         synchronized( this ) {
112             if( keepRunning && !isIconVisible ) {
113                 isIconVisible = true;
114                 repaint();
115             }
116         }
117         keepFlashing = false;
118     }
119     
120     /**
121      * Switch the current image and repaint
122      */

123     protected void flashIcon() {
124         isIconVisible = !isIconVisible;
125         
126         repaint();
127     }
128
129     public void paint(java.awt.Graphics JavaDoc g) {
130         if( isIconVisible ) {
131             icon.paintIcon( this, g, 0, 0 );
132         }
133     }
134
135     public void mouseReleased(MouseEvent JavaDoc e) {}
136
137     public void mousePressed(MouseEvent JavaDoc e) {
138         stopFlashing();
139     }
140
141     public void mouseExited(MouseEvent JavaDoc e) {
142         stopFlashing();
143     }
144
145     public void mouseEntered(MouseEvent JavaDoc e) {
146         stopFlashing();
147     }
148
149     public void mouseClicked(MouseEvent JavaDoc e) {
150         if( isIconVisible ) {
151             disappear();
152             onMouseClick();
153         }
154     }
155     
156     /**
157      * Invoked when the user clicks the icon.
158      */

159     protected abstract void onMouseClick();
160
161     /**
162      * Invoked when the disappear timer expired.
163      */

164     protected abstract void timeout();
165
166     public Cursor JavaDoc getCursor() {
167
168         if( isIconVisible ) {
169             return Cursor.getPredefinedCursor( Cursor.HAND_CURSOR );
170         }
171         return Cursor.getDefaultCursor();
172     }
173
174     public Point JavaDoc getToolTipLocation( MouseEvent JavaDoc event ) {
175
176         JToolTip JavaDoc tip = createToolTip();
177         tip.setTipText( getToolTipText() );
178         Dimension JavaDoc d = tip.getPreferredSize();
179         
180         
181         return new Point JavaDoc( getWidth()-d.width, -d.height );
182     }
183     
184     private class Timer implements Runnable JavaDoc {
185         public void run() {
186             synchronized( FlashingIcon.this ) {
187                 try {
188                     long currentTime = System.currentTimeMillis();
189                     if( keepFlashing ) {
190                         if( currentTime - startTime < STOP_FLASHING_DELAY ) {
191                             flashIcon();
192                         } else {
193                             stopFlashing();
194                         }
195                     }
196                     if( currentTime - startTime >= DISAPPEAR_DELAY_MILLIS ) {
197                         disappear();
198                         timeout();
199                     } else {
200                         if( null != timerTask )
201                             timerTask.schedule( 500 );
202                     }
203                 } catch( Throwable JavaDoc e ) {
204                     //swallow all exceptions to avoid endless exception <-> notification loop
205
e.printStackTrace();
206                 }
207             }
208         }
209     }
210 }
211
Popular Tags