1 19 20 package org.netbeans.modules.autoupdate; 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 42 abstract class FlashingIcon extends JComponent implements MouseListener { 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 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 61 protected FlashingIcon( Icon icon ) { 62 this.icon = icon; 63 Dimension d = new Dimension ( icon.getIconWidth(), icon.getIconHeight() ); 64 setMinimumSize( d ); 65 setMaximumSize( d ); 66 setPreferredSize( d ); 67 68 addMouseListener( this ); 69 } 70 71 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 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 112 public void stopFlashing() { 113 synchronized( this ) { 114 if( keepRunning && !isIconVisible ) { 115 isIconVisible = true; 116 repaint(); 117 } 118 } 119 keepFlashing = false; 120 } 121 122 125 protected void flashIcon() { 126 isIconVisible = !isIconVisible; 127 128 repaint(); 129 } 130 131 public void paint(java.awt.Graphics g) { 132 if( isIconVisible ) { 133 icon.paintIcon( this, g, 0, 0 ); 134 } 135 } 136 137 public void mouseReleased(MouseEvent e) {} 138 139 public void mousePressed(MouseEvent e) { 140 stopFlashing(); 141 } 142 143 public void mouseExited(MouseEvent e) { 144 stopFlashing(); 145 } 146 147 public void mouseEntered(MouseEvent e) { 148 stopFlashing(); 149 } 150 151 public void mouseClicked(MouseEvent e) { 152 if( isIconVisible ) { 153 disappear(); 154 onMouseClick(); 155 } 156 } 157 158 161 protected abstract void onMouseClick(); 162 163 166 protected abstract void timeout(); 167 168 public Cursor getCursor() { 169 170 if( isIconVisible ) { 171 return Cursor.getPredefinedCursor( Cursor.HAND_CURSOR ); 172 } 173 return Cursor.getDefaultCursor(); 174 } 175 176 public Point getToolTipLocation( MouseEvent event ) { 177 178 JToolTip tip = createToolTip(); 179 tip.setTipText( getToolTipText() ); 180 Dimension d = tip.getPreferredSize(); 181 182 183 Point retValue = new Point ( getWidth()-d.width, -d.height ); 184 return retValue; 185 } 186 187 private class Timer implements Runnable { 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 |