KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > keyboard > KeyboardMap


1 package SnowMailClient.keyboard;
2
3 import java.util.*;
4
5 /** base for all keyboards maps
6 */

7 public class KeyboardMap
8 {
9    protected final Vector<KeyboardKey> keys = new Vector<KeyboardKey>();
10    private String JavaDoc name = "";
11    private int columnCount;
12
13    public Vector<KeyboardKey> getKeys() { return keys; }
14    public final int getColumnCount() { return columnCount; }
15    public final String JavaDoc getName() { return name; }
16
17    /** overwrite if less keys are visible (hidden mappings)
18    */

19    public int getVisibleKeys() { return keys.size(); } //16; }
20

21    /** overwrite if more specific, this allow to choose precisely available fonts...
22    */

23    public String JavaDoc getCharset() { return "utf-8"; }
24
25    public KeyboardMap(String JavaDoc name, int columnCount)
26    {
27      this.name = name;
28      this.columnCount = columnCount;
29    }
30
31    public Vector<Object JavaDoc> getVectorRepresentation()
32    {
33       Vector<Object JavaDoc> rep = new Vector<Object JavaDoc>();
34       rep.addElement(1); // 0: version
35
rep.addElement(name); // 1
36
rep.addElement(columnCount); // 2
37
Vector<Object JavaDoc> keysRep = new Vector<Object JavaDoc>();
38       rep.addElement(keysRep); // 3
39
for(KeyboardKey key: keys)
40       {
41         keysRep.addElement(key.getVectorRepresentation());
42       }
43       return rep;
44    }
45
46    public void createFromVectorRepresentation(Vector<Object JavaDoc> rep)
47    {
48       int version = (Integer JavaDoc) rep.elementAt(0);
49       name = (String JavaDoc) rep.elementAt(1);
50       columnCount = (Integer JavaDoc) rep.elementAt(2);
51       @SuppressWarnings JavaDoc("unchecked")
52       Vector<Vector<Object JavaDoc>> keysRep = (Vector<Vector<Object JavaDoc>>) rep.elementAt(3);
53       keys.clear();
54       for(Vector<Object JavaDoc> krep: keysRep)
55       {
56         keys.add(new KeyboardKey(krep));
57       }
58    }
59
60
61    public Vector<KeyboardKey> getKeyStartingWithShortcut(String JavaDoc _s)
62    {
63       Vector<KeyboardKey> rep = new Vector<KeyboardKey>();
64       if(_s.length()==0) return rep;
65
66       // first char determine case
67
String JavaDoc s = _s;
68       if(Character.isUpperCase(_s.charAt(0)))
69       {
70         s = _s.toUpperCase();
71         for(KeyboardKey k:keys)
72         {
73            for(String JavaDoc sc: k.getAllShortcuts())
74            {
75              if(sc.toUpperCase().startsWith(s))
76              {
77                rep.addElement(k);
78                k.matchCapital = true;
79                break; // only add one per key
80
}
81            }
82         }
83       }
84       else
85       {
86         s = _s.toLowerCase();
87         for(KeyboardKey k:keys)
88         {
89            for(String JavaDoc sc: k.getAllShortcuts())
90            {
91              if(sc.toLowerCase().startsWith(s))
92              {
93                rep.addElement(k);
94                k.matchCapital = false;
95                break; // only add one per key
96
}
97            }
98         }
99       }
100
101       if(rep.size()>0) return rep; // found
102

103       return rep;
104    }
105
106
107    public KeyboardKey getKeyWithShortcut(String JavaDoc _s)
108    {
109       if(_s.length()==0) return null;
110
111       // first char determine case
112
String JavaDoc s = _s;
113       if(Character.isUpperCase(_s.charAt(0)))
114       {
115          // search for upper matches
116
String JavaDoc supper = _s.toUpperCase();
117
118          for(KeyboardKey k:keys)
119          {
120             for(String JavaDoc sc: k.getAllShortcuts())
121             {
122               if(sc.toUpperCase().equals(supper))
123               {
124                 k.matchCapital = true;
125                 return k;
126               }
127             }
128          }
129
130       }
131       else
132       {
133          // search for lower matches
134
String JavaDoc slow = _s.toLowerCase();
135
136          for(KeyboardKey k:keys)
137          {
138             for(String JavaDoc sc: k.getAllShortcuts())
139             {
140               if(sc.toLowerCase().equals(slow))
141               {
142                 k.matchCapital = false;
143                 return k;
144               }
145             }
146          }
147       }
148
149
150       return null;
151    }
152
153    public String JavaDoc toString() { return this.getName(); }
154
155 } // KeyboardMap
Popular Tags