1 /* 
2  * Copyright 2005 Paul Hinds
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 */
16package org.tp23.antinstaller.renderer.text;
17
18import java.io.PrintStream;
19
20
21/**
22 *
23 * <p>Used for the Text/Console input to show pages of text as opposed to displaying
24* a long list of text that scrolls off the top of the page. </p>
25 * <p>Copyright: Copyright (c) 2004</p>
26 * <p>Company: tp23</p>
27 * @author Paul Hinds
28 * @version $Id: Pager.java,v 1.2 2006/12/21 00:09:19 teknopaul Exp $
29 */
30public class Pager {
31
32    private char[] text;
33    private int linesPerPage = 20;
34    private int charsPerLine = 80;
35    private int stringIndex = 0;
36
37    public Pager(String text) {
38        this.text = text.toCharArray();
39    }
40    public Pager(){
41    }
42
43    public String getText() {
44        return new String(text);
45    }
46
47    public void setText(String text) {
48        this.text = text.toCharArray();
49    }
50    /**
51     * Print the rest of the text
52     */
53    public void rest(PrintStream out){
54        while(next(out));
55}
56    /**
57     * Print the next page
58     * @param out PrintStream
59     * @return boolean true if there is more text
60     */
61    public boolean next(PrintStream out){
62        int lineChars = 0;
63        int lastSpace = -1;
64        // loop past charaters, increment with lines
65        char[] lineBuffer = new char[charsPerLine+1];
66        for (int lines = 0; lines < linesPerPage;) {
67            if(stringIndex >= text.length){
68                return false;
69            }
70            lineBuffer[lineChars] = text[stringIndex];
71            if(text[stringIndex] == ' '){
72                lastSpace = lineChars;
73            }
74            if(text[stringIndex] == '\n'){
75                String tmp = new String(lineBuffer, 0, lineChars + 1);
76                out.print(tmp);
77                lines++;
78                lineChars = 0;
79                lastSpace = -1;
80            }
81            else if(lineChars == charsPerLine){
82                // handle lines ending with the last full word
83                if(lastSpace != -1){
84                    out.println(new String(lineBuffer, 0, lastSpace));
85                    stringIndex = stringIndex - (charsPerLine - lastSpace);
86                }
87                else {
88                    out.println(new String(lineBuffer, 0, lineChars));
89                }
90                lines++;
91                lineChars = 0;
92                lastSpace = -1;
93            }
94            else{
95                lineChars++;
96            }
97            stringIndex++;
98        }
99        return true;
00    }
01}
02