📄 lookup.java
字号:
/* * File: Lookup.java * Project: MPI Linguistic Application * Date: 02 May 2007 * * Copyright (C) 2001-2007 Max Planck Institute for Psycholinguistics * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *//* * $Id: Lookup.java,v 1.2 2005/11/24 08:56:03 hasloe Exp $ *//** * This class is an implementation of the java.awt.im.spi.InputMethod. The * locales which defined here will be available at the JVM, if the class is * inside on of the extension folders at startup. The input methods * provided by this class share one property: all have over-the-spot * lookup windows which are composed by a word or a character. The * lookupwindow contains a list of characters. The user selects a * character from the lookupwindow by pressing enter or space. The user * navigates through the lookupwindow with up, down, and PgUp and PgDn. */package mpi.eudico.client.im.spi.lookup;import java.awt.AWTEvent;import java.awt.Color;import java.awt.Dimension;import java.awt.Label;import java.awt.Rectangle;import java.awt.Toolkit;import java.awt.Window;import java.awt.event.InputMethodEvent;import java.awt.event.KeyEvent;import java.awt.event.MouseEvent;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.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.text.AttributedString;import java.util.ArrayList;import java.util.HashMap;import java.util.Hashtable;import java.util.Locale;/** * DOCUMENT ME! * * @author $Author: hasloe $ * @version $Revision: 1.2 $ */public class Lookup implements InputMethod { /** The size of the lookup window in elements. */ static final int lookupWindowSizeInElements = 10; /** * The International Phonetic Association has standardised a phonetic * alphabet. These constants refer to the IPA alphabet in the revision as * of 1996. The default input method is RTR. * * <p> * Here is the only place, where the IPA-96 locales should be defined. All * other Eudico classes should refer to this constant. Locales are using * lowercase letters only. This is described somewhere. Suns default * namings for the two chinese locales are politically stupid. I therefore * have to define my one names. Strange enough, in the upper left icon, * the name "Simplified..." is used, but I cannot find a way in the API to * display it... I may have time to sort that out later * </p> */ public static final Locale IPA96_RTR = new Locale("ipa-96", "", "rtr"); /** Holds value of property DOCUMENT ME! */ public static final Locale CHINESE_SIM = new Locale("chinese", "", "simplified"); /** Holds value of property DOCUMENT ME! */ public static final Locale CHINESE_TRA = new Locale("chinese", "", "traditional"); /** * This array defines the locales for which input methods are implemented. */ static Locale[] SUPPORTED_LOCALES = { Lookup.IPA96_RTR, Lookup.CHINESE_SIM, Lookup.CHINESE_TRA }; //Locale.SIMPLIFIED_CHINESE, //Locale.TRADITIONAL_CHINESE /** resources for the locales. */ private static Hashtable hashedFilenames; static { hashedFilenames = new java.util.Hashtable(); hashedFilenames.put(Lookup.SUPPORTED_LOCALES[0], "ipa96.u8"); hashedFilenames.put(Lookup.SUPPORTED_LOCALES[1], "PinyinSC.u8"); hashedFilenames.put(Lookup.SUPPORTED_LOCALES[2], "PinyinTC.u8"); } //private Common common; START // windows - shared by all instances of this input method private static Window statusWindow; private static Label statusWindowLabel; private static boolean statusWindowIsShown = false; private static HashMap pinyinHash; // lookup information - per instance private String[] lookupCandidates; private Locale[] lookupLocales; private int lookupCandidateCount; private int lookupCandidateIndex; // the number of elements in the lookup window private LookupList lookupList; private int lookupSelection; // the index in the current window... bad! // per-instance state private InputMethodContext inputMethodContext; //private boolean active; // not yet used private Locale locale; private boolean converted; private StringBuffer rawText = new StringBuffer(); private String convertedText; //private Common common; STOP /** * @see java.awt.im.spi.InputMethod */ public Lookup() throws IOException, UnsupportedEncodingException { } /** * DOCUMENT ME! * * @param s DOCUMENT ME! */ private static final void debugln(String s) { // System.getProperty("debug") seems to return null on Linux if "debug" not defined if (!"true".equals(System.getProperty("debug"))) { return; } System.out.println(s); } /** * DOCUMENT ME! * * @param locale DOCUMENT ME! */ private final void updateStatusWindow(Locale locale) { if (statusWindowLabel == null) { return; // gnagnagna } statusWindowLabel.setText((locale == null) ? "default" : locale.getDisplayName()); } /** * Sideeffect: pinyinHash Precondition: locale is not null and known * Postcondition: pinyinHash is set up. Errorcondition: missig resource * --> IOException * * @param locale DOCUMENT ME! * * @throws IOException DOCUMENT ME! */ private final void initializeHash(Locale locale) throws IOException { synchronized (getClass()) { Lookup.pinyinHash = new HashMap(); // Read UTF8 character stream BufferedReader datafile = new BufferedReader(new InputStreamReader( getClass().getResourceAsStream((String) hashedFilenames.get( locale)), "UTF8")); String buffer; while ((buffer = datafile.readLine()) != null) { int index = buffer.indexOf("\t"); String pinyin = buffer.substring(0, index); ArrayList newlist = new ArrayList(); int oldindex = index + 1; do { index = buffer.indexOf(" ", oldindex); if (index == -1) { index = buffer.length(); } String hanzi = buffer.substring(oldindex, index); if (hanzi.length() > 0) { newlist.add(hanzi); } oldindex = index + 1; } while (oldindex < buffer.length()); Lookup.pinyinHash.put(pinyin.intern(), newlist); } datafile.close(); } } /** * Add this character to the raw text and look it up in the hash. * * @param ch DOCUMENT ME! * * @return DOCUMENT ME! */ private final boolean wordResultsInHash(char ch) { return pinyinHash.containsKey(this.rawText + new Character(ch).toString()); } /** * DOCUMENT ME! * * @param ch DOCUMENT ME! * * @return DOCUMENT ME! */ private final boolean lookupCharacter(char ch) { if (wordResultsInHash(ch)) { /* The user starts a pinyin word. A lookup list will be opened. */ rawText.append(ch); sendText(false); return true; } return false; } /** * smart scrolling * * @param up DOCUMENT ME! */ private final void UpDownHandler(boolean up) { debugln("lookupCandidateIndex " + lookupCandidateIndex); debugln("lookupCandidateCount " + lookupCandidateCount); debugln("lookupSelection " + lookupSelection); if (up) { // are we allowed to move up? if ((lookupSelection + lookupCandidateIndex) == 0) { return; } // smart scroll if ((lookupSelection == 1) && (lookupCandidateIndex != 0)) { scrollHandler(up, 1); // you moved the elements: don't move the cursor return; } } else { // are we allowed to move down? if ((lookupCandidateCount - lookupCandidateIndex) < lookupWindowSizeInElements) { // we are the last scroll window, which will not be filled // completely with elements in general. if ((lookupSelection + lookupCandidateIndex) == (lookupCandidateCount - 1)) { return; } } else { // there is more scroll space if (lookupSelection == (lookupWindowSizeInElements - 2)) { // we will let the elements move backward scrollHandler(up, 1); // so we have to keep the cursor still return; } // end of the road if (lookupSelection == (lookupWindowSizeInElements - 1)) { return; } } } // change variable lookupSelection -= (up ? 1 : (-1)); lookupList.selectCandidate(lookupSelection); } /** * scroll the lookup window for the size of the lookup window * * @param up whether to scroll up or down */ private final void scrollHandler(boolean up) { scrollHandler(up, Lookup.lookupWindowSizeInElements); } /** * scroll the lookup window for a given number of elements * * @param up whether to scroll up or down * @param jumpsize number of elements to scroll */ private final void scrollHandler(boolean up, int jumpsize) { if (up) { // Move look up list back, if possible if ((lookupCandidateIndex - jumpsize) >= 0) { lookupCandidateIndex -= jumpsize; lookupList.updateCandidates(lookupCandidateIndex); } else { int scroll = jumpsize - lookupCandidateIndex; lookupCandidateIndex = 0; lookupList.updateCandidates(lookupCandidateIndex); if ((lookupSelection - scroll) > 0) { lookupSelection -= scroll; } else { lookupSelection = 0; } lookupList.selectCandidate(lookupSelection); } } else { // Move look up list forward, if possible if ((lookupCandidateIndex + jumpsize) < lookupCandidateCount) { int scroll = 0; if ((lookupCandidateIndex + jumpsize) > (lookupCandidateCount - jumpsize)) { scroll = lookupCandidateCount - lookupCandidateIndex - jumpsize - 1; } lookupCandidateIndex += jumpsize; if (lookupCandidateIndex > (lookupCandidateCount - jumpsize + 1)) { lookupCandidateIndex = lookupCandidateCount - jumpsize + 1; } lookupList.updateCandidates(lookupCandidateIndex); // the last scrolled lookup windows is not completely filled in general lookupSelection += scroll; if ((lookupSelection + lookupCandidateIndex) >= lookupCandidateCount) { lookupSelection = lookupCandidateCount - 1 - lookupCandidateIndex; } lookupList.selectCandidate(lookupSelection); } else { lookupCandidateIndex = lookupCandidateCount - jumpsize + 1; lookupList.updateCandidates(lookupCandidateIndex); lookupSelection = lookupCandidateCount - 1 - lookupCandidateIndex; lookupList.selectCandidate(lookupSelection); } } } /** * Handle non-character keys, such as arrows * * @param e DOCUMENT ME! */ private final void handlePressedKey(KeyEvent e) { if (lookupList == null) { return; } /* There is a lookup list. The user continues a pinyin word */ // Two _KP_ (keypad) constants added for Linux support switch (e.getKeyCode()) { case KeyEvent.VK_UP: UpDownHandler(true); break; case KeyEvent.VK_KP_UP: UpDownHandler(true); break; // Linux? case KeyEvent.VK_DOWN: UpDownHandler(false); break; case KeyEvent.VK_KP_DOWN: UpDownHandler(false); break; // Linux? case KeyEvent.VK_PAGE_UP: scrollHandler(true); break; case KeyEvent.VK_PAGE_DOWN: scrollHandler(false); break; //case KeyEvent.VK_CONTROL: System.out.println("hello control");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -