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