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.awt.Toolkit;
  41: import java.text.Annotation;
  42: import java.text.AttributedCharacterIterator;
  43: import java.util.Map;
  44: 
  45: /**
  46:  * This describes the highlight attributes of text composed in an input method.
  47:  * The description includes an abstract level (whether text has been converted
  48:  * yet, and whether it is selected), and a concrete level (which style
  49:  * attributes are used in rendering). If no concrete level is defined, the
  50:  * renderer should use
  51:  * {@link Toolkit#mapInputMethodHighlight(InputMethodHighlight)}. An example
  52:  * of conversion state is kana -> kanji.
  53:  *
  54:  * <p>Instances of this class are typically used in
  55:  * AttributedCharacterIterators, and may be wrapped in Annotations to separate
  56:  * text segments.
  57:  *
  58:  * @author Eric Blake (ebb9@email.byu.edu)
  59:  * @see AttributedCharacterIterator
  60:  * @see Annotation
  61:  * @since 1.2
  62:  * @status updated to 1.4
  63:  */
  64: public class InputMethodHighlight
  65: {
  66:   /** Raw text state (before conversion). */
  67:   public static final int RAW_TEXT = 0;
  68: 
  69:   /** Converted text state (after conversion). */
  70:   public static final int CONVERTED_TEXT = 1;
  71: 
  72:   /** Default do-nothing highlighting for unselected raw text. */
  73:   public static final InputMethodHighlight UNSELECTED_RAW_TEXT_HIGHLIGHT
  74:     = new InputMethodHighlight(false, RAW_TEXT);
  75: 
  76:   /** Default do-nothing highlighting for selected raw text. */
  77:   public static final InputMethodHighlight SELECTED_RAW_TEXT_HIGHLIGHT
  78:     = new InputMethodHighlight(true, RAW_TEXT);
  79: 
  80:   /** Default do-nothing highlighting for unselected converted text. */
  81:   public static final InputMethodHighlight UNSELECTED_CONVERTED_TEXT_HIGHLIGHT
  82:     = new InputMethodHighlight(false, CONVERTED_TEXT);
  83: 
  84:   /** Default do-nothing highlighting for selected converted text. */
  85:   public static final InputMethodHighlight SELECTED_CONVERTED_TEXT_HIGHLIGHT
  86:     = new InputMethodHighlight(true, CONVERTED_TEXT);
  87: 
  88:   /** Whether the highlighting applies to selected text. */
  89:   private final boolean selected;
  90: 
  91:   /** The state of highlighted text. */
  92:   private final int state;
  93: 
  94:   /** Any variation on the highlighting style. */
  95:   private final int variation;
  96: 
  97:   /** The unmodifiable map of rendering styles. */
  98:   private final Map style;
  99: 
 100:   /**
 101:    * Create an input method highlight style, with variation 0 and null style
 102:    * mapping.
 103:    *
 104:    * @param selected whether the text range is selected
 105:    * @param state either {@link #RAW_TEXT} or {@link #CONVERTED_TEXT}
 106:    * @throws IllegalArgumentException if state is invalid
 107:    */
 108:   public InputMethodHighlight(boolean selected, int state)
 109:   {
 110:     this(selected, state, 0, null);
 111:   }
 112: 
 113:   /**
 114:    * Create an input method highlight style, with null style mapping.
 115:    *
 116:    * @param selected whether the text range is selected
 117:    * @param state either {@link #RAW_TEXT} or {@link #CONVERTED_TEXT}
 118:    * @param variation the style variation
 119:    * @throws IllegalArgumentException if state is invalid
 120:    */
 121:   public InputMethodHighlight(boolean selected, int state, int variation)
 122:   {
 123:     this(selected, state, variation, null);
 124:   }
 125: 
 126:   /**
 127:    * Create an input method highlight style.
 128:    *
 129:    * @param selected whether the text range is selected
 130:    * @param state either {@link #RAW_TEXT} or {@link #CONVERTED_TEXT}
 131:    * @param variation the style variation
 132:    * @param style an unmodifiable map of rendering styles, or null
 133:    * @throws IllegalArgumentException if state is invalid
 134:    * @since 1.3
 135:    */
 136:   public InputMethodHighlight(boolean selected, int state, int variation,
 137:                               Map style)
 138:   {
 139:     if (state != RAW_TEXT && state != CONVERTED_TEXT)
 140:       throw new IllegalArgumentException();
 141:     this.selected = selected;
 142:     this.state = state;
 143:     this.variation = variation;
 144:     this.style = style;
 145:   }
 146: 
 147:   /**
 148:    * Return whether the highlighting applies to selected text.
 149:    *
 150:    * @return the selection status
 151:    */
 152:   public boolean isSelected()
 153:   {
 154:     return selected;
 155:   }
 156: 
 157:   /**
 158:    * Return the conversion state of the highlighted text.
 159:    *
 160:    * @return one of {@link #RAW_TEXT} or {@link #CONVERTED_TEXT}
 161:    */
 162:   public int getState()
 163:   {
 164:     return state;
 165:   }
 166: 
 167:   /**
 168:    * Return the highlighting style variation.
 169:    *
 170:    * @return the variation
 171:    */
 172:   public int getVariation()
 173:   {
 174:     return variation;
 175:   }
 176: 
 177:   /**
 178:    * Return the rendering style attributes map, or null if it should be the
 179:    * default mapping.
 180:    *
 181:    * @return the style map
 182:    * @since 1.3
 183:    */
 184:   public Map getStyle()
 185:   {
 186:     return style;
 187:   }
 188: } // class InputMethodHighlight