|
What this is
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 |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
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.