001: /*
002: *******************************************************************************
003: * Copyright (C) 1996-2004, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007: package com.ibm.icu.dev.demo.impl;
008:
009: import java.text.*;
010:
011: public final class Selection {
012:
013: public int anchor;
014: public int caret;
015: public boolean clickAfter;
016:
017: public int getStart() {
018: return anchor < caret ? anchor : caret;
019: }
020:
021: public int getEnd() {
022: return anchor > caret ? anchor : caret;
023: }
024:
025: public boolean isCaret() {
026: return anchor == caret;
027: }
028:
029: public Selection set(Selection other) {
030: anchor = other.anchor;
031: caret = other.caret;
032: clickAfter = other.clickAfter;
033: return this ;
034: }
035:
036: public Selection set(int anchor, int caret, boolean clickAfter) {
037: this .anchor = anchor;
038: this .caret = caret;
039: this .clickAfter = clickAfter;
040: return this ;
041: }
042:
043: public boolean equals(Object other) {
044: Selection other2 = (Selection) other;
045: return anchor == other2.anchor && caret == other2.caret
046: && clickAfter == other2.clickAfter;
047: }
048:
049: public boolean isLessThan(Selection other) {
050: return getStart() < other.getEnd();
051: }
052:
053: public Selection pin(String text) {
054: if (anchor > text.length()) {
055: anchor = text.length();
056: } else if (anchor < 0) {
057: anchor = 0;
058: }
059: if (caret > text.length()) {
060: caret = text.length();
061: clickAfter = true;
062: } else if (caret < 0) {
063: caret = 0;
064: clickAfter = false;
065: }
066: return this ;
067: }
068:
069: public Selection swap(Selection after) {
070: int temp = anchor;
071: anchor = after.anchor;
072: after.anchor = temp;
073: temp = caret;
074: caret = after.caret;
075: after.caret = temp;
076: boolean b = clickAfter;
077: clickAfter = after.clickAfter;
078: after.clickAfter = b;
079: return this ;
080: }
081:
082: public Selection fixAfterReplace(int start, int end, int len) {
083: if (anchor >= start) {
084: if (anchor < end)
085: anchor = end;
086: anchor = start + len + anchor - end;
087: }
088: if (caret >= start) {
089: if (caret < end)
090: caret = end;
091: caret = start + len + caret - end;
092: }
093: return this ;
094: }
095:
096: // Mac & Windows considerably different
097: // Mac: end++. If start!=end, start=end
098: // SHIFT: move end right
099: // CTL: no different
100: // Windows:
101: // UNSHIFTED: if start!=end, start = end, else start=end=end+1;
102: // anchor = tip = start
103: // SHIFT: tip++
104: // CTL: if start!=end, start = end = nextbound(end-1),
105: // else start=end=nextbound(end)
106: // anchor = tip = start
107: // CTL/SHIFT: tip = nextbound(tip)
108:
109: public Selection nextBound(BreakIterator breaker, int direction,
110: boolean extend) {
111: if (!extend && anchor != caret)
112: caret -= direction;
113: caret = next(caret, breaker, direction, true);
114: if (!extend)
115: anchor = caret;
116: clickAfter = false;
117: return this ;
118: }
119:
120: // expand start and end to word breaks--if they are not already on one
121: public void expand(BreakIterator breaker) {
122: if (anchor <= caret) {
123: anchor = next(anchor, breaker, -1, false);
124: caret = next(caret, breaker, 1, false);
125: /*
126: try {
127: breaker.following(anchor);
128: anchor = breaker.previous();
129: } catch (Exception e) {}
130: try {
131: caret = breaker.following(caret-1);
132: } catch (Exception e) {}
133: */
134: } else {
135: anchor = next(anchor, breaker, 1, false);
136: caret = next(caret, breaker, -1, false);
137: /*
138: try {
139: breaker.following(caret);
140: caret = breaker.previous();
141: } catch (Exception e) {}
142: try {
143: anchor = breaker.following(anchor-1);
144: } catch (Exception e) {}
145: */
146: }
147: }
148:
149: // different = false - move to next boundary, unless on one
150: // true - move to next boundary, even if on one
151: public static int next(int position, BreakIterator breaker,
152: int direction, boolean different) {
153: if (!different)
154: position -= direction;
155: try {
156: if (direction > 0) {
157: position = breaker.following(position);
158: } else {
159: breaker.following(position - 1);
160: position = breaker.previous();
161: }
162: } catch (Exception e) {
163: }
164: return position;
165: }
166: }
|