001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.openide.windows;
043:
044: import java.io.BufferedReader;
045: import java.io.IOException;
046: import java.io.InputStreamReader;
047: import java.io.PrintStream;
048: import java.io.Reader;
049: import java.io.StringWriter;
050: import javax.swing.Action;
051: import org.openide.util.Lookup;
052:
053: /** A factory for IO tabs shown in the output window. To create a new tab to
054: * write to, call e.g. <code>IOProvider.getDefault().getIO("MyTab", false)</code>
055: * (pass true if there may be an existing tab with the same name and you want
056: * to write to a new tab).
057: *
058: * @author Jesse Glick
059: * @since 3.14
060: */
061: public abstract class IOProvider {
062:
063: /**
064: * Get the default I/O provider.
065: * <p>
066: * Normally this is taken from {@link Lookup#getDefault} but if there is no
067: * instance in lookup, a fallback instance is created which just uses the
068: * standard system I/O streams. This is useful for unit tests and perhaps
069: * for standalone usage of various libraries.
070: * @return the default instance (never null)
071: */
072: public static IOProvider getDefault() {
073: IOProvider iop = (IOProvider) Lookup.getDefault().lookup(
074: IOProvider.class);
075: if (iop == null) {
076: iop = new Trivial();
077: }
078: return iop;
079: }
080:
081: /** Subclass constructor. */
082: protected IOProvider() {
083: }
084:
085: /**
086: * Get a named instance of InputOutput, which represents an output tab in
087: * the output window. Streams for reading/writing can be accessed via
088: * getters on the returned instance.
089: *
090: * @param name A localised display name for the tab
091: * @param newIO if <tt>true</tt>, a new <code>InputOutput</code> is returned, else an existing <code>InputOutput</code> of the same name may be returned
092: * @return an <code>InputOutput</code> instance for accessing the new tab
093: * @see InputOutput
094: */
095: public abstract InputOutput getIO(String name, boolean newIO);
096:
097: /**
098: *Gets a named instance of InputOutput with additional actions displayed in the
099: * toolbar.
100: * Streams for reading/writing can be accessed via
101: * getters on the returned instance.
102: * Additional actions are displayed on the output's toolbar.
103: *
104: * @param name A localized display name for the tab
105: * @param additionalActions array of actions that are added to the toolbar, Can be empty array, but not null.
106: * The number of actions should not exceed 5 and each should have the <code>Action.SMALL_ICON</code> property defined.
107: * @return an <code>InputOutput</code> instance for accessing the new tab
108: * @see InputOutput
109: * @since 1.6 <br>
110: * Note: The method is non-abstract for backward compatibility reasons only. If you are
111: * extending <code>IOProvider</code> and implementing its abstract classes, you are encouraged to override
112: * this method as well. The default implementation falls back to the <code>getIO(name, newIO)</code> method, ignoring the actions passed.
113: */
114: public InputOutput getIO(String name, Action[] additionalActions) {
115: return getIO(name, true);
116: }
117:
118: /** Support writing to the Output Window on the main tab or a similar output device.
119: * @return a writer for the standard NetBeans output area
120: */
121: public abstract OutputWriter getStdOut();
122:
123: /** Fallback implementation. */
124: private static final class Trivial extends IOProvider {
125:
126: public Trivial() {
127: }
128:
129: public InputOutput getIO(String name, boolean newIO) {
130: return new TrivialIO(name);
131: }
132:
133: public OutputWriter getStdOut() {
134: return new TrivialOW(System.out, "stdout"); // NOI18N
135: }
136:
137: private final class TrivialIO implements InputOutput {
138:
139: private final String name;
140: private Reader in;
141:
142: public TrivialIO(String name) {
143: this .name = name;
144: }
145:
146: public Reader getIn() {
147: if (in == null) {
148: in = new BufferedReader(new InputStreamReader(
149: System.in));
150: }
151: return in;
152: }
153:
154: public OutputWriter getOut() {
155: return new TrivialOW(System.out, name);
156: }
157:
158: public OutputWriter getErr() {
159: return new TrivialOW(System.err, name);
160: }
161:
162: public Reader flushReader() {
163: return getIn();
164: }
165:
166: public boolean isClosed() {
167: return false;
168: }
169:
170: public boolean isErrSeparated() {
171: return false;
172: }
173:
174: public boolean isFocusTaken() {
175: return false;
176: }
177:
178: public void closeInputOutput() {
179: }
180:
181: public void select() {
182: }
183:
184: public void setErrSeparated(boolean value) {
185: }
186:
187: public void setErrVisible(boolean value) {
188: }
189:
190: public void setFocusTaken(boolean value) {
191: }
192:
193: public void setInputVisible(boolean value) {
194: }
195:
196: public void setOutputVisible(boolean value) {
197: }
198:
199: }
200:
201: private static final class TrivialOW extends OutputWriter {
202:
203: private static int count = 0;
204: private final String name;
205: private final PrintStream stream;
206:
207: public TrivialOW(PrintStream stream, String name) {
208: // XXX using super(new PrintWriter(stream)) does not seem to work for some reason!
209: super (new StringWriter());
210: this .stream = stream;
211: if (name != null) {
212: this .name = name;
213: } else {
214: this .name = "anon-" + ++count; // NOI18N
215: }
216: }
217:
218: private void prefix(boolean hyperlink) {
219: if (hyperlink) {
220: stream.print("[" + name + "]* "); // NOI18N
221: } else {
222: stream.print("[" + name + "] "); // NOI18N
223: }
224: }
225:
226: public void println(String s, OutputListener l)
227: throws IOException {
228: prefix(l != null);
229: stream.println(s);
230: }
231:
232: public void reset() throws IOException {
233: }
234:
235: public void println(float x) {
236: prefix(false);
237: stream.println(x);
238: }
239:
240: public void println(double x) {
241: prefix(false);
242: stream.println(x);
243: }
244:
245: public void println() {
246: prefix(false);
247: stream.println();
248: }
249:
250: public void println(Object x) {
251: prefix(false);
252: stream.println(x);
253: }
254:
255: public void println(int x) {
256: prefix(false);
257: stream.println(x);
258: }
259:
260: public void println(char x) {
261: prefix(false);
262: stream.println(x);
263: }
264:
265: public void println(long x) {
266: prefix(false);
267: stream.println(x);
268: }
269:
270: public void println(char[] x) {
271: prefix(false);
272: stream.println(x);
273: }
274:
275: public void println(boolean x) {
276: prefix(false);
277: stream.println(x);
278: }
279:
280: public void println(String x) {
281: prefix(false);
282: stream.println(x);
283: }
284:
285: }
286:
287: }
288:
289: }
|