2626import processing .app .syntax .JEditTextArea ;
2727import processing .app .syntax .TextAreaPainter ;
2828
29+
2930/**
30- * on-the-spot style input support for CJK.(Chinese, Japanese, Korean).
31- * This class is implemented to fix Bug #854 from 2010-02-16.
31+ * On-the-spot style input support for CJK (Chinese, Japanese, Korean).
3232 *
33- * @see <a href="https://processing.org/bugs/bugzilla/854.html">Bug 854 : implement input method support for Japanese (and other languages)</a>
34- * @see <a href="https://processing.org/bugs/bugzilla/1531.html">Bug 1531 : Can't input full-width space when Japanese IME is on.</a>
35- * @see <a href="http://docs.oracle.com/javase/8/docs/technotes/guides/imf/index.html">Java Input Method Framework
36- * (IMF) Technology</a>
33+ * @see <a href="https://processing.org/bugs/bugzilla/854.html">Bugzilla 854: implement input method support for Japanese (and other languages)</a>
34+ * @see <a href="https://processing.org/bugs/bugzilla/1531.html">Bugzilla 1531: Can't input full-width space when Japanese IME is on.</a>
35+ * @see <a href="http://docs.oracle.com/javase/8/docs/technotes/guides/imf/index.html">Java Input Method Framework (IMF) Technology</a>
3736 * @see <a href="http://docs.oracle.com/javase/tutorial/2d/text/index.html">The Java Tutorials</a>
3837 *
3938 * @author Takashi Maekawa (takachin@generative.info)
4039 * @author Satoshi Okita
4140 */
42- public class InputMethodSupport implements InputMethodRequests ,
43- InputMethodListener {
41+ public class InputMethodSupport implements InputMethodRequests , InputMethodListener {
4442
45- private static final Attribute [] CUSTOM_IM_ATTRIBUTES = {
43+ static private final Attribute [] CUSTOM_IM_ATTRIBUTES = {
4644 TextAttribute .INPUT_METHOD_HIGHLIGHT ,
4745 };
4846
49- private int committed_count = 0 ;
47+ private int committedCount = 0 ;
5048 private JEditTextArea textArea ;
5149 private AttributedString composedTextString ;
5250
@@ -56,9 +54,13 @@ public InputMethodSupport(JEditTextArea textArea) {
5654 this .textArea .addInputMethodListener (this );
5755 }
5856
57+
5958 /////////////////////////////////////////////////////////////////////////////
59+
6060 // InputMethodRequest
61+
6162 /////////////////////////////////////////////////////////////////////////////
63+
6264 @ Override
6365 public Rectangle getTextLocation (TextHitInfo offset ) {
6466 if (Base .DEBUG ) {
@@ -82,11 +84,13 @@ public TextHitInfo getLocationOffset(int x, int y) {
8284 return null ;
8385 }
8486
87+
8588 @ Override
8689 public int getInsertPositionOffset () {
87- return textArea .getCaretPosition () * - 1 ;
90+ return - textArea .getCaretPosition ();
8891 }
8992
93+
9094 @ Override
9195 public AttributedCharacterIterator getCommittedText (int beginIndex ,
9296 int endIndex , AttributedCharacterIterator .Attribute [] attributes ) {
@@ -95,26 +99,33 @@ public AttributedCharacterIterator getCommittedText(int beginIndex,
9599 return new AttributedString (textAreaString ).getIterator ();
96100 }
97101
102+
98103 @ Override
99104 public int getCommittedTextLength () {
100- return committed_count ;
105+ return committedCount ;
101106 }
102107
108+
103109 @ Override
104110 public AttributedCharacterIterator cancelLatestCommittedText (
105111 AttributedCharacterIterator .Attribute [] attributes ) {
106112 return null ;
107113 }
108114
115+
109116 @ Override
110117 public AttributedCharacterIterator getSelectedText (
111118 AttributedCharacterIterator .Attribute [] attributes ) {
112119 return null ;
113120 }
114121
122+
115123 /////////////////////////////////////////////////////////////////////////////
124+
116125 // InputMethodListener
126+
117127 /////////////////////////////////////////////////////////////////////////////
128+
118129 /**
119130 * Handles events from InputMethod.
120131 *
@@ -132,49 +143,44 @@ public void inputMethodTextChanged(InputMethodEvent event) {
132143 }
133144
134145 AttributedCharacterIterator text = event .getText (); // text = composedText + commitedText
135- committed_count = event .getCommittedCharacterCount ();
136-
137-
138- // The caret for Input Method.
139- // if you type a character by a input method, original caret become off.
140- // a JEditTextArea is not implemented by the AttributedStirng and TextLayout.
141- // so JEditTextArea Caret On-off logic.
142- //
143- // japanese : if the enter key pressed, event.getText is null.
144- // japanese : if first space key pressed, event.getText is null.
145- // chinese(pinin) : if a space key pressed, event.getText is null.
146- // taiwan(bopomofo): ?
147- // korean : ?
146+ committedCount = event .getCommittedCharacterCount ();
147+
148+ // The caret for Input Method. If you type a character by a input method,
149+ // original caret position will be incorrect. JEditTextArea is not
150+ // implemented using AttributedString and TextLayout.
148151 textArea .setCaretVisible (false );
152+
153+ // Japanese : if the enter key pressed, event.getText is null.
154+ // Japanese : if first space key pressed, event.getText is null.
155+ // Chinese (pinin) : if a space key pressed, event.getText is null.
156+ // Taiwan (bopomofo): ?
157+ // Korean : ?
158+
149159 // Korean Input Method
150- if (text != null && text .getEndIndex () - (text .getBeginIndex () + committed_count ) <= 0 ) {
160+ if (text != null && text .getEndIndex () - (text .getBeginIndex () + committedCount ) <= 0 ) {
151161 textArea .setCaretVisible (true );
152162 }
153163 // Japanese Input Method
154164 if (text == null ) {
155165 textArea .setCaretVisible (true );
156166 }
157167
158- char c ;
159168 if (text != null ) {
160- int toCopy = committed_count ;
161- c = text .first ();
162- while (toCopy -- > 0 ) {
163- if (Base .DEBUG ) {
164- Messages .log ("INSERT:'" + c + "'" );
165- }
166- this .insertCharacter (c );
169+ int remaining = committedCount ;
170+ char c = text .first ();
171+ while (remaining -- > 0 ) {
172+ insertCharacter (c );
167173 c = text .next ();
168174 }
169175
170176 CompositionTextPainter compositionPainter = textArea .getPainter ().getCompositionTextpainter ();
171177 if (Base .DEBUG ) {
172- Messages .log (" textArea.getCaretPosition() + committed_count: " + (textArea .getCaretPosition () + committed_count ));
178+ Messages .log (" textArea.getCaretPosition() + committed_count: " + (textArea .getCaretPosition () + committedCount ));
173179 }
174- compositionPainter .setComposedTextLayout (getTextLayout (text , committed_count ), textArea .getCaretPosition () + committed_count );
180+ compositionPainter .setComposedTextLayout (getTextLayout (text , committedCount ), textArea .getCaretPosition () + committedCount );
175181 compositionPainter .setCaret (event .getCaret ());
176- } else {
177- // hide input method.
182+
183+ } else { // otherwise hide the input method
178184 CompositionTextPainter compositionPainter = textArea .getPainter ().getCompositionTextpainter ();
179185 compositionPainter .setComposedTextLayout (null , 0 );
180186 compositionPainter .setCaret (null );
@@ -183,6 +189,7 @@ public void inputMethodTextChanged(InputMethodEvent event) {
183189 textArea .repaint ();
184190 }
185191
192+
186193 private TextLayout getTextLayout (AttributedCharacterIterator text , int committedCount ) {
187194 boolean antialias = Preferences .getBoolean ("editor.smooth" );
188195 TextAreaPainter painter = textArea .getPainter ();
@@ -213,11 +220,13 @@ private TextLayout getTextLayout(AttributedCharacterIterator text, int committed
213220 return new TextLayout (composedTextString .getIterator (), frc );
214221 }
215222
223+
216224 @ Override
217225 public void caretPositionChanged (InputMethodEvent event ) {
218226 event .consume ();
219227 }
220228
229+
221230 private void insertCharacter (char c ) {
222231 if (Base .DEBUG ) {
223232 Messages .log ("debug: insertCharacter(char c) textArea.getCaretPosition()=" + textArea .getCaretPosition ());
0 commit comments