KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > core > util > IndentedWriter


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.core.util;
21
22 import java.io.*;
23
24 /**
25  * Indented writer. Provides methods to increase and decrease indentation
26  * of each line.
27  *
28  * @author tl
29  */

30 public final class IndentedWriter extends PrintWriter {
31     private boolean needIndent = true;
32     private String JavaDoc indent = "";
33
34     /**
35      * Create a new PrintWriter.
36      *
37      * @param out a character-output stream
38      * @param autoFlush a boolean; if true, the println() methods will flush
39      * the output buffer
40      */

41     public IndentedWriter(Writer out, boolean autoFlush) {
42         super(out, autoFlush);
43     }
44
45     /**
46      * Constructor
47      *
48      * @param out output writer
49      */

50     public IndentedWriter(Writer out) {
51         super(out);
52     }
53
54     /**
55      * Create a new PrintWriter from an existing OutputStream. This
56      * convenience constructor creates the necessary intermediate
57      * OutputStreamWriter, which will convert characters into bytes using the
58      * default character encoding.
59      *
60      * @param out An output stream
61      * @param autoFlush A boolean; if true, the println() methods will flush
62      * the output buffer
63      *
64      * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
65      */

66     public IndentedWriter(OutputStream out, boolean autoFlush) {
67         super(out, autoFlush);
68     }
69
70     /**
71      * Create a new PrintWriter, without automatic line flushing, from an
72      * existing OutputStream. This convenience constructor creates the
73      * necessary intermediate OutputStreamWriter, which will convert characters
74      * into bytes using the default character encoding.
75      *
76      * @param out an output stream
77      *
78      * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
79      */

80     public IndentedWriter(OutputStream out) {
81         super(out);
82     }
83
84     public void println() {
85         super.println();
86         needIndent = true;
87     }
88
89     public void write(String JavaDoc s) {
90         if(needIndent) {
91             super.write(indent);
92             needIndent = false;
93         }
94         super.write(s);
95     }
96
97     /**
98      * Increases the indentation
99      */

100     public void indent() {
101         indent += " ";
102     }
103
104     /**
105      * Decrease the indentation
106      */

107     public void unindent() {
108         indent = indent.substring(4);
109     }
110 }
111
Popular Tags