001 /*
002 * GateIMDescriptor.java
003 *
004 * Copyright (c) 1998-2005, The University of Sheffield.
005 *
006 * This file is part of GATE (see http://gate.ac.uk/), and is free
007 * software, licenced under the GNU Library General Public License,
008 * Version 2, June1991.
009 *
010 * A copy of this licence is included in the distribution in the file
011 * licence.html, and is also available at http://gate.ac.uk/gate/licence.html.
012 *
013 * Valentin Tablan, October 2000
014 *
015 * $Id: GateIMDescriptor.java 13025 2010-08-25 14:06:50Z valyt $
016 */
017 package guk.im;
018
019 import guk.GukBomStrippingInputStreamReader;
020
021 import java.awt.AWTException;
022 import java.awt.Image;
023 import java.awt.im.spi.InputMethod;
024 import java.awt.im.spi.InputMethodDescriptor;
025 import java.io.*;
026 import java.util.*;
027
028
029 /**
030 * Provides a way for the Gate input method to be discovered by the system.
031 *
032 * @see java.awt.im
033 * @see java.awt.im.spi
034 */
035 public class GateIMDescriptor implements InputMethodDescriptor {
036
037 /**
038 * Default constructor.
039 */
040 public GateIMDescriptor() {
041 try{
042 InputStream is = GateIM.class.getResourceAsStream(
043 GateIM.getIMBase() + "im.list");
044 if (is==null) throw new IllegalArgumentException(
045 "Failed to retrieve resource 'im.list'. Please reset classpath.");
046 BufferedReader br = new GukBomStrippingInputStreamReader(is);
047 String line = br.readLine();
048 StringTokenizer st;
049 String filename, language, country, variant;
050 supportedLocales = new HashMap();
051 while(line != null){
052 //skip comments and empty lines
053 if(line.startsWith("#") || line.startsWith("//") ||
054 line.length() == 0 ){
055 line = br.readLine();
056 continue;
057 }
058 language = country = variant = null;
059 st = new StringTokenizer(line, "\t", false);
060 if(st.hasMoreTokens()){
061 //get the file
062 filename = st.nextToken();
063 if(st.hasMoreTokens()){
064 //get the language
065 language = st.nextToken();
066 if(st.hasMoreElements()){
067 //get the country
068 country = st.nextToken();
069 if(country.equals("--")) country = "";
070 if(st.hasMoreElements()){
071 //get the variant
072 variant = st.nextToken();
073 supportedLocales.put(new Locale(language,country,variant),
074 filename);
075 } else {
076 //no variant
077 supportedLocales.put(new Locale(language,country), filename);
078 }
079 } else {
080 //no country
081 throw new IllegalArgumentException(
082 "Invalid input methods definition file!\n");
083 }
084 } else {
085 //no language
086 throw new IllegalArgumentException(
087 "Invalid input methods definition file!\n");
088 }
089 }
090 line = br.readLine();
091 }
092 } catch(IOException ioe){
093 ioe.printStackTrace();
094 }
095 }
096
097 /**
098 * Gets an Array with the locales supported by the Gate input method.
099 *
100 * @exception AWTException
101 */
102 public Locale[] getAvailableLocales() throws AWTException {
103 java.util.List locales = new ArrayList(supportedLocales.keySet());
104 Collections.sort(locales, new Comparator(){
105 /**
106 * Comparison method used for sorting the available locales.
107 *
108 * @param a
109 * @param b
110 */
111 public int compare(Object a, Object b){
112 if(a instanceof Locale && b instanceof Locale){
113 Locale l1 = (Locale) a;
114 Locale l2 = (Locale) b;
115 return l1.getDisplayLanguage().compareTo(l2.getDisplayLanguage());
116 }else throw new ClassCastException();
117 }// int compare(Object a, Object b)
118 });
119 return (Locale[])locales.toArray(new Locale[0]);
120 }
121
122 /**
123 * Is the available locales list dynamic. Always returns <tt>false</tt>;
124 *
125 */
126 public boolean hasDynamicLocaleList() {
127 return false;
128 }
129
130 /**
131 * Returns the display name for the input method for a given locale.
132 *
133 * @param inputLocale the locale for which the display name is sought
134 * @param displayLanguage the current locale to be used for displaying the
135 * name
136 */
137 public String getInputMethodDisplayName(Locale inputLocale,
138 Locale displayLanguage) {
139 if(inputLocale == null) return "GATE Unicode Input Methods";
140 return inputLocale.getDisplayName(inputLocale);
141 }
142
143 /**
144 * Provides an icon for the gate input method.
145 *
146 * @param inputLocale
147 */
148 public Image getInputMethodIcon(Locale inputLocale) {
149 //not yet!
150 return null;
151 }
152
153 /**
154 * Creates a new {@link GateIM} object and returns a handle.
155 *
156 * @exception Exception
157 */
158 public InputMethod createInputMethod() throws Exception {
159 return new GateIM(supportedLocales);
160 }
161
162 /* static public void main(String[] args){
163 try{
164 GateIMDescriptor gd = new GateIMDescriptor();
165 InputMethod im = gd.createInputMethod();
166 // im.setLocale(new Locale("ar","","Windows"));
167 //try all locales
168 Locale[] locales = gd.getAvailableLocales();
169 for(int i=0; i < locales.length; i++) im.setLocale(locales[i]);
170 }catch(Exception e){
171 e.printStackTrace();
172 }
173 }
174 */
175 /**
176 * The available locales. Maps from locale to filename.
177 *
178 */
179 Map supportedLocales;
180 }// class GateIMDescriptor implements InputMethodDescriptor
|