1
17 package guk.im;
18
19 import java.awt.AWTException;
20 import java.awt.Image;
21 import java.awt.im.spi.InputMethod;
22 import java.awt.im.spi.InputMethodDescriptor;
23 import java.io.*;
24 import java.util.*;
25
26
27
33 public class GateIMDescriptor implements InputMethodDescriptor {
34
35
38 public GateIMDescriptor() {
39 try{
40 InputStream is = GateIM.class.getResourceAsStream(
41 GateIM.getIMBase() + "im.list");
42 if (is==null) throw new IllegalArgumentException(
43 "Failed to retrieve resource 'im.list'. Please reset classpath.");
44 BufferedReader br = new BufferedReader(new InputStreamReader(is));
45 String line = br.readLine();
46 StringTokenizer st;
47 String filename, language, country, variant;
48 supportedLocales = new HashMap();
49 while(line != null){
50 if(line.startsWith("#") || line.startsWith("//") ||
52 line.length() == 0 ){
53 line = br.readLine();
54 continue;
55 }
56 language = country = variant = null;
57 st = new StringTokenizer(line, "\t", false);
58 if(st.hasMoreTokens()){
59 filename = st.nextToken();
61 if(st.hasMoreTokens()){
62 language = st.nextToken();
64 if(st.hasMoreElements()){
65 country = st.nextToken();
67 if(country.equals("--")) country = "";
68 if(st.hasMoreElements()){
69 variant = st.nextToken();
71 supportedLocales.put(new Locale(language,country,variant),
72 filename);
73 } else {
74 supportedLocales.put(new Locale(language,country), filename);
76 }
77 } else {
78 throw new IllegalArgumentException(
80 "Invalid input methods definition file!\n");
81 }
82 } else {
83 throw new IllegalArgumentException(
85 "Invalid input methods definition file!\n");
86 }
87 }
88 line = br.readLine();
89 }
90 } catch(IOException ioe){
91 ioe.printStackTrace();
92 }
93 }
94
95
100 public Locale[] getAvailableLocales() throws AWTException {
101 java.util.List locales = new ArrayList(supportedLocales.keySet());
102 Collections.sort(locales, new Comparator(){
103
109 public int compare(Object a, Object b){
110 if(a instanceof Locale && b instanceof Locale){
111 Locale l1 = (Locale) a;
112 Locale l2 = (Locale) b;
113 return l1.getDisplayLanguage().compareTo(l2.getDisplayLanguage());
114 }else throw new ClassCastException();
115 } });
117 return (Locale[])locales.toArray(new Locale[0]);
118 }
119
120
124 public boolean hasDynamicLocaleList() {
125 return false;
126 }
127
128
135 public String getInputMethodDisplayName(Locale inputLocale,
136 Locale displayLanguage) {
137 if(inputLocale == null) return "GATE Unicode Input Methods";
138 return inputLocale.getDisplayName(inputLocale);
139 }
140
141
146 public Image getInputMethodIcon(Locale inputLocale) {
147 return null;
149 }
150
151
156 public InputMethod createInputMethod() throws Exception {
157 return new GateIM(supportedLocales);
158 }
159
160
173
177 Map supportedLocales;
178 }