InputMethodEvent.CARET_POSITION_CHANGED : InputMethodEvent « java.awt.im.spi « Java by API

Java by API
1. com.sun.image.codec.jpeg
2. java.applet
3. java.awt
4. java.awt.datatransfer
5. java.awt.dnd
6. java.awt.event
7. java.awt.font
8. java.awt.geom
9. java.awt.im.spi
10. java.awt.image
11. java.awt.print
12. java.beans
13. java.beans.beancontext
14. java.io
15. java.lang
16. java.lang.annotation
17. java.lang.instrument
18. java.lang.management
19. java.lang.ref
20. java.lang.reflect
21. java.math
22. java.net
23. java.nio
24. java.nio.channels
25. java.nio.charset
26. java.rmi.dgc
27. java.rmi.server
28. java.security
29. java.security.cert
30. java.security.spec
31. java.sql
32. java.text
33. java.text.spi
34. java.util
35. java.util.concurrent
36. java.util.concurrent.atomic
37. java.util.concurrent.locks
38. java.util.jar
39. java.util.logging
40. java.util.prefs
41. java.util.regex
42. java.util.spi
43. java.util.zip
44. javax.accessibility
45. javax.activation
46. javax.annotation.security
47. javax.comm
48. javax.crypto
49. javax.crypto.spec
50. javax.ejb
51. javax.imageio
52. javax.imageio.event
53. javax.jws
54. javax.mail
55. javax.mail.internet
56. javax.media.jai
57. javax.microedition.io
58. javax.microedition.lcdui
59. javax.microedition.media
60. javax.microedition.media.control
61. javax.microedition.midlet
62. javax.microedition.pim
63. javax.microedition.rms
64. javax.naming
65. javax.naming.directory
66. javax.naming.event
67. javax.naming.ldap
68. javax.naming.spi
69. javax.net
70. javax.net.ssl
71. javax.persistence
72. javax.print
73. javax.print.attribute
74. javax.print.attribute.standard
75. javax.print.event
76. javax.script
77. javax.servlet
78. javax.servlet.http
79. javax.sound.midi
80. javax.sound.sampled
81. javax.sql
82. javax.sql.rowset
83. javax.swing
84. javax.swing.border
85. javax.swing.colorchooser
86. javax.swing.event
87. javax.swing.filechooser
88. javax.swing.plaf.basic
89. javax.swing.plaf.metal
90. javax.swing.plaf.synth
91. javax.swing.table
92. javax.swing.text
93. javax.swing.text.html
94. javax.swing.text.html.parser
95. javax.swing.text.rtf
96. javax.swing.tree
97. javax.swing.undo
98. javax.tools
99. javax.transaction
100. javax.xml
101. javax.xml.bind
102. javax.xml.bind.annotation
103. javax.xml.crypto.dsig
104. javax.xml.crypto.dsig.keyinfo
105. javax.xml.namespace
106. javax.xml.parsers
107. javax.xml.soap
108. javax.xml.stream
109. javax.xml.stream.events
110. javax.xml.transform
111. javax.xml.transform.dom
112. javax.xml.transform.stream
113. javax.xml.validation
114. javax.xml.ws
115. javax.xml.xpath
116. junit.extensions
117. junit.framework
118. junit.textui
119. org.apache.commons.lang
120. org.apache.commons.lang.builder
121. org.apache.commons.lang.exception
122. org.apache.commons.lang.time
123. org.apache.commons.logging
124. org.apache.commons.math
125. org.eclipse.jface.action
126. org.eclipse.jface.dialogs
127. org.eclipse.jface.operation
128. org.eclipse.jface.viewers
129. org.eclipse.jface.window
130. org.eclipse.jface.wizard
131. org.eclipse.swt
132. org.eclipse.swt.browser
133. org.eclipse.swt.custom
134. org.eclipse.swt.dnd
135. org.eclipse.swt.events
136. org.eclipse.swt.graphics
137. org.eclipse.swt.layout
138. org.eclipse.swt.ole.win32
139. org.eclipse.swt.printing
140. org.eclipse.swt.program
141. org.eclipse.swt.widgets
142. org.junit
143. org.w3c.dom
144. org.xml.sax
145. org.xml.sax.helpers
146. sun.audio
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java by API » java.awt.im.spi » InputMethodEvent 
InputMethodEvent.CARET_POSITION_CHANGED
  

