KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > keyboard > KeyboardKey


1 package SnowMailClient.keyboard;
2
3 import javax.swing.*;
4 import java.awt.*;
5 import java.util.*;
6
7 public class KeyboardKey
8 {
9
10   public String JavaDoc name;
11   public int codeCAPITAL, codeSMALL;
12   public String JavaDoc[] shortcuts;
13
14   public JButton button; // set by the view, used for highlights
15

16   // if matching, tells if the capital is matching
17
public boolean matchCapital = true;
18
19
20   public Vector<Object JavaDoc> getVectorRepresentation()
21   {
22      Vector<Object JavaDoc> rep = new Vector<Object JavaDoc>();
23      rep.addElement(1); // version
24
rep.addElement(name);
25      rep.addElement(codeCAPITAL);
26      rep.addElement(codeSMALL);
27      rep.addElement(shortcuts);
28      return rep;
29   }
30
31
32   public void createFromVectorRepresentation(Vector<Object JavaDoc> rep)
33   {
34      int version = (Integer JavaDoc) rep.elementAt(0);
35      name = (String JavaDoc) rep.elementAt(1);
36      codeCAPITAL = (Integer JavaDoc) rep.elementAt(2);
37      codeSMALL = (Integer JavaDoc) rep.elementAt(3);
38      shortcuts = (String JavaDoc[]) rep.elementAt(4);
39   }
40
41   public KeyboardKey(Vector<Object JavaDoc> rep)
42   {
43      createFromVectorRepresentation(rep);
44   }
45
46
47   /** Separator
48   */

49   public KeyboardKey()
50   {
51     name = "";
52     shortcuts = new String JavaDoc[] {};
53     codeCAPITAL = -1;
54     codeSMALL = -1;
55   }
56
57
58   public final boolean isSpacer()
59   {
60     return name.equals("");
61   }
62
63
64   /** represent a single CAPITAL letter
65   */

66   public KeyboardKey(String JavaDoc name, int uniCode, String JavaDoc... shortcuts)
67   {
68     this.name = name;
69     this.shortcuts = shortcuts;
70     if(Character.isUpperCase(uniCode))
71     {
72       codeCAPITAL = uniCode;
73       char small = Character.toLowerCase((char) uniCode);
74       codeSMALL = (int) small;
75     }
76     else if(Character.isLowerCase(uniCode))
77     {
78       codeSMALL = uniCode;
79       char up = Character.toUpperCase((char) uniCode);
80       codeCAPITAL = (int) up;
81     }
82     else
83     {
84       codeSMALL = uniCode;
85       codeCAPITAL = uniCode;
86     }
87   }
88
89   /** represent a single CAPITAL letter
90   */

91   public KeyboardKey(String JavaDoc name, int uniCodeCAPITAL, int codeSmall, String JavaDoc... shortcuts)
92   {
93     this.name = name;
94     this.shortcuts = shortcuts;
95     codeCAPITAL = uniCodeCAPITAL;
96     codeSMALL = codeSmall;
97   }
98
99   public void setIsCandidate(boolean is)
100   {
101     //isCandidate = is;
102
if(button!=null)
103     {
104       if(is)
105       {
106         button.setBackground(Color.green);
107       }
108       else
109       {
110         button.setBackground(UIManager.getColor("Button.background"));
111       }
112     }
113   }
114
115   public String JavaDoc toString()
116   {
117     if(matchCapital) return toStringCAPITAL();
118     return toStringSMALL();
119   }
120
121   public String JavaDoc toStringCAPITAL()
122   {
123     //return ""+(char) codeCAPITAL;
124
return toStringSMALL().toUpperCase();
125   }
126
127   public String JavaDoc toStringSMALL()
128   {
129     return ""+(char) codeSMALL;
130   }
131
132   public String JavaDoc[] getAllShortcuts() { return shortcuts; }
133
134
135   public String JavaDoc toStringAllShortcuts()
136   {
137      if(shortcuts.length==0) return "";
138      if(shortcuts.length==1) return shortcuts[0];
139
140      StringBuffer JavaDoc completion = new StringBuffer JavaDoc("[");
141      for(String JavaDoc s: getAllShortcuts())
142      {
143         completion.append(""+s+" ");
144      }
145      completion.setLength(completion.length()-4); // remove last separator
146
completion.append("]");
147      return completion.toString();
148   }
149
150   public static void main(String JavaDoc[] a)
151   {
152      KeyboardDialog.main(null);
153   }
154
155 } // KeyboardKey
Popular Tags