~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to debuggerjpda/ui/src/org/netbeans/modules/debugger/jpda/ui/IOManager.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.debugger.jpda.ui;
 
43
 
 
44
import java.io.IOException;
 
45
import java.util.Hashtable;
 
46
import java.util.LinkedList;
 
47
import org.netbeans.api.debugger.jpda.JPDADebugger;
 
48
import org.openide.awt.StatusDisplayer;
 
49
import org.openide.util.RequestProcessor;
 
50
import org.openide.windows.IOProvider;
 
51
import org.openide.windows.InputOutput;
 
52
import org.openide.windows.OutputEvent;
 
53
import org.openide.windows.OutputListener;
 
54
import org.openide.windows.OutputWriter;
 
55
 
 
56
public class IOManager {
 
57
 
 
58
//    /** DebuggerManager output constant. */
 
59
//    public static final int                 DEBUGGER_OUT = 1;
 
60
//    /** Process output constant. */
 
61
//    public static final int                 PROCESS_OUT = 2;
 
62
//    /** Status line output constant. */
 
63
//    public static final int                 STATUS_OUT = 4;
 
64
//    /** All outputs constant. */
 
65
//    public static final int                 ALL_OUT = DEBUGGER_OUT + 
 
66
//                                                PROCESS_OUT + STATUS_OUT;
 
67
//    /** Standart process output constant. */
 
68
//    public static final int                 STD_OUT = 1;
 
69
//    /** Error process output constant. */
 
70
//    public static final int                 ERR_OUT = 2;
 
71
 
 
72
    
 
73
    // variables ...............................................................
 
74
    
 
75
    protected InputOutput                   debuggerIO = null;
 
76
    private OutputWriter                    debuggerOut;
 
77
    private OutputWriter                    debuggerErr;
 
78
    private String                          name;
 
79
    private boolean                         closed = false;
 
80
    
 
81
    /** output writer Thread */
 
82
    private Hashtable                       lines = new Hashtable ();
 
83
    private Listener                        listener = new Listener ();
 
84
 
 
85
    
 
86
    // init ....................................................................
 
87
    
 
88
    public IOManager(String title) {
 
89
        debuggerIO = IOProvider.getDefault ().getIO (title, true);
 
90
        debuggerIO.setFocusTaken (false);
 
91
        debuggerIO.setErrSeparated(false);
 
92
        debuggerOut = debuggerIO.getOut ();
 
93
        debuggerErr = debuggerIO.getErr();
 
94
        debuggerIO.select();
 
95
    }
 
96
    
 
97
    
 
98
    // public interface ........................................................
 
99
 
 
100
    private LinkedList buffer = new LinkedList ();
 
101
    private RequestProcessor.Task task;
 
102
    
 
103
    /**
 
104
     * Prints given text to the output.
 
105
     */
 
106
    public void println (
 
107
        String text, 
 
108
        Line line
 
109
    ) {
 
110
        println(text, line, false);
 
111
    }
 
112
    
 
113
    /**
 
114
     * Prints given text to the output.
 
115
     */
 
116
    public void println (
 
117
        String text, 
 
118
        Line line,
 
119
        boolean important
 
120
    ) {
 
121
        if (text == null)
 
122
            throw new NullPointerException ();
 
123
        synchronized (buffer) {
 
124
            buffer.addLast (new Text (text, line, important));
 
125
        if (task == null)
 
126
            task = new RequestProcessor("Debugger Output", 1).post (new Runnable () {
 
127
                public void run () {
 
128
                    synchronized (buffer) {
 
129
                        int i, k = buffer.size ();
 
130
                        for (i = 0; i < k; i++) {
 
131
                            Text t = (Text) buffer.removeFirst ();
 
132
                            try {
 
133
                                //if ((t.where & DEBUGGER_OUT) != 0) {
 
134
                                    Listener listener;
 
135
                                    if (t.line != null) {
 
136
                                        listener = IOManager.this.listener;
 
137
                                        lines.put (t.text, t.line);
 
138
                                    } else {
 
139
                                        listener = null;
 
140
                                    }
 
141
                                    if (t.important) {
 
142
                                        if (listener != null) {
 
143
                                            debuggerErr.println(t.text, listener, t.important);
 
144
                                        } else {
 
145
                                            debuggerErr.println(t.text);
 
146
                                        }
 
147
                                        debuggerIO.select();
 
148
                                        debuggerErr.flush();
 
149
                                    } else {
 
150
                                        if (listener != null) {
 
151
                                            debuggerOut.println(t.text, listener, t.important);
 
152
                                        } else {
 
153
                                            debuggerOut.println(t.text);
 
154
                                        }
 
155
                                        debuggerOut.flush();
 
156
                                    }
 
157
                                //}
 
158
                               // if ((t.where & STATUS_OUT) != 0) 
 
159
                                    StatusDisplayer.getDefault ().setStatusText (t.text);
 
160
                            } catch (IOException ex) {
 
161
                                ex.printStackTrace ();
 
162
                            }
 
163
                        }
 
164
                        if (closed) {
 
165
                            debuggerOut.close ();
 
166
                            debuggerErr.close();
 
167
                        }
 
168
                    }
 
169
                }
 
170
            }, 500, Thread.MIN_PRIORITY);
 
171
        else 
 
172
            task.schedule (500);
 
173
        }
 
174
    }
 
175
 
 
176
    void closeStream () {
 
177
        synchronized (buffer) {
 
178
            closed = true;
 
179
            if (task != null) {
 
180
                task.schedule(500);
 
181
            }
 
182
        }
 
183
    }
 
184
 
 
185
    void close () {
 
186
        debuggerIO.closeInputOutput ();
 
187
    }
 
188
    
 
189
    
 
190
    // innerclasses ............................................................
 
191
    
 
192
    private class Listener implements OutputListener {
 
193
        public void outputLineSelected (OutputEvent ev) {
 
194
        }
 
195
        public void outputLineAction (OutputEvent ev) {
 
196
            String t = ev.getLine ();
 
197
            Line l = (Line) lines.get (t);
 
198
            if (l == null) return;
 
199
            l.show ();
 
200
        }
 
201
        public void outputLineCleared (OutputEvent ev) {
 
202
            lines = new Hashtable ();
 
203
        }
 
204
    }
 
205
    
 
206
    private static class Text {
 
207
        private String text;
 
208
        private Line line;
 
209
        private boolean important;
 
210
        
 
211
        private Text (String text, Line line, boolean important) {
 
212
            this.text = text;
 
213
            this.line = line;
 
214
            this.important = important;
 
215
        }
 
216
    }
 
217
    
 
218
    static class Line {
 
219
        private String url;
 
220
        private int lineNumber;
 
221
        private JPDADebugger debugger;
 
222
        
 
223
        Line (String url, int lineNumber, JPDADebugger debugger) {
 
224
            this.url = url;
 
225
            this.lineNumber = lineNumber;
 
226
        }
 
227
        
 
228
        void show () {
 
229
            EditorContextBridge.getContext().showSource (url, lineNumber, debugger);
 
230
        }
 
231
    }
 
232
}