KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > ui > IOManager


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.debugger.jpda.ui;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import org.netbeans.api.debugger.jpda.JPDADebugger;
26 import org.openide.awt.StatusDisplayer;
27 import org.openide.util.NbBundle;
28 import org.openide.util.RequestProcessor;
29 import org.openide.windows.IOProvider;
30 import org.openide.windows.InputOutput;
31 import org.openide.windows.OutputEvent;
32 import org.openide.windows.OutputListener;
33 import org.openide.windows.OutputWriter;
34
35 public class IOManager {
36
37 // /** DebuggerManager output constant. */
38
// public static final int DEBUGGER_OUT = 1;
39
// /** Process output constant. */
40
// public static final int PROCESS_OUT = 2;
41
// /** Status line output constant. */
42
// public static final int STATUS_OUT = 4;
43
// /** All outputs constant. */
44
// public static final int ALL_OUT = DEBUGGER_OUT +
45
// PROCESS_OUT + STATUS_OUT;
46
// /** Standart process output constant. */
47
// public static final int STD_OUT = 1;
48
// /** Error process output constant. */
49
// public static final int ERR_OUT = 2;
50

51     
52     // variables ...............................................................
53

54     protected InputOutput debuggerIO = null;
55     private OutputWriter debuggerOut;
56     private OutputWriter debuggerErr;
57     private String JavaDoc name;
58     private boolean closed = false;
59     
60     /** output writer Thread */
61     private Hashtable JavaDoc lines = new Hashtable JavaDoc ();
62     private Listener listener = new Listener ();
63
64     
65     // init ....................................................................
66

67     public IOManager(String JavaDoc title) {
68         debuggerIO = IOProvider.getDefault ().getIO (title, true);
69         debuggerIO.setFocusTaken (false);
70         debuggerIO.setErrSeparated(false);
71         debuggerOut = debuggerIO.getOut ();
72         debuggerErr = debuggerIO.getErr();
73         debuggerIO.select();
74     }
75     
76     
77     // public interface ........................................................
78

79     private LinkedList JavaDoc buffer = new LinkedList JavaDoc ();
80     private RequestProcessor.Task task;
81     
82     /**
83      * Prints given text to the output.
84      */

85     public void println (
86         String JavaDoc text,
87         Line line
88     ) {
89         println(text, line, false);
90     }
91     
92     /**
93      * Prints given text to the output.
94      */

95     public void println (
96         String JavaDoc text,
97         Line line,
98         boolean important
99     ) {
100         if (text == null)
101             throw new NullPointerException JavaDoc ();
102         synchronized (buffer) {
103             buffer.addLast (new Text (text, line, important));
104         }
105         if (task == null)
106             task = RequestProcessor.getDefault ().post (new Runnable JavaDoc () {
107                 public void run () {
108                     synchronized (buffer) {
109                         int i, k = buffer.size ();
110                         for (i = 0; i < k; i++) {
111                             Text t = (Text) buffer.removeFirst ();
112                             try {
113                                 //if ((t.where & DEBUGGER_OUT) != 0) {
114
Listener listener;
115                                     if (t.line != null) {
116                                         listener = IOManager.this.listener;
117                                         lines.put (t.text, t.line);
118                                     } else {
119                                         listener = null;
120                                     }
121                                     if (t.important) {
122                                         debuggerErr.println (t.text, listener, t.important);
123                                         debuggerIO.select();
124                                         debuggerErr.flush();
125                                     } else {
126                                         debuggerOut.println (t.text, listener, t.important);
127                                         debuggerOut.flush();
128                                     }
129                                     if (closed) {
130                                         debuggerOut.close ();
131                                         debuggerErr.close();
132                                     }
133                                 //}
134
// if ((t.where & STATUS_OUT) != 0)
135
StatusDisplayer.getDefault ().setStatusText (t.text);
136                             } catch (IOException JavaDoc ex) {
137                                 ex.printStackTrace ();
138                             }
139                         }
140                     }
141                 }
142             }, 500, Thread.MIN_PRIORITY);
143         else
144             task.schedule (500);
145     }
146
147     void closeStream () {
148         debuggerOut.close ();
149         closed = true;
150     }
151
152     void close () {
153         debuggerIO.closeInputOutput ();
154     }
155     
156     
157     // innerclasses ............................................................
158

159     private class Listener implements OutputListener {
160         public void outputLineSelected (OutputEvent ev) {
161         }
162         public void outputLineAction (OutputEvent ev) {
163             String JavaDoc t = ev.getLine ();
164             Line l = (Line) lines.get (t);
165             if (l == null) return;
166             l.show ();
167         }
168         public void outputLineCleared (OutputEvent ev) {
169             lines = new Hashtable JavaDoc ();
170         }
171     }
172     
173     private static class Text {
174         private String JavaDoc text;
175         private Line line;
176         private boolean important;
177         
178         private Text (String JavaDoc text, Line line, boolean important) {
179             this.text = text;
180             this.line = line;
181             this.important = important;
182         }
183     }
184     
185     static class Line {
186         private String JavaDoc url;
187         private int lineNumber;
188         private JPDADebugger debugger;
189         
190         Line (String JavaDoc url, int lineNumber, JPDADebugger debugger) {
191             this.url = url;
192             this.lineNumber = lineNumber;
193         }
194         
195         void show () {
196             EditorContextBridge.showSource (url, lineNumber, debugger);
197         }
198     }
199 }
200
Popular Tags