KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > output2 > NbWriter


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.core.output2;
21
22 import org.openide.windows.OutputWriter;
23 import org.openide.windows.OutputListener;
24 import java.io.IOException JavaDoc;
25 import org.openide.util.Exceptions;
26
27
28 /**
29  * Wrapper around a replacable instance of OutWriter. An OutWriter can be disposed on any thread, but it may
30  * still be visible in the GUI until the tab change gets handled in the EDT; also, a writer once obtained
31  * should be reusable, but OutWriter is useless once it has been disposed. So this class wraps an OutWriter,
32  * which it replaces when reset() is called; an OutputDocument is implemented directly over an
33  * OutWriter, so the immutable OutWriter lasts until the OutputDocument is destroyed.
34  */

35 class NbWriter extends OutputWriter {
36     private final NbIO owner;
37     /**
38      * Make an output writer.
39      */

40     public NbWriter(OutWriter real, NbIO owner) {
41         super(real);
42         this.owner = owner;
43     }
44
45     public void println(String JavaDoc s, OutputListener l) throws IOException JavaDoc {
46         ((OutWriter) out).println (s, l);
47     }
48
49     
50     public void println(String JavaDoc s, OutputListener l, boolean important) throws IOException JavaDoc {
51         ((OutWriter) out).println (s, l, important);
52     }
53
54     /**
55      * Replaces the wrapped OutWriter.
56      *
57      * @throws IOException
58      */

59     public void reset() throws IOException JavaDoc {
60         if (!((OutWriter) out).hasStorage() && !((OutWriter) out).isDisposed() || ((OutWriter) out).isEmpty()) {
61             //Someone calling reset multiple times or on initialization
62
if (!out().isDisposed()) {
63                 if (Controller.LOG) Controller.log ("Extra call to Reset on " + this + " for " + out);
64                 //#49173 - Clear action causes call to reset(); call to start writing
65
//more output is another call to reset(), so it is ignored - so
66
//the tab title is not updated when a new stream is updated.
67
owner.setStreamClosed(false);
68                 return;
69             }
70         }
71         synchronized (this) {
72             if (out != null) {
73                 if (Controller.LOG) Controller.log ("Disposing old OutWriter");
74                 out().dispose();
75             }
76             if (Controller.LOG) Controller.log ("NbWriter.reset() replacing old OutWriter");
77             out = new OutWriter(owner);
78             lock = out;
79             if (err != null) {
80                 err.setWrapped((OutWriter) out);
81             }
82             owner.reset();
83         }
84     }
85
86     OutWriter out() {
87         return (OutWriter) out;
88     }
89     
90     ErrWriter err() {
91         return err;
92     }
93
94     private ErrWriter err = null;
95     public synchronized ErrWriter getErr() {
96         if (err == null) {
97             err = new ErrWriter ((OutWriter) out, this);
98         }
99         return err;
100     }
101
102     public void close() {
103         boolean wasClosed = isClosed();
104         if (Controller.LOG) Controller.log ("NbWriter.close wasClosed=" + wasClosed + " out is " + out + " out is closed " + ((OutWriter) out).isClosed());
105         if (!wasClosed || !((OutWriter) out).isClosed()) {
106             synchronized (lock) {
107                 try {
108                     if (Controller.LOG) Controller.log ( "Now closing OutWriter");
109                     out.close();
110                 } catch (IOException JavaDoc ioe) {
111                     Exceptions.printStackTrace(ioe);
112                 }
113             }
114         }
115         boolean isClosed = isClosed();
116         if (wasClosed != isClosed) {
117             if (Controller.LOG) Controller.log ("Setting streamClosed on InputOutput to " + isClosed);
118             owner.setStreamClosed(isClosed);
119         }
120     }
121
122     public boolean isClosed() {
123         OutWriter ow = (OutWriter) out;
124         synchronized (ow) {
125             boolean result = ow.isClosed();
126             if (result && err != null && !(ow.checkError())) {
127                 result &= err.isClosed();
128             }
129             return result;
130         }
131     }
132
133     public void notifyErrClosed() {
134         if (isClosed()) {
135             if (Controller.LOG) Controller.log ("NbWriter.notifyErrClosed - error stream has been closed");
136             owner.setStreamClosed(isClosed());
137         }
138     }
139     
140     /**
141      * If not overridden, the super impl will append extra \n's
142      */

143     public void println (String JavaDoc s) {
144         synchronized (lock) {
145             ((OutWriter) out).println(s);
146         }
147     }
148 }
149
Popular Tags