home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 6.3 KB | 200 lines |
- /*
- * @(#)TextHitInfo.java 1.21 98/03/18
- *
- * Copyright 1997, 1998 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- /*
- * (C) Copyright Taligent, Inc. 1996 - 1997, All Rights Reserved
- * (C) Copyright IBM Corp. 1996 - 1998, All Rights Reserved
- *
- * The original version of this source code and documentation is
- * copyrighted and owned by Taligent, Inc., a wholly-owned subsidiary
- * of IBM. These materials are provided under terms of a License
- * Agreement between Taligent and Sun. This technology is protected
- * by multiple US and International patents.
- *
- * This notice and attribution to Taligent may not be removed.
- * Taligent is a registered trademark of Taligent, Inc.
- *
- */
-
- package java.awt.font;
- import java.lang.String;
-
- /**
- * TextHitInfo represents a character position in a text model, and a
- * <b>bias</b>, or "side," of the character. Biases are either LEADING
- * (the left edge, for a left-to-right character) or TRAILING (the
- * right edge, for a left-to-right character). Instances of
- * TextHitInfo are used to specify caret and insertion positions within
- * text.
- * <p>
- * For example, consider the text "abc". The TextHitInfo (1, TRAILING)
- * corresponds to the right side of the 'b' in the text.
- * <p>
- * TextHitInfo is used primarily by TextLayout and clients of
- * TextLayout. Clients of TextLayout will query TextHitInfo instances
- * for an insertion offset, where new text will be inserted into the
- * text model. The insertion offset is equals to the character
- * position in the TextHitInfo if the bias is LEADING, and one
- * character after if the bias is TRAILING. The insertion offset for
- * TextHitInfo (1, TRAILING) is 2.
- * <p>
- * Sometimes it is convenient to construct a TextHitInfo with the same
- * insertion offset as an existing one, but on the opposite character.
- * <code>getOtherHit</code> will construct a new TextHitInfo with the
- * same insertion offset as an existing one, with a hit on the
- * character on the other side of the insertion offset. Calling
- * <code>getOtherHit</code> on (1, TRAILING) would return (2, LEADING).
- * In general, <code>getOtherHit</code> for (n, TRAILING) returns
- * (n+1, LEADING) and <code>getOtherHit</code> for (n, LEADING)
- * returns (n-1, TRAILING).
- * <p>
- * <strong>Example</strong>:<p>
- * Converting a graphical point to an insertion point within a text
- * model
- * <blockquote><pre>
- * TextLayout layout = ...;
- * Point2D hitPoint = ...;
- * TextHitInfo hitInfo = layout.hitTestChar(hitPoint.x, hitPoint.y);
- * int insPoint = hitInfo.getInsertionOffset();
- * // insPoint is relative to layout; may need to adjust for use
- * // in a text model
- * </pre></blockquote>
- *
- * @see TextLayout
- */
-
- public final class TextHitInfo {
- private int charIndex;
- private boolean isLeadingEdge;
-
- public static boolean LEADING = true;
- public static boolean TRAILING = false;
-
- /**
- * Constructor for TextHitInfo.
- * @param charIndex the index of the character hit.
- * @param isLeadingEdge true if the leading edge of the
- * character was hit.
- */
- private TextHitInfo(int charIndex, boolean isLeadingEdge) {
- this.charIndex = charIndex;
- this.isLeadingEdge = isLeadingEdge;
- }
-
- /**
- * Returns the index of the character hit.
- */
- public int getCharIndex() {
- return charIndex;
- }
-
- /**
- * Returns true if the leading edge of the character was hit.
- */
- public boolean isLeadingEdge() {
- return isLeadingEdge;
- }
-
- /**
- * Returns the insertion index. This is the character index if
- * the leading edge of the character was hit, and one greater
- * than the character index if the trailing edge was hit.
- */
- public int getInsertionIndex() {
- return isLeadingEdge ? charIndex : charIndex + 1;
- }
-
- /**
- * Returns the hash code.
- */
- public int hashCode() {
- return charIndex;
- }
-
- /**
- * Return true if the object is a TextHitInfo and this TextHitInfo
- * is equal to it.
- */
- public boolean equals(Object obj) {
- return (obj instanceof TextHitInfo) && equals((TextHitInfo)obj);
- }
-
- /**
- * Return true if the hit's character index and edge are the same.
- *
- * This is not the same as having the same insertion offset.
- */
- public boolean equals(TextHitInfo hitInfo) {
- return hitInfo != null && charIndex == hitInfo.charIndex &&
- isLeadingEdge == hitInfo.isLeadingEdge;
- }
-
- /**
- * Return a string representing the hit, for debugging use only.
- */
- public String toString() {
- return "TextHitInfo[" + charIndex + (isLeadingEdge ? "L" : "T")+"]";
- }
-
- /**
- * Create a hit on the leading edge of the character at charIndex.
- */
- public static TextHitInfo leading(int charIndex) {
- return new TextHitInfo(charIndex, true);
- }
-
- /**
- * Create a hit on the trailing edge of the character at charIndex.
- */
- public static TextHitInfo trailing(int charIndex) {
- return new TextHitInfo(charIndex, false);
- }
-
- /**
- * Create a hit at offset, associated with the character before
- * the offset.
- */
- public static TextHitInfo beforeOffset(int offset) {
- return new TextHitInfo(offset-1, false);
- }
-
- /**
- * Create a hit at offset, associated with the character after
- * the offset.
- */
- public static TextHitInfo afterOffset(int offset) {
- return new TextHitInfo(offset, true);
- }
-
- /**
- * Creates a hit on the other side of the insertion point.
- * This hit remains unchanged.
- */
- public TextHitInfo getOtherHit() {
- if (isLeadingEdge) {
- return trailing(charIndex - 1);
- } else {
- return leading(charIndex + 1);
- }
- }
-
- /**
- * Creates a hit whose character index is offset by delta from this
- * hit. This hit remains unchanged.
- */
- public TextHitInfo getOffsetHit(int delta) {
- return new TextHitInfo(charIndex + delta, isLeadingEdge);
- }
- }
-