1 19 20 package org.netbeans.modules.tasklist.suggestions; 21 22 import java.util.List ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.beans.PropertyChangeEvent ; 28 import java.beans.PropertyChangeListener ; 29 import javax.swing.text.Document ; 30 import javax.swing.event.DocumentListener ; 31 import javax.swing.event.DocumentEvent ; 32 import org.netbeans.modules.tasklist.client.SuggestionPerformer; 33 import org.netbeans.modules.tasklist.client.SuggestionManager; 34 import org.openide.cookies.EditorCookie; 35 import org.openide.ErrorManager; 36 import org.openide.loaders.DataObject; 37 import org.openide.nodes.Node; 38 import org.netbeans.modules.tasklist.core.*; 39 import org.openide.text.CloneableEditorSupport; 40 41 101 102 class SuggestionCache implements DocumentListener , PropertyChangeListener { 103 104 private HashMap map = null; 105 106 125 public void add(Document doc, DataObject dobj, List suggestions) { 126 if (map == null) { 127 map = new HashMap (60); } 129 130 doc.removeDocumentListener(this); doc.addDocumentListener(this); 132 133 EditorCookie.Observable observable = 134 (EditorCookie.Observable)dobj.getCookie( 135 EditorCookie.Observable.class); 136 if (observable != null) { 137 observable.removePropertyChangeListener(this); observable.addPropertyChangeListener(this); 139 } 140 Entry entry = new Entry(doc, suggestions, observable); 141 map.put(doc, entry); 142 } 143 144 151 public List lookup(Document doc) { 152 if (map == null) { 153 return null; 154 } 155 Entry entry = (Entry)map.get(doc); 156 if (entry == null) { 157 return null; 158 } 159 return entry.list; 160 } 161 162 163 public void flush() { 164 if (map == null) { 165 return; 166 } 167 map.clear(); 168 } 169 170 175 public void remove(Document doc) { 176 Entry entry = (Entry)map.remove(doc); 177 if (entry != null) { 178 doc.removeDocumentListener(this); 180 if (entry.listener != null) { 181 entry.listener.removePropertyChangeListener(this); 182 } 183 } 184 } 185 186 187 public void propertyChange(PropertyChangeEvent ev) { 188 String prop = ev.getPropertyName(); 189 if (prop.equals(EditorCookie.Observable.PROP_DOCUMENT)) { 190 try { 191 EditorCookie ec = (EditorCookie)ev.getSource(); 192 Document doc = ec.getDocument(); 193 invalidate(doc); 194 } catch (Exception e) { 195 ErrorManager.getDefault().log("ev.getSource().getClass() = " + ev.getSource().getClass().getName()); 196 ErrorManager.getDefault(). 197 notify(ErrorManager.INFORMATIONAL, e); 198 } 199 } 200 } 201 202 203 private void invalidate(Document doc) { 204 remove(doc); 205 } 206 207 public void changedUpdate(DocumentEvent e) { 209 } 211 212 public void insertUpdate(DocumentEvent e) { 213 invalidate(e.getDocument()); 214 } 215 216 public void removeUpdate(DocumentEvent e) { 217 invalidate(e.getDocument()); 218 } 219 220 private static class Entry { 221 List list; 222 Document doc; 223 EditorCookie.Observable listener; 224 225 Entry(Document doc, List list, EditorCookie.Observable listener) { 226 this.list = list; 227 this.doc = doc; 228 this.listener = listener; 229 } 230 } 231 } 232 | Popular Tags |