KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > NbEditorUtilities


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 package org.netbeans.modules.editor;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import javax.swing.SwingUtilities JavaDoc;
25 import javax.swing.Timer JavaDoc;
26 import javax.swing.JEditorPane JavaDoc;
27 import javax.swing.text.AbstractDocument JavaDoc;
28 import javax.swing.text.Document JavaDoc;
29 import javax.swing.text.BadLocationException JavaDoc;
30 import javax.swing.text.Element JavaDoc;
31 import javax.swing.text.JTextComponent JavaDoc;
32 import org.netbeans.editor.BaseDocument;
33 import org.netbeans.editor.Utilities;
34 import org.netbeans.editor.JumpList;
35 import org.netbeans.editor.ext.ExtSyntaxSupport;
36 import org.openide.cookies.LineCookie;
37 import org.openide.cookies.EditorCookie;
38 import org.openide.loaders.DataObject;
39 import org.openide.text.Line;
40 import org.openide.windows.TopComponent;
41 import org.openide.util.Lookup;
42 import org.openide.ErrorManager;
43 import org.openide.util.NbBundle;
44 import java.util.MissingResourceException JavaDoc;
45 import java.awt.Toolkit JavaDoc;
46 import javax.swing.text.EditorKit JavaDoc;
47 import org.openide.filesystems.FileObject;
48
49 /**
50 * Various utilities
51 *
52 * @author Miloslav Metelka
53 * @version 1.00
54 */

