1 19 20 package org.openide.windows; 21 22 import java.io.BufferedReader ; 23 import java.io.IOException ; 24 import java.io.InputStreamReader ; 25 import java.io.PrintStream ; 26 import java.io.Reader ; 27 import java.io.StringWriter ; 28 import javax.swing.Action ; 29 import org.openide.util.Lookup; 30 31 39 public abstract class IOProvider { 40 41 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 59 protected IOProvider() {} 60 61 71 public abstract InputOutput getIO(String name, boolean newIO); 72 73 74 91 public InputOutput getIO(String name, Action [] additionalActions) { 92 return getIO(name, true); 93 } 94 95 98 public abstract OutputWriter getStdOut(); 99 100 101 private static final class Trivial extends IOProvider { 102 103 public Trivial() {} 104 105 public InputOutput getIO(String name, boolean newIO) { 106 return new TrivialIO(name); 107 } 108 109 public OutputWriter getStdOut() { 110 return new TrivialOW(System.out, "stdout"); } 112 113 private final class TrivialIO implements InputOutput { 114 115 private final String name; 116 private Reader in; 117 118 public TrivialIO(String name) { 119 this.name = name; 120 } 121 122 public Reader getIn() { 123 if (in == null) { 124 in = new BufferedReader (new InputStreamReader (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 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 name; 173 private final PrintStream stream; 174 175 public TrivialOW(PrintStream stream, String name) { 176 super(new StringWriter ()); 178 this.stream = stream; 179 if (name != null) { 180 this.name = name; 181 } else { 182 this.name = "anon-" + ++count; } 184 } 185 186 private void prefix(boolean hyperlink) { 187 if (hyperlink) { 188 stream.print("[" + name + "]* "); } else { 190 stream.print("[" + name + "] "); } 192 } 193 194 public void println(String s, OutputListener l) throws IOException { 195 prefix(l != null); 196 stream.println(s); 197 } 198 199 public void reset() throws IOException {} 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 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 x) { 247 prefix(false); 248 stream.println(x); 249 } 250 251 } 252 253 } 254 255 } 256 | Popular Tags |