1 19 20 package org.netbeans.modules.javacore.internalapi; 21 22 import java.io.IOException ; 23 import javax.swing.text.StyledDocument ; 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 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 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 ex) { 55 throw (RuntimeException ) ErrorManager.getDefault().annotate(new RuntimeException (), 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 |