55
56 public class NbEditorUtilities {
57
58     /** Get the dataobject from the document's StreamDescriptionProperty property. */
59     public static DataObject getDataObject(Document JavaDoc doc) {
60         Object JavaDoc sdp = doc.getProperty(Document.StreamDescriptionProperty);
61         if (sdp instanceof DataObject) {
62             return (DataObject)sdp;
63         }
64         return null;
65     }
66
67     /**
68      * Verify whether the given document is still being actively used
69      * by the corresponding editor support.
70      */

71     public static boolean isDocumentActive(Document JavaDoc doc) {
72         DataObject dob = getDataObject(doc);
73         if (dob != null) {
74             EditorCookie editorCookie = (EditorCookie)dob.getCookie(EditorCookie.class);
75             if (editorCookie != null) {
76                 Document JavaDoc ecDoc = editorCookie.getDocument(); // returns null if closed
77
if (ecDoc == doc) { // actively used by ec
78
return true;
79                 }
80             }
81         }
82
83         return false;
84     }
85
86     /** Get the fileobject from the document's StreamDescriptionProperty property. */
87     public static FileObject getFileObject(Document JavaDoc doc) {
88         Object JavaDoc sdp = doc.getProperty(Document.StreamDescriptionProperty);
89         if (sdp instanceof FileObject) {
90             return (FileObject)sdp;
91         }
92         if (sdp instanceof DataObject) {
93             return ((DataObject)sdp).getPrimaryFile();
94         }
95         return null;
96     }
97
98     /** This method is a composition of <tt>Utilities.getIdentifierBlock()</tt>
99     * and <tt>SyntaxSupport.getFunctionBlock()</tt>.
100     * @return null if there's no identifier at the given position.
101     * identifier block if there's identifier but it's not a function call.
102     * three member array for the case that there is an identifier followed
103     * by the function call character. The first two members are members
104     * of the identifier block and the third member is the second member
105     * of the function block.
106     */

107     public static int[] getIdentifierAndMethodBlock(BaseDocument doc, int offset)
108     throws BadLocationException JavaDoc {
109         int[] idBlk = Utilities.getIdentifierBlock(doc, offset);
110         if (idBlk != null) {
111             int[] funBlk = ((ExtSyntaxSupport)doc.getSyntaxSupport()).getFunctionBlock(idBlk);
112             if (funBlk != null) {
113                 return new int[] { idBlk[0], idBlk[1], funBlk[1] };
114             }
115         }
116         return idBlk;
117     }
118
119     /** Get the line object from the given position.
120     * @param doc document for which the line is being retrieved
121     * @param offset position in the document
122     * @param original whether to retrieve the original line (true) before
123     * the modifications were done or the current line (false)
124     * @return the line object
125     * @deprecated Replaced by more generic method having {@link javax.swing.text.Document} parameter.
126     */

127     public static Line getLine(BaseDocument doc, int offset, boolean original) {
128         DataObject dob = getDataObject(doc);
129         if (dob != null) {
130             LineCookie lc = (LineCookie)dob.getCookie(LineCookie.class);
131             if (lc != null) {
132                 Line.Set lineSet = lc.getLineSet();
133                 if (lineSet != null) {
134                     try {
135                         int lineOffset = Utilities.getLineOffset(doc, offset);
136                         return original
137                                ? lineSet.getOriginal(lineOffset)
138                                : lineSet.getCurrent(lineOffset);
139                     } catch (BadLocationException JavaDoc e) {
140                     }
141
142                 }
143             }
144         }
145         return null;
146     }
147
148     /** Get the line object from the given position.
149      * @param doc document for which the line is being retrieved
150      * @param offset position in the document
151      * @param original whether to retrieve the original line (true) before
152      * the modifications were done or the current line (false)
153      * @return the line object
154      */

155     public static Line getLine(Document JavaDoc doc, int offset, boolean original) {
156         DataObject dob = getDataObject(doc);
157         if (dob != null) {
158             LineCookie lc = (LineCookie)dob.getCookie(LineCookie.class);
159             if (lc != null) {
160                 Line.Set lineSet = lc.getLineSet();
161                 if (lineSet != null) {
162                     Element JavaDoc lineRoot = (doc instanceof AbstractDocument JavaDoc)
163                         ? ((AbstractDocument JavaDoc)doc).getParagraphElement(0).getParentElement()
164                         : doc.getDefaultRootElement();
165                     int lineIndex = lineRoot.getElementIndex(offset);
166                     return original
167                            ? lineSet.getOriginal(lineIndex)
168                            : lineSet.getCurrent(lineIndex);
169                 }
170             }
171         }
172         return null;
173     }
174
175     /** Get the line object from the component's document and caret position */
176     public static Line getLine(JTextComponent JavaDoc target, boolean original) {
177         return getLine((BaseDocument)target.getDocument(),
178                        target.getCaret().getDot(), original);
179     }
180
181     /** Get the top-component for the target copmonent */
182     public static TopComponent getTopComponent(JTextComponent JavaDoc target) {
183         return (TopComponent)SwingUtilities.getAncestorOfClass(TopComponent.class, target);
184     }
185
186     /** Get the top-component for the target copmonent */
187     public static TopComponent getOuterTopComponent(JTextComponent JavaDoc target) {
188         TopComponent tc = null;
189         TopComponent parent = (TopComponent)SwingUtilities.getAncestorOfClass(TopComponent.class, target);
190         while (parent != null) {
191             tc = parent;
192             parent = (TopComponent)SwingUtilities.getAncestorOfClass(TopComponent.class, tc);
193         }
194         return tc;
195     }
196     
197
198     /** Add the jump-list entry for the for the component that's opened
199     * over the given dataobject if any.
200     */

201     public static void addJumpListEntry(DataObject dob) {
202         final EditorCookie ec = (EditorCookie)dob.getCookie(EditorCookie.class);
203         if (ec != null) {
204             final Timer JavaDoc timer = new Timer JavaDoc(500, null);
205             timer.addActionListener(
206                 new ActionListener JavaDoc() {
207
208                     private int countDown = 10;
209
210                     public void actionPerformed(ActionEvent JavaDoc evt) {
211                         SwingUtilities.invokeLater(
212                             new Runnable JavaDoc() {
213                                 public void run() {
214                                     if (--countDown >= 0) {
215                                         JEditorPane JavaDoc[] panes = ec.getOpenedPanes();
216                                         if (panes != null && panes.length > 0) {
217                                             JumpList.checkAddEntry(panes[0]);
218                                             timer.stop();
219                                         }
220                                     } else {
221                                         timer.stop();
222                                     }
223                                 }
224                             }
225                         );
226                     }
227                 }
228             );
229             timer.start();
230         }
231     }
232
233     /** Merge two string arrays into one. */
234     public static String JavaDoc[] mergeStringArrays(String JavaDoc[] a1, String JavaDoc[] a2) {
235         String JavaDoc[] ret = new String JavaDoc[a1.length + a2.length];
236         for (int i = 0; i < a1.length; i++) {
237             ret[i] = a1[i];
238         }
239         for (int i = 0; i < a2.length; i++) {
240             ret[a1.length + i] = a2[i];
241         }
242         return ret;
243     }
244
245     /**
246      * Gets the mime type of a document. If the mime type can't be determined
247      * this method will return <code>null</code>. This method should work reliably
248      * for Netbeans documents that have their mime type stored in a special
249      * property. For any other documents it will probably just return <code>null</code>.
250      *
251      * @param doc The document to get the mime type for.
252      *
253      * @return The mime type of the document or <code>null</code>.
254      * @see NbEditorDocument#MIME_TYPE_PROP
255      */

256     public static String JavaDoc getMimeType(Document JavaDoc doc) {
257         return (String JavaDoc)doc.getProperty(NbEditorDocument.MIME_TYPE_PROP);
258     }
259
260     /**
261      * Gets the mime type of a document in <code>JTextComponent</code>. If
262      * the mime type can't be determined this method will return <code>null</code>.
263      * It tries to determine the document's mime type first and if that does not
264      * work it uses mime type from the <code>EditorKit</code> attached to the
265      * component.
266      *
267      * @param component The component to get the mime type for.
268      *
269      * @return The mime type of a document opened in the component or <code>null</code>.
270      * @since 1.29
271      */

272     public static String JavaDoc getMimeType(JTextComponent JavaDoc component) {
273         Document JavaDoc doc = component.getDocument();
274         String JavaDoc mimeType = getMimeType(doc);
275         if (mimeType == null) {
276             EditorKit JavaDoc kit = component.getUI().getEditorKit(component);
277             if (kit != null) {
278                 mimeType = kit.getContentType();
279             }
280         }
281         return mimeType;
282     }
283     
284     /** Displays ErrorManager window with the localized message. If bundleKey parameter is not founded in bundle
285      * it is considered as displayable text value. */

286     public static void invalidArgument(String JavaDoc bundleKey) {
287         IllegalArgumentException JavaDoc iae=new IllegalArgumentException JavaDoc("Invalid argument"); //NOI18N
288
Toolkit.getDefaultToolkit().beep();
289         ErrorManager errMan=(ErrorManager)Lookup.getDefault().lookup(ErrorManager.class);
290         
291         if (errMan!=null) {
292             errMan.annotate(iae, ErrorManager.USER, iae.getMessage(), getString(bundleKey), null, null); //NOI18N
293
}
294         throw iae;
295     }
296     
297     private static String JavaDoc getString(String JavaDoc key) {
298         try {
299             return NbBundle.getBundle(NbEditorUtilities.class).getString(key);
300         } catch (MissingResourceException JavaDoc e) {
301             org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, e);
302             return key;
303         }
304     }
305
306
307 }
308
Popular Tags