1 19 24 25 package org.netbeans.core.output2; 26 27 import org.openide.windows.InputOutput; 28 import org.openide.windows.OutputWriter; 29 30 import javax.swing.*; 31 import java.awt.*; 32 import java.io.IOException ; 33 import java.io.Reader ; 34 import org.openide.util.Exceptions; 35 36 42 class NbIO implements InputOutput { 43 44 private Boolean focusTaken = null; 45 private Boolean closed = null; 46 private String name; 47 48 private Action[] actions; 49 50 private NbWriter out = null; 51 52 private Icon icon; 53 54 58 NbIO(String name, Action[] toolbarActions) { 59 this(name); 60 this.actions = toolbarActions; 61 } 62 63 64 NbIO (String name) { 65 this.name = name; 66 } 67 68 public void closeInputOutput() { 69 if (Controller.LOG) Controller.log("CLOSE INPUT OUTPUT CALLED FOR " + this); 70 if (out != null) { 71 if (Controller.LOG) Controller.log (" - Its output is non null, calling close() on " + out); 72 out.close(); 73 } 74 post (this, IOEvent.CMD_CLOSE, true); 75 } 76 77 String getName() { 78 return name; 79 } 80 81 public OutputWriter getErr() { 82 return ((NbWriter) getOut()).getErr(); 83 } 84 85 NbWriter writer() { 86 return out; 87 } 88 89 void dispose() { 90 if (Controller.LOG) Controller.log (this + ": IO " + getName() + " is being disposed"); 91 if (out != null) { 92 if (Controller.LOG) Controller.log (this + ": Still has an OutWriter. Disposing it"); 93 out().dispose(); 94 out = null; 95 if (in != null) { 96 in.eof(); 97 in = null; 98 } 99 focusTaken = null; 100 } 101 NbIOProvider.dispose(this); 102 } 103 104 public OutputWriter getOut() { 105 synchronized (this) { 106 if (out == null) { 107 OutWriter realout = new OutWriter(this); 108 out = new NbWriter(realout, this); 109 } 110 } 111 return out; 112 } 113 114 115 OutWriter out() { 116 return out == null ? null : out.out(); 117 } 118 119 void setClosed (boolean val) { 120 closed = val ? Boolean.TRUE : Boolean.FALSE; 121 } 122 123 public boolean isClosed() { 124 return Boolean.TRUE.equals(closed); 125 } 126 127 public boolean isErrSeparated() { 128 return false; 129 } 130 131 public boolean isFocusTaken() { 132 return Boolean.TRUE.equals(focusTaken); 133 } 134 135 boolean isStreamClosed() { 136 return out == null ? true : streamClosedSet ? streamClosed : false; 137 } 138 139 public void select() { 140 if (Controller.LOG) Controller.log (this + ": select"); 141 post (this, IOEvent.CMD_SELECT, true); 142 } 143 144 public void setErrSeparated(boolean value) { 145 } 147 148 public void setErrVisible(boolean value) { 149 } 151 152 public void setFocusTaken(boolean value) { 153 focusTaken = value ? Boolean.TRUE : Boolean.FALSE; 154 post (this, IOEvent.CMD_FOCUS_TAKEN, value); 155 } 156 157 public void setInputVisible(boolean value) { 158 if (Controller.LOG) Controller.log(NbIO.this + ": SetInputVisible"); 159 post (this, IOEvent.CMD_INPUT_VISIBLE, value); 160 } 161 162 public void setOutputVisible(boolean value) { 163 } 165 166 boolean hasStreamClosed() { 167 return streamClosedSet; 168 } 169 170 private boolean streamClosed = false; 171 private boolean streamClosedSet = false; 172 public void setStreamClosed(boolean value) { 173 if (Controller.LOG) Controller.log ("setStreamClosed on " + this + " to " + value); 174 if (streamClosed != value || !streamClosedSet) { 175 streamClosed = value; 176 streamClosedSet = true; 177 post (this, IOEvent.CMD_STREAM_CLOSED, value); 178 } 179 } 180 181 public void setToolbarActions(Action[] a) { 182 this.actions = a; 183 post (this, IOEvent.CMD_SET_TOOLBAR_ACTIONS, a); 184 } 185 186 Action[] getToolbarActions() { 187 return actions; 188 } 189 190 boolean checkReset() { 191 boolean result = wasReset; 192 wasReset = false; 193 return result; 194 } 195 196 private boolean wasReset = false; 197 public void reset() { 198 if (Controller.LOG) Controller.log (this + ": reset"); 199 closed = null; 200 boolean wasReset = true; 201 streamClosedSet = false; 202 streamClosed = false; 203 204 if (in != null) { 205 in.eof(); 206 in.reuse(); 207 } 208 post (this, IOEvent.CMD_RESET, true); 209 } 210 211 private static void post (NbIO io, int command, boolean val) { 212 IOEvent evt = new IOEvent (io, command, val); 213 post (evt); 214 } 215 216 private static void post (NbIO io, int command, Object data) { 217 IOEvent evt = new IOEvent (io, command, data); 218 post (evt); 219 } 220 221 static void post (IOEvent evt) { 222 if (SwingUtilities.isEventDispatchThread()) { 223 if (Controller.LOG) Controller.log ("Synchronously dispatching " + evt + " from call on EQ"); 224 evt.dispatch(); 225 } else { 226 if (Controller.LOG) Controller.log ("Asynchronously posting " + evt + " to EQ"); 227 EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue(); 228 eq.postEvent(evt); 229 } 230 } 231 232 public String toString() { 233 return "NbIO@" + System.identityHashCode(this) + " " + getName(); 234 } 235 236 IOReader in() { 237 return in; 238 } 239 240 private IOReader in = null; 241 public Reader getIn() { 242 if (in == null) { 243 in = new IOReader(); 244 } 245 return in; 246 } 247 248 public Reader flushReader() { 249 return getIn(); 250 } 251 252 public void setIcon(Icon icn) { 253 icon = icn; 254 post (this, IOEvent.CMD_ICON, icn); 255 256 } 257 258 public Icon getIcon() { 259 return icon; 260 } 261 262 class IOReader extends Reader { 263 private boolean pristine = true; 264 IOReader() { 265 super (new StringBuffer ()); 266 } 267 268 void reuse() { 269 pristine = true; 270 inputClosed = false; 271 } 272 273 private StringBuffer buffer() { 274 return (StringBuffer ) lock; 275 } 276 277 public void pushText (String txt) { 278 if (Controller.LOG) Controller.log (NbIO.this + ": Input text: " + txt); 279 synchronized (lock) { 280 buffer().append (txt); 281 lock.notifyAll(); 282 } 283 } 284 285 private boolean inputClosed = false; 286 public void eof() { 287 synchronized (lock) { 288 try { 289 close(); 290 } catch (IOException ioe) { 291 Exceptions.printStackTrace(ioe); 292 } 293 } 294 } 295 296 private void checkPristine() throws IOException { 297 if (SwingUtilities.isEventDispatchThread()) { 298 throw new IOException ("Cannot call read() from the event thread, it will deadlock"); 299 } 300 if (pristine) { 301 NbIO.this.setInputVisible(true); 302 pristine = false; 303 } 304 } 305 306 public int read(char cbuf[], int off, int len) throws IOException { 307 if (Controller.LOG) Controller.log (NbIO.this + ":Input read: " + off + " len " + len); 308 checkPristine(); 309 synchronized (lock) { 310 while (!inputClosed && buffer().length() == 0) { 311 try { 312 lock.wait(); 313 } catch (InterruptedException e) { 314 throw (IOException ) new IOException ("Interrupted: " + 315 e.getMessage()).initCause(e); 316 } 317 } 318 if (inputClosed) { 319 reuse(); 320 return -1; 321 } 322 int realLen = Math.min (buffer().length(), len); 323 buffer().getChars(0, realLen, cbuf, off); 324 buffer().delete(0, realLen); 325 return realLen; 326 } 327 } 328 329 public int read() throws IOException { 330 if (Controller.LOG) Controller.log (NbIO.this + ": Input read one char"); 331 checkPristine(); 332 synchronized (lock) { 333 while (!inputClosed && buffer().length() == 0) { 334 try { 335 lock.wait(); 336 } catch (InterruptedException e) { 337 throw (IOException ) new IOException ("Interrupted: " + 338 e.getMessage()).initCause(e); 339 } 340 } 341 if (inputClosed) { 342 reuse(); 343 return -1; 344 } 345 int i = (int) buffer().charAt(0); 346 buffer().deleteCharAt(0); 347 return i; 348 } 349 } 350 351 public boolean ready() throws IOException { 352 synchronized (lock) { 353 return !inputClosed && buffer().length() > 0; 354 } 355 } 356 357 public long skip(long n) throws IOException { 358 if (Controller.LOG) Controller.log (NbIO.this + ": Input skip " + n); 359 checkPristine(); 360 synchronized (lock) { 361 while (!inputClosed && buffer().length() == 0) { 362 try { 363 lock.wait(); 364 } catch (InterruptedException e) { 365 throw (IOException ) new IOException ("Interrupted: " + 366 e.getMessage()).initCause(e); 367 } 368 } 369 if (inputClosed) { 370 reuse(); 371 return 0; 372 } 373 int realLen = Math.min (buffer().length(), (int) n); 374 buffer().delete(0, realLen); 375 return realLen; 376 } 377 } 378 379 public void close() throws IOException { 380 if (Controller.LOG) Controller.log (NbIO.this + ": Input close"); 381 setInputVisible(false); 382 synchronized (lock) { 383 inputClosed = true; 384 buffer().setLength(0); 385 lock.notifyAll(); 386 } 387 } 388 389 public boolean isClosed() { 390 return inputClosed; 391 } 392 } 393 394 } 395 | Popular Tags |