1 19 20 package org.netbeans.modules.xml.xam.ui.column; 21 22 import java.awt.AlphaComposite ; 23 import java.awt.Color ; 24 import java.awt.Composite ; 25 import java.awt.Graphics ; 26 import java.awt.Graphics2D ; 27 import java.awt.GraphicsConfiguration ; 28 import java.awt.Image ; 29 import java.awt.Transparency ; 30 import java.awt.event.ActionEvent ; 31 import java.awt.event.ActionListener ; 32 import java.awt.event.FocusEvent ; 33 import java.awt.event.MouseEvent ; 34 import javax.swing.Action ; 35 import javax.swing.JButton ; 36 import javax.swing.Timer ; 37 38 43 public class TimerButton extends JButton implements ActionListener { 44 45 private static final long serialVersionUID = 1L; 46 private Timer timer; 47 private Image disabledImage; 48 private Image enabledImage; 49 private int count; 50 51 public TimerButton(Action a) { 52 super(a); 53 } 54 55 private Timer getTimer() { 56 if (timer == null) { 57 timer = new Timer (400, this); 58 timer.setRepeats(true); 59 } 60 return timer; 61 } 62 63 public void actionPerformed(java.awt.event.ActionEvent e) { 64 count++; 65 if (count > 5) { 66 timer.setDelay(75); 67 } else if (count > 2) { 68 timer.setDelay(200); 69 } 70 performAction(); 71 } 72 73 private void performAction() { 74 if (!isEnabled()) { 75 stopTimer(); 76 return; 77 } 78 getAction().actionPerformed(new ActionEvent (this, 79 ActionEvent.ACTION_PERFORMED, getActionCommand())); 80 } 81 82 private void startTimer() { 83 performAction(); 84 Timer t = getTimer(); 85 if (t.isRunning()) { 86 return; 87 } 88 repaint(); 89 t.setDelay(400); 90 t.start(); 91 } 92 93 private void stopTimer() { 94 if (timer != null) { 95 timer.stop(); 96 } 97 repaint(); 98 count = 0; 99 } 100 101 protected void processMouseEvent(MouseEvent me) { 102 if (isEnabled() && me.getID() == me.MOUSE_PRESSED) { 103 startTimer(); 104 } else if (me.getID() == me.MOUSE_RELEASED) { 105 stopTimer(); 106 } else { 107 super.processMouseEvent(me); 108 } 109 } 110 111 protected void processFocusEvent(FocusEvent fe) { 112 super.processFocusEvent(fe); 113 if (fe.getID() == fe.FOCUS_LOST) { 114 stopTimer(); 115 } 116 } 117 } 118 | Popular Tags |