J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1998-1999 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.awt.im; |
| 27 | |
| 28 | import java.awt.AWTException; |
| 29 | import java.awt.im.spi.InputMethodDescriptor; |
| 30 | import java.util.Locale; |
| 31 | |
| 32 | /** |
| 33 | * Provides complete information to make and handle the selection |
| 34 | * of an input method and a locale. Immutable class. |
| 35 | */ |
| 36 | final class InputMethodLocator { |
| 37 | |
| 38 | private InputMethodDescriptor descriptor; |
| 39 | |
| 40 | // Currently `loader' is always the class loader for a |
| 41 | // descriptor. `loader' is provided for future extensions to be |
| 42 | // able to load input methods from somewhere else, and to support |
| 43 | // per input method name space. |
| 44 | private ClassLoader loader; |
| 45 | |
| 46 | private Locale locale; |
| 47 | |
| 48 | InputMethodLocator(InputMethodDescriptor descriptor, ClassLoader loader, Locale locale) { |
| 49 | if (descriptor == null) { |
| 50 | throw new NullPointerException("descriptor can't be null"); |
| 51 | } |
| 52 | this.descriptor = descriptor; |
| 53 | this.loader = loader; |
| 54 | this.locale = locale; |
| 55 | } |
| 56 | |
| 57 | public boolean equals(Object other) { |
| 58 | if (other == this) { |
| 59 | return true; |
| 60 | } |
| 61 | if (other == null || this.getClass() != other.getClass()) { |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | InputMethodLocator otherLocator = (InputMethodLocator) other; |
| 66 | if (!descriptor.getClass().equals(otherLocator.descriptor.getClass())) { |
| 67 | return false; |
| 68 | } |
| 69 | if (loader == null && otherLocator.loader != null |
| 70 | || loader != null && !loader.equals(otherLocator.loader)) { |
| 71 | return false; |
| 72 | } |
| 73 | if (locale == null && otherLocator.locale != null |
| 74 | || locale != null && !locale.equals(otherLocator.locale)) { |
| 75 | return false; |
| 76 | } |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | public int hashCode() { |
| 81 | int result = descriptor.hashCode(); |
| 82 | if (loader != null) { |
| 83 | result |= loader.hashCode() << 10; |
| 84 | } |
| 85 | if (locale != null) { |
| 86 | result |= locale.hashCode() << 20; |
| 87 | } |
| 88 | return result; |
| 89 | } |
| 90 | |
| 91 | InputMethodDescriptor getDescriptor() { |
| 92 | return descriptor; |
| 93 | } |
| 94 | |
| 95 | ClassLoader getClassLoader() { |
| 96 | return loader; |
| 97 | } |
| 98 | |
| 99 | Locale getLocale() { |
| 100 | return locale; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Returns whether support for locale is available from |
| 105 | * the input method. |
| 106 | */ |
| 107 | boolean isLocaleAvailable(Locale locale) { |
| 108 | try { |
| 109 | Locale[] locales = descriptor.getAvailableLocales(); |
| 110 | for (int i = 0; i < locales.length; i++) { |
| 111 | if (locales[i].equals(locale)) { |
| 112 | return true; |
| 113 | } |
| 114 | } |
| 115 | } catch (AWTException e) { |
| 116 | // treat this as no locale available |
| 117 | } |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Returns an input method locator that has locale forLocale, |
| 123 | * but otherwise the same data as this locator. Does not |
| 124 | * check whether the input method actually supports forLocale - |
| 125 | * use {@link #isLocaleAvailable} for that. |
| 126 | */ |
| 127 | InputMethodLocator deriveLocator(Locale forLocale) { |
| 128 | if (forLocale == locale) { |
| 129 | return this; |
| 130 | } else { |
| 131 | return new InputMethodLocator(descriptor, loader, forLocale); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Returns whether this and other describe the same input method |
| 137 | * engine, ignoring the locale setting. |
| 138 | */ |
| 139 | boolean sameInputMethod(InputMethodLocator other) { |
| 140 | if (other == this) { |
| 141 | return true; |
| 142 | } |
| 143 | if (other == null) { |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | if (!descriptor.getClass().equals(other.descriptor.getClass())) { |
| 148 | return false; |
| 149 | } |
| 150 | if (loader == null && other.loader != null |
| 151 | || loader != null && !loader.equals(other.loader)) { |
| 152 | return false; |
| 153 | } |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Returns a string that can be used as an action command string. |
| 159 | * The first part of the string identifies the input method; it does |
| 160 | * not include '\n'. If getLocale is not null, getLocale().toString() |
| 161 | * is appended, separated by '\n'. |
| 162 | */ |
| 163 | String getActionCommandString() { |
| 164 | String inputMethodString = descriptor.getClass().getName(); |
| 165 | if (locale == null) { |
| 166 | return inputMethodString; |
| 167 | } else { |
| 168 | return inputMethodString + "\n" + locale.toString(); |
| 169 | } |
| 170 | } |
| 171 | } |