1 19 20 package org.netbeans.api.languages; 21 22 import java.lang.ref.WeakReference ; 23 import java.util.WeakHashMap ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 import javax.swing.text.AttributeSet ; 27 import javax.swing.text.Document ; 28 29 import org.netbeans.api.languages.ASTNode; 30 import org.netbeans.api.languages.ASTToken; 31 32 33 38 public class Highlighting { 39 40 41 private static Map <Document ,WeakReference <Highlighting>> highlightings = new WeakHashMap <Document ,WeakReference <Highlighting>> (); 42 43 { 44 } 46 47 52 public static Highlighting getHighlighting (Document document) { 53 WeakReference <Highlighting> wr = highlightings.get (document); 54 Highlighting highlighting = wr == null ? null : wr.get (); 55 if (highlighting == null) { 56 highlighting = new Highlighting (); 57 highlightings.put (document, new WeakReference <Highlighting> (highlighting)); 58 } 59 return highlighting; 60 } 61 62 63 64 private Map <ASTNode,AttributeSet > highlights = new HashMap <ASTNode,AttributeSet > (); 65 private Map <Integer ,Map <String ,AttributeSet >> tokens = new HashMap <Integer ,Map <String ,AttributeSet >> (); 66 67 private Highlighting () {} 68 69 75 public void highlight (ASTItem item, AttributeSet as) { 76 if (item instanceof ASTNode) { 77 highlights.put ((ASTNode) item, as); 78 return; 79 } 80 ASTToken token = (ASTToken) item; 81 Integer id = new Integer (token.getOffset ()); 82 Map <String ,AttributeSet > m = tokens.get (id); 83 if (m == null) { 84 m = new HashMap <String ,AttributeSet > (); 85 tokens.put (id, m); 86 } 87 m.put (token.getIdentifier (), as); 88 } 89 90 95 public void removeHighlight (ASTItem item) { 96 if (item instanceof ASTNode) { 97 highlights.remove ((ASTNode) item); 98 return; 99 } 100 ASTToken token = (ASTToken) item; 101 Integer id = new Integer (token.getOffset ()); 102 Map m = (Map ) tokens.get (id); 103 if (m == null) return; 104 m.remove (token.getIdentifier ()); 105 if (m.isEmpty ()) 106 tokens.remove (id); 107 } 108 109 114 public AttributeSet get (ASTItem item) { 115 if (item instanceof ASTNode) 116 return (AttributeSet ) highlights.get ((ASTNode) item); 117 ASTToken token = (ASTToken) item; 118 Integer id = new Integer (token.getOffset ()); 119 Map m = (Map ) tokens.get (id); 120 if (m == null) return null; 121 return (AttributeSet ) m.get (token.getIdentifier ()); 122 } 123 } 124 | Popular Tags |