in progress
[jalview.git] / forester / java / src / org / forester / archaeopteryx / util / TypomaticJButton.java
1
2 package org.forester.archaeopteryx.util;
3
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6 import java.awt.event.MouseEvent;
7 import java.awt.event.MouseListener;
8
9 import javax.swing.Action;
10 import javax.swing.Icon;
11 import javax.swing.JButton;
12 import javax.swing.Timer;
13
14 public final class TypomaticJButton extends JButton implements ActionListener, MouseListener {
15
16     private static final long serialVersionUID = 7435606927739361980L;
17     private boolean           pressed          = false;
18     private boolean           repeat_enabled   = true;
19     private Timer             timer            = null;
20     private int               initial_delay    = 600;
21     private int               delay            = 200;
22     private int               modifiers        = 0;
23
24     public TypomaticJButton() {
25         super();
26         init();
27     }
28
29     public TypomaticJButton( final Action a ) {
30         super( a );
31         init();
32     }
33
34     public TypomaticJButton( final Icon icon ) {
35         super( icon );
36         init();
37     }
38
39     public TypomaticJButton( final String text ) {
40         super( text );
41         init();
42     }
43
44     public TypomaticJButton( final String text, final Icon icon ) {
45         super( text, icon );
46         init();
47     }
48
49     @Override
50     final public void actionPerformed( final ActionEvent ae ) {
51         if ( ae.getSource() == timer ) {
52             final ActionEvent event = new ActionEvent( this,
53                                                        ActionEvent.ACTION_PERFORMED,
54                                                        super.getActionCommand(),
55                                                        modifiers );
56             super.fireActionPerformed( event );
57         }
58     }
59
60     final public int getDelay() {
61         return delay;
62     }
63
64     final public int getInitialDelay() {
65         return initial_delay;
66     }
67
68     final private void init() {
69         addMouseListener( this );
70         timer = new Timer( delay, this );
71         timer.setRepeats( true );
72     }
73
74     final public boolean isRepeatEnabled() {
75         return repeat_enabled;
76     }
77
78     @Override
79     final public void mouseClicked( final MouseEvent me ) {
80         if ( me.getSource() == this ) {
81             pressed = false;
82             if ( timer.isRunning() ) {
83                 timer.stop();
84             }
85         }
86     }
87
88     @Override
89     final public void mouseEntered( final MouseEvent e ) {
90         if ( ( e.getSource() == this ) && isEnabled() && isRepeatEnabled() ) {
91             if ( pressed && !timer.isRunning() ) {
92                 modifiers = e.getModifiers();
93                 timer.setInitialDelay( delay );
94                 timer.start();
95             }
96         }
97     }
98
99     @Override
100     final public void mouseExited( final MouseEvent e ) {
101         if ( e.getSource() == this ) {
102             if ( timer.isRunning() ) {
103                 timer.stop();
104             }
105         }
106     }
107
108     @Override
109     final public void mousePressed( final MouseEvent e ) {
110         if ( ( e.getSource() == this ) && isEnabled() && isRepeatEnabled() ) {
111             pressed = true;
112             if ( !timer.isRunning() ) {
113                 modifiers = e.getModifiers();
114                 timer.setInitialDelay( initial_delay );
115                 timer.start();
116             }
117         }
118     }
119
120     @Override
121     final public void mouseReleased( final MouseEvent e ) {
122         if ( e.getSource() == this ) {
123             pressed = false;
124             if ( timer.isRunning() ) {
125                 timer.stop();
126             }
127         }
128     }
129
130     final public void setDelay( final int d ) {
131         delay = d;
132     }
133
134     @Override
135     final public void setEnabled( final boolean e ) {
136         if ( e != super.isEnabled() ) {
137             pressed = false;
138             if ( timer.isRunning() ) {
139                 timer.stop();
140             }
141         }
142         super.setEnabled( e );
143     }
144
145     final public void setInitialDelay( final int d ) {
146         initial_delay = d;
147     }
148
149     final public void setRepeatEnabled( final boolean e ) {
150         if ( !e ) {
151             pressed = false;
152             if ( timer.isRunning() ) {
153                 timer.stop();
154             }
155         }
156         repeat_enabled = e;
157     }
158 }