]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/demos/src/com/ibm/icu/dev/demo/impl/Selection.java
Clean up imports.
[Dictionary.git] / jars / icu4j-52_1 / demos / src / com / ibm / icu / dev / demo / impl / Selection.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 1996-2010, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.dev.demo.impl;
8 import java.text.BreakIterator;
9
10 public final class Selection {
11
12     public int anchor;
13     public int caret;
14     public boolean clickAfter;
15
16     public int getStart() {
17         return anchor < caret ? anchor : caret;
18     }
19
20     public int getEnd() {
21         return anchor > caret ? anchor : caret;
22     }
23
24     public boolean isCaret() {
25         return anchor == caret;
26     }
27
28     public Selection set(Selection other) {
29         anchor = other.anchor;
30         caret = other.caret;
31         clickAfter = other.clickAfter;
32         return this;
33     }
34
35     public Selection set(int anchor, int caret, boolean clickAfter) {
36         this.anchor = anchor;
37         this.caret = caret;
38         this.clickAfter = clickAfter;
39         return this;
40     }
41
42     public boolean equals(Object other) {
43         Selection other2 = (Selection)other;
44         return anchor == other2.anchor
45           && caret == other2.caret
46           && clickAfter == other2.clickAfter;
47     }
48
49     public boolean isLessThan(Selection other) {
50         return getStart() < other.getEnd();
51     }
52
53     public Selection pin(String text) {
54         if (anchor > text.length()) {
55             anchor = text.length();
56         } else if (anchor < 0) {
57             anchor = 0;
58         }
59         if (caret > text.length()) {
60             caret = text.length();
61             clickAfter = true;
62         } else if (caret < 0) {
63             caret = 0;
64             clickAfter = false;
65         }
66         return this;
67     }
68
69     public Selection swap(Selection after) {
70         int temp = anchor;
71         anchor = after.anchor;
72         after.anchor = temp;
73         temp = caret;
74         caret = after.caret;
75         after.caret = temp;
76         boolean b = clickAfter;
77         clickAfter = after.clickAfter;
78         after.clickAfter = b;
79         return this;
80     }
81
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;
86         }
87         if (caret >= start) {
88             if (caret < end) caret = end;
89             caret = start + len + caret - end;
90         }
91         return this;
92     }
93
94         // Mac & Windows considerably different
95         // Mac: end++. If start!=end, start=end
96         //  SHIFT: move end right
97         //  CTL: no different
98         // Windows:
99         //  UNSHIFTED: if start!=end, start = end, else start=end=end+1;
100         //       anchor = tip = start
101         //  SHIFT: tip++
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)
106
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;
112         clickAfter = false;
113         return this;
114     }
115
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);
121             /*
122             try {
123                 breaker.following(anchor);
124                 anchor = breaker.previous();
125             } catch (Exception e) {}
126             try {
127                 caret = breaker.following(caret-1);
128             } catch (Exception e) {}
129             */
130         } else {
131             anchor = next(anchor,breaker,1,false);
132             caret = next(caret,breaker,-1,false);
133             /*
134             try {
135                 breaker.following(caret);
136                 caret = breaker.previous();
137             } catch (Exception e) {}
138             try {
139                 anchor = breaker.following(anchor-1);
140             } catch (Exception e) {}
141             */
142         }
143     }
144
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;
150         try {
151             if (direction > 0) {
152                 position = breaker.following(position);
153             } else {
154                 breaker.following(position-1);
155                 position = breaker.previous();
156             }
157         } catch (Exception e) {}
158         return position;
159     }
160 }
161