001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.core;
043:
044: import java.awt.Cursor;
045: import java.awt.Dimension;
046: import java.awt.Point;
047: import java.awt.event.MouseEvent;
048: import java.awt.event.MouseListener;
049: import javax.swing.Icon;
050: import javax.swing.JComponent;
051: import javax.swing.JToolTip;
052: import org.openide.util.RequestProcessor;
053: import org.openide.util.RequestProcessor.Task;
054:
055: /**
056: * A flashing icon to provide visual feedback for the user when something
057: * not very important happens in the system.
058: * The icon is flashed for a few seconds and then remains visible for a while longer.
059: *
060: * @author saubrecht
061: */
062: abstract class FlashingIcon extends JComponent implements MouseListener {
063:
064: private static final long STOP_FLASHING_DELAY = 5 * 1000;
065: private static final long DISAPPEAR_DELAY_MILLIS = STOP_FLASHING_DELAY + 30 * 1000;
066:
067: private Icon icon;
068:
069: private boolean keepRunning = false;
070: private boolean isIconVisible = false;
071: private boolean keepFlashing = true;
072: private long startTime = 0;
073: private RequestProcessor rp;
074: private Task timerTask;
075:
076: /**
077: * Creates a new instance of FlashingIcon
078: *
079: * @param icon The icon that will be flashing (blinking)
080: */
081: protected FlashingIcon(Icon icon) {
082: this .icon = icon;
083: Dimension d = new Dimension(icon.getIconWidth(), icon
084: .getIconHeight());
085: setMinimumSize(d);
086: setMaximumSize(d);
087: setPreferredSize(d);
088: setVisible(false);
089:
090: addMouseListener(this );
091: rp = new RequestProcessor("Exception Notification Icon"); //NOI18N
092: }
093:
094: /**
095: * Start flashing of the icon. If the icon is already flashing, the timer
096: * is reset.
097: * If the icon is visible but not flashing, it starts flashing again
098: * and the disappear timer is reset.
099: */
100: public void startFlashing() {
101: synchronized (this ) {
102: startTime = System.currentTimeMillis();
103: isIconVisible = !isIconVisible;
104: keepRunning = true;
105: keepFlashing = true;
106: if (null == timerTask) {
107: timerTask = rp.create(new Timer());
108: }
109: timerTask.run();
110: this .setVisible(true);
111: }
112: repaint();
113: }
114:
115: /**
116: * Stop the flashing and hide the icon.
117: */
118: public void disappear() {
119: synchronized (this ) {
120: keepRunning = false;
121: isIconVisible = false;
122: keepFlashing = false;
123: if (null != timerTask)
124: timerTask.cancel();
125: timerTask = null;
126: setToolTipText(null);
127: this .setVisible(false);
128: }
129: repaint();
130: }
131:
132: /**
133: * Stop flashing of the icon. The icon remains visible and active (listens
134: * for mouse clicks and displays tooltip) until the disappear timer expires.
135: */
136: public void stopFlashing() {
137: synchronized (this ) {
138: if (keepRunning && !isIconVisible) {
139: isIconVisible = true;
140: repaint();
141: }
142: }
143: keepFlashing = false;
144: }
145:
146: /**
147: * Switch the current image and repaint
148: */
149: protected void flashIcon() {
150: isIconVisible = !isIconVisible;
151:
152: repaint();
153: }
154:
155: @Override
156: public void paint(java.awt.Graphics g) {
157: if (isIconVisible) {
158: icon.paintIcon(this , g, 0, 0);
159: }
160: }
161:
162: public void mouseReleased(MouseEvent e) {
163: }
164:
165: public void mousePressed(MouseEvent e) {
166: stopFlashing();
167: }
168:
169: public void mouseExited(MouseEvent e) {
170: stopFlashing();
171: }
172:
173: public void mouseEntered(MouseEvent e) {
174: stopFlashing();
175: }
176:
177: public void mouseClicked(MouseEvent e) {
178: if (isIconVisible) {
179: disappear();
180: onMouseClick();
181: }
182: }
183:
184: /**
185: * Invoked when the user clicks the icon.
186: */
187: protected abstract void onMouseClick();
188:
189: /**
190: * Invoked when the disappear timer expired.
191: */
192: protected abstract void timeout();
193:
194: @Override
195: public Cursor getCursor() {
196:
197: if (isIconVisible) {
198: return Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
199: }
200: return Cursor.getDefaultCursor();
201: }
202:
203: @Override
204: public Point getToolTipLocation(MouseEvent event) {
205:
206: JToolTip tip = createToolTip();
207: tip.setTipText(getToolTipText());
208: Dimension d = tip.getPreferredSize();
209:
210: return new Point(getWidth() - d.width, -d.height);
211: }
212:
213: private class Timer implements Runnable {
214: public void run() {
215: synchronized (FlashingIcon.this ) {
216: try {
217: long currentTime = System.currentTimeMillis();
218: if (keepFlashing) {
219: if (currentTime - startTime < STOP_FLASHING_DELAY) {
220: flashIcon();
221: } else {
222: stopFlashing();
223: }
224: }
225: if (currentTime - startTime >= DISAPPEAR_DELAY_MILLIS) {
226: disappear();
227: timeout();
228: } else {
229: if (null != timerTask)
230: timerTask.schedule(500);
231: }
232: } catch (Throwable e) {
233: //swallow all exceptions to avoid endless exception <-> notification loop
234: e.printStackTrace();
235: }
236: }
237: }
238: }
239: }
|