KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > LineBreakingReader


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 package org.eclipse.jdt.internal.ui.text;
12
13
14 import java.io.BufferedReader JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.Reader JavaDoc;
17
18 import com.ibm.icu.text.BreakIterator;
19 import org.eclipse.swt.graphics.GC;
20
21 /*
22  * Not a real reader. Could change if requested
23  */

24 public class LineBreakingReader {
25
26     private BufferedReader JavaDoc fReader;
27     private GC fGC;
28     private int fMaxWidth;
29
30     private String JavaDoc fLine;
31     private int fOffset;
32
33     private BreakIterator fLineBreakIterator;
34     private boolean fBreakWords;
35
36     /**
37      * Creates a reader that breaks an input text to fit in a given width.
38      *
39      * @param reader Reader of the input text
40      * @param gc The graphic context that defines the currently used font sizes
41      * @param maxLineWidth The max width (pixels) where the text has to fit in
42      */

43     public LineBreakingReader(Reader JavaDoc reader, GC gc, int maxLineWidth) {
44         fReader= new BufferedReader JavaDoc(reader);
45         fGC= gc;
46         fMaxWidth= maxLineWidth;
47         fOffset= 0;
48         fLine= null;
49         fLineBreakIterator= BreakIterator.getLineInstance();
50         fBreakWords= true;
51     }
52
53     public boolean isFormattedLine() {
54         return fLine != null;
55     }
56
57     /**
58      * Reads the next line. The lengths of the line will not exceed the given maximum
59      * width.
60      *
61      * @return the next line
62      * @throws IOException
63      */

64     public String JavaDoc readLine() throws IOException JavaDoc {
65         if (fLine == null) {
66             String JavaDoc line= fReader.readLine();
67             if (line == null)
68                 return null;
69
70             int lineLen= fGC.textExtent(line).x;
71             if (lineLen < fMaxWidth) {
72                 return line;
73             }
74             fLine= line;
75             fLineBreakIterator.setText(line);
76             fOffset= 0;
77         }
78         int breakOffset= findNextBreakOffset(fOffset);
79         String JavaDoc res;
80         if (breakOffset != BreakIterator.DONE) {
81             res= fLine.substring(fOffset, breakOffset);
82             fOffset= findWordBegin(breakOffset);
83             if (fOffset == fLine.length()) {
84                 fLine= null;
85             }
86         } else {
87             res= fLine.substring(fOffset);
88             fLine= null;
89         }
90         return res;
91     }
92
93     private int findNextBreakOffset(int currOffset) {
94         int currWidth= 0;
95         int nextOffset= fLineBreakIterator.following(currOffset);
96         while (nextOffset != BreakIterator.DONE) {
97             String JavaDoc word= fLine.substring(currOffset, nextOffset);
98             int wordWidth= fGC.textExtent(word).x;
99             int nextWidth= wordWidth + currWidth;
100             if (nextWidth > fMaxWidth) {
101                 if (currWidth > 0)
102                     return currOffset;
103
104                 if (!fBreakWords)
105                     return nextOffset;
106
107                 // need to fit into fMaxWidth
108
int length= word.length();
109                 while (length >= 0) {
110                     length--;
111                     word= word.substring(0, length);
112                     wordWidth= fGC.textExtent(word).x;
113                     if (wordWidth + currWidth < fMaxWidth)
114                         return currOffset + length;
115                 }
116                 return nextOffset;
117             }
118             currWidth= nextWidth;
119             currOffset= nextOffset;
120             nextOffset= fLineBreakIterator.next();
121         }
122         return nextOffset;
123     }
124
125     private int findWordBegin(int idx) {
126         while (idx < fLine.length() && Character.isWhitespace(fLine.charAt(idx))) {
127             idx++;
128         }
129         return idx;
130     }
131 }
132
Popular Tags