📄 cityinputmethod.java
字号:
} if (!(event instanceof InputEvent)) { System.out.println("CityInputMethod.dispatch called with event that's not an InputEvent"); } /* In case the event has been already consumed or the component is already gone, * simply returns. */ InputEvent inputEvent = (InputEvent) event; Component comp = inputEvent.getComponent(); if (inputEvent.isConsumed() || null == comp || !comp.isShowing()) { return; } int eventType = event.getID(); if (eventType == KeyEvent.KEY_RELEASED) { KeyEvent e = (KeyEvent) event; int keyCode = e.getKeyCode(); if (lookupList != null) { // if candidate window is active if (e.isControlDown()) { if (keyCode == KeyEvent.VK_DOWN) { // Control + Arrow Down commits chunks closeLookupWindow(); commit(selectedSeg); e.consume(); } } else { // select candidate by Arrow Up/Down if (keyCode == KeyEvent.VK_DOWN) { if (++lookupSelection == lookupCandidateCount) { lookupSelection = 0; } selectCandidate(lookupSelection); e.consume(); } else if (keyCode == KeyEvent.VK_UP) { if (--lookupSelection < 0) { lookupSelection = lookupCandidateCount; } selectCandidate(lookupSelection); e.consume(); } } } else { if (e.isControlDown()) { if (keyCode == KeyEvent.VK_DOWN) { // Control + Arrow Down commits chunks commit(selectedSeg); e.consume(); } } else { // move selected segment by Arrow Right/Left if ((keyCode == KeyEvent.VK_RIGHT) && (converted == true)) { if (selectedSeg < (numSegs - 1)) { selectedSeg++; sendText(false); e.consume(); } else { Toolkit.getDefaultToolkit().beep(); } } else if ((keyCode == KeyEvent.VK_LEFT) && (converted == true)) { if (selectedSeg > (committedSeg + 1)) { selectedSeg--; sendText(false); e.consume(); } else { Toolkit.getDefaultToolkit().beep(); } } } } } else if (eventType == MouseEvent.MOUSE_CLICKED) { MouseEvent e = (MouseEvent) event; Point pnt = comp.getLocationOnScreen(); int x = (int)pnt.getX() + e.getX(); int y = (int)pnt.getY() + e.getY(); TextHitInfo hit = inputMethodContext.getLocationOffset(x,y); if (hit != null) { // within composed text if (converted) { selectedSeg = findSegment(hit.getInsertionIndex()); sendText(false); e.consume(); } else { insertionPoint = hit.getInsertionIndex(); } } else { // if hit outside composition, simply commit all. commitAll(); } } else if (eventType == KeyEvent.KEY_TYPED) { KeyEvent e = (KeyEvent) event; if (handleCharacter(e.getKeyChar())) { e.consume(); } } else if (eventType == KeyEvent.KEY_PRESSED) { KeyEvent e = (KeyEvent) event; if ((e.getKeyCode() == KeyEvent.VK_ENTER) && (lookupList != null || converted || rawText.length() != 0)) { e.consume(); } } } /** * find segment at insertion point */ int findSegment(int insertion) { for (int i = committedSeg + 1; i < numSegs; i++) { if ((segPos[i][0] < insertion) && (insertion < segPos[i][1])) { return i; } } return 0; } public void activate() { if (active) { System.out.println("CityInputMethod.activate called while active"); } active = true; synchronized (statusWindow) { statusWindowOwner = this; updateStatusWindow(locale); if (!statusWindow.isVisible()) { statusWindow.setVisible(true); } setStatusWindowForeground(Color.black); } } public void deactivate(boolean isTemporary) { closeLookupWindow(); if (!active) { System.out.println("CityInputMethod.deactivate called while not active"); } setStatusWindowForeground(Color.lightGray); active = false; } public void hideWindows() { if (active) { System.out.println("CityInputMethod.hideWindows called while active"); } closeLookupWindow(); synchronized (statusWindow) { if (statusWindowOwner == this) { statusWindow.setVisible(false); } } } public void removeNotify() { } public void endComposition() { if (rawText.length() != 0) { commitAll(); } closeLookupWindow(); } public void notifyClientWindowChange(Rectangle location) { clientWindowLocation = location; synchronized (statusWindow) { if (!attachedStatusWindow || statusWindowOwner != this) { return; } if (location != null) { statusWindow.setLocation(location.x, location.y+location.height); if (!statusWindow.isVisible()) { if (active) { setStatusWindowForeground(Color.black); } else { setStatusWindowForeground(Color.lightGray); } statusWindow.setVisible(true); } } else { statusWindow.setVisible(false); } } } public void dispose() { if (active) { System.out.println("CityInputMethod.dispose called while active"); } if (disposed) { System.out.println("CityInputMethod.disposed called repeatedly"); } closeLookupWindow(); synchronized (statusWindow) { cityInputMethodInstances.remove(this); if (cityInputMethodInstances.isEmpty()) { statusWindow.dispose(); } } disposed = true; } public Object getControlObject() { return null; } public void setCompositionEnabled(boolean enable) { // not supported yet throw new UnsupportedOperationException(); } public boolean isCompositionEnabled() { // always enabled return true; } private void initializeTables() throws IOException { synchronized (this.getClass()) { if (templates == null) { cityNames = loadProperties("names.properties"); cityAliases = loadProperties("aliases.properties"); cityLanguages = loadProperties("languages.properties"); templates = loadProperties("templates.properties"); } } } /** Use doPrivileged block to avoid the security exceptions * since the user code which triggers this action, e.g. * open IM selection menu from a ColorChooserDialog, may in the call stack. * Since reading properties file is relatively safe since it is located in the * jar file itself, not from user's local file system. * @exception IOException when reading properties file fails. */ private Properties loadProperties(final String fileName) throws IOException { try { return (Properties) AccessController.doPrivileged(new PrivilegedExceptionAction() { public Object run() throws IOException { Properties props = new Properties(); InputStream stream = this.getClass().getResourceAsStream(fileName); props.load(stream); stream.close(); return props; } }); } catch(PrivilegedActionException pae) { throw (IOException)pae.getCause(); } } /** * Attempts to handle a typed character. * @return whether the character was handled */ private boolean handleCharacter(char ch) { if (lookupList != null) { if (ch == ' ') { if (++lookupSelection == lookupCandidateCount) { lookupSelection = 0; } selectCandidate(lookupSelection); return true; } else if (ch == '\n') { commitAll(); closeLookupWindow(); return true; } else if ('1' <= ch && ch <= '0' + lookupCandidateCount) { selectCandidate(ch - '1'); closeLookupWindow(); return true; } else { Toolkit.getDefaultToolkit().beep(); return true; } } else if (converted) { if (ch == ' ') { convertAgain(); return true; } else if (ch == '\n') { commitAll(); return true; } else { Toolkit.getDefaultToolkit().beep(); return true; } } else { if (ch == ' ') { int length = rawText.length(); if (length == 3 || length == 6 || length == 9) { convertFirstTime(); return true; } } else if (ch == '\n') { if (rawText.length() != 0) { commitAll(); return true; } } else if (ch == '\b') { if (insertionPoint > 0) { rawText.deleteCharAt(insertionPoint - 1); --insertionPoint; sendText(false); return true; } } else if ('a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z') { rawText.insert(insertionPoint++, ch); sendText(false); return true; } if (rawText.length() != 0) { Toolkit.getDefaultToolkit().beep(); return true; } } return false; } /* * Looks up the entry for key in the given table, taken the * input method's locale into consideration. */ String lookup(String lookupName, Properties table) { String result = null; String localeLookupName = lookupName + "_" + locale; while (true) { result = (String) table.get(localeLookupName); if (result != null) { break; } int index = localeLookupName.lastIndexOf("_"); if (index == -1) { break; } localeLookupName = localeLookupName.substring(0, index);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -