| GateIMDescriptor.java |
1 /*
2 * GateIMDescriptor.java
3 *
4 * Copyright (c) 1998-2005, The University of Sheffield.
5 *
6 * This file is part of GATE (see http://gate.ac.uk/), and is free
7 * software, licenced under the GNU Library General Public License,
8 * Version 2, June1991.
9 *
10 * A copy of this licence is included in the distribution in the file
11 * licence.html, and is also available at http://gate.ac.uk/gate/licence.html.
12 *
13 * Valentin Tablan, October 2000
14 *
15 * $Id: GateIMDescriptor.java,v 1.7 2005/01/11 13:51:38 ian Exp $
16 */
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 /**
28 * Provides a way for the Gate input method to be discovered by the system.
29 *
30 * @see java.awt.im
31 * @see java.awt.im.spi
32 */
33 public class GateIMDescriptor implements InputMethodDescriptor {
34
35 /**
36 * Default constructor.
37 */
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 //skip comments and empty lines
51 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 //get the file
60 filename = st.nextToken();
61 if(st.hasMoreTokens()){
62 //get the language
63 language = st.nextToken();
64 if(st.hasMoreElements()){
65 //get the country
66 country = st.nextToken();
67 if(country.equals("--")) country = "";
68 if(st.hasMoreElements()){
69 //get the variant
70 variant = st.nextToken();
71 supportedLocales.put(new Locale(language,country,variant),
72 filename);
73 } else {
74 //no variant
75 supportedLocales.put(new Locale(language,country), filename);
76 }
77 } else {
78 //no country
79 throw new IllegalArgumentException(
80 "Invalid input methods definition file!\n");
81 }
82 } else {
83 //no language
84 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 /**
96 * Gets an Array with the locales supported by the Gate input method.
97 *
98 * @exception AWTException
99 */
100 public Locale[] getAvailableLocales() throws AWTException {
101 java.util.List locales = new ArrayList(supportedLocales.keySet());
102 Collections.sort(locales, new Comparator(){
103 /**
104 * Comparison method used for sorting the available locales.
105 *
106 * @param a
107 * @param b
108 */
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 }// int compare(Object a, Object b)
116 });
117 return (Locale[])locales.toArray(new Locale[0]);
118 }
119
120 /**
121 * Is the available locales list dynamic. Always returns <tt>false</tt>;
122 *
123 */
124 public boolean hasDynamicLocaleList() {
125 return false;
126 }
127
128 /**
129 * Returns the display name for the input method for a given locale.
130 *
131 * @param inputLocale the locale for which the display name is sought
132 * @param displayLanguage the current locale to be used for displaying the
133 * name
134 */
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 /**
142 * Provides an icon for the gate input method.
143 *
144 * @param inputLocale
145 */
146 public Image getInputMethodIcon(Locale inputLocale) {
147 //not yet!
148 return null;
149 }
150
151 /**
152 * Creates a new {@link GateIM} object and returns a handle.
153 *
154 * @exception Exception
155 */
156 public InputMethod createInputMethod() throws Exception {
157 return new GateIM(supportedLocales);
158 }
159
160 /* static public void main(String[] args){
161 try{
162 GateIMDescriptor gd = new GateIMDescriptor();
163 InputMethod im = gd.createInputMethod();
164 // im.setLocale(new Locale("ar","","Windows"));
165 //try all locales
166 Locale[] locales = gd.getAvailableLocales();
167 for(int i=0; i < locales.length; i++) im.setLocale(locales[i]);
168 }catch(Exception e){
169 e.printStackTrace();
170 }
171 }
172 */
173 /**
174 * The available locales. Maps from locale to filename.
175 *
176 */
177 Map supportedLocales;
178 }// class GateIMDescriptor implements InputMethodDescriptor
179