KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwt > awt > font > TextHitInfo


1 /*
2    SwingWT
3    Copyright(c)2003-2004 Daniel Naab
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: dannaab@users.sourceforge.net
9
10    $Log: TextHitInfo.java,v $
11    Revision 1.3 2004/04/16 22:45:32 dannaab
12    Add copyright msg
13
14 */

15
16 package swingwt.awt.font;
17
18 /**
19  * @author Naab
20  * @version %I%, %G%
21  */

22 public class TextHitInfo
23 {
24     private int charIndex;
25     private boolean isLeadingEdge;
26
27     public static TextHitInfo leading(int charIndex) { return new TextHitInfo(charIndex, true); }
28     public static TextHitInfo trailing(int charIndex) { return new TextHitInfo(charIndex, false); }
29     public static TextHitInfo beforeOffset(int offset) { return new TextHitInfo(offset - 1, false); }
30     public static TextHitInfo afterOffset(int offset) { return new TextHitInfo(offset, true); }
31
32     private TextHitInfo(int charIndex, boolean isLeadingEdge)
33     {
34         this.charIndex = charIndex;
35         this.isLeadingEdge = isLeadingEdge;
36     }
37
38     public TextHitInfo getOffsetHit(int delta) { return new TextHitInfo(charIndex + delta, isLeadingEdge); }
39
40     public int getCharIndex() { return charIndex; }
41     public boolean isLeadingEdge() { return isLeadingEdge; }
42
43     public int getInsertionIndex()
44     {
45         int insertionIndex = charIndex;
46         if (!isLeadingEdge) insertionIndex++;
47
48         return insertionIndex;
49     }
50
51     public TextHitInfo getOtherHit()
52     {
53         TextHitInfo textHitInfo;
54
55         if (isLeadingEdge)
56             textHitInfo = trailing(charIndex - 1);
57         else
58             textHitInfo = leading(charIndex + 1);
59
60         return textHitInfo;
61     }
62
63     public int hashCode() { return charIndex; }
64     public boolean equals(Object JavaDoc obj)
65     {
66         boolean equals = false;
67
68         if ( (obj != null) &&
69              (obj instanceof TextHitInfo) &&
70              (charIndex == ((TextHitInfo)obj).charIndex) &&
71              (isLeadingEdge == ((TextHitInfo)obj).isLeadingEdge) )
72             equals = true;
73
74         return equals;
75     }
76 }
77
Popular Tags