alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
/*
 * ExtPlainView.java
 *
 * Created on May 9, 2004, 4:29 PM
 */

package org.netbeans.core.output2;

import org.openide.ErrorManager;

import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;

/**
 * Extension to PlainView which can paint hyperlinked lines in different
 * colors.  For the limited styles that the output window supports, this
 * is considerably simpler and has less overhead than the default handling
 * of StyledDocument.
 *
 * @author  Tim Boudreau
 */
class ExtPlainView extends PlainView {
    private final Segment SEGMENT = new Segment(); 
    /** Creates a new instance of ExtPlainView */
    ExtPlainView(Element elem) {
        super (elem);
    }

    protected int drawSelectedText(Graphics g, int x,
                                   int y, int p0, int p1) throws BadLocationException {
                                       
        Document doc = getDocument();
        if (doc instanceof OutputDocument) {                                         
            Segment s = SwingUtilities.isEventDispatchThread() ? SEGMENT : 
                new Segment(); 
            doc.getText(p0, p1 - p0, s);
            g.setColor(getColorForLocation(p0, doc, true));
            int ret = Utilities.drawTabbedText(s, x, y, g, this, p0);
            if (g.getColor() == WrappedTextView.selectedLinkFg) {
                //#47263 - start hyperlink underline at first
                //non-whitespace character
                underline(g, s, x, p0, y);
            }
            return ret;
        } else {
            return super.drawUnselectedText (g, x, y, p0, p1);
        }
    }

    
    protected int drawUnselectedText(Graphics g, int x, int y, 
                                     int p0, int p1) throws BadLocationException {
        Document doc = getDocument();
        if (doc instanceof OutputDocument) {                                         
            Segment s = SwingUtilities.isEventDispatchThread() ? SEGMENT : 
                new Segment(); 
            doc.getText(p0, p1 - p0, s);
            g.setColor(getColorForLocation(p0, doc, false));
            int ret = Utilities.drawTabbedText(s, x, y, g, this, p0);
            if (g.getColor() == WrappedTextView.unselectedLinkFg) {
                //#47263 - start hyperlink underline at first
                //non-whitespace character
                underline(g, s, x, p0, y);
            }
            return ret;
        } else {
            return super.drawUnselectedText (g, x, y, p0, p1);
        }
    }

    private void underline(Graphics g, Segment s, int x, int p0, int y) {
        int wid = g.getFontMetrics().charWidth(' '); //NOI18N
        char[] txt = s.array;
        int txtOffset = s.offset;
        int txtCount = s.count;
        int n = s.offset + s.count;
        int tabCount = 0;
        int wsCount = 0;
        for (int i = s.offset; i < n; i++) {
            if (txt[i] == '\t') { //NOI18N
                x = (int) nextTabStop((float) x,
                p0 + i - txtOffset);
                tabCount++;
            } else if (Character.isWhitespace(txt[i])) {
                x += wid;
                wsCount++;
            } else {
                break;
            }
        }
        int end = x + (wid * (txtCount - (wsCount + tabCount + 1)));
        if (end > x) {
            g.drawLine (x, y+1, end, y+1);
        }
    }

    private static Color getColorForLocation (int start, Document d, boolean selected) {
        OutputDocument od = (OutputDocument) d;
        int line = od.getElementIndex (start);
        boolean hyperlink = od.getLines().isHyperlink(line);
        boolean isErr = od.getLines().isErr(line);
        return hyperlink ? 
            selected ? 
                WrappedTextView.selectedLinkFg : 
                WrappedTextView.unselectedLinkFg :
            selected ? isErr ? 
                WrappedTextView.selectedErr : 
                WrappedTextView.selectedFg : 
            isErr ? 
                WrappedTextView.unselectedErr : 
                WrappedTextView.unselectedFg;
    }

}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.