806 lines · 28.7 KB · Java
RawDownload
        InputContext inputContext = currentInputContext;        if (inputContext != null) {            InputMethodLocator locator = inputContext.getInputMethodLocator();            if (locator != null) {                return locator.getActionCommandString();            }        }        return null;    }     synchronized void changeInputMethod(String choice) {        InputMethodLocator locator = null;         String inputMethodName = choice;        String localeString = null;        int index = choice.indexOf('\n');        if (index != -1) {            localeString = choice.substring(index + 1);            inputMethodName = choice.substring(0, index);        }        if (hostAdapterLocator.getActionCommandString().equals(inputMethodName)) {            locator = hostAdapterLocator;        } else {            for (int i = 0; i < javaInputMethodLocatorList.size(); i++) {                InputMethodLocator candidate = javaInputMethodLocatorList.get(i);                String name = candidate.getActionCommandString();                if (name.equals(inputMethodName)) {                    locator = candidate;                    break;                }            }        }         if (locator != null && localeString != null) {            String language = "", country = "", variant = "";            int postIndex = localeString.indexOf('_');            if (postIndex == -1) {                language = localeString;            } else {                language = localeString.substring(0, postIndex);                int preIndex = postIndex + 1;                postIndex = localeString.indexOf('_', preIndex);                if (postIndex == -1) {                    country = localeString.substring(preIndex);                } else {                    country = localeString.substring(preIndex, postIndex);                    variant = localeString.substring(postIndex + 1);                }            }            Locale locale = new Locale(language, country, variant);            locator = locator.deriveLocator(locale);        }         if (locator == null)            return;         // tell the input context about the change        if (requestInputContext != null) {            requestInputContext.changeInputMethod(locator);            requestInputContext = null;             // remember the selection            putPreferredInputMethod(locator);        }    }     InputMethodLocator findInputMethod(Locale locale) {        // look for preferred input method first        InputMethodLocator locator = getPreferredInputMethod(locale);        if (locator != null) {            return locator;        }         if (hostAdapterLocator != null && hostAdapterLocator.isLocaleAvailable(locale)) {            return hostAdapterLocator.deriveLocator(locale);        }         // Update the locator list        initializeInputMethodLocatorList();         for (int i = 0; i < javaInputMethodLocatorList.size(); i++) {            InputMethodLocator candidate = javaInputMethodLocatorList.get(i);            if (candidate.isLocaleAvailable(locale)) {                return candidate.deriveLocator(locale);            }        }        return null;    }     Locale getDefaultKeyboardLocale() {        Toolkit toolkit = Toolkit.getDefaultToolkit();        if (toolkit instanceof InputMethodSupport) {            return ((InputMethodSupport)toolkit).getDefaultKeyboardLocale();        } else {            return Locale.getDefault();        }    }     /**     * Returns a InputMethodLocator object that the     * user prefers for the given locale.     *     * @param locale Locale for which the user prefers the input method.     */    private synchronized InputMethodLocator getPreferredInputMethod(Locale locale) {        InputMethodLocator preferredLocator = null;         if (!hasMultipleInputMethods()) {            // No need to look for a preferred Java input method            return null;        }         // look for the cached preference first.        preferredLocator = (InputMethodLocator)preferredLocatorCache.get(locale.toString().intern());        if (preferredLocator != null) {            return preferredLocator;        }         // look for the preference in the user preference tree        String nodePath = findPreferredInputMethodNode(locale);        String descriptorName = readPreferredInputMethod(nodePath);        Locale advertised;         // get the locator object        if (descriptorName != null) {            // check for the host adapter first            if (hostAdapterLocator != null &&                hostAdapterLocator.getDescriptor().getClass().getName().equals(descriptorName)) {                advertised = getAdvertisedLocale(hostAdapterLocator, locale);                if (advertised != null) {                    preferredLocator = hostAdapterLocator.deriveLocator(advertised);                    preferredLocatorCache.put(locale.toString().intern(), preferredLocator);                }                return preferredLocator;            }            // look for Java input methods            for (int i = 0; i < javaInputMethodLocatorList.size(); i++) {                InputMethodLocator locator = javaInputMethodLocatorList.get(i);                InputMethodDescriptor descriptor = locator.getDescriptor();                if (descriptor.getClass().getName().equals(descriptorName)) {                    advertised = getAdvertisedLocale(locator, locale);                    if (advertised != null) {                        preferredLocator = locator.deriveLocator(advertised);                        preferredLocatorCache.put(locale.toString().intern(), preferredLocator);                    }                    return preferredLocator;                }            }             // maybe preferred input method information is bogus.            writePreferredInputMethod(nodePath, null);        }         return null;    }     private String findPreferredInputMethodNode(Locale locale) {        if (userRoot == null) {            return null;        }         // create locale node relative path        String nodePath = preferredIMNode + "/" + createLocalePath(locale);         // look for the descriptor        while (!nodePath.equals(preferredIMNode)) {            try {                if (userRoot.nodeExists(nodePath)) {                    if (readPreferredInputMethod(nodePath) != null) {                        return nodePath;                    }                }            } catch (BackingStoreException bse) {            }             // search at parent's node            nodePath = nodePath.substring(0, nodePath.lastIndexOf('/'));        }         return null;    }     private String readPreferredInputMethod(String nodePath) {        if ((userRoot == null) || (nodePath == null)) {            return null;        }         return userRoot.node(nodePath).get(descriptorKey, null);    }     /**     * Writes the preferred input method descriptor class name into     * the user's Preferences tree in accordance with the given locale.     *     * @param inputMethodLocator input method locator to remember.     */    private synchronized void putPreferredInputMethod(InputMethodLocator locator) {        InputMethodDescriptor descriptor = locator.getDescriptor();        Locale preferredLocale = locator.getLocale();         if (preferredLocale == null) {            // check available locales of the input method            try {                Locale[] availableLocales = descriptor.getAvailableLocales();                if (availableLocales.length == 1) {                    preferredLocale = availableLocales[0];                } else {                    // there is no way to know which locale is the preferred one, so do nothing.                    return;                }            } catch (AWTException ae) {                // do nothing here, either.                return;            }        }         // for regions that have only one language, we need to regard        // "xx_YY" as "xx" when putting the preference into tree        if (preferredLocale.equals(Locale.JAPAN)) {            preferredLocale = Locale.JAPANESE;        }        if (preferredLocale.equals(Locale.KOREA)) {            preferredLocale = Locale.KOREAN;        }        if (preferredLocale.equals(new Locale("th", "TH"))) {            preferredLocale = new Locale("th");        }         // obtain node        String path = preferredIMNode + "/" + createLocalePath(preferredLocale);         // write in the preference tree        writePreferredInputMethod(path, descriptor.getClass().getName());        preferredLocatorCache.put(preferredLocale.toString().intern(),            locator.deriveLocator(preferredLocale));         return;    }     private String createLocalePath(Locale locale) {        String language = locale.getLanguage();        String country = locale.getCountry();        String variant = locale.getVariant();        String localePath = null;        if (!variant.equals("")) {            localePath = "_" + language + "/_" + country + "/_" + variant;        } else if (!country.equals("")) {            localePath = "_" + language + "/_" + country;        } else {            localePath = "_" + language;        }         return localePath;    }     private void writePreferredInputMethod(String path, String descriptorName) {        if (userRoot != null) {            Preferences node = userRoot.node(path);             // record it            if (descriptorName != null) {                node.put(descriptorKey, descriptorName);            } else {                node.remove(descriptorKey);            }        }    }     private Preferences getUserRoot() {        return (Preferences)AccessController.doPrivileged(new PrivilegedAction() {            public Object run() {                return Preferences.userRoot();            }        });    }     private Locale getAdvertisedLocale(InputMethodLocator locator, Locale locale) {        Locale advertised = null;         if (locator.isLocaleAvailable(locale)) {            advertised = locale;        } else if (locale.getLanguage().equals("ja")) {            // for Japanese, Korean, and Thai, check whether the input method supports            // language or language_COUNTRY.            if (locator.isLocaleAvailable(Locale.JAPAN)) {                advertised = Locale.JAPAN;            } else if (locator.isLocaleAvailable(Locale.JAPANESE)) {                advertised = Locale.JAPANESE;            }        } else if (locale.getLanguage().equals("ko")) {            if (locator.isLocaleAvailable(Locale.KOREA)) {                advertised = Locale.KOREA;            } else if (locator.isLocaleAvailable(Locale.KOREAN)) {                advertised = Locale.KOREAN;            }        } else if (locale.getLanguage().equals("th")) {            if (locator.isLocaleAvailable(new Locale("th", "TH"))) {                advertised = new Locale("th", "TH");            } else if (locator.isLocaleAvailable(new Locale("th"))) {                advertised = new Locale("th");            }        }         return advertised;    }}

Lines 501–806 of 806. Previous lines