@@ -185,190 +185,3 @@ public int getInsertPositionOffset() {
185185 return textArea .getCaretPosition () * -1 ;
186186 }
187187}
188- package processing .app .syntax .im ;
189-
190- import java .awt .Font ;
191- import java .awt .FontMetrics ;
192- import java .awt .Graphics2D ;
193- import java .awt .Point ;
194- import java .awt .Rectangle ;
195- import java .awt .font .FontRenderContext ;
196- import java .awt .font .TextAttribute ;
197- import java .awt .font .TextLayout ;
198- import java .text .AttributedCharacterIterator ;
199- import java .text .AttributedString ;
200-
201- import javax .swing .text .BadLocationException ;
202-
203- import processing .app .syntax .JEditTextArea ;
204- import processing .app .syntax .TextAreaPainter ;
205-
206- /**
207- * This class Manage texts from input method
208- * by begin-process-end steps.
209- *
210- * First, if a user start inputing via input method,
211- * beginCompositionText is called from InputMethodSupport.
212- * Second, the user continues from input method, processCompositionText is called
213- * and reflect user inputs to text area.
214- * Finally the user try to commit text, endCompositionText is called.
215- *
216- * @author Takashi Maekawa (takachin@generative.info)
217- */
218-
219- public class CompositionTextManager {
220- private JEditTextArea textArea ;
221- private String prevComposeString ;
222- private int prevCommittedCount ;
223- private boolean isInputProcess ;
224- private int initialCaretPosition ;
225- public static final int COMPOSING_UNDERBAR_HEIGHT = 5 ;
226-
227- /**
228- * Create text manager class with a textarea.
229- * @param textArea texarea component for PDE.
230- */
231- public CompositionTextManager (JEditTextArea textArea ) {
232- this .textArea = textArea ;
233- prevComposeString = "" ;
234- isInputProcess = false ;
235- prevCommittedCount = 0 ;
236- }
237-
238- /**
239- * Get this text manager is whether in input process or not.
240- */
241- public boolean getIsInputProcess () {
242- return isInputProcess ;
243- }
244-
245- /**
246- * Called when a user begins input from input method.
247- * This method initializes text manager.
248- *
249- * @param text Text from InputMethodEvent.
250- * @param commited_count Numbers of committed characters in text.
251- */
252- public void beginCompositionText (AttributedCharacterIterator text , int committed_count ) {
253- isInputProcess = true ;
254- prevComposeString = "" ;
255- initialCaretPosition = textArea .getCaretPosition ();
256- processCompositionText (text , committed_count );
257- }
258-
259- /**
260- * Called when a user processing input characters and
261- * select candidates from input method.
262- *
263- * @param text Text from InputMethodEvent.
264- * @param commited_count Numbers of committed characters in text.
265- */
266- public void processCompositionText (AttributedCharacterIterator text , int committed_count ) {
267- int layoutCaretPosition = initialCaretPosition + committed_count ;
268- CompositionTextPainter compositionPainter = textArea .getPainter ().getCompositionTextpainter ();
269- compositionPainter .setComposedTextLayout (getTextLayout (text , committed_count ), layoutCaretPosition );
270- int textLength = text .getEndIndex () - text .getBeginIndex () - committed_count ;
271- StringBuffer unCommitedStringBuf = new StringBuffer (textLength );
272- char c ;
273- for (c = text .setIndex (committed_count ); c != AttributedCharacterIterator .DONE
274- && textLength > 0 ; c = text .next (), --textLength ) {
275- unCommitedStringBuf .append (c );
276- }
277- String unCommittedString = unCommitedStringBuf .toString ();
278- try {
279- if (canRemovePreviousInput (committed_count )){
280- textArea .getDocument ().remove (layoutCaretPosition , prevComposeString .length ());
281- }
282- textArea .getDocument ().insertString (layoutCaretPosition , unCommittedString , null );
283- if (committed_count > 0 ){
284- initialCaretPosition = initialCaretPosition + committed_count ;
285- }
286- prevComposeString = unCommittedString ;
287- prevCommittedCount = committed_count ;
288- } catch (BadLocationException e ) {
289- e .printStackTrace ();
290- }
291- }
292-
293- private boolean canRemovePreviousInput (int committed_count ){
294- return (prevCommittedCount == committed_count || prevCommittedCount > committed_count );
295- }
296-
297- /**
298- * Called when a user fixed text from input method or delete all
299- * composition text. This method resets CompositionTextPainter.
300- *
301- * @param text Text from InputMethodEvent.
302- * @param commited_count Numbers of committed characters in text.
303- */
304- public void endCompositionText (AttributedCharacterIterator text , int committed_count ) {
305- isInputProcess = false ;
306- /*
307- * If there are no committed characters, remove it all from textarea.
308- * This case will happen if a user delete all composing characters by backspace or delete key.
309- * If it does, these previous characters are needed to be deleted.
310- */
311- if (committed_count == 0 ){
312- removeNotCommittedText (text );
313- }
314- CompositionTextPainter compositionPainter = textArea .getPainter ().getCompositionTextpainter ();
315- compositionPainter .invalidateComposedTextLayout (initialCaretPosition + committed_count );
316- prevComposeString = "" ;
317- isInputProcess = false ;
318- }
319-
320- private void removeNotCommittedText (AttributedCharacterIterator text ){
321- if (prevComposeString .length () == 0 ) {
322- return ;
323- }
324- try {
325- textArea .getDocument ().remove (initialCaretPosition , prevComposeString .length ());
326- } catch (BadLocationException e ) {
327- e .printStackTrace ();
328- }
329- }
330-
331- private TextLayout getTextLayout (AttributedCharacterIterator text , int committed_count ) {
332- AttributedString composed = new AttributedString (text , committed_count , text .getEndIndex ());
333- Font font = textArea .getPainter ().getFont ();
334- FontRenderContext context = ((Graphics2D ) (textArea .getPainter ().getGraphics ())).getFontRenderContext ();
335- composed .addAttribute (TextAttribute .FONT , font );
336- TextLayout layout = new TextLayout (composed .getIterator (), context );
337- return layout ;
338- }
339-
340- private Point getCaretLocation () {
341- Point loc = new Point ();
342- TextAreaPainter painter = textArea .getPainter ();
343- FontMetrics fm = painter .getFontMetrics ();
344- int offsetY = fm .getHeight () - COMPOSING_UNDERBAR_HEIGHT ;
345- int lineIndex = textArea .getCaretLine ();
346- loc .y = lineIndex * fm .getHeight () + offsetY ;
347- int offsetX = textArea .getCaretPosition ()
348- - textArea .getLineStartOffset (lineIndex );
349- loc .x = textArea .offsetToX (lineIndex , offsetX );
350- return loc ;
351- }
352-
353- public Rectangle getTextLocation () {
354- Point caret = getCaretLocation ();
355- return getCaretRectangle (caret .x , caret .y );
356- }
357-
358- private Rectangle getCaretRectangle (int x , int y ) {
359- TextAreaPainter painter = textArea .getPainter ();
360- Point origin = painter .getLocationOnScreen ();
361- int height = painter .getFontMetrics ().getHeight ();
362- return new Rectangle (origin .x + x , origin .y + y , 0 , height );
363- }
364-
365- public AttributedCharacterIterator getCommittedText (int beginIndex , int endIndex ) {
366- int length = endIndex - beginIndex ;
367- String textAreaString = textArea .getText (beginIndex , length );
368- return new AttributedString (textAreaString ).getIterator ();
369- }
370-
371- public int getInsertPositionOffset () {
372- return textArea .getCaretPosition () * -1 ;
373- }
374- }
0 commit comments