KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.netbeans.core.output2;
20
21 import java.awt.Point JavaDoc;
22 import java.awt.event.MouseEvent JavaDoc;
23 import javax.swing.text.BadLocationException JavaDoc;
24 import javax.swing.text.Segment JavaDoc;
25 import org.netbeans.core.output2.ui.AbstractOutputPane;
26
27 import javax.swing.*;
28 import javax.swing.text.Document JavaDoc;
29 import javax.swing.text.PlainDocument JavaDoc;
30 import java.awt.*;
31 import java.awt.event.ComponentEvent JavaDoc;
32 import java.awt.event.ComponentListener JavaDoc;
33 import java.awt.event.KeyEvent JavaDoc;
34 import org.openide.util.NbPreferences;
35
36
37 class OutputPane extends AbstractOutputPane implements ComponentListener JavaDoc {
38     protected void documentChanged() {
39         super.documentChanged();
40         findOutputTab().documentChanged();
41     }
42
43     protected void caretEnteredLine(int line) {
44         findOutputTab().caretEnteredLine(line);
45     }
46
47     protected void lineClicked(int line, Point JavaDoc p) {
48         if (!(getDocument() instanceof OutputDocument) || !inLeadingOrTrailingWhitespace(line, p)) {
49             findOutputTab().lineClicked(line);
50         }
51     }
52     
53     private boolean linePressed (int line, Point JavaDoc p) {
54         boolean result;
55         if (!(getDocument() instanceof OutputDocument) || !inLeadingOrTrailingWhitespace(line, p)) {
56             result = findOutputTab().linePressed (line, p);
57         } else {
58             result = false;
59         }
60         return result;
61     }
62
63     protected void postPopupMenu(Point JavaDoc p, Component src) {
64         findOutputTab().postPopupMenu(p, src);
65     }
66
67     public void setMouseLine (int line, Point JavaDoc p) {
68         Document JavaDoc doc = getDocument();
69         if (doc instanceof OutputDocument) {
70             boolean link = line != -1 && ((OutputDocument) doc).getLines().isHyperlink(line);
71             if (link && p != null) {
72                 //#47256 - Don't set the cursor if the mouse if over
73
//whitespace
74
if (inLeadingOrTrailingWhitespace(line, p)) {
75                     link = false;
76                     line = -1;
77                 }
78             }
79             textView.setCursor(link ? Cursor.getPredefinedCursor(Cursor.HAND_CURSOR) :
80                     Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
81         }
82         super.setMouseLine(line, p);
83     }
84     
85     private boolean inLeadingOrTrailingWhitespace (int line, Point JavaDoc p) {
86         if (line == -1) {
87             return true;
88         }
89         assert getDocument() instanceof OutputDocument;
90         assert p != null;
91         OutputDocument doc = (OutputDocument) getDocument();
92         int lineStart = doc.getLineStart(line);
93         int lineEnd = doc.getLineEnd(line);
94
95         try {
96             doc.getText (lineStart, lineEnd - lineStart, seg);
97             char curr = seg.first();
98             while (Character.isWhitespace(curr) && curr != Segment.DONE) {
99                 lineStart++;
100                 curr = seg.next();
101             }
102             curr = seg.last();
103             while (Character.isWhitespace(curr) && curr != Segment.DONE) {
104                 lineEnd--;
105                 curr = seg.previous();
106             }
107             if (lineEnd <= lineStart) {
108                 line = -1;
109             } else {
110                 Rectangle startRect = textView.modelToView(lineStart);
111                 Rectangle endRect = textView.modelToView(lineEnd);
112                 if (p.y >= startRect.y && p.y <= endRect.y && isWrapped()) {
113                     endRect.x = 0;
114                     endRect.width = getWidth();
115                 }
116                 boolean cursorIsNotOverLeadingOrTrailingWhitespace =
117                     p.x >= startRect.x && p.y >= startRect.y &&
118                     p.x <= endRect.x + endRect.width &&
119                     p.y <= endRect.y + endRect.height;
120                 if (!cursorIsNotOverLeadingOrTrailingWhitespace) {
121                     line = -1;
122                 }
123             }
124         } catch (BadLocationException JavaDoc e) {
125             //do nothing
126
}
127         return line == -1;
128     }
129     
130     private Segment JavaDoc seg = new Segment JavaDoc();
131     
132     /**
133      * Only calls super if there are hyperlinks in the document to avoid huge
134      * numbers of calls to viewToModel if the cursor is never going to be
135      * changed anyway.
136      */

137     public void mouseMoved (MouseEvent JavaDoc evt) {
138         Document JavaDoc doc = getDocument();
139         if (doc instanceof OutputDocument) {
140             if (((OutputDocument) doc).getLines().hasHyperlinks()) {
141                 super.mouseMoved(evt);
142             }
143         }
144     }
145     
146     public void mousePressed(MouseEvent JavaDoc e) {
147         super.mousePressed(e);
148         if (e.getSource() == textView && SwingUtilities.isLeftMouseButton(e)) {
149             int pos = textView.viewToModel(e.getPoint());
150             if (pos != -1) {
151                 int line = textView.getDocument().getDefaultRootElement().getElementIndex(pos);
152                 if (line >= 0) {
153                     if (linePressed(line, e.getPoint())) {
154                         e.consume();
155                         return;
156                     }
157                 }
158             }
159         }
160         //Refine possibly to focus just what is important..
161
findOutputTab().setToFocus((Component)e.getSource());
162         findOutputTab().requestActive();
163     }
164
165     private OutputTab findOutputTab() {
166         return (OutputTab) SwingUtilities.getAncestorOfClass (OutputTab.class, this);
167     }
168
169     protected void setDocument (Document JavaDoc doc) {
170         if (doc == null) {
171             Document JavaDoc d = getDocument();
172             if (d != null) {
173                 d.removeDocumentListener(this);
174             }
175             textView.setDocument (new PlainDocument JavaDoc());
176             return;
177         }
178         textView.setEditorKit (new OutputEditorKit(isWrapped(), textView));
179         super.setDocument(doc);
180         updateKeyBindings();
181     }
182     
183     
184     public void setWrapped (boolean val) {
185         if (val != isWrapped() || !(getEditorKit() instanceof OutputEditorKit)) {
186             NbPreferences.forModule(OutputPane.class).putBoolean("wrap", val); //NOI18N
187
final int pos = textView.getCaret().getDot();
188             Cursor cursor = textView.getCursor();
189             try {
190                 textView.setCursor (Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
191                 setEditorKit (new OutputEditorKit(val, textView));
192             } finally {
193                 textView.setCursor (cursor);
194             }
195             if (val) {
196                 getViewport().addChangeListener(this);
197             } else {
198                 getViewport().removeChangeListener(this);
199             }
200             
201             //Don't try to set the caret position until the view has
202
//been fully readjusted to its new dimensions, scroll bounds, etc.
203
SwingUtilities.invokeLater (new Runnable JavaDoc() {
204                 private boolean first = true;
205                 public void run() {
206                     if (first) {
207                         first = false;
208                         SwingUtilities.invokeLater(this);
209                         return;
210                     }
211                     textView.getCaret().setDot(pos);
212                 }
213             });
214             if (getDocument() instanceof OutputDocument && ((OutputDocument) getDocument()).getLines().isGrowing()) {
215                 lockScroll();
216             }
217             if (!val) {
218                 //If there are long lines, it will suddenly get scrolled to the right
219
//with the non-wrapping editor kit, so fix that
220
getHorizontalScrollBar().setValue(getHorizontalScrollBar().getModel().getMinimum());
221             }
222             validate();
223         }
224     }
225     
226     public boolean isWrapped() {
227         if (getEditorKit() instanceof OutputEditorKit) {
228             return getEditorKit() instanceof OutputEditorKit
229               && ((OutputEditorKit) getEditorKit()).isWrapped();
230         } else {
231             return NbPreferences.forModule(OutputPane.class).getBoolean("wrap", false); //NOI18N
232
}
233     }
234     
235     private static final boolean GTK = "GTK".equals(UIManager.getLookAndFeel().getID());
236     protected JEditorPane createTextView() {
237         JEditorPane result = GTK ? new GEP() : new JEditorPane();
238         result.addComponentListener(this);
239         
240         // we don't want the background to be gray even though the text there is not editable
241
result.setDisabledTextColor(result.getBackground());
242         
243         //#83118 - remove the "control shift 0" from editor pane to lt the Open Project action through
244
InputMap map = result.getInputMap();
245         MyInputMap myMap = new MyInputMap();
246         myMap.setParent(map);
247         result.setInputMap(result.WHEN_FOCUSED, myMap);
248         
249         return result;
250     }
251     
252     //#83118 - remove the "control shift 0" from editor pane to lt the Open Project action through
253
protected class MyInputMap extends InputMap {
254         
255         public MyInputMap() {
256             super();
257         }
258         
259         public Object JavaDoc get(KeyStroke keyStroke) {
260             KeyStroke stroke = KeyStroke.getKeyStroke("control shift O");
261             if (keyStroke.equals(stroke)) {
262                 return null;
263             }
264             stroke = KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, Event.CTRL_MASK);
265             if (keyStroke.equals(stroke)) {
266                 return null;
267             }
268             Object JavaDoc retValue;
269             retValue = super.get(keyStroke);
270             return retValue;
271         }
272         
273     }
274     
275
276     private int prevW = -1;
277     public void componentResized(ComponentEvent JavaDoc e) {
278         int w = textView.getWidth();
279         if (prevW != w) {
280             if (isWrapped()) {
281                 WrappedTextView view = ((OutputEditorKit) getEditorKit()).view();
282                 if (view != null) {
283                     view.setChanged();
284                     textView.repaint();
285                 }
286             }
287         }
288         prevW = w;
289     }
290
291     public void componentMoved(ComponentEvent JavaDoc e) {
292         //do nothing
293
}
294
295     public void componentShown(ComponentEvent JavaDoc e) {
296         //do nothing
297
}
298
299     public void componentHidden(ComponentEvent JavaDoc e) {
300         //do nothing
301
}
302     
303     private static final class GEP extends JEditorPane {
304         public java.awt.Color JavaDoc getBackground() {
305             return UIManager.getColor("text");
306         }
307     }
308
309 }
310
Popular Tags