View Javadoc

1   /*
2    * Copyright 2007 Markku Saarela 
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, 
11   * software distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. 
14   * See the License for the specific language governing permissions and limitations under the License. 
15   */
16  package net.sourceforge.jivalo.editor.key;
17  
18  import java.awt.event.InputEvent;
19  import java.awt.event.KeyEvent;
20  import java.io.ObjectStreamException;
21  import java.io.Serializable;
22  import java.util.Arrays;
23  import java.util.Collections;
24  import java.util.List;
25  
26  /**
27   * @author <a href="mailto:ivalo@iki.fi">Markku Saarela</a>
28   *
29   */
30  public class InputEventKeyModifier implements Serializable
31  {
32  
33      private static final long serialVersionUID = 1L;
34  
35      /**
36       * The Alt key modifier.
37       */
38      public static final InputEventKeyModifier ALT_MASK = 
39          new InputEventKeyModifier( "ALT_MASK", InputEvent.ALT_MASK );
40  
41      /**
42       * The Control key modifier.
43       */
44      public static final InputEventKeyModifier CTRL_MASK = 
45          new InputEventKeyModifier( "CTRL_MASK", InputEvent.CTRL_MASK );
46  
47      /**
48       * The Shift key modifier.
49       */
50      public static final InputEventKeyModifier SHIFT_MASK = 
51          new InputEventKeyModifier( "SHIFT_MASK", InputEvent.SHIFT_MASK );
52  
53  
54      private static final InputEventKeyModifier[] PRIVATE_VALUES = 
55          { ALT_MASK, CTRL_MASK, SHIFT_MASK};
56  
57      private static final List VALUES = Collections.unmodifiableList( Arrays
58              .asList( PRIVATE_VALUES ) );
59  
60      // next index to assign to an enumerator
61      private static int nextIndex = 0;
62  
63      // index for this enumerator
64      private final int index = nextIndex++;
65  
66      private final String name;
67      
68      private final int modifier;
69  
70      private InputEventKeyModifier( String name, int modifier )
71      {
72          this.name = name;
73          this.modifier = modifier;
74      }
75  
76      /**
77       * @return the modifier
78       */
79      public int getModifier()
80      {
81          return this.modifier;
82      }
83  
84      public String toString()
85      {
86          return KeyEvent.getKeyModifiersText( this.modifier );
87      }
88  
89      public boolean equals( Object o )
90      {
91  
92          if ( !( o instanceof InputEventKeyModifier ) )
93              return false;
94  
95          InputEventKeyModifier s = ( InputEventKeyModifier ) o;
96  
97          return this.name.equals( s.name );
98      }
99  
100     // Lazily initialize, cached hashCode
101     private volatile int hashCode = 0;
102 
103     public int hashCode()
104     {
105 
106         if ( hashCode == 0 )
107         {
108             int result = 17;
109             result = 37 * result + this.name.hashCode();
110             hashCode = result;
111         }
112         return hashCode;
113     }
114 
115     /**
116      * @throws CloneNotSupportedException.
117      *             This guarantees that enums are never cloned, which is
118      *             necessary to preserve their "singleton" status.
119      */
120     protected final Object clone() throws CloneNotSupportedException
121     {
122 
123         throw new CloneNotSupportedException();
124     }
125 
126     // return alternative object
127     // as result of deserialization
128     private Object readResolve() throws ObjectStreamException
129     {
130         return PRIVATE_VALUES[index];
131     }
132 
133     /**
134      * 
135      * @param value
136      * @return
137      * @throws NullPointerException
138      *             if value is null
139      * @throws IllegalArgumentException
140      *             if InputEventKeyModifier has no constant with the specified value.
141      */
142     public static InputEventKeyModifier valueOf( String value )
143     {
144 
145         if ( value == null )
146         {
147             throw new NullPointerException( "value" );
148         }
149         InputEventKeyModifier o = null;
150 
151         for ( int i = 0; i < PRIVATE_VALUES.length; i++ )
152         {
153             if ( PRIVATE_VALUES[i].name.equalsIgnoreCase( value.trim() ) )
154             {
155                 o = PRIVATE_VALUES[i];
156                 break;
157             }
158         }
159 
160         if ( o == null )
161         {
162             throw new IllegalArgumentException( value
163                     + " is not valid value of InputEventKeyModifier." );
164         }
165         return o;
166     }
167 
168     public static List values()
169     {
170 
171         return VALUES;
172     }
173 
174 }