KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > spelling > PropertiesValueBreakIterator


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.internal.ui.text.spelling;
13
14 import com.ibm.icu.text.BreakIterator;
15 import java.text.CharacterIterator JavaDoc;
16 import java.util.Locale JavaDoc;
17
18 /**
19  * {@link BreakIterator} implementation for properties values.
20  *
21  * @since 3.1
22  */

23 public class PropertiesValueBreakIterator extends BreakIterator {
24
25     /** Parent break iterator from which breaks will be changed and filtered */
26     private BreakIterator fParent;
27
28     /** Underlying character iterator */
29     private CharacterIterator JavaDoc fText;
30
31     /**
32      * Initialize with the given locale.
33      *
34      * @param locale the locale
35      */

36     public PropertiesValueBreakIterator(Locale JavaDoc locale) {
37         fParent= BreakIterator.getWordInstance(locale);
38     }
39
40     /*
41      * @see java.text.BreakIterator#current()
42      */

43     public int current() {
44         return correct(fParent.current());
45     }
46
47     /*
48      * @see java.text.BreakIterator#first()
49      */

50     public int first() {
51         return fParent.first();
52     }
53
54     /*
55      * @see java.text.BreakIterator#last()
56      */

57     public int last() {
58         return fParent.last();
59     }
60
61     /*
62      * @see java.text.BreakIterator#next()
63      */

64     public int next() {
65         int next= fParent.next();
66         while (!stopAt(next))
67             next= fParent.next();
68         return correct(next);
69     }
70
71     /*
72      * @see java.text.BreakIterator#previous()
73      */

74     public int previous() {
75         int previous= fParent.previous();
76         while (!stopAt(previous))
77             previous= fParent.previous();
78         return correct(previous);
79     }
80
81     /*
82      * @see java.text.BreakIterator#following(int)
83      */

84     public int following(int offset) {
85         int following= fParent.following(offset);
86         while (!stopAt(following))
87             following= fParent.following(offset);
88         return correct(following);
89     }
90
91     /*
92      * @see java.text.BreakIterator#next(int)
93      */

94     public int next(int n) {
95         int next= fParent.next(n);
96         while (!stopAt(next))
97             next= fParent.next(n);
98         return correct(next);
99     }
100
101     /*
102      * @see java.text.BreakIterator#getText()
103      */

104     public CharacterIterator JavaDoc getText() {
105         return fParent.getText();
106     }
107
108     /*
109      * @see java.text.BreakIterator#setText(java.text.CharacterIterator)
110      */

111     public void setText(CharacterIterator JavaDoc newText) {
112         fText= newText;
113         fParent.setText(newText);
114     }
115
116     /**
117      * Should stop at the given index?
118      *
119      * @param index the index
120      * @return <code>true</code> iff the iterator should stop at the given index
121      */

122     private boolean stopAt(int index) {
123         if (index == BreakIterator.DONE)
124             return true;
125         if (index > getBeginIndex() && index < getEndIndex() - 1 && !Character.isWhitespace(charAt(index - 1)) && !Character.isWhitespace(charAt(index)) && (charAt(index - 1) == '&' || charAt(index) == '&'))
126             return false;
127         return true;
128     }
129
130     /**
131      * Returns the corrected break index of the given index.
132      *
133      * @param index the index
134      * @return the corrected index
135      */

136     private int correct(int index) {
137         if (index == BreakIterator.DONE)
138             return index;
139         if (index > getBeginIndex() && index < getEndIndex() - 1 && !Character.isWhitespace(charAt(index - 1)) && charAt(index - 1) == '\\')
140             return index - 1;
141         return index;
142     }
143
144     /**
145      * Returns the underlying character at the given index.
146      *
147      * @param index the index
148      * @return the character
149      */

150     private char charAt(int index) {
151         int oldIndex= fText.getIndex();
152         char ch= fText.setIndex(index);
153         fText.setIndex(oldIndex);
154         return ch;
155     }
156
157     /**
158      * Returns the exclusive end index of the underlying character iterator.
159      *
160      * @return the exclusive end index of the underlying character iterator
161      */

162     private int getEndIndex() {
163         return fText.getEndIndex();
164     }
165
166     /**
167      * Returns the inclusive begin index of the underlying character iterator.
168      *
169      * @return the inclusive begin index of the underlying character iterator
170      */

171     private int getBeginIndex() {
172         return fText.getBeginIndex();
173     }
174 }
175
Popular Tags