KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > search > types > TextDetail


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
21 package org.netbeans.modules.search.types;
22
23
24 import javax.swing.JEditorPane JavaDoc;
25 import javax.swing.SwingUtilities JavaDoc;
26 import javax.swing.text.Caret JavaDoc;
27
28 import org.openide.cookies.EditorCookie;
29 import org.openide.cookies.LineCookie;
30 import org.openide.loaders.DataObject;
31 import org.openide.text.Line;
32
33 import java.awt.*;
34 import org.openidex.search.SearchHistory;
35 import org.openidex.search.SearchPattern;
36
37
38 /**
39  * Holds details about one search hit in the text document.
40  *
41  * @author Tomas Pavek
42  * @author Marian Petras
43  */

44 public class TextDetail extends Object JavaDoc {
45
46     /** Property name which indicates this detail to show. */
47     public static final int DH_SHOW = 1;
48     /** Property name which indicates this detail to go to. */
49     public static final int DH_GOTO = 2;
50     /** Property name which indicates this detail to hide. */
51     public static final int DH_HIDE = 3;
52     
53     /** Data object. */
54     private DataObject dobj;
55     /** Line number where search result occures.*/
56     private int line;
57     /** Text of the line. */
58     private String JavaDoc lineText;
59     /** Column where search result starts. */
60     private int column;
61     /** Length of search result which to mark. */
62     private int markLength;
63     /** Line. */
64     private Line lineObj;
65     /** SearchPattern used to create the hit of this DetailNode */
66     private SearchPattern searchPattern;
67
68     
69     
70     /** Constructor using data object.
71      * @param pattern SearchPattern used to create the hit of this DetailNode
72      */

73     public TextDetail(DataObject dobj, SearchPattern pattern) {
74         this.dobj = dobj;
75         this.searchPattern = pattern;
76     }
77
78     /**
79      * Shows the search detail on the DataObject.
80      * The document is opened in the editor, the caret is positioned on the right line and column
81      * and searched string is marked.
82      *
83      * @param how indicates how to show detail.
84      * @see #DH_GOTO
85      * @see #DH_SHOW
86      * @see #DH_HIDE */

87     public void showDetail(int how) {
88         if (dobj == null) {
89             Toolkit.getDefaultToolkit().beep();
90             return;
91         }
92         if (lineObj == null) { // try to get Line from DataObject
93
LineCookie lineCookie = (LineCookie) dobj.getCookie(LineCookie.class);
94             if (lineCookie != null) {
95                 Line.Set lineSet = lineCookie.getLineSet();
96                 try {
97                     lineObj = lineSet.getOriginal(line - 1);
98                 } catch (IndexOutOfBoundsException JavaDoc ioobex) {
99                     // The line doesn't exist - go to the last line
100
lineObj = lineSet.getOriginal(findMaxLine(lineSet));
101                     column = markLength = 0;
102                 }
103             }
104             if (lineObj == null) {
105                 Toolkit.getDefaultToolkit().beep();
106                 return;
107             }
108         }
109
110         if (how == DH_HIDE) {
111             return;
112         }
113         EditorCookie edCookie = (EditorCookie) dobj.getCookie(EditorCookie.class);
114         if (edCookie != null)
115             edCookie.open();
116         if (how == DH_SHOW) {
117             lineObj.show(Line.SHOW_TRY_SHOW, column - 1);
118         }
119         else if (how == DH_GOTO) {
120             lineObj.show(Line.SHOW_GOTO, column - 1);
121         }
122         if (markLength > 0 && edCookie != null) {
123             final JEditorPane JavaDoc[] panes = edCookie.getOpenedPanes();
124             if (panes != null && panes.length > 0) {
125                 // Necessary since above lineObj.show leads to invoke later as well.
126
SwingUtilities.invokeLater(new Runnable JavaDoc() {
127                     public void run() {
128                         Caret JavaDoc caret = panes[0].getCaret(); // http://www.netbeans.org/issues/show_bug.cgi?id=23626
129
caret.moveDot(caret.getDot() + markLength);
130                     }
131                 });
132             }
133         }
134         SearchHistory.getDefault().setLastSelected(searchPattern);
135     }
136
137     /** Getter for <code>lineText</code> property. */
138     public String JavaDoc getLineText() {
139         return lineText;
140     }
141     
142     /** Setter for <code>lineText</code> property. */
143     public void setLineText(String JavaDoc text) {
144         lineText = text;
145     }
146     
147
148     /**
149      * Gets the <code>DataObject</code> where the searched text was found.
150      *
151      * @return data object or <code>null</code> if no data object is available
152      */

153     public DataObject getDataObject() {
154         return dobj;
155     }
156
157     /** Gets the line position of the text. */
158     public int getLine() {
159         return line;
160     }
161
162     /** Sets the line position of the text. */
163     public void setLine(int line) {
164         this.line = line;
165     }
166
167     /** Gets the column position of the text or 0 (1 based). */
168     public int getColumn() {
169         return column;
170     }
171
172     /** Sets the column position of the text. */
173     public void setColumn(int col) {
174         column = col;
175     }
176
177     /** Gets the length of the text that should be marked when the detail is shown. */
178     public void setMarkLength(int len) {
179         markLength = len;
180     }
181
182     /** @return length or 0 */
183     public int getMarkLength() {
184         return markLength;
185     }
186     
187     /**
188      * Returns the maximum line in the <code>set</code>.
189      * Used to display the end of file when the corresponding
190      * line no longer exists. (Copied from org.openide.text)
191      *
192      * @param set the set we want to search.
193      * @return maximum line in the <code>set</code>.
194      */

195     private static int findMaxLine(Line.Set set) {
196         int from = 0;
197         int to = 32000;
198         
199         for (;;) {
200             try {
201                 set.getOriginal(to);
202                 // if the line exists, double the max number, but keep
203
// for reference that it exists
204
from = to;
205                 to *= 2;
206             } catch (IndexOutOfBoundsException JavaDoc ex) {
207                 break;
208             }
209         }
210         
211         while (from < to) {
212             int middle = (from + to + 1) / 2;
213             
214             try {
215                 set.getOriginal(middle);
216                 // line exists
217
from = middle;
218             } catch (IndexOutOfBoundsException JavaDoc ex) {
219                 // line does not exists, we have to search lower
220
to = middle - 1;
221             }
222         }
223         
224         return from;
225     }
226
227 }
228
Popular Tags