/*
 * @(#)CodePointInputMethod.java  1.6 05/11/17
 
 * Copyright (c) 2006 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:
 
 * -Redistribution 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 MIDROSYSTEMS, INC. ("SUN")
 * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS 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 THIS SOFTWARE, 
 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 
 * You acknowledge that this software is not designed, licensed or intended
 * for use in the design, construction, operation or maintenance of any
 * nuclear facility.
 */

/*
 * @(#)CodePointInputMethod.java  1.6 05/11/17
 */

//package com.sun.inputmethods.internal.codepointim;
import java.awt.AWTEvent;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.InputMethodEvent;
import java.awt.event.KeyEvent;
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.IOException;
import java.text.AttributedString;
import java.util.Locale;

/**
 * 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
 * accompanying file README.txt for more information.
 *
 @author Brian Beck
 */
public class CodePointInputMethod implements InputMethod {

    private static final int UNSET           = 0;
    private static final int ESCAPE          = 1// \u0000       - \uFFFF
    private static final int SPECIAL_ESCAPE  = 2// \U000000     - \U10FFFF
    private static final int SURROGATE_PAIR  = 3// \uD800\uDC00 - \uDBFF\uDFFF

    private InputMethodContext context;
    private Locale locale;
    private StringBuffer buffer;
    private int insertionPoint;
    private int format = UNSET;


    public CodePointInputMethod() throws IOException {
    }

