1   /*
2    * GateIMDescriptor.java
3    *
4    * Copyright (c) 2000-2001, 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.3 2001/04/17 18:09:11 oana Exp $
16   */
17  package guk.im;
18  
19  import java.util.Locale;
20  import java.awt.*;
21  import java.awt.Image;
22  import java.awt.im.spi.InputMethod;
23  import java.awt.im.spi.InputMethodDescriptor;
24  import java.io.*;
25  import java.util.*;
26  
27  import guk.*;
28  
29  
30  /**
31   * Provides a way for the Gate input method to be discovered by the system.
32   *
33   * @see java.awt.im
34   * @see java.awt.im.spi
35   */
36  public class GateIMDescriptor implements InputMethodDescriptor {
37  
38    /**
39     * Default constructor.
40     */
41    public GateIMDescriptor() {
42      try{
43        InputStream is = GateIM.class.getResourceAsStream(
44                           GateIM.getIMBase() + "im.list");
45        if (is==null) throw new IllegalArgumentException(
46                "Failed to retrieve resource 'im.list'. Please reset classpath.");
47        BufferedReader br = new BufferedReader(new InputStreamReader(is));
48        String line = br.readLine();
49        StringTokenizer st;
50        String filename, language, country, variant;
51        supportedLocales = new HashMap();
52        while(line != null){
53          //skip comments and empty lines
54          if(line.startsWith("#") || line.startsWith("//") ||
55             line.length() == 0 ){
56            line = br.readLine();
57            continue;
58          }
59          language = country = variant = null;
60          st = new StringTokenizer(line, "\t", false);
61          if(st.hasMoreTokens()){
62            //get the file
63            filename = st.nextToken();
64            if(st.hasMoreTokens()){
65              //get the language
66              language = st.nextToken();
67              if(st.hasMoreElements()){
68                //get the country
69                country = st.nextToken();
70                if(country.equals("--")) country = "";
71                if(st.hasMoreElements()){
72                  //get the variant
73                  variant = st.nextToken();
74                  supportedLocales.put(new Locale(language,country,variant),
75                                       filename);
76                } else {
77                  //no variant
78                  supportedLocales.put(new Locale(language,country), filename);
79                }
80              } else {
81                //no country
82                throw new IllegalArgumentException(
83                  "Invalid input methods definition file!\n");
84              }
85            } else {
86              //no language
87              throw new IllegalArgumentException(
88                  "Invalid input methods definition file!\n");
89            }
90          }
91          line = br.readLine();
92        }
93      } catch(IOException ioe){
94        ioe.printStackTrace();
95      }
96    }
97  
98    /**
99     * Gets an Array with the locales supported by the Gate input method.
100    *
101    * @exception AWTException
102    */
103   public Locale[] getAvailableLocales() throws AWTException {
104     java.util.List locales = new ArrayList(supportedLocales.keySet());
105     Collections.sort(locales, new Comparator(){
106       /**
107        * Comparison method used for sorting the available locales.
108        *
109        * @param a
110        * @param b
111        */
112       public int compare(Object a, Object b){
113         if(a instanceof Locale && b instanceof Locale){
114           Locale l1 = (Locale) a;
115           Locale l2 = (Locale) b;
116           return l1.getDisplayLanguage().compareTo(l2.getDisplayLanguage());
117         }else throw new ClassCastException();
118       }// int compare(Object a, Object b)
119     });
120     return (Locale[])locales.toArray(new Locale[0]);
121   }
122 
123   /**
124    * Is the available locales list dynamic. Always returns <tt>false</tt>;
125    *
126    */
127   public boolean hasDynamicLocaleList() {
128     return false;
129   }
130 
131   /**
132    * Returns the display name for the input method for a given locale.
133    *
134    * @param inputLocale the locale for which the display name is sought
135    * @param displayLanguage the current locale to be used for displaying the
136    *     name
137    */
138   public String getInputMethodDisplayName(Locale inputLocale,
139                                           Locale displayLanguage) {
140     if(inputLocale == null) return "Gate Unicode Input Methods";
141     return inputLocale.getDisplayName(inputLocale);
142   }
143 
144   /**
145    * Provides an icon for the gate input method.
146    *
147    * @param inputLocale
148    */
149   public Image getInputMethodIcon(Locale inputLocale) {
150     //not yet!
151     return null;
152   }
153 
154   /**
155    * Creates a new {@link GateIM} object and returns a handle.
156    *
157    * @exception Exception
158    */
159   public InputMethod createInputMethod() throws Exception {
160     return new GateIM(supportedLocales);
161   }
162 
163 /*  static public void main(String[] args){
164     try{
165       GateIMDescriptor gd = new GateIMDescriptor();
166       InputMethod im = gd.createInputMethod();
167 //      im.setLocale(new Locale("ar","","Windows"));
168       //try all locales
169       Locale[] locales = gd.getAvailableLocales();
170       for(int i=0; i < locales.length; i++) im.setLocale(locales[i]);
171     }catch(Exception e){
172       e.printStackTrace();
173     }
174   }
175 */
176   /**
177    * The available locales. Maps from locale to filename.
178    *
179    */
180   Map supportedLocales;
181 }// class GateIMDescriptor implements InputMethodDescriptor
182