0N/A/*
2362N/A * Copyright (c) 1998, 1999, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.awt.im;
0N/A
0N/Aimport java.awt.AWTException;
0N/Aimport java.awt.im.spi.InputMethodDescriptor;
0N/Aimport java.util.Locale;
0N/A
0N/A/**
0N/A * Provides complete information to make and handle the selection
0N/A * of an input method and a locale. Immutable class.
0N/A */
0N/Afinal class InputMethodLocator {
0N/A
0N/A private InputMethodDescriptor descriptor;
0N/A
0N/A // Currently `loader' is always the class loader for a
0N/A // descriptor. `loader' is provided for future extensions to be
0N/A // able to load input methods from somewhere else, and to support
0N/A // per input method name space.
0N/A private ClassLoader loader;
0N/A
0N/A private Locale locale;
0N/A
0N/A InputMethodLocator(InputMethodDescriptor descriptor, ClassLoader loader, Locale locale) {
0N/A if (descriptor == null) {
0N/A throw new NullPointerException("descriptor can't be null");
0N/A }
0N/A this.descriptor = descriptor;
0N/A this.loader = loader;
0N/A this.locale = locale;
0N/A }
0N/A
0N/A public boolean equals(Object other) {
0N/A if (other == this) {
0N/A return true;
0N/A }
0N/A if (other == null || this.getClass() != other.getClass()) {
0N/A return false;
0N/A }
0N/A
0N/A InputMethodLocator otherLocator = (InputMethodLocator) other;
0N/A if (!descriptor.getClass().equals(otherLocator.descriptor.getClass())) {
0N/A return false;
0N/A }
0N/A if (loader == null && otherLocator.loader != null
0N/A || loader != null && !loader.equals(otherLocator.loader)) {
0N/A return false;
0N/A }
0N/A if (locale == null && otherLocator.locale != null
0N/A || locale != null && !locale.equals(otherLocator.locale)) {
0N/A return false;
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A public int hashCode() {
0N/A int result = descriptor.hashCode();
0N/A if (loader != null) {
0N/A result |= loader.hashCode() << 10;
0N/A }
0N/A if (locale != null) {
0N/A result |= locale.hashCode() << 20;
0N/A }
0N/A return result;
0N/A }
0N/A
0N/A InputMethodDescriptor getDescriptor() {
0N/A return descriptor;
0N/A }
0N/A
0N/A ClassLoader getClassLoader() {
0N/A return loader;
0N/A }
0N/A
0N/A Locale getLocale() {
0N/A return locale;
0N/A }
0N/A
0N/A /**
0N/A * Returns whether support for locale is available from
0N/A * the input method.
0N/A */
0N/A boolean isLocaleAvailable(Locale locale) {
0N/A try {
0N/A Locale[] locales = descriptor.getAvailableLocales();
0N/A for (int i = 0; i < locales.length; i++) {
0N/A if (locales[i].equals(locale)) {
0N/A return true;
0N/A }
0N/A }
0N/A } catch (AWTException e) {
0N/A // treat this as no locale available
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Returns an input method locator that has locale forLocale,
0N/A * but otherwise the same data as this locator. Does not
0N/A * check whether the input method actually supports forLocale -
0N/A * use {@link #isLocaleAvailable} for that.
0N/A */
0N/A InputMethodLocator deriveLocator(Locale forLocale) {
0N/A if (forLocale == locale) {
0N/A return this;
0N/A } else {
0N/A return new InputMethodLocator(descriptor, loader, forLocale);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns whether this and other describe the same input method
0N/A * engine, ignoring the locale setting.
0N/A */
0N/A boolean sameInputMethod(InputMethodLocator other) {
0N/A if (other == this) {
0N/A return true;
0N/A }
0N/A if (other == null) {
0N/A return false;
0N/A }
0N/A
0N/A if (!descriptor.getClass().equals(other.descriptor.getClass())) {
0N/A return false;
0N/A }
0N/A if (loader == null && other.loader != null
0N/A || loader != null && !loader.equals(other.loader)) {
0N/A return false;
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Returns a string that can be used as an action command string.
0N/A * The first part of the string identifies the input method; it does
0N/A * not include '\n'. If getLocale is not null, getLocale().toString()
0N/A * is appended, separated by '\n'.
0N/A */
0N/A String getActionCommandString() {
0N/A String inputMethodString = descriptor.getClass().getName();
0N/A if (locale == null) {
0N/A return inputMethodString;
0N/A } else {
0N/A return inputMethodString + "\n" + locale.toString();
0N/A }
0N/A }
0N/A}