KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.awt.Component JavaDoc;
23 import java.awt.Point JavaDoc;
24 import java.awt.Toolkit JavaDoc;
25 import java.awt.event.ActionEvent JavaDoc;
26 import java.awt.event.ActionListener JavaDoc;
27 import java.util.regex.Matcher JavaDoc;
28 import javax.swing.Action JavaDoc;
29 import javax.swing.JComponent JavaDoc;
30 import javax.swing.SwingUtilities JavaDoc;
31 import javax.swing.text.Document JavaDoc;
32 import org.netbeans.core.output2.ui.AbstractOutputPane;
33 import org.netbeans.core.output2.ui.AbstractOutputTab;
34
35 /**
36  * A component representing one tab in the output window.
37  */

38 final class OutputTab extends AbstractOutputTab {
39     private NbIO io;
40
41     OutputTab (NbIO io) {
42         this.io = io;
43         if (Controller.LOG) Controller.log ("Created an output component for " + io);
44         OutputDocument doc = new OutputDocument (((NbWriter) io.getOut()).out());
45         setDocument (doc);
46     }
47
48     public void addNotify() {
49         super.addNotify();
50         if (io != null) io.setClosed(false);
51     }
52
53     public void removeNotify() {
54         if (io != null) io.setClosed(true);
55         super.removeNotify();
56     }
57
58     public void setDocument (Document JavaDoc doc) {
59         if (Controller.LOG) Controller.log ("Set document on " + this + " with " + io);
60         assert SwingUtilities.isEventDispatchThread();
61         Document JavaDoc old = getDocument();
62         hasOutputListeners = false;
63         firstNavigableListenerLine = -1;
64         super.setDocument(doc);
65         if (old != null && old instanceof OutputDocument) {
66             ((OutputDocument) old).dispose();
67         }
68     }
69
70     public void setIO (NbIO io) {
71         if (Controller.LOG) Controller.log ("Replacing io on " + this + " with " + io + " out is " + (io != null ? io.getOut() : null));
72         if (io != null) {
73             setDocument (new OutputDocument(((NbWriter) io.getOut()).out()));
74             io.setClosed(false);
75         } else {
76             if (this.io != null) this.io.setClosed(true);
77             this.io = null;
78             setDocument(null);
79         }
80     }
81
82     public OutputDocument getDocument() {
83         Document JavaDoc d = getOutputPane().getDocument();
84         if (d instanceof OutputDocument) {
85             return (OutputDocument) d;
86         }
87         return null;
88     }
89
90     protected AbstractOutputPane createOutputPane() {
91         return new OutputPane();
92     }
93
94     protected void inputSent(String JavaDoc txt) {
95         if (Controller.LOG) Controller.log("Input sent on OutputTab: " + txt);
96         getOutputPane().lockScroll();
97         findOutputWindow().inputSent(this, txt);
98     }
99
100     protected void inputEof() {
101         if (Controller.LOG) Controller.log ("Input EOF on OutputTab: ");
102         findOutputWindow().inputEof(this);
103     }
104
105     public void hasSelectionChanged(boolean val) {
106         OutputWindow win = findOutputWindow();
107         if (win != null) {
108             win.hasSelectionChanged(this, val);
109         }
110     }
111
112     public NbIO getIO() {
113         return io;
114     }
115
116     private long timestamp = 0;
117     void updateTimestamp() {
118         timestamp = System.currentTimeMillis();
119     }
120
121     long getTimestamp() {
122         return timestamp;
123     }
124
125     private OutputWindow findOutputWindow() {
126         if (getParent() != null) {
127             return (OutputWindow) SwingUtilities.getAncestorOfClass(OutputWindow.class, this);
128         } else {
129             return (OutputWindow) getClientProperty ("outputWindow"); //NOI18N
130
}
131     }
132     
133     void requestActive() {
134         findOutputWindow().requestActive();
135     }
136
137     public void lineClicked(int line) {
138         findOutputWindow().lineClicked (this, line);
139     }
140     
141     boolean linePressed (int line, Point JavaDoc p) {
142         OutWriter out = getIO().out();
143         if (out != null) {
144             return out.getLines().getListenerForLine(line) != null;
145         } else {
146             return false;
147         }
148     }
149
150     public void postPopupMenu(Point JavaDoc p, Component JavaDoc src) {
151         findOutputWindow().postPopupMenu(this, p, src);
152     }
153
154     public void caretEnteredLine(int line) {
155         findOutputWindow().caretEnteredLine(this, line);
156     }
157     
158     private int firstNavigableListenerLine = -1;
159     /**
160      * Do not unlock scrollbar unless there is a bona-fide error to
161      * show - deprecation warnings should be ignored.
162      */

163     public int getFirstNavigableListenerLine() {
164         if (firstNavigableListenerLine != -1) {
165             return firstNavigableListenerLine;
166         }
167         
168         int result = -1;
169         OutWriter out = io.out();
170         if (out != null) {
171             if (Controller.LOG) Controller.log ("Looking for first appropriate" +
172                 " listener line to send the caret to");
173             result = out.getLines().firstImportantListenerLine();
174         }
175         return result;
176     }
177     
178     public String JavaDoc toString() {
179         return "OutputTab@" + System.identityHashCode(this) + " for " + io;
180     }
181
182     private boolean hasOutputListeners = false;
183     public void documentChanged() {
184         OutputWindow win = findOutputWindow();
185         if (win != null) {
186             boolean hadOutputListeners = hasOutputListeners;
187             if (getFirstNavigableListenerLine() == -1) {
188                 return;
189             }
190             hasOutputListeners = getIO().out() != null && getIO().out().getLines().firstListenerLine() >= 0;
191             if (hasOutputListeners != hadOutputListeners) {
192                 win.hasOutputListenersChanged(this, hasOutputListeners);
193             }
194             win.documentChanged(this);
195         }
196     }
197
198     /**
199      * Determine if the new caret position is close enough that the scrollbar should be re-locked
200      * to the end of the document.
201      *
202      * @param dot The caret position
203      * @return if it should be locked
204      */

205     public boolean shouldRelock(int dot) {
206         if (io != null) {
207             OutWriter w = io.out();
208             if (w != null && !w.isClosed()) {
209                 int dist = Math.abs(w.getLines().getCharCount() - dot);
210                 return dist < 100;
211             }
212         }
213         return false;
214     }
215     
216     ActionListener JavaDoc getFindActionListener(Action JavaDoc next, Action JavaDoc prev, Action JavaDoc copy) {
217         if (findActionListener == null) {
218             findActionListener = new FindActionListener(this, next, prev, copy);
219         }
220         return findActionListener;
221     }
222     
223     private ActionListener JavaDoc findActionListener;
224     
225     /**
226      * An action listener which listens to the default button of the find
227      * dialog.
228      */

229     static class FindActionListener implements ActionListener JavaDoc {
230         OutputTab tab;
231         Action JavaDoc findNextAction;
232         Action JavaDoc findPreviousAction;
233         Action JavaDoc copyAction;
234         FindActionListener(OutputTab tab, Action JavaDoc findNextAction, Action JavaDoc findPreviousAction, Action JavaDoc copyAction) {
235             this.tab = tab;
236             this.findNextAction = findNextAction;
237             this.findPreviousAction = findPreviousAction;
238             this.copyAction = copyAction;
239         }
240
241         public void actionPerformed(ActionEvent JavaDoc e) {
242             FindDialogPanel panel = (FindDialogPanel)
243                 SwingUtilities.getAncestorOfClass(FindDialogPanel.class,
244                 (JComponent JavaDoc) e.getSource());
245             if (panel == null) {
246                 //dialog disposed
247
panel = (FindDialogPanel) ((JComponent JavaDoc)
248                     e.getSource()).getClientProperty("panel"); //NOI18N
249
}
250
251             String JavaDoc s = panel.getPattern();
252             if (s == null || s.length() == 0) {
253                 Toolkit.getDefaultToolkit().beep();
254
255                 return;
256             }
257             OutWriter out = tab.getIO().out();
258             if (out != null && !out.isDisposed()) {
259                 Matcher JavaDoc matcher = out.getLines().find(s);
260                 if (matcher != null && matcher.find()) {
261                     int start = matcher.start();
262                     int end = matcher.end();
263                     tab.getOutputPane().setSelection(start, end);
264                     findNextAction.setEnabled(true);
265                     findPreviousAction.setEnabled(true);
266                     copyAction.setEnabled(true);
267                     panel.getTopLevelAncestor().setVisible(false);
268                     tab.requestFocus();
269                 }
270             }
271         }
272     }
273 }
274
Popular Tags