blob: 17bbf717cef1c8f87c62f8d2a0dfac4372f786b3 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
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/**
28 * The TextHitInfo class provides information about a caret position
29 * in a text model for insertion or deletion of a character in a text.
30 * The TextHitInfo defines two biases of the character: leading or trailing.
31 * Leading position means the left edge of the specified character
32 * (TextHitInfo.leading(2) method for "text" returns the left side of "x").
33 * Trailing position means the right edge of the specified character
34 * (TextHitInfo.trailing(2) method for "text" returns the right side of "x").
35 */
36public final class TextHitInfo {
37
38 /** The char idx. */
39 private int charIdx; // Represents character index in the line
40
41 /** The is trailing. */
42 private boolean isTrailing;
43
44 /**
45 * Instantiates a new text hit info.
46 *
47 * @param idx the idx
48 * @param isTrailing the is trailing
49 */
50 private TextHitInfo(int idx, boolean isTrailing) {
51 charIdx = idx;
52 this.isTrailing = isTrailing;
53 }
54
55 /**
56 * To string.
57 *
58 * @return the string
59 */
60 @Override
61 public String toString() {
62 return new String(
63 "TextHitInfo[" + charIdx + ", " + //$NON-NLS-1$ //$NON-NLS-2$
64 (isTrailing?"Trailing":"Leading") + "]" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
65 );
66 }
67
68 /**
69 * Compares this TextHitInfo object with the specified object.
70 *
71 * @param obj the Object to be compared.
72 *
73 * @return true, if the specified object is a TextHitInfo object
74 * with the same data values as this TextHitInfo, false otherwise.
75 */
76 @Override
77 public boolean equals(Object obj) {
78 if (obj instanceof TextHitInfo) {
79 return equals((TextHitInfo) obj);
80 }
81 return false;
82 }
83
84 /**
85 * Compares this TextHitInfo object with the specified TextHitInfo
86 * object.
87 *
88 * @param thi the TextHitInfo object to be compared.
89 *
90 * @return true, if this TextHitInfo object has the same data values as the
91 * specified TextHitInfo object, false otherwise.
92 */
93 public boolean equals(TextHitInfo thi) {
94 return
95 thi != null &&
96 thi.charIdx == charIdx &&
97 thi.isTrailing == isTrailing;
98 }
99
100 /**
101 * Gets a TextHitInfo object with its character index
102 * at the specified offset from the character index of
103 * this TextHitInfo.
104 *
105 * @param offset the offset.
106 *
107 * @return the TextHitInfo.
108 */
109 public TextHitInfo getOffsetHit(int offset) {
110 return new TextHitInfo(charIdx + offset, isTrailing);
111 }
112
113 /**
114 * Gets a TextHitInfo associated with the other side of
115 * the insertion point.
116 *
117 * @return the other hit.
118 */
119 public TextHitInfo getOtherHit() {
120 return isTrailing ?
121 new TextHitInfo(charIdx+1, false) :
122 new TextHitInfo(charIdx-1, true);
123 }
124
125 /**
126 * Returns true if the leading edge of the character is hit,
127 * false if the trailing edge of the character is hit.
128 *
129 * @return true if the leading edge of the character is hit,
130 * false if the trailing edge of the character is hit.
131 */
132 public boolean isLeadingEdge() {
133 return !isTrailing;
134 }
135
136 /**
137 * Hash code.
138 *
139 * @return the int
140 */
141 @Override
142 public int hashCode() {
143 return HashCode.combine(charIdx, isTrailing);
144 }
145
146 /**
147 * Gets the insertion index.
148 *
149 * @return the insertion index: character index if the leading edge
150 * is hit, or character index + 1 if the trailing edge is hit.
151 */
152 public int getInsertionIndex() {
153 return isTrailing ? charIdx+1 : charIdx;
154 }
155
156 /**
157 * Gets the index of the character hit.
158 *
159 * @return the character hit's index.
160 */
161 public int getCharIndex() {
162 return charIdx;
163 }
164
165 /**
166 * Returns a TextHitInfo associated with the trailing edge of
167 * the character at the specified char index.
168 *
169 * @param charIndex the char index.
170 *
171 * @return the TextHitInfo associated with the trailing edge of
172 * the character at the specified char index.
173 */
174 public static TextHitInfo trailing(int charIndex) {
175 return new TextHitInfo(charIndex, true);
176 }
177
178 /**
179 * Returns a TextHitInfo object associated with the leading edge
180 * of the character at the specified char index.
181 *
182 * @param charIndex the char index.
183 *
184 * @return the TextHitInfo object associated with the leading edge
185 * of the character at the specified char index.
186 */
187 public static TextHitInfo leading(int charIndex) {
188 return new TextHitInfo(charIndex, false);
189 }
190
191 /**
192 * Returns a (trailing) TextHitInfo object associated with the character
193 * before the specified offset.
194 *
195 * @param offset the offset.
196 *
197 * @return the TextHitInfo object associated with the character
198 * before the specified offset.
199 */
200 public static TextHitInfo beforeOffset(int offset) {
201 return new TextHitInfo(offset-1, true);
202 }
203
204 /**
205 * Returns a (leading) TextHitInfo object associated with the character
206 * after the specified offset.
207 *
208 * @param offset the offset.
209 *
210 * @return the TextHitInfo object associated with the character
211 * after the specified offset.
212 */
213 public static TextHitInfo afterOffset(int offset) {
214 return new TextHitInfo(offset, false);
215 }
216}