blob: 1140fab5e1cd9a697d78458ffcfe1d7022421a1e [file] [log] [blame]
david_williamscfdb2cd2004-11-11 08:37:49 +00001/*******************************************************************************
2 * Copyright (c) 2001, 2004 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 * Jens Lukowski/Innoopract - initial renaming/restructuring
11 *
12 *******************************************************************************/
nitind13950662005-02-13 20:17:33 +000013package org.eclipse.wst.sse.ui.internal;
david_williamscfdb2cd2004-11-11 08:37:49 +000014
15
16
17import java.io.BufferedReader;
18import java.io.IOException;
19import java.io.Reader;
20
21import org.eclipse.swt.graphics.GC;
22
23/*
24 * Not a real reader. Could change if requested
25 */
26public class StructuredTextLineBreakingReader {
27 private GC fGC;
28 private int fIndex;
29 private String fLine;
30 private int fMaxWidth;
31
32 private BufferedReader fReader;
33
34 /**
35 * Creates a reader that breaks an input text to fit in a given width.
36 *
37 * @param reader
38 * Reader of the input text
39 * @param gc
40 * The graphic context that defines the currently used font
41 * sizes
42 * @param maxLineWidth
43 * The max width (pixes) where the text has to fit in
44 */
45 public StructuredTextLineBreakingReader(Reader reader, GC gc, int maxLineWidth) {
46 fReader = new BufferedReader(reader);
47 fGC = gc;
48 fMaxWidth = maxLineWidth;
49 fLine = null;
50 fIndex = 0;
51 }
52
53 private int findNextBreakIndex(int currIndex) {
54 int currWidth = 0;
55 int lineLength = fLine.length();
56
57 while (currIndex < lineLength) {
58 char ch = fLine.charAt(currIndex);
59 int nextIndex = currIndex + 1;
60 // leading whitespaces are counted to the following word
61 if (Character.isWhitespace(ch)) {
62 while (nextIndex < lineLength && Character.isWhitespace(fLine.charAt(nextIndex))) {
63 nextIndex++;
64 }
65 }
66 while (nextIndex < lineLength && !Character.isWhitespace(fLine.charAt(nextIndex))) {
67 nextIndex++;
68 }
69 String word = fLine.substring(currIndex, nextIndex);
70 int wordWidth = fGC.textExtent(word).x;
71 int nextWidth = wordWidth + currWidth;
72 if (nextWidth > fMaxWidth && wordWidth < fMaxWidth) {
73 return currIndex;
74 }
75 currWidth = nextWidth;
76 currIndex = nextIndex;
77 }
78 return currIndex;
79 }
80
81 private int findWordBegin(int idx) {
82 while (idx < fLine.length() && Character.isWhitespace(fLine.charAt(idx))) {
83 idx++;
84 }
85 return idx;
86 }
87
88 /**
89 * Reads the next line. The lengths of the line will not exceed the gived
90 * maximum width.
91 */
92 public String readLine() throws IOException {
93 if (fLine == null) {
94 String line = fReader.readLine();
95 if (line == null) {
96 return null;
97 }
98 int lineLen = fGC.textExtent(line).x;
99 if (lineLen < fMaxWidth) {
100 return line;
101 }
102 fLine = line;
103 fIndex = 0;
104 }
105 int breakIdx = findNextBreakIndex(fIndex);
106 String res = fLine.substring(fIndex, breakIdx);
107 if (breakIdx < fLine.length()) {
108 fIndex = findWordBegin(breakIdx);
109 } else {
110 fLine = null;
111 }
112 return res;
113 }
114}