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 name; 11 public int codeCAPITAL, codeSMALL; 12 public String [] shortcuts; 13 14 public JButton button; 16 public boolean matchCapital = true; 18 19 20 public Vector<Object > getVectorRepresentation() 21 { 22 Vector<Object > rep = new Vector<Object >(); 23 rep.addElement(1); 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 > rep) 33 { 34 int version = (Integer ) rep.elementAt(0); 35 name = (String ) rep.elementAt(1); 36 codeCAPITAL = (Integer ) rep.elementAt(2); 37 codeSMALL = (Integer ) rep.elementAt(3); 38 shortcuts = (String []) rep.elementAt(4); 39 } 40 41 public KeyboardKey(Vector<Object > rep) 42 { 43 createFromVectorRepresentation(rep); 44 } 45 46 47 49 public KeyboardKey() 50 { 51 name = ""; 52 shortcuts = new String [] {}; 53 codeCAPITAL = -1; 54 codeSMALL = -1; 55 } 56 57 58 public final boolean isSpacer() 59 { 60 return name.equals(""); 61 } 62 63 64 66 public KeyboardKey(String name, int uniCode, String ... 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 91 public KeyboardKey(String name, int uniCodeCAPITAL, int codeSmall, String ... 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 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 toString() 116 { 117 if(matchCapital) return toStringCAPITAL(); 118 return toStringSMALL(); 119 } 120 121 public String toStringCAPITAL() 122 { 123 return toStringSMALL().toUpperCase(); 125 } 126 127 public String toStringSMALL() 128 { 129 return ""+(char) codeSMALL; 130 } 131 132 public String [] getAllShortcuts() { return shortcuts; } 133 134 135 public String toStringAllShortcuts() 136 { 137 if(shortcuts.length==0) return ""; 138 if(shortcuts.length==1) return shortcuts[0]; 139 140 StringBuffer completion = new StringBuffer ("["); 141 for(String s: getAllShortcuts()) 142 { 143 completion.append(""+s+" "); 144 } 145 completion.setLength(completion.length()-4); completion.append("]"); 147 return completion.toString(); 148 } 149 150 public static void main(String [] a) 151 { 152 KeyboardDialog.main(null); 153 } 154 155 } | Popular Tags |