Source for java.awt.im.InputMethodHighlight

   1: /* InputMethodHighlight.java -- highlights the current text selection
   2:    Copyright (C) 2002, 2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package java.awt.im;
  39: 
  40: import java.util.Map;
  41: 
  42: /**
  43:  * This describes the highlight attributes of text composed in an input method.
  44:  * The description includes an abstract level (whether text has been converted
  45:  * yet, and whether it is selected), and a concrete level (which style
  46:  * attributes are used in rendering). If no concrete level is defined, the
  47:  * renderer should use
  48:  * {@link Toolkit#mapInputMethodHighlight(InputMethodHighlight)}. An example
  49:  * of conversion state is kana -> kanji.
  50:  *
  51:  * <p>Instances of this class are typically used in
  52:  * AttributedCharacterIterators, and may be wrapped in Annotations to separate
  53:  * text segments.
  54:  *
  55:  * @author Eric Blake (ebb9@email.byu.edu)
  56:  * @see AttributedCharacterIterators
  57:  * @see Annotation
  58:  * @since 1.2
  59:  * @status updated to 1.4
  60:  */
  61: public class InputMethodHighlight
  62: {
  63:   /** Raw text state (before conversion). */
  64:   public static final int RAW_TEXT = 0;
  65: 
  66:   /** Converted text state (after conversion). */
  67:   public static final int CONVERTED_TEXT = 1;
  68: 
  69:   /** Default do-nothing highlighting for unselected raw text. */
  70:   public static final InputMethodHighlight UNSELECTED_RAW_TEXT_HIGHLIGHT
  71:     = new InputMethodHighlight(false, RAW_TEXT);
  72: 
  73:   /** Default do-nothing highlighting for selected raw text. */
  74:   public static final InputMethodHighlight SELECTED_RAW_TEXT_HIGHLIGHT
  75:     = new InputMethodHighlight(true, RAW_TEXT);
  76: 
  77:   /** Default do-nothing highlighting for unselected converted text. */
  78:   public static final InputMethodHighlight UNSELECTED_CONVERTED_TEXT_HIGHLIGHT
  79:     = new InputMethodHighlight(false, CONVERTED_TEXT);
  80: 
  81:   /** Default do-nothing highlighting for selected converted text. */
  82:   public static final InputMethodHighlight SELECTED_CONVERTED_TEXT_HIGHLIGHT
  83:     = new InputMethodHighlight(true, CONVERTED_TEXT);
  84: 
  85:   /** Whether the highlighting applies to selected text. */
  86:   private final boolean selected;
  87: 
  88:   /** The state of highlighted text. */
  89:   private final int state;
  90: 
  91:   /** Any variation on the highlighting style. */
  92:   private final int variation;
  93: 
  94:   /** The unmodifiable map of rendering styles. */
  95:   private final Map style;
  96: 
  97:   /**
  98:    * Create an input method highlight style, with variation 0 and null style
  99:    * mapping.
 100:    *
 101:    * @param selected whether the text range is selected
 102:    * @param state either {@link #RAW_TEXT} or {@link #CONVERTED_TEXT}
 103:    * @throws IllegalArgumentException if state is invalid
 104:    */
 105:   public InputMethodHighlight(boolean selected, int state)
 106:   {
 107:     this(selected, state, 0, null);
 108:   }
 109: 
 110:   /**
 111:    * Create an input method highlight style, with null style mapping.
 112:    *
 113:    * @param selected whether the text range is selected
 114:    * @param state either {@link #RAW_TEXT} or {@link #CONVERTED_TEXT}
 115:    * @param variation the style variation
 116:    * @throws IllegalArgumentException if state is invalid
 117:    */
 118:   public InputMethodHighlight(boolean selected, int state, int variation)
 119:   {
 120:     this(selected, state, variation, null);
 121:   }
 122: 
 123:   /**
 124:    * Create an input method highlight style.
 125:    *
 126:    * @param selected whether the text range is selected
 127:    * @param state either {@link #RAW_TEXT} or {@link #CONVERTED_TEXT}
 128:    * @param variation the style variation
 129:    * @param style an unmodifiable map of rendering styles, or null
 130:    * @throws IllegalArgumentException if state is invalid
 131:    * @since 1.3
 132:    */
 133:   public InputMethodHighlight(boolean selected, int state, int variation,
 134:                               Map style)
 135:   {
 136:     if (state != RAW_TEXT && state != CONVERTED_TEXT)
 137:       throw new IllegalArgumentException();
 138:     this.selected = selected;
 139:     this.state = state;
 140:     this.variation = variation;
 141:     this.style = style;
 142:   }
 143: 
 144:   /**
 145:    * Return whether the highlighting applies to selected text.
 146:    *
 147:    * @return the selection status
 148:    */
 149:   public boolean isSelected()
 150:   {
 151:     return selected;
 152:   }
 153: 
 154:   /**
 155:    * Return the conversion state of the highlighted text.
 156:    *
 157:    * @return one of {@link #RAW_TEXT} or {@link #CONVERTED_TEXT}
 158:    */
 159:   public int getState()
 160:   {
 161:     return state;
 162:   }
 163: 
 164:   /**
 165:    * Return the highlighting style variation.
 166:    *
 167:    * @return the variation
 168:    */
 169:   public int getVariation()
 170:   {
 171:     return variation;
 172:   }
 173: 
 174:   /**
 175:    * Return the rendering style attributes map, or null if it should be the
 176:    * default mapping.
 177:    *
 178:    * @return the style map
 179:    * @since 1.3
 180:    */
 181:   public Map getStyle()
 182:   {
 183:     return style;
 184:   }
 185: } // class InputMethodHighlight