KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > autoupdate > 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.modules.autoupdate;
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 // Copied from org.netbeans.core.FlashingIcon
34
/**
35  *
36  * A flashing icon to provide visual feedback for the user when something
37  * not very important happens in the system.
38  * The icon is flashed for a few seconds and then remains visible for a while longer.
39  *
40  * @author saubrecht
41  */

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

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

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

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

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

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

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

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