View Javadoc

1   /**
2    *  MicroEmulator
3    *  Copyright (C) 2006-2007 Bartek Teodorczyk <barteo@barteo.net>
4    *  Copyright (C) 2006-2007 Vlad Skarzhevskyy
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or (at your option) any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   *  @version $Id: ButtonName.java 1477 2007-11-05 16:18:42Z vlads $
21   */
22  package org.microemu.device.impl;
23  
24  import java.lang.reflect.Field;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  /**
29   * 
30   * This class defines Buttons available on Device
31   * 
32   * Implementation is Java 5 enum in java 1.4
33   * 
34   * @author vlads
35   * 
36   */
37  public final class ButtonName {
38  
39  	private static Map altNames = new HashMap();
40  
41  	public static final ButtonName SOFT1 = new ButtonName();
42  
43  	public static final ButtonName SOFT2 = new ButtonName();
44  
45  	public static final ButtonName SOFT3 = new ButtonName();
46  
47  	public static final ButtonName SELECT = new ButtonName("SEL");
48  
49  	public static final ButtonName UP = new ButtonName("U");
50  
51  	public static final ButtonName DOWN = new ButtonName("D");
52  
53  	public static final ButtonName LEFT = new ButtonName("L");
54  
55  	public static final ButtonName RIGHT = new ButtonName("R");
56  
57  	// public static final ButtonName BACK = new ButtonName();
58  
59  	public static final ButtonName BACK_SPACE = new ButtonName();
60  
61  	public static final ButtonName DELETE = new ButtonName();
62  
63  	// public static final ButtonName CLEAR = DELETE;
64  
65  	public static final ButtonName KEY_NUM0 = new ButtonName("0");
66  
67  	public static final ButtonName KEY_NUM1 = new ButtonName("1");
68  
69  	public static final ButtonName KEY_NUM2 = new ButtonName("2");
70  
71  	public static final ButtonName KEY_NUM3 = new ButtonName("3");
72  
73  	public static final ButtonName KEY_NUM4 = new ButtonName("4");
74  
75  	public static final ButtonName KEY_NUM5 = new ButtonName("5");
76  
77  	public static final ButtonName KEY_NUM6 = new ButtonName("6");
78  
79  	public static final ButtonName KEY_NUM7 = new ButtonName("7");
80  
81  	public static final ButtonName KEY_NUM8 = new ButtonName("8");
82  
83  	public static final ButtonName KEY_NUM9 = new ButtonName("9");
84  
85  	public static final ButtonName KEY_STAR = new ButtonName(new String[] { "*", "STAR", "ASTERISK" });
86  
87  	public static final ButtonName KEY_POUND = new ButtonName(new String[] { "#", "POUND" });
88  
89  	private String name = "n/a";
90  
91  	public static ButtonName getButtonName(String functionName) {
92  		String name = functionName.toUpperCase();
93  		try {
94  			Field field = ButtonName.class.getField(name);
95  			if (field.getType() == ButtonName.class) {
96  				return (ButtonName) field.get(null);
97  			}
98  		} catch (NoSuchFieldException e) {
99  		} catch (IllegalAccessException e) {
100 		}
101 		ButtonName btn = (ButtonName) altNames.get(name);
102 		if (btn == null) {
103 			// User defined button
104 			btn = new ButtonName();
105 			btn.name = functionName;
106 		}
107 
108 		return btn;
109 	}
110 
111 	static {
112 		// Name all the Buttons by filed name
113 		Field[] fields = ButtonName.class.getDeclaredFields();
114 		for (int i = 0; i < fields.length; i++) {
115 			if (fields[i].getType() == ButtonName.class) {
116 				try {
117 					((ButtonName) fields[i].get(null)).name = fields[i].getName();
118 				} catch (IllegalAccessException e) {
119 
120 				}
121 			}
122 		}
123 	}
124 
125 	private ButtonName() {
126 
127 	}
128 
129 	private ButtonName(String name) {
130 		altNames.put(name, this);
131 	}
132 
133 	private ButtonName(String[] names) {
134 		for (int i = 0; i < names.length; i++) {
135 			altNames.put(names[i], this);
136 		}
137 	}
138 
139 	public String getName() {
140 		return name;
141 	}
142 
143 	public String toString() {
144 		return name;
145 	}
146 
147 }