1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17/* 18 * @author Oleg V. Khaschansky 19 * @version $Revision$ 20 */ 21 22package java.awt.font; 23 24import org.apache.harmony.misc.HashCode; 25 26/** 27 * The TextHitInfo class provides information about a caret position in a text 28 * model for insertion or deletion of a character in a text. The TextHitInfo 29 * defines two biases of the character: leading or trailing. Leading position 30 * means the left edge of the specified character (TextHitInfo.leading(2) method 31 * for "text" returns the left side of "x"). Trailing position means the right 32 * edge of the specified character (TextHitInfo.trailing(2) method for "text" 33 * returns the right side of "x"). 34 * 35 * @since Android 1.0 36 */ 37public final class TextHitInfo { 38 39 /** 40 * The char idx. 41 */ 42 private int charIdx; // Represents character index in the line 43 44 /** 45 * The is trailing. 46 */ 47 private boolean isTrailing; 48 49 /** 50 * Instantiates a new text hit info. 51 * 52 * @param idx 53 * the idx. 54 * @param isTrailing 55 * the is trailing. 56 */ 57 private TextHitInfo(int idx, boolean isTrailing) { 58 charIdx = idx; 59 this.isTrailing = isTrailing; 60 } 61 62 /** 63 * Returns the textual string representation of this TextHitInfo instance. 64 * 65 * @return the string representation. 66 */ 67 @Override 68 public String toString() { 69 return new String("TextHitInfo[" + charIdx + ", " + //$NON-NLS-1$ //$NON-NLS-2$ 70 (isTrailing ? "Trailing" : "Leading") + "]" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ 71 ); 72 } 73 74 /** 75 * Compares this TextHitInfo object with the specified object. 76 * 77 * @param obj 78 * the Object to be compared. 79 * @return true, if the specified object is a TextHitInfo object with the 80 * same data values as this TextHitInfo, false otherwise. 81 */ 82 @Override 83 public boolean equals(Object obj) { 84 if (obj instanceof TextHitInfo) { 85 return equals((TextHitInfo)obj); 86 } 87 return false; 88 } 89 90 /** 91 * Compares this TextHitInfo object with the specified TextHitInfo object. 92 * 93 * @param thi 94 * the TextHitInfo object to be compared. 95 * @return true, if this TextHitInfo object has the same data values as the 96 * specified TextHitInfo object, false otherwise. 97 */ 98 public boolean equals(TextHitInfo thi) { 99 return thi != null && thi.charIdx == charIdx && thi.isTrailing == isTrailing; 100 } 101 102 /** 103 * Gets a TextHitInfo object with its character index at the specified 104 * offset from the character index of this TextHitInfo. 105 * 106 * @param offset 107 * the offset. 108 * @return the TextHitInfo. 109 */ 110 public TextHitInfo getOffsetHit(int offset) { 111 return new TextHitInfo(charIdx + offset, isTrailing); 112 } 113 114 /** 115 * Gets a TextHitInfo associated with the other side of the insertion point. 116 * 117 * @return the other hit. 118 */ 119 public TextHitInfo getOtherHit() { 120 return isTrailing ? new TextHitInfo(charIdx + 1, false) 121 : new TextHitInfo(charIdx - 1, true); 122 } 123 124 /** 125 * Returns true if the leading edge of the character is hit, false if the 126 * trailing edge of the character is hit. 127 * 128 * @return true if the leading edge of the character is hit, false if the 129 * trailing edge of the character is hit. 130 */ 131 public boolean isLeadingEdge() { 132 return !isTrailing; 133 } 134 135 /** 136 * Returns the hash code value of this TextHitInfo instance. 137 * 138 * @return the hash code value. 139 */ 140 @Override 141 public int hashCode() { 142 return HashCode.combine(charIdx, isTrailing); 143 } 144 145 /** 146 * Gets the insertion index. 147 * 148 * @return the insertion index: character index if the leading edge is hit, 149 * or character index + 1 if the trailing edge is hit. 150 */ 151 public int getInsertionIndex() { 152 return isTrailing ? charIdx + 1 : charIdx; 153 } 154 155 /** 156 * Gets the index of the character hit. 157 * 158 * @return the character hit's index. 159 */ 160 public int getCharIndex() { 161 return charIdx; 162 } 163 164 /** 165 * Returns a TextHitInfo associated with the trailing edge of the character 166 * at the specified char index. 167 * 168 * @param charIndex 169 * the char index. 170 * @return the TextHitInfo associated with the trailing edge of the 171 * character at the specified char index. 172 */ 173 public static TextHitInfo trailing(int charIndex) { 174 return new TextHitInfo(charIndex, true); 175 } 176 177 /** 178 * Returns a TextHitInfo object associated with the leading edge of the 179 * character at the specified char index. 180 * 181 * @param charIndex 182 * the char index. 183 * @return the TextHitInfo object associated with the leading edge of the 184 * character at the specified char index. 185 */ 186 public static TextHitInfo leading(int charIndex) { 187 return new TextHitInfo(charIndex, false); 188 } 189 190 /** 191 * Returns a (trailing) TextHitInfo object associated with the character 192 * before the specified offset. 193 * 194 * @param offset 195 * the offset. 196 * @return the TextHitInfo object associated with the character before the 197 * specified offset. 198 */ 199 public static TextHitInfo beforeOffset(int offset) { 200 return new TextHitInfo(offset - 1, true); 201 } 202 203 /** 204 * Returns a (leading) TextHitInfo object associated with the character 205 * after the specified offset. 206 * 207 * @param offset 208 * the offset. 209 * @return the TextHitInfo object associated with the character after the 210 * specified offset. 211 */ 212 public static TextHitInfo afterOffset(int offset) { 213 return new TextHitInfo(offset, false); 214 } 215} 216