KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xam > ui > column > TimerButton


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.xam.ui.column;
21
22 import java.awt.AlphaComposite JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.Composite JavaDoc;
25 import java.awt.Graphics JavaDoc;
26 import java.awt.Graphics2D JavaDoc;
27 import java.awt.GraphicsConfiguration JavaDoc;
28 import java.awt.Image JavaDoc;
29 import java.awt.Transparency JavaDoc;
30 import java.awt.event.ActionEvent JavaDoc;
31 import java.awt.event.ActionListener JavaDoc;
32 import java.awt.event.FocusEvent JavaDoc;
33 import java.awt.event.MouseEvent JavaDoc;
34 import javax.swing.Action JavaDoc;
35 import javax.swing.JButton JavaDoc;
36 import javax.swing.Timer JavaDoc;
37
38 /**
39  * A convenience button class which will continue re-firing its action
40  * on a timer for as long as the button is depressed. Used for left-right
41  * scroll buttons.
42  */

43 public class TimerButton extends JButton JavaDoc implements ActionListener JavaDoc {
44     /** silence compiler warnings */
45     private static final long serialVersionUID = 1L;
46     private Timer JavaDoc timer;
47     private Image JavaDoc disabledImage;
48     private Image JavaDoc enabledImage;
49     private int count;
50
51     public TimerButton(Action JavaDoc a) {
52         super(a);
53     }
54
55     private Timer JavaDoc getTimer() {
56         if (timer == null) {
57             timer = new Timer JavaDoc(400, this);
58             timer.setRepeats(true);
59         }
60         return timer;
61     }
62
63     public void actionPerformed(java.awt.event.ActionEvent JavaDoc 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 JavaDoc(this,
79                 ActionEvent.ACTION_PERFORMED, getActionCommand()));
80     }
81
82     private void startTimer() {
83         performAction();
84         Timer JavaDoc 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 JavaDoc 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 JavaDoc fe) {
112         super.processFocusEvent(fe);
113         if (fe.getID() == fe.FOCUS_LOST) {
114             stopTimer();
115         }
116     }
117 }
118
Popular Tags