KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > buchuki > ensmer > input > event > EnsmerKeyEvent


1 /*
2  * Copyright 2004 Dusty Phillips
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.buchuki.ensmer.input.event;
17
18 import java.awt.event.*;
19
20 /**
21  * Represents a KeyEvent for a specific key. It can model an event that has
22  * occured or an event that can occur. If no actual event is represented,
23  * getInputEvent() will return null. This class acts as an abstract class for
24  * the KeyPressEvent and KeyReleaseEvents that share a lot of functionality.
25  *
26  * @author Dusty Phillips [dusty@buchuki.com]
27  */

28 public class EnsmerKeyEvent extends EnsmerInputEvent {
29
30     /**
31      * Create a EnsmerKeyEvent with a reference to the KeyCode it should
32      * model. This constructor is used when no actual InputEvent was
33      * generated.
34      *
35      * @param keycode One of the constants from the java.awt.KeyEvent class
36      * representing a key that was pressed
37      */

38     public EnsmerKeyEvent(int keycode) {
39         this.keycode = keycode;
40     }
41
42     /**
43      * Create a EnsmerKeyEvent with a reference to the actual KeyEvent that
44      * occured. This constructor is used when an actual InputEvent has been
45      * generated. The keycode is retrieved from the event itself.
46      *
47      * @param event an InputEvent that was generated
48      */

49     public EnsmerKeyEvent(KeyEvent event) {
50         super(event);
51         this.keycode = event.getKeyCode();
52     }
53
54     /**
55      * Compare this EnsmerKeyEvent to another object to determine if they are equal.
56      * Objects are equal if they are the same class and have the same keycode.
57      * Also, to allow generic matching, if any of the objects has a keycode of
58      * -1, then they are equal if they are the same class.
59      *
60      * @param obj the object to compare to
61      */

62     @Override JavaDoc
63     public boolean equals(Object JavaDoc obj) {
64         if (getClass().equals(obj.getClass())) {
65             EnsmerKeyEvent ob = (EnsmerKeyEvent) obj;
66             return (ob.getKeyCode() == -1 || keycode == -1 || ob.getKeyCode() == keycode);
67         }
68         else {
69             return false;
70         }
71     }
72
73     /**
74      * Retrieve the hashcode for this object. Events are not likely to be
75      * hashed, but this attempts to keep the equals() contract. NOTE: This
76      * class does NOT keep the equals contract if the keycode is -1. In that
77      * case, the object is equal to all instances of the class, which is impossible
78      * to hash. <strong>DO NOT</strong> attempt any hashing implementation in which
79      * an event with a keycode of -1 will EVER be used as a key in the hashmap.
80      */

81     @Override JavaDoc
82     public int hashCode() {
83         return keycode + getClass().hashCode();
84     }
85
86     /**
87      * Get access to the keycode for this object
88      *
89      * @return a keycode (from java.awt.KeyEvent) representing a key that was
90      * pressed
91      */

92     public int getKeyCode() {
93         return keycode;
94     }
95
96     /**
97      * The keycode for the key that was pressed
98      */

99     private int keycode;
100 }
101
Popular Tags