    /**
     * This is the input method's main routine.  The composed text is stored
     * in buffer.
     */
    public void dispatchEvent(AWTEvent event) {
        // This input method handles KeyEvent only.
        if (!(event instanceof KeyEvent)) {
            return;
        }

        KeyEvent e = (KeyEventevent;
        int eventID = event.getID();
        boolean notInCompositionMode = buffer.length() == 0;

        if (eventID == KeyEvent.KEY_PRESSED) {
            // If we are not in composition mode, pass through
            if (notInCompositionMode)  {
                return;
            }

            switch (e.getKeyCode()) {
                case KeyEvent.VK_LEFT:
                    moveCaretLeft();
                    break;
                case KeyEvent.VK_RIGHT:
                    moveCaretRight();
                    break;
            }
        else if (eventID == KeyEvent.KEY_TYPED) {
            char c = e.getKeyChar();

            // If we are not in composition mode, wait a back slash
            if (notInCompositionMode)  {
                // If the type character is not a back slash, pass through
                if (c != '\\') {
                    return;
                }

                startComposition()// Enter to composition mode
            else {
                switch (c) { 
                case ' '// Exit from composition mode
                    finishComposition();
                    break;
                case '\u007f':  // Delete
                    deleteCharacter();
                    break;
                case '\b':  // BackSpace
                    deletePreviousCharacter();
                    break;
                case '\u001b':  // Escape
                    cancelComposition();
                    break;
                case '\n':  // Return
                case '\t':  // Tab
                    sendCommittedText();
                    break;
                default:
                    composeUnicodeEscape(c);
                    break;
                }
            }
        else {  // KeyEvent.KEY_RELEASED
            // If we are not in composition mode, pass through
            if (notInCompositionMode)  {
                return;
            }
        }

        e.consume();
    }

    private void composeUnicodeEscape(char c) {
        switch (buffer.length()) {
            case  1:  // \\
                waitEscapeCharacter(c);
                break;
            case 2:  // \\u or \\U
            case 3:  // \\ux or \\Ux
            case 4:  // \\uxx or \\Uxx
                waitDigit(c);
                break;
            case 5:  // \\uxxx or \\Uxxx
                if (format == SPECIAL_ESCAPE) {
                    waitDigit(c);
                else {
                    waitDigit2(c);
                }
                break;
            case 6:  // \\uxxxx or \\Uxxxx
                if (format == SPECIAL_ESCAPE) {
                    waitDigit(c);
                else if (format == SURROGATE_PAIR) {
                    waitBackSlashOrLowSurrogate(c);
                else {
                    beep();
                }
                break;
            case 7:  // \\Uxxxxx
                // Only SPECIAL_ESCAPE format uses this state.
                // Since the second "\\u" of SURROGATE_PAIR format is inserted
                // automatically, users don't have to type these keys.
                waitDigit(c);
                break;
            case 8:  // \\uxxxx\\u
            case 9:  // \\uxxxx\\ux
            case 10// \\uxxxx\\uxx
            case 11// \\uxxxx\\uxxx
                if (format == SURROGATE_PAIR) {
                    waitDigit(c);
                else {
                    beep();
                }
                break;
            default:
                beep();
                break;
        }
    }

    private void waitEscapeCharacter(char c) {
        if (c == 'u' || c == 'U') {
            buffer.append(c);
            insertionPoint++;
            sendComposedText();
            format = (c == 'u'? ESCAPE : SPECIAL_ESCAPE;
        else {
            if (c != '\\') {
                buffer.append(c);
                insertionPoint++;
            }
            sendCommittedText();
        }
    }

    private void waitDigit(char c) {
        if (Character.digit(c, 16!= -1) {
            buffer.insert(insertionPoint++, c);
            sendComposedText();
        else {
            beep();
        }
    }

    private void waitDigit2(char c) {
        if (Character.digit(c, 16!= -1) {
            buffer.insert(insertionPoint++, c);
            char codePoint = (char)getCodePoint(buffer, 25);
            if (Character.isHighSurrogate(codePoint)) {
                format = SURROGATE_PAIR;
                buffer.append("\\u");
                insertionPoint = 8;
            else {
                format = ESCAPE;
            }
            sendComposedText();
        else {
            beep();
        }
    }

    private void waitBackSlashOrLowSurrogate(char c) {
        if (insertionPoint == 6) {
            if (c == '\\') {
                buffer.append(c);
                buffer.append('u');
                insertionPoint = 8;
                sendComposedText();
            else if (Character.digit(c, 16!= -1) {
                buffer.append("\\u");
                buffer.append(c);
                insertionPoint = 9;
                sendComposedText();
            else {
                beep();
            }
        else {
            beep();
        }
    }

    /**
     * Send the composed text to the client.
     */
    private void sendComposedText() {
        AttributedString as = new AttributedString(buffer.toString());
        as.addAttribute(TextAttribute.INPUT_METHOD_HIGHLIGHT,
                        InputMethodHighlight.SELECTED_RAW_TEXT_HIGHLIGHT);
        context.dispatchInputMethodEvent(
                                  InputMethodEvent.INPUT_METHOD_TEXT_CHANGED,
                                  as.getIterator()0,
                                  TextHitInfo.leading(insertionPoint)null);
    }

    /**
     * Send the committed text to the client.
     */
    private void sendCommittedText() {
        AttributedString as = new AttributedString(buffer.toString());
        context.dispatchInputMethodEvent(
                                  InputMethodEvent.INPUT_METHOD_TEXT_CHANGED,
                                  as.getIterator(), buffer.length(),
                                  TextHitInfo.leading(insertionPoint)null);

        buffer.setLength(0);
        insertionPoint = 0;
        format = UNSET;
    }

    /**
     * Move the insertion point one position to the left in the composed text.
     * Do not let the caret move to the left of the "\\u" or "\\U".
     */
    private void moveCaretLeft() {
        int len = buffer.length();
        if (--insertionPoint < 2) {
            insertionPoint++;
            beep();
        else if (format == SURROGATE_PAIR && insertionPoint == 7) {
            insertionPoint = 8;
            beep();
        }

        context.dispatchInputMethodEvent(
                                  InputMethodEvent.CARET_POSITION_CHANGED,
                                  null, 0,
                                  TextHitInfo.leading(insertionPoint)null);
    }

    /**
     * Move the insertion point one position to the right in the composed text.
     */
    private void moveCaretRight() {
        int len = buffer.length();
        if (++insertionPoint > len) {
            insertionPoint = len;
            beep();
        }

        context.dispatchInputMethodEvent(
                                  InputMethodEvent.CARET_POSITION_CHANGED,
                                  null, 0,
                                  TextHitInfo.leading(insertionPoint)null);
    }

    /**
     * Delete the character preceding the insertion point in the composed text.
     * If the insertion point is not at the end of the composed text and the
     * preceding text is "\\u" or "\\U", ring the bell.
     */
    private void deletePreviousCharacter() {
        if (insertionPoint == 2) {
            if (buffer.length() == 2) {
                cancelComposition();
            else {
                // Do not allow deletion of the leading "\\u" or "\\U" if there
                // are other digits in the composed text.
                beep();
            }
        else if (insertionPoint == 8) {
            if (buffer.length() == 8) {
                if (format == SURROGATE_PAIR) {
                    buffer.deleteCharAt(--insertionPoint);
                }
                buffer.deleteCharAt(--insertionPoint);
                sendComposedText();
            else {
                // Do not allow deletion of the second "\\u" if there are other
                // digits in the composed text.
                beep();
            }
        else {
            buffer.deleteCharAt(--insertionPoint);
            if (buffer.length() == 0) {
                sendCommittedText();
            else {
                sendComposedText();
            }
        }
    }

    /**
     * Delete the character following the insertion point in the composed text.
     * If the insertion point is at the end of the composed text, ring the bell.
     */
    private void deleteCharacter() {
        if (insertionPoint < buffer.length()) {
            buffer.deleteCharAt(insertionPoint);
            sendComposedText();
        else {
            beep();
        }
    }

    private void startComposition() {
        buffer.append('\\');
        insertionPoint = 1;
        sendComposedText();
    }

    private void cancelComposition() {
        buffer.setLength(0);
        insertionPoint = 0;
        sendCommittedText();
    }

    private void finishComposition() {
        int len = buffer.length();
        if (len == && format != SPECIAL_ESCAPE) {
            char codePoint = (char)getCodePoint(buffer, 25);
            if (Character.isValidCodePoint(codePoint&& codePoint != 0xFFFF) {
                buffer.setLength(0);
                buffer.append(codePoint);
          sendCommittedText();
                return;
            }
        else if (len == && format == SPECIAL_ESCAPE) {
            int codePoint = getCodePoint(buffer, 27);
            if (Character.isValidCodePoint(codePoint&& codePoint != 0xFFFF) {
                buffer.setLength(0);
                buffer.appendCodePoint(codePoint);
          sendCommittedText();
                return;
            }
        else if (len == 12 && format == SURROGATE_PAIR) {
      char[] codePoint = {
                (char)getCodePoint(buffer, 25),
          (char)getCodePoint(buffer, 811)
            };
            if (Character.isHighSurrogate(codePoint[0]) &&
                Character.isLowSurrogate(codePoint[1])) {
          buffer.setLength(0);
          buffer.append(codePoint);
          sendCommittedText();
                return;
            }
        }

        beep();
    }

    private int getCodePoint(StringBuffer sb, int from, int to) {
        int value = 0;
        for (int i = from; i <= to; i++) {
            value = (value<<4+ Character.digit(sb.charAt(i)16);
        }
        return value;
    }

    private static void beep() {
        Toolkit.getDefaultToolkit().beep();
    }


    public void activate() {
        if (buffer == null) {
            buffer = new StringBuffer(12);
            insertionPoint = 0;
        }
    }

    public void deactivate(boolean isTemporary) {
        if (!isTemporary) {
            buffer = null;
        }
    }

    public void dispose() {
    }

    public Object getControlObject() {
        return null;
    }

    public void endComposition() {
        sendCommittedText();
    }

    public Locale getLocale() {
        return locale;
    }

    public void hideWindows() {
    }

    public boolean isCompositionEnabled() {
        // always enabled
        return true;
    }

    public void notifyClientWindowChange(Rectangle location) {
    }

    public void reconvert() {
        // not supported yet
        throw new UnsupportedOperationException();
    }

    public void removeNotify() {
    }

    public void setCharacterSubsets(Character.Subset[] subsets) {
    }

    public void setCompositionEnabled(boolean enable) {
        // not supported yet
        throw new UnsupportedOperationException();
    }

    public void setInputMethodContext(InputMethodContext context) {
        this.context = context;
    }

    /*
     * The Code Point Input Method supports all locales.
     */
    public boolean setLocale(Locale locale) {
        this.locale = locale;
        return true;
    }
}

   
    
  
Related examples in the same category
1. InputMethodEvent.INPUT_METHOD_TEXT_CHANGED
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.