001: /*
002: * Copyright 2005 Paul Hinds
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.tp23.antinstaller.renderer.text;
017:
018: import java.io.PrintStream;
019:
020: /**
021: *
022: * <p>Used for the Text/Console input to show pages of text as opposed to displaying
023: * a long list of text that scrolls off the top of the page. </p>
024: * <p>Copyright: Copyright (c) 2004</p>
025: * <p>Company: tp23</p>
026: * @author Paul Hinds
027: * @version $Id: Pager.java,v 1.2 2006/12/21 00:09:19 teknopaul Exp $
028: */
029: public class Pager {
030:
031: private char[] text;
032: private int linesPerPage = 20;
033: private int charsPerLine = 80;
034: private int stringIndex = 0;
035:
036: public Pager(String text) {
037: this .text = text.toCharArray();
038: }
039:
040: public Pager() {
041: }
042:
043: public String getText() {
044: return new String(text);
045: }
046:
047: public void setText(String text) {
048: this .text = text.toCharArray();
049: }
050:
051: /**
052: * Print the rest of the text
053: */
054: public void rest(PrintStream out) {
055: while (next(out))
056: ;
057: }
058:
059: /**
060: * Print the next page
061: * @param out PrintStream
062: * @return boolean true if there is more text
063: */
064: public boolean next(PrintStream out) {
065: int lineChars = 0;
066: int lastSpace = -1;
067: // loop past charaters, increment with lines
068: char[] lineBuffer = new char[charsPerLine + 1];
069: for (int lines = 0; lines < linesPerPage;) {
070: if (stringIndex >= text.length) {
071: return false;
072: }
073: lineBuffer[lineChars] = text[stringIndex];
074: if (text[stringIndex] == ' ') {
075: lastSpace = lineChars;
076: }
077: if (text[stringIndex] == '\n') {
078: String tmp = new String(lineBuffer, 0, lineChars + 1);
079: out.print(tmp);
080: lines++;
081: lineChars = 0;
082: lastSpace = -1;
083: } else if (lineChars == charsPerLine) {
084: // handle lines ending with the last full word
085: if (lastSpace != -1) {
086: out.println(new String(lineBuffer, 0, lastSpace));
087: stringIndex = stringIndex
088: - (charsPerLine - lastSpace);
089: } else {
090: out.println(new String(lineBuffer, 0, lineChars));
091: }
092: lines++;
093: lineChars = 0;
094: lastSpace = -1;
095: } else {
096: lineChars++;
097: }
098: stringIndex++;
099: }
100: return true;
101: }
102: }
|