KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > InputOutputProviderImpl


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.apisupport.project;
21
22 import java.io.IOException JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.io.Reader JavaDoc;
25 import java.io.StringReader JavaDoc;
26 import java.io.StringWriter JavaDoc;
27 import junit.framework.Assert;
28 import org.netbeans.junit.NbTestCase;
29 import org.openide.ErrorManager;
30 import org.openide.windows.IOProvider;
31 import org.openide.windows.InputOutput;
32 import org.openide.windows.OutputListener;
33 import org.openide.windows.OutputWriter;
34
35 /**
36  * @author Jaroslav Tulach
37  */

38 public class InputOutputProviderImpl extends IOProvider {
39
40     static NbTestCase running;
41     
42     /** Creates a new instance of InputOutputProviderImpl */
43     public InputOutputProviderImpl() {}
44     
45     public static void registerCase(NbTestCase r) {
46         running = r;
47     }
48     
49     public InputOutput getIO(String JavaDoc name, boolean newIO) {
50         return new IO(name);
51     }
52     
53     public OutputWriter getStdOut() {
54         Assert.assertNotNull("A test case must be registered", running);
55         return new OW("stdout");
56     }
57     
58     private static class OW extends OutputWriter {
59         
60         private ErrorManager err;
61         
62         public OW(String JavaDoc prefix) {
63             super(new StringWriter JavaDoc());
64             err = ErrorManager.getDefault().getInstance("output[" + prefix + "]");
65         }
66         
67         public void println(String JavaDoc s, OutputListener l) throws IOException JavaDoc {
68             write("println: " + s + " listener: " + l);
69             flush();
70         }
71         
72         public void reset() throws IOException JavaDoc {
73             write("Internal reset");
74             flush();
75         }
76         
77         public void write(char[] buf, int off, int len) {
78             write(new String JavaDoc(buf, off, len));
79         }
80         
81         public void write(int c) {
82             write(String.valueOf((char)c));
83         }
84         
85         public void write(char[] buf) {
86             write(buf, 0, buf.length);
87         }
88         
89         public void write(String JavaDoc s, int off, int len) {
90             write(s.substring(off, off + len));
91         }
92         public void write(String JavaDoc s) {
93             err.log(s);
94         }
95     }
96     
97     private static class IO implements InputOutput {
98         
99         private OW w;
100         private boolean closed;
101         
102         public IO(String JavaDoc n) {
103             w = new OW(n);
104             w.write("Created IO named '" + n + "'");
105             w.flush();
106         }
107         
108         public OutputWriter getOut() {
109             return w;
110         }
111         
112         public Reader JavaDoc getIn() {
113             w.write("Creating reader");
114             return new StringReader JavaDoc("");
115         }
116         
117         public OutputWriter getErr() {
118             return w;
119         }
120         
121         public void closeInputOutput() {
122             w.write("closeInputOutput");
123             closed = true;
124         }
125         
126         public boolean isClosed() {
127             w.write("isClosed");
128             return closed;
129         }
130         
131         public void setOutputVisible(boolean value) {
132             w.write("setOutputVisible: " + value);
133         }
134         
135         public void setErrVisible(boolean value) {
136             w.write("setErrVisible: " + value);
137         }
138         
139         public void setInputVisible(boolean value) {
140             w.write("setInputVisible: " + value);
141         }
142         
143         public void select() {
144             w.write("select");
145         }
146         
147         public boolean isErrSeparated() {
148             return false;
149         }
150         
151         public void setErrSeparated(boolean value) {}
152         
153         public boolean isFocusTaken() {
154             return false;
155         }
156         
157         public void setFocusTaken(boolean value) {}
158         
159         public Reader JavaDoc flushReader() {
160             return getIn();
161         }
162         
163     }
164 }
165
Popular Tags