KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > internalapi > GuardedQuery


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.javacore.internalapi;
21
22 import java.io.IOException JavaDoc;
23 import javax.swing.text.StyledDocument JavaDoc;
24 import org.netbeans.jmi.javamodel.Resource;
25 import org.netbeans.modules.javacore.FileGuardedResolver;
26 import org.netbeans.modules.javacore.jmiimpl.javamodel.ResourceImpl;
27 import org.openide.ErrorManager;
28 import org.openide.cookies.EditorCookie;
29 import org.openide.loaders.DataObject;
30 import org.openide.text.PositionBounds;
31
32 /**
33  *
34  * @author Jan Becicka
35  */

36 public final class GuardedQuery {
37
38     private static GuardedResolver fileResolver = null;
39     private static GuardedResolver documentResolver = null;
40
41     private GuardedQuery() {
42     }
43     
44     public static boolean isSectionGuarded(Resource res, PositionBounds pos) {
45         ResourceImpl resource = (ResourceImpl) res;
46         DataObject dobj = JavaMetamodel.getManager().getDataObject(resource);
47         EditorCookie editor = (EditorCookie) dobj.getCookie(EditorCookie.class);
48         StyledDocument JavaDoc doc = null;
49         if (editor != null) {
50             doc = editor.getDocument();
51             if (doc == null && resource.getParser().isFromDocument()) {
52                 try {
53                     doc = editor.openDocument();
54                 } catch (IOException JavaDoc ex) {
55                     throw (RuntimeException JavaDoc) ErrorManager.getDefault().annotate(new RuntimeException JavaDoc(), ex);
56                 }
57             }
58         }
59         if (doc != null) {
60             return getDocumentGuardedResolver().isSectionGuarded(resource, pos);
61         } else {
62             return getFileGuardedResolver().isSectionGuarded(resource, pos);
63         }
64     }
65     
66     private static GuardedResolver getDocumentGuardedResolver() {
67         if (documentResolver == null)
68             return getFileGuardedResolver();
69         return documentResolver;
70     }
71     
72     private static GuardedResolver getFileGuardedResolver() {
73         return FileGuardedResolver.getDefault();
74     }
75     
76     public static void setDocumentGuardedResolver(GuardedResolver resolver) {
77         documentResolver = resolver;
78     }
79 }
80
Popular Tags