1 19 20 package org.netbeans.api.languages; 21 22 import javax.swing.text.Document ; 23 import javax.swing.text.JTextComponent ; 24 import org.netbeans.api.lexer.TokenSequence; 25 import org.netbeans.modules.editor.NbEditorUtilities; 26 import org.openide.cookies.EditorCookie; 27 import org.openide.loaders.DataObject; 28 29 34 public abstract class Context { 35 36 37 42 public abstract JTextComponent getJTextComponent (); 43 44 49 public abstract Document getDocument (); 50 51 56 public abstract TokenSequence getTokenSequence (); 57 58 63 public static Context create (Document doc, TokenSequence tokenSequence) { 64 return new CookieImpl (doc, tokenSequence); 65 } 66 67 private static class CookieImpl extends Context { 68 69 private Document doc; 70 private JTextComponent component; 71 private TokenSequence tokenSequence; 72 73 CookieImpl ( 74 Document doc, 75 TokenSequence tokenSequence 76 ) { 77 this.doc = doc; 78 this.tokenSequence = tokenSequence; 79 } 80 81 public JTextComponent getJTextComponent () { 82 if (component == null) { 83 DataObject dob = NbEditorUtilities.getDataObject (doc); 84 EditorCookie ec = (EditorCookie) dob.getLookup ().lookup (EditorCookie.class); 85 if (ec.getOpenedPanes ().length > 0) 86 component = ec.getOpenedPanes () [0]; 87 } 88 return component; 89 } 90 91 public Document getDocument () { 92 return doc; 93 } 94 95 public TokenSequence getTokenSequence () { 96 return tokenSequence; 97 } 98 } 99 } 100 101 102 | Popular Tags |