001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.ant.debugger;
043:
044: import java.io.IOException;
045: import java.util.Hashtable;
046: import java.util.LinkedList;
047: import javax.swing.SwingUtilities;
048: import org.openide.awt.StatusDisplayer;
049: import org.openide.text.Annotatable;
050: import org.openide.text.Line;
051: import org.openide.util.NbBundle;
052: import org.openide.util.RequestProcessor;
053: import org.openide.windows.IOProvider;
054: import org.openide.windows.InputOutput;
055: import org.openide.windows.OutputEvent;
056: import org.openide.windows.OutputListener;
057: import org.openide.windows.OutputWriter;
058:
059: public class IOManager {
060:
061: // variables ...............................................................
062:
063: protected InputOutput debuggerIO = null;
064: private OutputWriter debuggerOut;
065: private String name;
066: private boolean closed = false;
067:
068: /** output writer Thread */
069: private Hashtable lines = new Hashtable();
070: private Listener listener = new Listener();
071:
072: // init ....................................................................
073:
074: public IOManager(String title) {
075: debuggerIO = IOProvider.getDefault().getIO(title, true);
076: debuggerIO.setFocusTaken(false);
077: debuggerOut = debuggerIO.getOut();
078: }
079:
080: // public interface ........................................................
081:
082: private LinkedList buffer = new LinkedList();
083: private RequestProcessor.Task task;
084:
085: /**
086: * Prints given text to the output.
087: */
088: public void println(final String text, final Object line) {
089: if (text == null)
090: throw new NullPointerException();
091: synchronized (buffer) {
092: buffer.addLast(new Text(text, line));
093: }
094: if (task == null)
095: task = RequestProcessor.getDefault().post(new Runnable() {
096: public void run() {
097: synchronized (buffer) {
098: int i, k = buffer.size();
099: for (i = 0; i < k; i++) {
100: Text t = (Text) buffer.removeFirst();
101: try {
102: if (t.line != null) {
103: debuggerOut.println(t.text,
104: listener);
105: lines.put(t.text, t.line);
106: } else
107: debuggerOut.println(t.text);
108: debuggerOut.flush();
109: if (closed)
110: debuggerOut.close();
111: StatusDisplayer.getDefault()
112: .setStatusText(t.text);
113: } catch (IOException ex) {
114: ex.printStackTrace();
115: }
116: }
117: }
118: }
119: }, 100, Thread.MIN_PRIORITY);
120: else
121: task.schedule(100);
122: }
123:
124: void closeStream() {
125: debuggerOut.close();
126: closed = true;
127: close();
128: }
129:
130: void close() {
131: debuggerIO.closeInputOutput();
132: }
133:
134: // innerclasses ............................................................
135:
136: private class Listener implements OutputListener {
137: public void outputLineSelected(OutputEvent ev) {
138: }
139:
140: public void outputLineAction(final OutputEvent ev) {
141: SwingUtilities.invokeLater(new Runnable() {
142: public void run() {
143: String t = ev.getLine();
144: Object a = lines.get(t);
145: if (a == null)
146: return;
147: Utils.showLine(a);
148: }
149: });
150: }
151:
152: public void outputLineCleared(OutputEvent ev) {
153: lines = new Hashtable();
154: }
155: }
156:
157: private static class Text {
158: private String text;
159: private Object line;
160:
161: private Text(String text, Object line) {
162: this.text = text;
163: this.line = line;
164: }
165: }
166: }
|