KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > debugger > 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.ant.debugger;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import javax.swing.SwingUtilities JavaDoc;
26 import org.openide.awt.StatusDisplayer;
27 import org.openide.text.Annotatable;
28 import org.openide.text.Line;
29 import org.openide.util.NbBundle;
30 import org.openide.util.RequestProcessor;
31 import org.openide.windows.IOProvider;
32 import org.openide.windows.InputOutput;
33 import org.openide.windows.OutputEvent;
34 import org.openide.windows.OutputListener;
35 import org.openide.windows.OutputWriter;
36
37
38 public class IOManager {
39
40     // variables ...............................................................
41

42     protected InputOutput debuggerIO = null;
43     private OutputWriter debuggerOut;
44     private String JavaDoc name;
45     private boolean closed = false;
46
47     
48     /** output writer Thread */
49     private Hashtable JavaDoc lines = new Hashtable JavaDoc ();
50     private Listener listener = new Listener ();
51
52     
53     // init ....................................................................
54

55     public IOManager (
56         String JavaDoc title
57     ) {
58         debuggerIO = IOProvider.getDefault ().getIO (title, true);
59         debuggerIO.setFocusTaken (false);
60         debuggerOut = debuggerIO.getOut ();
61     }
62     
63     
64     // public interface ........................................................
65

66     private LinkedList JavaDoc buffer = new LinkedList JavaDoc ();
67     private RequestProcessor.Task task;
68     
69     /**
70     * Prints given text to the output.
71     */

72     public void println (
73         final String JavaDoc text,
74         final Object JavaDoc line
75     ) {
76         if (text == null)
77             throw new NullPointerException JavaDoc ();
78         synchronized (buffer) {
79             buffer.addLast (new Text (text, line));
80         }
81         if (task == null)
82             task = RequestProcessor.getDefault ().post (new Runnable JavaDoc () {
83                 public void run () {
84                     synchronized (buffer) {
85                         int i, k = buffer.size ();
86                         for (i = 0; i < k; i++) {
87                             Text t = (Text) buffer.removeFirst ();
88                             try {
89                                 if (t.line != null) {
90                                     debuggerOut.println (t.text, listener);
91                                     lines.put (t.text, t.line);
92                                 } else
93                                     debuggerOut.println (t.text);
94                                 debuggerOut.flush ();
95                                 if (closed)
96                                     debuggerOut.close ();
97                                 StatusDisplayer.getDefault ().setStatusText (t.text);
98                             } catch (IOException JavaDoc ex) {
99                                 ex.printStackTrace ();
100                             }
101                         }
102                     }
103                 }
104             }, 100, Thread.MIN_PRIORITY);
105         else
106             task.schedule (100);
107     }
108
109     void closeStream () {
110         debuggerOut.close ();
111         closed = true;
112         close();
113     }
114
115     void close () {
116         debuggerIO.closeInputOutput ();
117     }
118     
119     
120     // innerclasses ............................................................
121

122     private class Listener implements OutputListener {
123         public void outputLineSelected (OutputEvent ev) {
124         }
125         public void outputLineAction (final OutputEvent ev) {
126             SwingUtilities.invokeLater (new Runnable JavaDoc () {
127                 public void run () {
128                     String JavaDoc t = ev.getLine ();
129                     Object JavaDoc a = lines.get (t);
130                     if (a == null) return;
131                     Utils.showLine (a);
132                 }
133             });
134         }
135         public void outputLineCleared (OutputEvent ev) {
136             lines = new Hashtable JavaDoc ();
137         }
138     }
139     
140     private static class Text {
141         private String JavaDoc text;
142         private Object JavaDoc line;
143         
144         private Text (String JavaDoc text, Object JavaDoc line) {
145             this.text = text;
146             this.line = line;
147         }
148     }
149 }
150
Popular Tags