KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > usermodel > HSSFRichTextString


1 /* ====================================================================
2    Copyright 2004 Apache Software Foundation
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 ==================================================================== */

16
17 package org.apache.poi.hssf.usermodel;
18
19 import java.util.Map JavaDoc;
20 import java.util.SortedMap JavaDoc;
21 import java.util.TreeMap JavaDoc;
22
23 /**
24  * Rich text unicode string. These strings can have fonts applied to
25  * arbitary parts of the string.
26  *
27  * @author Glen Stampoultzis (glens at apache.org)
28  */

29 public class HSSFRichTextString
30         implements Comparable JavaDoc
31 {
32     /** Place holder for indicating that NO_FONT has been applied here */
33     public static final short NO_FONT = -1;
34
35     String JavaDoc string;
36     SortedMap JavaDoc formattingRuns = new TreeMap JavaDoc();
37
38     public HSSFRichTextString()
39     {
40         this("");
41     }
42
43     public HSSFRichTextString( String JavaDoc string )
44     {
45         this.string = string;
46         this.formattingRuns.put(new Integer JavaDoc(0), new Short JavaDoc(NO_FONT));
47     }
48
49     /**
50      * Applies a font to the specified characters of a string.
51      *
52      * @param startIndex The start index to apply the font to (inclusive)
53      * @param endIndex The end index to apply the font to (exclusive)
54      * @param fontIndex The font to use.
55      */

56     public void applyFont(int startIndex, int endIndex, short fontIndex)
57     {
58         if (startIndex > endIndex)
59             throw new IllegalArgumentException JavaDoc("Start index must be less than end index.");
60         if (startIndex < 0 || endIndex > length())
61             throw new IllegalArgumentException JavaDoc("Start and end index not in range.");
62         if (startIndex == endIndex)
63             return;
64
65         Integer JavaDoc from = new Integer JavaDoc(startIndex);
66         Integer JavaDoc to = new Integer JavaDoc(endIndex);
67         short fontAtIndex = NO_FONT;
68         if (endIndex != length())
69             fontAtIndex = getFontAtIndex(endIndex);
70         formattingRuns.subMap(from, to).clear();
71         formattingRuns.put(from, new Short JavaDoc(fontIndex));
72         if (endIndex != length())
73         {
74             if (fontIndex != fontAtIndex)
75                 formattingRuns.put(to, new Short JavaDoc(fontAtIndex));
76         }
77     }
78
79     /**
80      * Applies a font to the specified characters of a string.
81      *
82      * @param startIndex The start index to apply the font to (inclusive)
83      * @param endIndex The end index to apply to font to (exclusive)
84      * @param font The index of the font to use.
85      */

86     public void applyFont(int startIndex, int endIndex, HSSFFont font)
87     {
88         applyFont(startIndex, endIndex, font.getIndex());
89     }
90
91     /**
92      * Sets the font of the entire string.
93      * @param font The font to use.
94      */

95     public void applyFont(HSSFFont font)
96     {
97         applyFont(0, string.length(), font);
98     }
99
100     /**
101      * Returns the plain string representation.
102      */

103     public String JavaDoc getString()
104     {
105         return string;
106     }
107
108     /**
109      * @return the number of characters in the font.
110      */

111     public int length()
112     {
113         return string.length();
114     }
115
116     /**
117      * Returns the font in use at a particular index.
118      *
119      * @param index The index.
120      * @return The font that's currently being applied at that
121      * index or null if no font is being applied or the
122      * index is out of range.
123      */

124     public short getFontAtIndex( int index )
125     {
126         if (index < 0 || index >= string.length())
127             throw new ArrayIndexOutOfBoundsException JavaDoc("Font index " + index + " out of bounds of string");
128         Integer JavaDoc key = new Integer JavaDoc(index + 1);
129         SortedMap JavaDoc head = formattingRuns.headMap(key);
130         if (head.isEmpty())
131             throw new IllegalStateException JavaDoc("Should not reach here. No font found.");
132         else
133             return ((Short JavaDoc) head.get(head.lastKey())).shortValue();
134     }
135
136     /**
137      * @return The number of formatting runs used. There will always be at
138      * least one of font NO_FONT.
139      *
140      * @see #NO_FONT
141      */

142     public int numFormattingRuns()
143     {
144         return formattingRuns.size();
145     }
146
147     /**
148      * The index within the string to which the specified formatting run applies.
149      * @param index the index of the formatting run
150      * @return the index within the string.
151      */

152     public int getIndexOfFormattingRun(int index)
153     {
154         Map.Entry JavaDoc[] runs = (Map.Entry JavaDoc[]) formattingRuns.entrySet().toArray(new Map.Entry JavaDoc[formattingRuns.size()] );
155         return ((Integer JavaDoc)runs[index].getKey()).intValue();
156     }
157
158     /**
159      * Gets the font used in a particular formatting run.
160      *
161      * @param index the index of the formatting run
162      * @return the font number used.
163      */

164     public short getFontOfFormattingRun(int index)
165     {
166         Map.Entry JavaDoc[] runs = (Map.Entry JavaDoc[]) formattingRuns.entrySet().toArray(new Map.Entry JavaDoc[formattingRuns.size()] );
167         return ((Short JavaDoc)(runs[index].getValue())).shortValue();
168     }
169
170     /**
171      * Compares one rich text string to another.
172      */

173     public int compareTo( Object JavaDoc o )
174     {
175         return 0; // todo
176
}
177
178     /**
179      * @return the plain text representation of this string.
180      */

181     public String JavaDoc toString()
182     {
183         return string;
184     }
185
186     /**
187      * Applies the specified font to the entire string.
188      *
189      * @param fontIndex the font to apply.
190      */

191     public void applyFont( short fontIndex )
192     {
193         applyFont(0, string.length(), fontIndex);
194     }
195 }
196
Popular Tags