1 19 20 package org.netbeans.editor; 21 22 import java.lang.ref.WeakReference ; 23 import java.awt.event.ActionListener ; 24 import java.awt.event.ActionEvent ; 25 import javax.swing.Timer ; 26 27 37 38 public class WeakTimerListener implements ActionListener { 39 40 private WeakReference ref; 41 42 private boolean stopTimer; 43 44 46 public WeakTimerListener(ActionListener source) { 47 this(source, true); 48 } 49 50 55 public WeakTimerListener(ActionListener source, boolean stopTimer) { 56 this.ref = new WeakReference (source); 57 this.stopTimer = stopTimer; 58 } 59 60 public void actionPerformed(ActionEvent evt) { 61 ActionListener src = (ActionListener )ref.get(); 62 if (src != null) { 63 src.actionPerformed(evt); 64 65 } else { if (evt.getSource() instanceof Timer ) { 67 Timer timer = (Timer )evt.getSource(); 68 timer.removeActionListener(this); 69 70 if (stopTimer) { 71 timer.stop(); 72 } 73 } 74 } 75 } 76 77 } 78 | Popular Tags |