1 19 package org.netbeans.modules.java.editor.overridden; 20 21 import java.beans.PropertyChangeEvent ; 22 import java.beans.PropertyChangeListener ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.Collections ; 26 import java.util.HashMap ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.logging.Level ; 30 import javax.swing.SwingUtilities ; 31 import org.netbeans.api.timers.TimesCollector; 32 import org.openide.cookies.EditorCookie; 33 import org.openide.filesystems.FileObject; 34 import org.openide.loaders.DataObject; 35 36 40 public class AnnotationsHolder implements PropertyChangeListener { 41 42 private static final Map <FileObject, AnnotationsHolder> file2Annotations = new HashMap <FileObject, AnnotationsHolder>(); 43 44 public static synchronized AnnotationsHolder get(FileObject file) { 45 AnnotationsHolder a = file2Annotations.get(file); 46 47 if (a != null) { 48 return a; 49 } 50 51 try { 52 DataObject od = DataObject.find(file); 53 EditorCookie.Observable ec = od.getCookie(EditorCookie.Observable.class); 54 55 if (ec == null) { 56 return null; 57 } 58 59 file2Annotations.put(file, a = new AnnotationsHolder(file, ec)); 60 61 return a; 62 } catch (IOException ex) { 63 IsOverriddenAnnotationHandler.LOG.log(Level.INFO, null, ex); 64 65 return null; 66 } 67 } 68 69 private final FileObject file; 70 private final EditorCookie.Observable ec; 71 72 private AnnotationsHolder(FileObject file, EditorCookie.Observable ec) { 73 this.file = file; 74 this.ec = ec; 75 this.annotations = new ArrayList <IsOverriddenAnnotation>(); 76 77 ec.addPropertyChangeListener(this); 78 79 SwingUtilities.invokeLater(new Runnable () { 80 public void run() { 81 checkForReset(); 82 } 83 }); 84 85 TimesCollector.getDefault().reportReference(file, AnnotationsHolder.class.getName(), "[M] Overridden AnnotationsHolder", this); 86 } 87 88 public void propertyChange(PropertyChangeEvent evt) { 89 if (EditorCookie.Observable.PROP_OPENED_PANES.endsWith(evt.getPropertyName()) || evt.getPropertyName() == null) { 90 checkForReset(); 91 } 92 } 93 94 private void checkForReset() { 95 assert SwingUtilities.isEventDispatchThread(); 96 97 if (ec.getOpenedPanes() == null) { 98 synchronized (AnnotationsHolder.class) { 100 file2Annotations.remove(file); 101 } 102 103 setNewAnnotations(Collections.<IsOverriddenAnnotation>emptyList()); 104 ec.removePropertyChangeListener(this); 105 } 106 } 107 108 private final List <IsOverriddenAnnotation> annotations; 109 110 public synchronized void setNewAnnotations(List <IsOverriddenAnnotation> as) { 111 final List <IsOverriddenAnnotation> toRemove = new ArrayList <IsOverriddenAnnotation>(annotations); 112 final List <IsOverriddenAnnotation> toAdd = new ArrayList <IsOverriddenAnnotation>(as); 113 114 annotations.clear(); 115 annotations.addAll(as); 116 117 Runnable doAttachDetach = new Runnable () { 118 public void run() { 119 for (IsOverriddenAnnotation a : toRemove) { 120 a.detachImpl(); 121 } 122 123 for (IsOverriddenAnnotation a : toAdd) { 124 a.attach(); 125 } 126 } 127 }; 128 129 if (SwingUtilities.isEventDispatchThread()) { 130 doAttachDetach.run(); 131 } else { 132 SwingUtilities.invokeLater(doAttachDetach); 133 } 134 } 135 136 public synchronized List <IsOverriddenAnnotation> getAnnotations() { 137 return new ArrayList <IsOverriddenAnnotation>(annotations); 138 } 139 } 140 | Popular Tags |