KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > editor > overridden > AnnotationsHolder


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.java.editor.overridden;
20
21 import java.beans.PropertyChangeEvent JavaDoc;
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import javax.swing.SwingUtilities JavaDoc;
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 /**
37  *
38  * @author Jan Lahoda
39  */

40 public class AnnotationsHolder implements PropertyChangeListener JavaDoc {
41     
42     private static final Map JavaDoc<FileObject, AnnotationsHolder> file2Annotations = new HashMap JavaDoc<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 JavaDoc 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 JavaDoc<IsOverriddenAnnotation>();
76         
77         ec.addPropertyChangeListener(this);
78         
79         SwingUtilities.invokeLater(new Runnable JavaDoc() {
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 JavaDoc 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             //reset:
99
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 JavaDoc<IsOverriddenAnnotation> annotations;
109     
110     public synchronized void setNewAnnotations(List JavaDoc<IsOverriddenAnnotation> as) {
111         final List JavaDoc<IsOverriddenAnnotation> toRemove = new ArrayList JavaDoc<IsOverriddenAnnotation>(annotations);
112         final List JavaDoc<IsOverriddenAnnotation> toAdd = new ArrayList JavaDoc<IsOverriddenAnnotation>(as);
113         
114         annotations.clear();
115         annotations.addAll(as);
116         
117         Runnable JavaDoc doAttachDetach = new Runnable JavaDoc() {
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 JavaDoc<IsOverriddenAnnotation> getAnnotations() {
137         return new ArrayList JavaDoc<IsOverriddenAnnotation>(annotations);
138     }
139 }
140
Popular Tags