david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 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 | *******************************************************************************/ |
nitind | 1395066 | 2005-02-13 20:17:33 +0000 | [diff] [blame] | 13 | package org.eclipse.wst.sse.ui.internal; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 14 | |
| 15 | |
| 16 | |
| 17 | import java.io.BufferedReader; |
| 18 | import java.io.IOException; |
| 19 | import java.io.Reader; |
| 20 | |
| 21 | import org.eclipse.swt.graphics.GC; |
| 22 | |
| 23 | /* |
| 24 | * Not a real reader. Could change if requested |
| 25 | */ |
| 26 | public 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 | } |