KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ExtPlainView.java
21  *
22  * Created on May 9, 2004, 4:29 PM
23  */

24
25 package org.netbeans.core.output2;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import javax.swing.*;
31 import javax.swing.text.*;
32 import java.awt.*;
33
34 /**
35  * Extension to PlainView which can paint hyperlinked lines in different
36  * colors. For the limited styles that the output window supports, this
37  * is considerably simpler and has less overhead than the default handling
38  * of StyledDocument.
39  *
40  * @author Tim Boudreau
41  */

42 class ExtPlainView extends PlainView {
43     private final Segment SEGMENT = new Segment();
44
45     /** set antialiasing hints when it's requested. */
46     private static final boolean antialias = Boolean.getBoolean ("swing.aatext") || //NOI18N
47
"Aqua".equals (UIManager.getLookAndFeel().getID()); // NOI18N
48

49     private static Map JavaDoc hintsMap = null;
50     
51     static final Map JavaDoc getHints() {
52         if (hintsMap == null) {
53             //Thanks to Phil Race for making this possible
54
hintsMap = (Map JavaDoc)(Toolkit.getDefaultToolkit().getDesktopProperty("awt.font.desktophints")); //NOI18N
55
if (hintsMap == null) {
56                 hintsMap = new HashMap JavaDoc();
57                 if (antialias)
58                     hintsMap.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
59             }
60         }
61         return hintsMap;
62     }
63     
64     /** Creates a new instance of ExtPlainView */
65     ExtPlainView(Element elem) {
66         super (elem);
67     }
68
69     public void paint(Graphics g, Shape allocation) {
70         ((Graphics2D)g).addRenderingHints(getHints());
71         super.paint(g, allocation);
72     }
73     
74     protected int drawSelectedText(Graphics g, int x,
75                                    int y, int p0, int p1) throws BadLocationException {
76                                        
77         Document doc = getDocument();
78         if (doc instanceof OutputDocument) {
79             Segment s = SwingUtilities.isEventDispatchThread() ? SEGMENT :
80                 new Segment();
81             doc.getText(p0, p1 - p0, s);
82             g.setColor(getColorForLocation(p0, doc, true));
83             int ret = Utilities.drawTabbedText(s, x, y, g, this, p0);
84             if (g.getColor() == WrappedTextView.selectedLinkFg || g.getColor() == WrappedTextView.selectedImportantLinkFg) {
85                 //#47263 - start hyperlink underline at first
86
//non-whitespace character
87
underline(g, s, x, p0, y);
88             }
89             return ret;
90         } else {
91             return super.drawUnselectedText (g, x, y, p0, p1);
92         }
93     }
94
95     
96     protected int drawUnselectedText(Graphics g, int x, int y,
97                                      int p0, int p1) throws BadLocationException {
98         Document doc = getDocument();
99         if (doc instanceof OutputDocument) {
100             Segment s = SwingUtilities.isEventDispatchThread() ? SEGMENT :
101                 new Segment();
102             doc.getText(p0, p1 - p0, s);
103             g.setColor(getColorForLocation(p0, doc, false));
104             int ret = Utilities.drawTabbedText(s, x, y, g, this, p0);
105             if (g.getColor() == WrappedTextView.selectedLinkFg || g.getColor() == WrappedTextView.selectedImportantLinkFg) {
106                 //#47263 - start hyperlink underline at first
107
//non-whitespace character
108
underline(g, s, x, p0, y);
109             }
110             return ret;
111         } else {
112             return super.drawUnselectedText (g, x, y, p0, p1);
113         }
114     }
115
116     private void underline(Graphics g, Segment s, int x, int p0, int y) {
117         int wid = g.getFontMetrics().charWidth(' '); //NOI18N
118
char[] txt = s.array;
119         int txtOffset = s.offset;
120         int txtCount = s.count;
121         int n = s.offset + s.count;
122         int tabCount = 0;
123         int wsCount = 0;
124         for (int i = s.offset; i < n; i++) {
125             if (txt[i] == '\t') { //NOI18N
126
x = (int) nextTabStop((float) x,
127                 p0 + i - txtOffset);
128                 tabCount++;
129             } else if (Character.isWhitespace(txt[i])) {
130                 x += wid;
131                 wsCount++;
132             } else {
133                 break;
134             }
135         }
136         int end = x + (wid * (txtCount - (wsCount + tabCount + 1)));
137         if (end > x) {
138             g.drawLine (x, y+1, end, y+1);
139         }
140     }
141
142     private static Color getColorForLocation (int start, Document d, boolean selected) {
143         OutputDocument od = (OutputDocument) d;
144         int line = od.getElementIndex (start);
145         boolean hyperlink = od.getLines().isHyperlink(line);
146         boolean important = hyperlink ? od.getLines().isImportantHyperlink(line) : false;
147         boolean isErr = od.getLines().isErr(line);
148         
149         return hyperlink ?
150             (important ?
151                 (selected ?
152                     WrappedTextView.selectedImportantLinkFg :
153                     WrappedTextView.unselectedImportantLinkFg) :
154                 (selected ?
155                     WrappedTextView.selectedLinkFg :
156                     WrappedTextView.unselectedLinkFg)) :
157             (selected ?
158                 (isErr ?
159                     WrappedTextView.selectedErr :
160                     WrappedTextView.selectedFg) :
161                 (isErr ?
162                     WrappedTextView.unselectedErr :
163                     WrappedTextView.unselectedFg));
164     }
165
166 }
167
Popular Tags