1 19 20 package org.netbeans.core; 21 22 import java.awt.Cursor ; 23 import java.awt.Dimension ; 24 import java.awt.Point ; 25 import java.awt.event.MouseEvent ; 26 import java.awt.event.MouseListener ; 27 import javax.swing.Icon ; 28 import javax.swing.JComponent ; 29 import javax.swing.JToolTip ; 30 import org.openide.util.RequestProcessor; 31 import org.openide.util.RequestProcessor.Task; 32 33 40 abstract class FlashingIcon extends JComponent implements MouseListener { 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 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 59 protected FlashingIcon( Icon icon ) { 60 this.icon = icon; 61 Dimension d = new Dimension ( icon.getIconWidth(), icon.getIconHeight() ); 62 setMinimumSize( d ); 63 setMaximumSize( d ); 64 setPreferredSize( d ); 65 66 addMouseListener( this ); 67 rp = new RequestProcessor( "Exception Notification Icon" ); } 69 70 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 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 110 public void stopFlashing() { 111 synchronized( this ) { 112 if( keepRunning && !isIconVisible ) { 113 isIconVisible = true; 114 repaint(); 115 } 116 } 117 keepFlashing = false; 118 } 119 120 123 protected void flashIcon() { 124 isIconVisible = !isIconVisible; 125 126 repaint(); 127 } 128 129 public void paint(java.awt.Graphics g) { 130 if( isIconVisible ) { 131 icon.paintIcon( this, g, 0, 0 ); 132 } 133 } 134 135 public void mouseReleased(MouseEvent e) {} 136 137 public void mousePressed(MouseEvent e) { 138 stopFlashing(); 139 } 140 141 public void mouseExited(MouseEvent e) { 142 stopFlashing(); 143 } 144 145 public void mouseEntered(MouseEvent e) { 146 stopFlashing(); 147 } 148 149 public void mouseClicked(MouseEvent e) { 150 if( isIconVisible ) { 151 disappear(); 152 onMouseClick(); 153 } 154 } 155 156 159 protected abstract void onMouseClick(); 160 161 164 protected abstract void timeout(); 165 166 public Cursor getCursor() { 167 168 if( isIconVisible ) { 169 return Cursor.getPredefinedCursor( Cursor.HAND_CURSOR ); 170 } 171 return Cursor.getDefaultCursor(); 172 } 173 174 public Point getToolTipLocation( MouseEvent event ) { 175 176 JToolTip tip = createToolTip(); 177 tip.setTipText( getToolTipText() ); 178 Dimension d = tip.getPreferredSize(); 179 180 181 return new Point ( getWidth()-d.width, -d.height ); 182 } 183 184 private class Timer implements Runnable { 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 e ) { 204 e.printStackTrace(); 206 } 207 } 208 } 209 } 210 } 211 | Popular Tags |