KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > windows > IOProvider


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.openide.windows;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.io.PrintStream JavaDoc;
26 import java.io.Reader JavaDoc;
27 import java.io.StringWriter JavaDoc;
28 import javax.swing.Action JavaDoc;
29 import org.openide.util.Lookup;
30
31 /** A factory for IO tabs shown in the output window. To create a new tab to
32  * write to, call e.g. <code>IOProvider.getDefault().getIO("MyTab", false)</code>
33  * (pass true if there may be an existing tab with the same name and you want
34  * to write to a new tab).
35  *
36  * @author Jesse Glick
37  * @since 3.14
38  */

39 public abstract class IOProvider {
40
41     /**
42      * Get the default I/O provider.
43      * <p>
44      * Normally this is taken from {@link Lookup#getDefault} but if there is no
45      * instance in lookup, a fallback instance is created which just uses the
46      * standard system I/O streams. This is useful for unit tests and perhaps
47      * for standalone usage of various libraries.
48      * @return the default instance (never null)
49      */

50     public static IOProvider getDefault() {
51         IOProvider iop = (IOProvider) Lookup.getDefault().lookup(IOProvider.class);
52         if (iop == null) {
53             iop = new Trivial();
54         }
55         return iop;
56     }
57
58     /** Subclass constructor. */
59     protected IOProvider() {}
60
61     /**
62      * Get a named instance of InputOutput, which represents an output tab in
63      * the output window. Streams for reading/writing can be accessed via
64      * getters on the returned instance.
65      *
66      * @param name A localised display name for the tab
67      * @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
68      * @return an <code>InputOutput</code> instance for accessing the new tab
69      * @see InputOutput
70      */

71     public abstract InputOutput getIO(String JavaDoc name, boolean newIO);
72
73     
74     /**
75      *Gets a named instance of InputOutput with additional actions displayed in the
76      * toolbar.
77      * Streams for reading/writing can be accessed via
78      * getters on the returned instance.
79      * Additional actions are displayed on the output's toolbar.
80      *
81      * @param name A localized display name for the tab
82      * @param additionalActions array of actions that are added to the toolbar, Can be empty array, but not null.
83      * The number of actions should not exceed 5 and each should have the <code>Action.SMALL_ICON</code> property defined.
84      * @return an <code>InputOutput</code> instance for accessing the new tab
85      * @see InputOutput
86      * @since 1.6 <br>
87      * Note: The method is non-abstract for backward compatibility reasons only. If you are
88      * extending <code>IOProvider</code> and implementing its abstract classes, you are encouraged to override
89      * this method as well. The default implementation falls back to the <code>getIO(name, newIO)</code> method, ignoring the actions passed.
90      */

91     public InputOutput getIO(String JavaDoc name, Action JavaDoc[] additionalActions) {
92         return getIO(name, true);
93     }
94     
95     /** Support writing to the Output Window on the main tab or a similar output device.
96      * @return a writer for the standard NetBeans output area
97      */

98     public abstract OutputWriter getStdOut();
99     
100     /** Fallback implementation. */
101     private static final class Trivial extends IOProvider {
102         
103         public Trivial() {}
104
105         public InputOutput getIO(String JavaDoc name, boolean newIO) {
106             return new TrivialIO(name);
107         }
108
109         public OutputWriter getStdOut() {
110             return new TrivialOW(System.out, "stdout"); // NOI18N
111
}
112         
113         private final class TrivialIO implements InputOutput {
114             
115             private final String JavaDoc name;
116             private Reader JavaDoc in;
117             
118             public TrivialIO(String JavaDoc name) {
119                 this.name = name;
120             }
121
122             public Reader JavaDoc getIn() {
123                 if (in == null) {
124                     in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in));
125                 }
126                 return in;
127             }
128
129             public OutputWriter getOut() {
130                 return new TrivialOW(System.out, name);
131             }
132
133             public OutputWriter getErr() {
134                 return new TrivialOW(System.err, name);
135             }
136
137             public Reader JavaDoc flushReader() {
138                 return getIn();
139             }
140
141             public boolean isClosed() {
142                 return false;
143             }
144
145             public boolean isErrSeparated() {
146                 return false;
147             }
148
149             public boolean isFocusTaken() {
150                 return false;
151             }
152
153             public void closeInputOutput() {}
154
155             public void select() {}
156
157             public void setErrSeparated(boolean value) {}
158
159             public void setErrVisible(boolean value) {}
160
161             public void setFocusTaken(boolean value) {}
162
163             public void setInputVisible(boolean value) {}
164
165             public void setOutputVisible(boolean value) {}
166             
167         }
168         
169         private static final class TrivialOW extends OutputWriter {
170             
171             private static int count = 0;
172             private final String JavaDoc name;
173             private final PrintStream JavaDoc stream;
174             
175             public TrivialOW(PrintStream JavaDoc stream, String JavaDoc name) {
176                 // XXX using super(new PrintWriter(stream)) does not seem to work for some reason!
177
super(new StringWriter JavaDoc());
178                 this.stream = stream;
179                 if (name != null) {
180                     this.name = name;
181                 } else {
182                     this.name = "anon-" + ++count; // NOI18N
183
}
184             }
185             
186             private void prefix(boolean hyperlink) {
187                 if (hyperlink) {
188                     stream.print("[" + name + "]* "); // NOI18N
189
} else {
190                     stream.print("[" + name + "] "); // NOI18N
191
}
192             }
193
194             public void println(String JavaDoc s, OutputListener l) throws IOException JavaDoc {
195                 prefix(l != null);
196                 stream.println(s);
197             }
198
199             public void reset() throws IOException JavaDoc {}
200
201             public void println(float x) {
202                 prefix(false);
203                 stream.println(x);
204             }
205
206             public void println(double x) {
207                 prefix(false);
208                 stream.println(x);
209             }
210
211             public void println() {
212                 prefix(false);
213                 stream.println();
214             }
215
216             public void println(Object JavaDoc x) {
217                 prefix(false);
218                 stream.println(x);
219             }
220
221             public void println(int x) {
222                 prefix(false);
223                 stream.println(x);
224             }
225
226             public void println(char x) {
227                 prefix(false);
228                 stream.println(x);
229             }
230
231             public void println(long x) {
232                 prefix(false);
233                 stream.println(x);
234             }
235
236             public void println(char[] x) {
237                 prefix(false);
238                 stream.println(x);
239             }
240
241             public void println(boolean x) {
242                 prefix(false);
243                 stream.println(x);
244             }
245
246             public void println(String JavaDoc x) {
247                 prefix(false);
248                 stream.println(x);
249             }
250             
251         }
252         
253     }
254
255 }
256
Popular Tags