< prev index next >

src/java.desktop/macosx/classes/sun/lwawt/macosx/CInputMethod.java

Print this page


   1 /*
   2  * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  38 import java.text.*;
  39 import javax.swing.text.JTextComponent;
  40 
  41 import sun.awt.AWTAccessor;
  42 import sun.awt.im.InputMethodAdapter;
  43 import sun.lwawt.*;
  44 
  45 import static sun.awt.AWTAccessor.ComponentAccessor;
  46 
  47 public class CInputMethod extends InputMethodAdapter {
  48     private InputMethodContext fIMContext;
  49     private Component fAwtFocussedComponent;
  50     private LWComponentPeer<?, ?> fAwtFocussedComponentPeer;
  51     private boolean isActive;
  52 
  53     private static Map<TextAttribute, Integer>[] sHighlightStyles;
  54 
  55     // Intitalize highlight mapping table and its mapper.
  56     static {
  57         @SuppressWarnings({"rawtypes", "unchecked"})
  58         Map<TextAttribute, Integer> styles[] = new Map[4];
  59         HashMap<TextAttribute, Integer> map;
  60 
  61         // UNSELECTED_RAW_TEXT_HIGHLIGHT
  62         map = new HashMap<TextAttribute, Integer>(1);
  63         map.put(TextAttribute.INPUT_METHOD_UNDERLINE,
  64                 TextAttribute.UNDERLINE_LOW_GRAY);
  65         styles[0] = Collections.unmodifiableMap(map);
  66 
  67         // SELECTED_RAW_TEXT_HIGHLIGHT
  68         map = new HashMap<TextAttribute, Integer>(1);
  69         map.put(TextAttribute.INPUT_METHOD_UNDERLINE,
  70                 TextAttribute.UNDERLINE_LOW_GRAY);
  71         styles[1] = Collections.unmodifiableMap(map);
  72 
  73         // UNSELECTED_CONVERTED_TEXT_HIGHLIGHT
  74         map = new HashMap<TextAttribute, Integer>(1);
  75         map.put(TextAttribute.INPUT_METHOD_UNDERLINE,
  76                 TextAttribute.UNDERLINE_LOW_ONE_PIXEL);
  77         styles[2] = Collections.unmodifiableMap(map);
  78 


 596                         length = fIMContext.getCommittedTextLength() - location;
 597                     }
 598 
 599                     AttributedCharacterIterator theIterator = null;
 600 
 601                     if (fCurrentText == null) {
 602                         theIterator = fIMContext.getCommittedText(location, location + length, null);
 603                     } else {
 604                         int insertSpot = fIMContext.getInsertPositionOffset();
 605 
 606                         if (location < insertSpot) {
 607                             theIterator = fIMContext.getCommittedText(location, location + length, null);
 608                         } else if (location >= insertSpot && location < insertSpot + fCurrentTextLength) {
 609                             theIterator = fCurrentText.getIterator(null, location - insertSpot, location - insertSpot +length);
 610                         } else  {
 611                             theIterator = fIMContext.getCommittedText(location - fCurrentTextLength, location - fCurrentTextLength + length, null);
 612                         }
 613                     }
 614 
 615                     // Get the characters from the iterator
 616                     char selectedText[] = new char[theIterator.getEndIndex() - theIterator.getBeginIndex()];
 617                     char current = theIterator.first();
 618                     int index = 0;
 619                     while (current != CharacterIterator.DONE) {
 620                         selectedText[index++] = current;
 621                         current = theIterator.next();
 622                     }
 623 
 624                     retString[0] = new String(selectedText);
 625                 }}
 626             }, fAwtFocussedComponent);
 627         } catch (InvocationTargetException ite) { ite.printStackTrace(); }
 628 
 629         synchronized(retString) { return retString[0]; }
 630     }
 631 
 632     /**
 633      * Cocoa wants the range of characters that are currently selected.  We have to synthesize this
 634      * by getting the insert location and the length of the selected text. NB:  This does NOT allow
 635      * for the fact that the insert point in Swing can come AFTER the selected text, making this
 636      * potentially incorrect.


   1 /*
   2  * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  38 import java.text.*;
  39 import javax.swing.text.JTextComponent;
  40 
  41 import sun.awt.AWTAccessor;
  42 import sun.awt.im.InputMethodAdapter;
  43 import sun.lwawt.*;
  44 
  45 import static sun.awt.AWTAccessor.ComponentAccessor;
  46 
  47 public class CInputMethod extends InputMethodAdapter {
  48     private InputMethodContext fIMContext;
  49     private Component fAwtFocussedComponent;
  50     private LWComponentPeer<?, ?> fAwtFocussedComponentPeer;
  51     private boolean isActive;
  52 
  53     private static Map<TextAttribute, Integer>[] sHighlightStyles;
  54 
  55     // Intitalize highlight mapping table and its mapper.
  56     static {
  57         @SuppressWarnings({"rawtypes", "unchecked"})
  58         Map<TextAttribute, Integer>[] styles = new Map[4];
  59         HashMap<TextAttribute, Integer> map;
  60 
  61         // UNSELECTED_RAW_TEXT_HIGHLIGHT
  62         map = new HashMap<TextAttribute, Integer>(1);
  63         map.put(TextAttribute.INPUT_METHOD_UNDERLINE,
  64                 TextAttribute.UNDERLINE_LOW_GRAY);
  65         styles[0] = Collections.unmodifiableMap(map);
  66 
  67         // SELECTED_RAW_TEXT_HIGHLIGHT
  68         map = new HashMap<TextAttribute, Integer>(1);
  69         map.put(TextAttribute.INPUT_METHOD_UNDERLINE,
  70                 TextAttribute.UNDERLINE_LOW_GRAY);
  71         styles[1] = Collections.unmodifiableMap(map);
  72 
  73         // UNSELECTED_CONVERTED_TEXT_HIGHLIGHT
  74         map = new HashMap<TextAttribute, Integer>(1);
  75         map.put(TextAttribute.INPUT_METHOD_UNDERLINE,
  76                 TextAttribute.UNDERLINE_LOW_ONE_PIXEL);
  77         styles[2] = Collections.unmodifiableMap(map);
  78 


 596                         length = fIMContext.getCommittedTextLength() - location;
 597                     }
 598 
 599                     AttributedCharacterIterator theIterator = null;
 600 
 601                     if (fCurrentText == null) {
 602                         theIterator = fIMContext.getCommittedText(location, location + length, null);
 603                     } else {
 604                         int insertSpot = fIMContext.getInsertPositionOffset();
 605 
 606                         if (location < insertSpot) {
 607                             theIterator = fIMContext.getCommittedText(location, location + length, null);
 608                         } else if (location >= insertSpot && location < insertSpot + fCurrentTextLength) {
 609                             theIterator = fCurrentText.getIterator(null, location - insertSpot, location - insertSpot +length);
 610                         } else  {
 611                             theIterator = fIMContext.getCommittedText(location - fCurrentTextLength, location - fCurrentTextLength + length, null);
 612                         }
 613                     }
 614 
 615                     // Get the characters from the iterator
 616                     char[] selectedText = new char[theIterator.getEndIndex() - theIterator.getBeginIndex()];
 617                     char current = theIterator.first();
 618                     int index = 0;
 619                     while (current != CharacterIterator.DONE) {
 620                         selectedText[index++] = current;
 621                         current = theIterator.next();
 622                     }
 623 
 624                     retString[0] = new String(selectedText);
 625                 }}
 626             }, fAwtFocussedComponent);
 627         } catch (InvocationTargetException ite) { ite.printStackTrace(); }
 628 
 629         synchronized(retString) { return retString[0]; }
 630     }
 631 
 632     /**
 633      * Cocoa wants the range of characters that are currently selected.  We have to synthesize this
 634      * by getting the insert location and the length of the selected text. NB:  This does NOT allow
 635      * for the fact that the insert point in Swing can come AFTER the selected text, making this
 636      * potentially incorrect.


< prev index next >