KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > commons > cbutil > CBAction


1 package com.ca.commons.cbutil;
2
3 import javax.swing.*;
4 import java.awt.event.ActionEvent JavaDoc;
5 import java.beans.PropertyChangeListener JavaDoc;
6
7 /**
8  * Apparently it is better to use key bindings rather than adding a KeyListener...
9  * "for reacting in a special way to particular keys, you usually should use key
10  * bindings instead of a key listener".
11  * This class lets the user set the key as an int. If a key is pressed and it
12  * matches the assigned int, a check is done for if it is an escape or enter key.
13  * (27 or 10). If escape, the doCancel method is called. If enter, the doClick
14  * method is called.
15  * Bug 4646.
16  *
17  * @author Trudi.
18  */

19 public abstract class CBAction
20         implements Action
21 {
22     /**
23      * Represents the escape key.
24      */

25     public static final int ESCAPE = 27;
26
27     /**
28      * Represents the enter key.
29      */

30     public static final int ENTER = 10;
31
32     /**
33      * User sets the int representation of the key they want to listen for.
34      */

35     private int key = 0;
36
37     /**
38      * Whether the listener is enabled.
39      */

40     private boolean enabled = true;
41
42     /**
43      * Empty constructor.
44      */

45     public CBAction()
46     {
47     }
48
49     /**
50      * Constructor that sets the key that is Action is associated with.
51      *
52      * @param key
53      */

54     public CBAction(int key)
55     {
56         this.key = key;
57     }
58
59     /**
60      * Returns the key.
61      *
62      * @return
63      */

64     public int getKey()
65     {
66         return key;
67     }
68
69     /**
70      * Sets the key.
71      *
72      * @param key an int representation of the keyboard key.
73      */

74     public void setKey(int key)
75     {
76         this.key = key;
77     }
78
79     /**
80      * Subclasses to implement this method.
81      *
82      * @param e
83      */

84     public abstract void actionPerformed(ActionEvent JavaDoc e);
85
86     //TE: implemented methods...
87
public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener)
88     {
89     }
90
91     public Object JavaDoc getValue(String JavaDoc key)
92     {
93         return key;
94     }
95
96     public boolean isEnabled()
97     {
98         return enabled;
99     }
100
101     public void putValue(String JavaDoc key, Object JavaDoc value)
102     {
103     }
104
105     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener)
106     {
107     }
108
109     public void setEnabled(boolean b)
110     {
111         enabled = b;
112     }
113 }
114
Popular Tags