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-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */


package org.netbeans.modules.search.types;


import javax.swing.JEditorPane;
import javax.swing.SwingUtilities;
import javax.swing.text.Caret;

import org.openide.cookies.EditorCookie;
import org.openide.cookies.LineCookie;
import org.openide.loaders.DataObject;
import org.openide.text.Annotation;
import org.openide.text.Line;
import org.openide.util.NbBundle;

import java.awt.*;


/** 
 * Holds details about one search hit in the text document.
 *
 * @author Tomas Pavek
 */
public class TextDetail extends Object {

    /** Property name which indicates this detail to show. */
    public static final int DH_SHOW = 1;
    /** Property name which indicates this detail to go to. */
    public static final int DH_GOTO = 2;
    /** Property name which indicates this detail to hide. */
    public static final int DH_HIDE = 3;
    
    /** Data object. */
    private DataObject dobj;
    /** Line number where search result occures.*/
    private int line;
    /** Text of the line. */ 
    private String lineText;
    /** Column where search result starts. */
    private int column;
    /** Length of search result which to mark. */
    private int markLength;
    /** Line. */
    private Line lineObj;
    /** Annotation used for this detail. */
    private Annotation annotation;
    
    
    /** Constructor using data object. */
    public TextDetail(DataObject dobj) {
        this.dobj = dobj;
    }

    /**
     * Shows the search detail on the DataObject.
     * The document is opened in the editor, the caret is positioned on the right line and column 
     * and searched string is marked.
     *
     * @param how indicates how to show detail. 
     * @see #DH_GOTO 
     * @see #DH_SHOW 
     * @see #DH_HIDE */
    public void showDetail(int how) {
        if (dobj == null) {
            Toolkit.getDefaultToolkit().beep();
            return;
        }
        if (lineObj == null) { // try to get Line from DataObject
            LineCookie lineCookie = (LineCookie) dobj.getCookie(LineCookie.class);
            if (lineCookie != null) {
                Line.Set lineSet = lineCookie.getLineSet();
                try {
                    lineObj = lineSet.getOriginal(line - 1);
                } catch (IndexOutOfBoundsException ioobex) {
                    // The line doesn't exist - go to the last line
                    lineObj = lineSet.getOriginal(findMaxLine(lineSet));
                    column = markLength = 0;
                }
            }
            if (lineObj == null) {
                Toolkit.getDefaultToolkit().beep();
                return;
            }
        }

        if (column > 0) { // exact position on the line is known
            if (how == DH_HIDE) {
                return;
            }
            EditorCookie edCookie = (EditorCookie) dobj.getCookie(EditorCookie.class);
            if (edCookie != null)
                edCookie.open();
            if (how == DH_SHOW) {
                lineObj.show(Line.SHOW_TRY_SHOW, column - 1);
            }
            else if (how == DH_GOTO) {
                lineObj.show(Line.SHOW_GOTO, column - 1);
            }
            if (markLength > 0 && edCookie != null) {
                final JEditorPane[] panes = edCookie.getOpenedPanes();
                if (panes != null && panes.length > 0) {
                    // Necessary since above lineObj.show leads to invoke later as well.
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            Caret caret = panes[0].getCaret(); // http://www.netbeans.org/issues/show_bug.cgi?id=23626
                            caret.moveDot(caret.getDot() + markLength);
                        }
                    });
                }
            }
        } else { // go to the line without column specified
            if (how == DH_SHOW) {
                lineObj.show(Line.SHOW_TRY_SHOW, 0);
                getAnnotation().attach(lineObj);
            } else if (how == DH_GOTO) {
                lineObj.show(Line.SHOW_GOTO, 0);
                getAnnotation().detach();
            } else if (how == DH_HIDE) {
                getAnnotation().detach();
            }
        }
    }

    /** Gets annotation used for this detail. */
    private synchronized Annotation getAnnotation() {
        if (annotation == null) {
            annotation = new Annotation() {
                public String getAnnotationType() {
                    return "org-netbeans-modules-search-RegExpAnnotation"; // NOI18N
                }
                
                public String getShortDescription() {
                    return NbBundle.getBundle(TextDetail.class).getString("LBL_AnnotationShortDesc");
                }
            };
        }
        
        return annotation;
    }
    
    /** Getter for lineText property. */
    public String getLineText() {
        return lineText;
    }
    
    /** Setter for lineText property. */
    public void setLineText(String text) {
        lineText = text;
    }
    

    /**
     * Gets the DataObject where the searched text was found. 
     *
     * @return data object or null if no data object is available
     */
    public DataObject getDataObject() {
        return dobj;
    }

    /** Gets the line position of the text. */
    public int getLine() {
        return line;
    }

    /** Sets the line position of the text. */
    public void setLine(int line) {
        this.line = line;
    }

    /** Gets the column position of the text or 0 (1 based). */
    public int getColumn() {
        return column;
    }

    /** Sets the column position of the text. */
    public void setColumn(int col) {
        column = col;
    }

    /** Gets the length of the text that should be marked when the detail is shown. */
    public void setMarkLength(int len) {
        markLength = len;
    }

    /** @return length or 0 */
    public int getMarkLength() {
        return markLength;
    }
    
    /**
     * Returns the maximum line in the set.
     * Used to display the end of file when the corresponding
     * line no longer exists. (Copied from org.openide.text)
     *
     * @param set the set we want to search.
     * @return maximum line in the set.
     */
    private static int findMaxLine(Line.Set set) {
        int from = 0;
        int to = 32000;
        
        for (;;) {
            try {
                set.getOriginal(to);
                // if the line exists, double the max number, but keep
                // for reference that it exists
                from = to;
                to *= 2;
            } catch (IndexOutOfBoundsException ex) {
                break;
            }
        }
        
        while (from < to) {
            int middle = (from + to + 1) / 2;
            
            try {
                set.getOriginal(middle);
                // line exists
                from = middle;
            } catch (IndexOutOfBoundsException ex) {
                // line does not exists, we have to search lower
                to = middle - 1;
            }
        }
        
        return from;
    }

}
... 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.