2 *******************************************************************************
\r
3 * Copyright (C) 1996-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.demo.impl;
\r
8 import java.text.BreakIterator;
\r
10 public final class Selection {
\r
14 public boolean clickAfter;
\r
16 public int getStart() {
\r
17 return anchor < caret ? anchor : caret;
\r
20 public int getEnd() {
\r
21 return anchor > caret ? anchor : caret;
\r
24 public boolean isCaret() {
\r
25 return anchor == caret;
\r
28 public Selection set(Selection other) {
\r
29 anchor = other.anchor;
\r
30 caret = other.caret;
\r
31 clickAfter = other.clickAfter;
\r
35 public Selection set(int anchor, int caret, boolean clickAfter) {
\r
36 this.anchor = anchor;
\r
38 this.clickAfter = clickAfter;
\r
42 public boolean equals(Object other) {
\r
43 Selection other2 = (Selection)other;
\r
44 return anchor == other2.anchor
\r
45 && caret == other2.caret
\r
46 && clickAfter == other2.clickAfter;
\r
49 public boolean isLessThan(Selection other) {
\r
50 return getStart() < other.getEnd();
\r
53 public Selection pin(String text) {
\r
54 if (anchor > text.length()) {
\r
55 anchor = text.length();
\r
56 } else if (anchor < 0) {
\r
59 if (caret > text.length()) {
\r
60 caret = text.length();
\r
62 } else if (caret < 0) {
\r
69 public Selection swap(Selection after) {
\r
71 anchor = after.anchor;
\r
72 after.anchor = temp;
\r
74 caret = after.caret;
\r
76 boolean b = clickAfter;
\r
77 clickAfter = after.clickAfter;
\r
78 after.clickAfter = b;
\r
82 public Selection fixAfterReplace(int start, int end, int len) {
\r
83 if (anchor >= start) {
\r
84 if (anchor < end) anchor = end;
\r
85 anchor = start + len + anchor - end;
\r
87 if (caret >= start) {
\r
88 if (caret < end) caret = end;
\r
89 caret = start + len + caret - end;
\r
94 // Mac & Windows considerably different
\r
95 // Mac: end++. If start!=end, start=end
\r
96 // SHIFT: move end right
\r
97 // CTL: no different
\r
99 // UNSHIFTED: if start!=end, start = end, else start=end=end+1;
\r
100 // anchor = tip = start
\r
102 // CTL: if start!=end, start = end = nextbound(end-1),
\r
103 // else start=end=nextbound(end)
\r
104 // anchor = tip = start
\r
105 // CTL/SHIFT: tip = nextbound(tip)
\r
107 public Selection nextBound(BreakIterator breaker,
\r
108 int direction, boolean extend) {
\r
109 if (!extend && anchor != caret) caret -= direction;
\r
110 caret = next(caret, breaker, direction, true);
\r
111 if (!extend) anchor = caret;
\r
112 clickAfter = false;
\r
116 // expand start and end to word breaks--if they are not already on one
\r
117 public void expand(BreakIterator breaker) {
\r
118 if (anchor <= caret) {
\r
119 anchor = next(anchor,breaker,-1,false);
\r
120 caret = next(caret,breaker,1,false);
\r
123 breaker.following(anchor);
\r
124 anchor = breaker.previous();
\r
125 } catch (Exception e) {}
\r
127 caret = breaker.following(caret-1);
\r
128 } catch (Exception e) {}
\r
131 anchor = next(anchor,breaker,1,false);
\r
132 caret = next(caret,breaker,-1,false);
\r
135 breaker.following(caret);
\r
136 caret = breaker.previous();
\r
137 } catch (Exception e) {}
\r
139 anchor = breaker.following(anchor-1);
\r
140 } catch (Exception e) {}
\r
145 // different = false - move to next boundary, unless on one
\r
146 // true - move to next boundary, even if on one
\r
147 public static int next(int position, BreakIterator breaker,
\r
148 int direction, boolean different) {
\r
149 if (!different) position -= direction;
\r
151 if (direction > 0) {
\r
152 position = breaker.following(position);
\r
154 breaker.following(position-1);
\r
155 position = breaker.previous();
\r
157 } catch (Exception e) {}
\r