Skip to main content
summaryrefslogtreecommitdiffstats
blob: f2afb1d4e66375bd49e82c835c394ee030ee3e51 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*******************************************************************************
 * Copyright (c) 2005, 2008 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.utility.internal;

import java.io.PrintWriter;
import java.io.Writer;

/**
 * Extend PrintWriter to automatically indent new lines.
 */
public class IndentingPrintWriter extends PrintWriter {

	private final String indent;
	private int indentLevel;
	private boolean needsIndent;

	public static String DEFAULT_INDENT = "\t";


	/**
	 * Construct a writer that indents with tabs.
	 */
	public IndentingPrintWriter(Writer out) {
		this(out, DEFAULT_INDENT);
	}
	
	/**
	 * Construct a writer that indents with the specified string.
	 */
	public IndentingPrintWriter(Writer out, String indent) {
		super(out);
		this.indent = indent;
		this.indentLevel = 0;
		this.needsIndent = true;
	}
	
	/**
	 * Set flag so following line is indented.
	 */
	@Override
	public void println() {
		synchronized (this.lock) {
			super.println();
			this.needsIndent = true;
		}
	}
	
	/**
	 * Print the appropriate indent.
	 */
	private void printIndent() {
		if (this.needsIndent) {
			this.needsIndent = false;
			for (int i = this.indentLevel; i-- > 0; ) {
				this.print(this.indent);
			}
		}
	}
	
	/**
	 * Write a portion of an array of characters.
	 */
	@Override
	public void write(char buf[], int off, int len) {
		synchronized (this.lock) {
			this.printIndent();
			super.write(buf, off, len);
		}
	}
	
	/**
	 * Write a single character.
	 */
	@Override
	public void write(int c) {
		synchronized (this.lock) {
			this.printIndent();
			super.write(c);
		}
	}
	
	/**
	 * Write a portion of a string.
	 */
	@Override
	public void write(String s, int off, int len) {
		synchronized (this.lock) {
			this.printIndent();
			super.write(s, off, len);
		}
	}
	
	/**
	 * Bump the indent level.
	 */
	public void indent() {
		this.incrementIndentLevel();
	}
	
	/**
	 * Decrement the indent level.
	 */
	public void undent() {
		this.decrementIndentLevel();
	}
	
	/**
	 * Bump the indent level.
	 */
	public void incrementIndentLevel() {
		synchronized (this.lock) {
			this.indentLevel++;
		}
	}
	
	/**
	 * Decrement the indent level.
	 */
	public void decrementIndentLevel() {
		synchronized (this.lock) {
			this.indentLevel--;
		}
	}
	
	/**
	 * Return the current indent level.
	 */
	public int indentLevel() {
		return this.indentLevel;
	}
	
	/**
	 * Allow the indent level to be set directly.
	 */
	public void setIndentLevel(int indentLevel) {
		synchronized (this.lock) {
			this.indentLevel = indentLevel;
		}
	}

}

Back to the top