1 19 package org.netbeans.modules.editor.highlights; 20 21 import java.beans.PropertyChangeEvent ; 22 import java.beans.PropertyChangeListener ; 23 import java.lang.ref.Reference ; 24 import java.lang.ref.WeakReference ; 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 import java.util.WeakHashMap ; 31 import javax.swing.event.ChangeEvent ; 32 import javax.swing.event.ChangeListener ; 33 import javax.swing.text.Document ; 34 import javax.swing.text.JTextComponent ; 35 import org.netbeans.editor.EditorUI; 36 import org.netbeans.editor.Registry; 37 import org.netbeans.editor.Utilities; 38 import org.netbeans.modules.editor.highlights.spi.*; 39 import org.openide.filesystems.FileObject; 40 import org.openide.loaders.DataObject; 41 import org.openide.util.WeakSet; 42 43 47 public final class HighlighterImpl implements PropertyChangeListener , ChangeListener { 48 49 private static HighlighterImpl INSTANCE = new HighlighterImpl(); 50 51 public static HighlighterImpl getDefault() { 52 return INSTANCE; 53 } 54 55 private Map comp2FO; 56 private Map fo2Comp; 57 58 private Map comp2Layer; 59 60 public HighlighterImpl() { 61 comp2FO = new WeakHashMap (); 62 fo2Comp = new WeakHashMap (); 63 comp2Layer = new WeakHashMap (); 64 } 65 66 private HighlightLayer getLayer(JTextComponent c) { 67 Reference r = (Reference ) comp2Layer.get(c); 68 69 if (r == null) 70 return null; 71 72 return (HighlightLayer) r.get(); 73 } 74 75 synchronized void assureRegistered(JTextComponent c) { 76 if (c == null || getLayer(c) != null) 77 return ; 78 79 comp2FO.put(c, null); 80 c.addPropertyChangeListener(this); 81 updateFileObjectMapping(c); 82 83 HighlightLayer layer = new HighlightLayer(); 84 85 comp2Layer.put(c, new WeakReference (layer)); 86 87 EditorUI eui = Utilities.getEditorUI(c); 88 89 if (eui != null) 90 eui.addLayer(layer, HighlightLayer.VISIBILITY); 91 } 92 93 private synchronized void updateFileObjectMapping(JTextComponent c) { 94 Document doc = c.getDocument(); 95 Object stream = doc.getProperty(Document.StreamDescriptionProperty); 96 97 FileObject old = (FileObject) comp2FO.put(c, null); 98 99 if (old != null) { 100 Collection components = (Collection ) fo2Comp.get(old); 101 102 if (components != null) { 103 components.remove(c); 104 } 105 } 106 107 if (stream != null && stream instanceof DataObject) { 108 FileObject fo = ((DataObject) stream).getPrimaryFile(); 109 110 comp2FO.put(c, fo); 111 getComponents(fo).add(c); 112 } 113 114 } 115 116 public void propertyChange(PropertyChangeEvent evt) { 117 if ("document".equals(evt.getPropertyName())) { updateFileObjectMapping((JTextComponent ) evt.getSource()); 119 } 120 } 121 122 private Collection getComponents(FileObject fo) { 123 Collection components = (Collection ) fo2Comp.get(fo); 124 125 if (components == null) { 126 fo2Comp.put(fo, components = new WeakSet()); 127 } 128 129 return components; 130 } 131 132 public synchronized void setHighlights(FileObject fo, String type, Collection highlights) { 133 for (Iterator i = getComponents(fo).iterator(); i.hasNext(); ) { 135 JTextComponent c = (JTextComponent ) i.next(); 136 HighlightLayer layer = getLayer(c); 137 138 if (layer == null) 139 continue; 140 141 layer.setHighlights(type, highlights); 142 143 c.repaint(); } 145 } 146 147 public void stateChanged(ChangeEvent e) { 148 assureRegistered(Registry.getMostActiveComponent()); 149 } 150 151 } 152 | Popular Tags |