📄 cityinputmethod.java
字号:
/* * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or * without modification, are permitted provided that the following * conditions are met: * * -Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * -Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * Neither the name of Sun Microsystems, Inc. or the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * This software is provided "AS IS," without a warranty of any * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF * THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that Software is not designed, licensed or * intended for use in the design, construction, operation or * maintenance of any nuclear facility. */package com.sun.demos.cityim.internal;import java.awt.AWTEvent;import java.awt.Color;import java.awt.Component;import java.awt.Dimension;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Point;import java.awt.Rectangle;import java.awt.Toolkit;import java.awt.Window;import java.awt.event.ComponentEvent;import java.awt.event.ComponentListener;import java.awt.event.InputEvent;import java.awt.event.InputMethodEvent;import java.awt.event.KeyEvent;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.font.TextAttribute;import java.awt.font.TextHitInfo;import java.awt.im.InputMethodHighlight;import java.awt.im.spi.InputMethod;import java.awt.im.spi.InputMethodContext;import java.io.InputStream;import java.io.IOException;import java.lang.reflect.Method;import java.text.AttributedString;import java.util.HashSet;import java.util.Iterator;import java.util.Locale;import java.util.Properties;import java.util.Vector;import java.security.AccessController;import java.security.PrivilegedAction;import java.security.PrivilegedExceptionAction;import java.security.PrivilegedActionException;import java.text.MessageFormat;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;public class CityInputMethod implements InputMethod { private static Locale YOMI = new Locale("ja", "JP", "YOMI"); private static Locale[] SUPPORTED_LOCALES = { Locale.ENGLISH, Locale.GERMAN, Locale.JAPANESE, YOMI, Locale.SIMPLIFIED_CHINESE, Locale.TRADITIONAL_CHINESE }; private static Locale[] LOOKUP_LOCALES = { Locale.ENGLISH, Locale.GERMAN, Locale.JAPANESE, YOMI, Locale.CHINESE, Locale.SIMPLIFIED_CHINESE, Locale.TRADITIONAL_CHINESE }; // lookup tables - shared by all instances of this input method private static Properties cityNames; private static Properties cityAliases; private static Properties cityLanguages; private static Properties templates; // windows - shared by all instances of this input method private static Window statusWindow; // current or last statusWindow owner instance private static CityInputMethod statusWindowOwner; // true if Solaris style; false if PC style private static boolean attachedStatusWindow = false; // remember the statusWindow location per instance private Rectangle clientWindowLocation; // status window location in PC style private static Point globalStatusWindowLocation; // keep live city input method instances (synchronized using statusWindow) private static HashSet cityInputMethodInstances = new HashSet(5); // non-null if Swing input method JFrame is available static Method methodCreateInputMethodJFrame = null; static { methodCreateInputMethodJFrame = (Method)AccessController.doPrivileged(new PrivilegedAction() { public Object run() { try { Class[] params = new Class[2]; params[0] = String.class; params[1] = Boolean.TYPE; return InputMethodContext.class.getMethod("createInputMethodJFrame", params); } catch (NoSuchMethodException e) { return null; } } }); } // input method window titles static final String statusWindowTitle = "City Input Method"; static final String lookupWindowTitle = "Lookup Window"; // lookup information - per instance private String[] lookupCandidates; private Locale[] lookupLocales; private int lookupCandidateCount; private LookupList lookupList; private int lookupSelection; // per-instance state InputMethodContext inputMethodContext; private boolean active; private boolean disposed; private Locale locale; private boolean converted; private StringBuffer rawText; private String convertedText; private int insertionPoint; private String[] rawTextSegs = null; private String[] convertedSegs = null; private String fmt = null; private int fieldPos[][] = null; private int segPos[][] = null; private int selectedSeg = 0; private int numSegs = 0; private int committedSeg = -1; private int previouslyCommittedCharacterCount = 0; public CityInputMethod() throws IOException { initializeTables(); rawText = new StringBuffer(); } public void setInputMethodContext(InputMethodContext context) { inputMethodContext = context; if (statusWindow == null) { Window sw; if (methodCreateInputMethodJFrame != null) { try { Object[] params = new Object[2]; params[0] = statusWindowTitle; params[1] = Boolean.FALSE; sw = (Window)methodCreateInputMethodJFrame.invoke(context, params); } catch (Exception e) { sw = context.createInputMethodWindow(statusWindowTitle, false); } } else { sw = context.createInputMethodWindow(statusWindowTitle, false); } JLabel label = new JLabel(); label.setOpaque(true); label.setForeground(Color.black); label.setBackground(Color.white); synchronized (this.getClass()) { if (statusWindow == null) { statusWindow = sw; statusWindow.addComponentListener(new ComponentListener() { public void componentResized(ComponentEvent e) {} public void componentMoved(ComponentEvent e) { synchronized (statusWindow) { if (!attachedStatusWindow) { Component comp = e.getComponent(); if (comp.isVisible()) { globalStatusWindowLocation = comp.getLocation(); } } } } public void componentShown(ComponentEvent e) {} public void componentHidden(ComponentEvent e) {} }); label.addMouseListener(new MouseListener() { public void mouseClicked(MouseEvent e) { int count = e.getClickCount(); if (count >= 2) { toggleStatusWindowStyle(); } } public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} }); if (statusWindow instanceof JFrame) { ((JFrame)statusWindow).getContentPane().add(label); } else { statusWindow.add(label); } statusWindowOwner = this; updateStatusWindow(locale); label.setSize(200, 50); statusWindow.pack(); } } } inputMethodContext.enableClientWindowNotification(this, attachedStatusWindow); synchronized (statusWindow) { cityInputMethodInstances.add(this); } } public boolean setLocale(Locale locale) { for (int i = 0; i < SUPPORTED_LOCALES.length; i++) { if (locale.equals(SUPPORTED_LOCALES[i])) { this.locale = locale; if (statusWindow != null) { updateStatusWindow(locale); } return true; } } return false; } public Locale getLocale() { return locale; } void updateStatusWindow(Locale locale) { synchronized (statusWindow) { JLabel label; if (statusWindow instanceof JFrame) { label = (JLabel) ((JFrame)statusWindow).getContentPane().getComponent(0); } else { label = (JLabel) statusWindow.getComponent(0); } String localeName = locale == null ? "null" : locale.getDisplayName(); String text = "Current locale: " + localeName; if (!label.getText().equals(text)) { label.setText(text); statusWindow.pack(); } if (attachedStatusWindow) { if (clientWindowLocation != null) { statusWindow.setLocation(clientWindowLocation.x, clientWindowLocation.y + clientWindowLocation.height); } } else { setPCStyleStatusWindow(); } } } private void setPCStyleStatusWindow() { synchronized (statusWindow) { if (globalStatusWindowLocation == null) { Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); globalStatusWindowLocation = new Point(d.width - statusWindow.getWidth(), d.height - statusWindow.getHeight() - 25); } statusWindow.setLocation(globalStatusWindowLocation.x, globalStatusWindowLocation.y); } } private void setStatusWindowForeground(Color fg) { synchronized (statusWindow) { if (statusWindowOwner != this) { return; } JLabel label; if (statusWindow instanceof JFrame) { label = (JLabel) ((JFrame)statusWindow).getContentPane().getComponent(0); } else { label = (JLabel) statusWindow.getComponent(0); } label.setForeground(fg); } } private void toggleStatusWindowStyle() { synchronized (statusWindow) { if (attachedStatusWindow) { attachedStatusWindow = false; setPCStyleStatusWindow(); } else { attachedStatusWindow = true; } Iterator itr = cityInputMethodInstances.iterator(); while (itr.hasNext()) { CityInputMethod im = (CityInputMethod)itr.next(); im.inputMethodContext.enableClientWindowNotification(im, attachedStatusWindow); } } } public void setCharacterSubsets(Character.Subset[] subsets) { // ignore } public void reconvert() { // not supported yet throw new UnsupportedOperationException(); } public void dispatchEvent(AWTEvent event) { if (!active && (event instanceof KeyEvent)) { System.out.println("CityInputMethod.dispatch called with KeyEvent while not active"); } if (disposed) { System.out.println("CityInputMethod.dispatch called after being disposed");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -