001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xml.xam.ui.column;
043:
044: import java.awt.AlphaComposite;
045: import java.awt.Color;
046: import java.awt.Composite;
047: import java.awt.Graphics;
048: import java.awt.Graphics2D;
049: import java.awt.GraphicsConfiguration;
050: import java.awt.Image;
051: import java.awt.Transparency;
052: import java.awt.event.ActionEvent;
053: import java.awt.event.ActionListener;
054: import java.awt.event.FocusEvent;
055: import java.awt.event.MouseEvent;
056: import javax.swing.Action;
057: import javax.swing.JButton;
058: import javax.swing.Timer;
059:
060: /**
061: * A convenience button class which will continue re-firing its action
062: * on a timer for as long as the button is depressed. Used for left-right
063: * scroll buttons.
064: */
065: public class TimerButton extends JButton implements ActionListener {
066: /** silence compiler warnings */
067: private static final long serialVersionUID = 1L;
068: private Timer timer;
069: private Image disabledImage;
070: private Image enabledImage;
071: private int count;
072:
073: public TimerButton(Action a) {
074: super (a);
075: }
076:
077: private Timer getTimer() {
078: if (timer == null) {
079: timer = new Timer(400, this );
080: timer.setRepeats(true);
081: }
082: return timer;
083: }
084:
085: public void actionPerformed(java.awt.event.ActionEvent e) {
086: count++;
087: if (count > 5) {
088: timer.setDelay(75);
089: } else if (count > 2) {
090: timer.setDelay(200);
091: }
092: performAction();
093: }
094:
095: private void performAction() {
096: if (!isEnabled()) {
097: stopTimer();
098: return;
099: }
100: getAction().actionPerformed(
101: new ActionEvent(this , ActionEvent.ACTION_PERFORMED,
102: getActionCommand()));
103: }
104:
105: private void startTimer() {
106: performAction();
107: Timer t = getTimer();
108: if (t.isRunning()) {
109: return;
110: }
111: repaint();
112: t.setDelay(400);
113: t.start();
114: }
115:
116: private void stopTimer() {
117: if (timer != null) {
118: timer.stop();
119: }
120: repaint();
121: count = 0;
122: }
123:
124: protected void processMouseEvent(MouseEvent me) {
125: if (isEnabled() && me.getID() == me.MOUSE_PRESSED) {
126: startTimer();
127: } else if (me.getID() == me.MOUSE_RELEASED) {
128: stopTimer();
129: } else {
130: super .processMouseEvent(me);
131: }
132: }
133:
134: protected void processFocusEvent(FocusEvent fe) {
135: super.processFocusEvent(fe);
136: if (fe.getID() == fe.FOCUS_LOST) {
137: stopTimer();
138: }
139: }
140: }
|