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