提交 a4166090 编写于 作者: M mrkam

7027693: /jfc/CodePointIM demo needs to be improved

Reviewed-by: alexp
上级 ed328ea1
/* /*
* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
...@@ -29,12 +29,12 @@ ...@@ -29,12 +29,12 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
/*
*/
import java.util.Locale; import java.util.Locale;
import java.util.MissingResourceException; import java.util.MissingResourceException;
import java.util.ResourceBundle; import java.util.ResourceBundle;
public class CodePointIM { public class CodePointIM {
// Actually, the main method is not required for InputMethod. // Actually, the main method is not required for InputMethod.
...@@ -42,11 +42,11 @@ public class CodePointIM { ...@@ -42,11 +42,11 @@ public class CodePointIM {
// not correct and encourage their reading README.txt. // not correct and encourage their reading README.txt.
public static void main(String[] args) { public static void main(String[] args) {
try { try {
ResourceBundle resource = ResourceBundle.getBundle("resources.codepoint", ResourceBundle resource = ResourceBundle.getBundle(
"resources.codepoint",
Locale.getDefault()); Locale.getDefault());
System.err.println(resource.getString("warning")); System.err.println(resource.getString("warning"));
} } catch (MissingResourceException e) {
catch (MissingResourceException e) {
System.err.println(e.toString()); System.err.println(e.toString());
} }
......
/* /*
* Copyright (c) 2002, 2004, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
...@@ -28,13 +28,8 @@ ...@@ -28,13 +28,8 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
/*
*/
package com.sun.inputmethods.internal.codepointim; package com.sun.inputmethods.internal.codepointim;
import java.text.AttributedCharacterIterator;
import java.util.Map;
import java.awt.AWTEvent; import java.awt.AWTEvent;
import java.awt.Toolkit; import java.awt.Toolkit;
...@@ -50,6 +45,7 @@ import java.io.IOException; ...@@ -50,6 +45,7 @@ import java.io.IOException;
import java.text.AttributedString; import java.text.AttributedString;
import java.util.Locale; import java.util.Locale;
/** /**
* The Code Point Input Method is a simple input method that allows Unicode * The Code Point Input Method is a simple input method that allows Unicode
* characters to be entered using their code point or code unit values. See the * characters to be entered using their code point or code unit values. See the
...@@ -63,14 +59,12 @@ public class CodePointInputMethod implements InputMethod { ...@@ -63,14 +59,12 @@ public class CodePointInputMethod implements InputMethod {
private static final int ESCAPE = 1; // \u0000 - \uFFFF private static final int ESCAPE = 1; // \u0000 - \uFFFF
private static final int SPECIAL_ESCAPE = 2; // \U000000 - \U10FFFF private static final int SPECIAL_ESCAPE = 2; // \U000000 - \U10FFFF
private static final int SURROGATE_PAIR = 3; // \uD800\uDC00 - \uDBFF\uDFFF private static final int SURROGATE_PAIR = 3; // \uD800\uDC00 - \uDBFF\uDFFF
private InputMethodContext context; private InputMethodContext context;
private Locale locale; private Locale locale;
private StringBuffer buffer; private StringBuffer buffer;
private int insertionPoint; private int insertionPoint;
private int format = UNSET; private int format = UNSET;
public CodePointInputMethod() throws IOException { public CodePointInputMethod() throws IOException {
} }
...@@ -221,7 +215,7 @@ public class CodePointInputMethod implements InputMethod { ...@@ -221,7 +215,7 @@ public class CodePointInputMethod implements InputMethod {
private void waitDigit2(char c) { private void waitDigit2(char c) {
if (Character.digit(c, 16) != -1) { if (Character.digit(c, 16) != -1) {
buffer.insert(insertionPoint++, c); buffer.insert(insertionPoint++, c);
char codePoint = (char)getCodePoint(buffer, 2, 5); char codePoint = (char) getCodePoint(buffer, 2, 5);
if (Character.isHighSurrogate(codePoint)) { if (Character.isHighSurrogate(codePoint)) {
format = SURROGATE_PAIR; format = SURROGATE_PAIR;
buffer.append("\\u"); buffer.append("\\u");
...@@ -383,7 +377,7 @@ public class CodePointInputMethod implements InputMethod { ...@@ -383,7 +377,7 @@ public class CodePointInputMethod implements InputMethod {
private void finishComposition() { private void finishComposition() {
int len = buffer.length(); int len = buffer.length();
if (len == 6 && format != SPECIAL_ESCAPE) { if (len == 6 && format != SPECIAL_ESCAPE) {
char codePoint = (char)getCodePoint(buffer, 2, 5); char codePoint = (char) getCodePoint(buffer, 2, 5);
if (Character.isValidCodePoint(codePoint) && codePoint != 0xFFFF) { if (Character.isValidCodePoint(codePoint) && codePoint != 0xFFFF) {
buffer.setLength(0); buffer.setLength(0);
buffer.append(codePoint); buffer.append(codePoint);
...@@ -400,11 +394,11 @@ public class CodePointInputMethod implements InputMethod { ...@@ -400,11 +394,11 @@ public class CodePointInputMethod implements InputMethod {
} }
} else if (len == 12 && format == SURROGATE_PAIR) { } else if (len == 12 && format == SURROGATE_PAIR) {
char[] codePoint = { char[] codePoint = {
(char)getCodePoint(buffer, 2, 5), (char) getCodePoint(buffer, 2, 5),
(char)getCodePoint(buffer, 8, 11) (char) getCodePoint(buffer, 8, 11)
}; };
if (Character.isHighSurrogate(codePoint[0]) && if (Character.isHighSurrogate(codePoint[0]) && Character.
Character.isLowSurrogate(codePoint[1])) { isLowSurrogate(codePoint[1])) {
buffer.setLength(0); buffer.setLength(0);
buffer.append(codePoint); buffer.append(codePoint);
sendCommittedText(); sendCommittedText();
...@@ -418,7 +412,7 @@ public class CodePointInputMethod implements InputMethod { ...@@ -418,7 +412,7 @@ public class CodePointInputMethod implements InputMethod {
private int getCodePoint(StringBuffer sb, int from, int to) { private int getCodePoint(StringBuffer sb, int from, int to) {
int value = 0; int value = 0;
for (int i = from; i <= to; i++) { for (int i = from; i <= to; i++) {
value = (value<<4) + Character.digit(sb.charAt(i), 16); value = (value << 4) + Character.digit(sb.charAt(i), 16);
} }
return value; return value;
} }
...@@ -427,7 +421,6 @@ public class CodePointInputMethod implements InputMethod { ...@@ -427,7 +421,6 @@ public class CodePointInputMethod implements InputMethod {
Toolkit.getDefaultToolkit().beep(); Toolkit.getDefaultToolkit().beep();
} }
public void activate() { public void activate() {
if (buffer == null) { if (buffer == null) {
buffer = new StringBuffer(12); buffer = new StringBuffer(12);
......
/* /*
* Copyright (c) 2002, 2004, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
...@@ -28,17 +28,15 @@ ...@@ -28,17 +28,15 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
/*
*/
package com.sun.inputmethods.internal.codepointim; package com.sun.inputmethods.internal.codepointim;
import java.awt.Image; import java.awt.Image;
import java.awt.im.spi.InputMethodDescriptor; import java.awt.im.spi.InputMethodDescriptor;
import java.awt.im.spi.InputMethod; import java.awt.im.spi.InputMethod;
import java.util.Locale; import java.util.Locale;
/** /**
* The CodePointInputMethod is a simple input method that allows Unicode * The CodePointInputMethod is a simple input method that allows Unicode
* characters to be entered via their hexadecimal code point values. * characters to be entered via their hexadecimal code point values.
...@@ -68,12 +66,12 @@ public class CodePointInputMethodDescriptor implements InputMethodDescriptor { ...@@ -68,12 +66,12 @@ public class CodePointInputMethodDescriptor implements InputMethodDescriptor {
*/ */
public Locale[] getAvailableLocales() { public Locale[] getAvailableLocales() {
Locale[] locales = { Locale[] locales = {
new Locale("","",""), new Locale("", "", ""), };
};
return locales; return locales;
} }
public synchronized String getInputMethodDisplayName(Locale inputLocale, Locale displayLanguage) { public synchronized String getInputMethodDisplayName(Locale inputLocale,
Locale displayLanguage) {
return "CodePoint Input Method"; return "CodePoint Input Method";
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册