KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > timers > TimesCollectorPeer


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.timers;
20
21 import java.beans.PropertyChangeListener JavaDoc;
22 import java.beans.PropertyChangeSupport JavaDoc;
23 import java.lang.ref.Reference JavaDoc;
24 import java.lang.ref.WeakReference JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.LinkedHashMap JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.WeakHashMap JavaDoc;
31 import javax.swing.event.ChangeEvent JavaDoc;
32 import javax.swing.event.ChangeListener JavaDoc;
33 import org.openide.filesystems.FileChangeAdapter;
34 import org.openide.filesystems.FileEvent;
35 import org.openide.filesystems.FileObject;
36 import org.openide.util.RequestProcessor;
37 import org.openide.util.Utilities;
38
39 /**
40  *
41  * @author Jan Lahoda
42  */

43 public final class TimesCollectorPeer {
44     
45     private List JavaDoc<Reference JavaDoc<FileObject>> files;
46     private Map JavaDoc<FileObject, Map JavaDoc<String JavaDoc, Description>> fo2Key2Desc;
47     
48     private static final TimesCollectorPeer INSTANCE = new TimesCollectorPeer();
49     
50     private PropertyChangeSupport JavaDoc pcs;
51     
52     public static TimesCollectorPeer getDefault() {
53         return INSTANCE;
54     }
55     
56     /** Creates a new instance of TimesCollectorPeer */
57     private TimesCollectorPeer() {
58         files = new ArrayList JavaDoc<Reference JavaDoc<FileObject>>();
59         fo2Key2Desc = new WeakHashMap JavaDoc<FileObject, Map JavaDoc<String JavaDoc, Description>>();
60         
61         pcs = new PropertyChangeSupport JavaDoc(this);
62     }
63     
64     public void reportTime(FileObject fo, String JavaDoc key, String JavaDoc message, long time) {
65         Map JavaDoc<String JavaDoc, Description> key2Desc = getKey2Desc(fo);
66         Description desc = new Description(message, time);
67         
68         key2Desc.put(key, desc);
69         
70         pcs.firePropertyChange("PROP", fo, key);
71     }
72     
73     public void reportReference( FileObject fo, String JavaDoc key, String JavaDoc message, Object JavaDoc object ) {
74         Map JavaDoc<String JavaDoc, Description> key2Desc = getKey2Desc(fo);
75         
76         // Little bit more complicated here
77
Description d = key2Desc.get( key );
78         assert d == null || d instanceof ObjectCountDescripton : "Illegal state";
79         
80         ObjectCountDescripton ocd = d == null ? new ObjectCountDescripton( this, fo, key, message ) : (ObjectCountDescripton)d;
81         ocd.add( object );
82         
83         key2Desc.put(key, ocd);
84         pcs.firePropertyChange("PROP", fo, key);
85     }
86     
87     private synchronized Map JavaDoc<String JavaDoc, Description> getKey2Desc(final FileObject fo) {
88         Map JavaDoc<String JavaDoc, Description> result = fo2Key2Desc.get(fo);
89         
90         if (result == null) {
91             files.add(new CleanableWeakReference<FileObject>(fo));
92             fo2Key2Desc.put(fo, result = new LinkedHashMap JavaDoc<String JavaDoc, Description>());
93             pcs.firePropertyChange("fos", null, fo);
94             if (fo != null) {
95                 fo.addFileChangeListener(new FileChangeAdapter() {
96                     public void fileDeleted(FileEvent ev) {
97                         for (Reference JavaDoc<FileObject> r : files) {
98                             if (r.get() == fo) {
99                                 files.remove(r);
100                                 break;
101                             }
102                         }
103                         fo2Key2Desc.remove(fo);
104                         pcs.firePropertyChange("fos", null, null);
105                     }
106                 });
107             }
108         }
109         
110          return result;
111     }
112     
113     public Description getDescription(FileObject fo, String JavaDoc key) {
114         return getKey2Desc(fo).get(key);
115     }
116     
117     public Collection JavaDoc<String JavaDoc> getKeysForFile(FileObject fo) {
118         return getKey2Desc(fo).keySet();
119     }
120     
121     public Collection JavaDoc<FileObject> getFiles() {
122         List JavaDoc<FileObject> result = new ArrayList JavaDoc<FileObject>();
123         
124         for (Reference JavaDoc<FileObject> r : files) {
125             FileObject f = r.get();
126             
127             if (f != null)
128                 result.add(f);
129         }
130         return result;
131     }
132     
133     public void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
134         pcs.addPropertyChangeListener(l);
135     }
136     
137     public void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
138         pcs.removePropertyChangeListener(l);
139     }
140
141     public void select(FileObject fo) {
142         getKey2Desc(fo);
143         pcs.firePropertyChange("selected", null, fo);
144     }
145
146     public static class Description {
147         private String JavaDoc message;
148         private long time;
149         
150         public Description(String JavaDoc message, long time) {
151             this.message = message;
152             this.time = time;
153         }
154
155         public String JavaDoc getMessage() {
156             return message;
157         }
158
159         public long getTime() {
160             return time;
161         }
162         
163     }
164     
165     public static class ObjectCountDescripton extends Description implements ChangeListener JavaDoc {
166         
167         private TimesCollectorPeer tcp;
168         private Reference JavaDoc<FileObject> fo;
169         private String JavaDoc key;
170         private InstanceWatcher iw = new InstanceWatcher();
171         
172         
173         public ObjectCountDescripton( TimesCollectorPeer tcp, FileObject fo, String JavaDoc key, String JavaDoc message ) {
174             super( message, 0 );
175             this.tcp = tcp;
176             this.fo = new WeakReference JavaDoc<FileObject>(fo);
177             this.key = key;
178             iw.addChangeListener( this );
179         }
180
181         public long getTime( ) {
182             return iw.size();
183         }
184         
185         public Collection JavaDoc getInstances() {
186             return iw.getInstances();
187         }
188         
189         private void add( Object JavaDoc o ) {
190             iw.add( o );
191         }
192         
193         public void stateChanged(ChangeEvent JavaDoc e) {
194             FileObject file = fo.get();
195             
196             if (file != null) {
197                 tcp.pcs.firePropertyChange("PROP", file, key);
198             }
199         }
200         
201     }
202     
203     private class CleanableWeakReference<T> extends WeakReference JavaDoc<T> implements Runnable JavaDoc {
204         
205         public CleanableWeakReference(T o) {
206             super(o, Utilities.activeReferenceQueue());
207         }
208
209         public void run() {
210             files.remove(this);
211             RequestProcessor.getDefault().post(new Runnable JavaDoc() {
212                 public void run() {
213                     pcs.firePropertyChange("fos", null, null);
214                 }
215             });
216         }
217         
218     }
219 }
220
Popular Tags