1 /*
   2  * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.lwawt.macosx;
  27 
  28 import java.awt.*;
  29 import java.awt.im.spi.*;
  30 import java.util.*;
  31 import java.util.List;
  32 
  33 /**
  34 * Provides sufficient information about an input method
  35  * to enable selection and loading of that input method.
  36  * The input method itself is only loaded when it is actually used.
  37  */
  38 
  39 public class CInputMethodDescriptor implements InputMethodDescriptor {
  40 
  41     static {
  42         nativeInit();
  43     }
  44 
  45     public CInputMethodDescriptor() {
  46     }
  47 
  48     /**
  49      * @see java.awt.im.spi.InputMethodDescriptor#getAvailableLocales
  50      */
  51     public Locale[] getAvailableLocales() {
  52         // returns a copy of internal list for public API
  53         Object[] locales = getAvailableLocalesInternal();
  54         Locale[] tmp = new Locale[locales.length];
  55         System.arraycopy(locales, 0, tmp, 0, locales.length);
  56         return tmp;
  57     }
  58 
  59     static Object[] getAvailableLocalesInternal() {
  60         List<Object> workList = nativeGetAvailableLocales();
  61         Locale currentLocale = CInputMethod.getNativeLocale();
  62 
  63         if (workList == null || workList.isEmpty()) {
  64             return new Object[] {
  65                     currentLocale != null ? currentLocale : Locale.getDefault()
  66             };
  67         } else {
  68             if (currentLocale != null && !workList.contains(currentLocale)) {
  69                 workList.add(currentLocale);
  70             }
  71             return workList.toArray();
  72         }
  73     }
  74 
  75     /**
  76         * @see java.awt.im.spi.InputMethodDescriptor#hasDynamicLocaleList
  77      */
  78     public boolean hasDynamicLocaleList() {
  79         return false;
  80     }
  81 
  82     /**
  83         * @see java.awt.im.spi.InputMethodDescriptor#getInputMethodDisplayName
  84      */
  85     public synchronized String getInputMethodDisplayName(Locale inputLocale, Locale displayLanguage) {
  86         String name = "System Input Methods";
  87         if (Locale.getDefault().equals(displayLanguage)) {
  88             name = Toolkit.getProperty("AWT.HostInputMethodDisplayName", name);
  89         }
  90         return name;
  91     }
  92 
  93     /**
  94         * @see java.awt.im.spi.InputMethodDescriptor#getInputMethodIcon
  95      */
  96     public Image getInputMethodIcon(Locale inputLocale) {
  97         // This should return the flag icon corresponding to the input Locale.
  98         return null;
  99     }
 100 
 101     /**
 102         * @see java.awt.im.spi.InputMethodDescriptor#createInputMethod
 103      */
 104     public InputMethod createInputMethod() throws Exception {
 105         return new CInputMethod();
 106     }
 107 
 108     public String toString() {
 109         Locale loc[] = getAvailableLocales();
 110         String locnames = null;
 111 
 112         for (int i = 0; i < loc.length; i++) {
 113             if (locnames == null) {
 114                 locnames = loc[i].toString();
 115             } else {
 116                 locnames += "," + loc[i];
 117             }
 118         }
 119         return getClass().getName() + "[" +
 120             "locales=" + locnames +
 121             ",localelist=" + (hasDynamicLocaleList() ? "dynamic" : "static") +
 122             "]";
 123     }
 124 
 125     private static native void nativeInit();
 126     private static native List<Object> nativeGetAvailableLocales();
 127 }