KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > uihandlerforprojects > OpenPrjLogging


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.uihandlerforprojects;
20
21 import java.beans.PropertyChangeEvent JavaDoc;
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.LogRecord JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31 import org.netbeans.api.project.Project;
32 import org.netbeans.api.project.ui.OpenProjects;
33 import org.netbeans.modules.uihandler.api.Activated;
34 import org.openide.util.NbBundle;
35 import org.openide.util.WeakListeners;
36 import org.openide.util.WeakSet;
37
38 /**
39  *
40  */

41 public class OpenPrjLogging implements Activated, PropertyChangeListener JavaDoc {
42     /** to kepp this object in memory */
43     private static OpenPrjLogging INSTANCE;
44     
45     private Set JavaDoc<Project> previous = new WeakSet<Project>();
46     private Logger JavaDoc logger;
47     /** Creates a new instance of OpenPrjLogging */
48     public OpenPrjLogging() {
49         INSTANCE = this;
50     }
51     
52     public void activated(Logger JavaDoc uiLogger) {
53         logger = Logger.getLogger(uiLogger.getName() + ".projects"); // NOI18N
54

55         LogRecord JavaDoc[] rec;
56         
57         Project[] arr = OpenProjects.getDefault().getOpenProjects();
58         synchronized (this) {
59             previous.clear();
60             previous.addAll(Arrays.asList(arr));
61             OpenProjects.getDefault().addPropertyChangeListener(WeakListeners.propertyChange(this, OpenProjects.getDefault()));
62             
63             rec = createRecord("UI_OPEN_PROJECTS", previous); // NOI18N
64
}
65         
66         log(rec);
67     }
68     
69     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
70         LogRecord JavaDoc[] addedRec, removedRec;
71         
72         Project[] arr = OpenProjects.getDefault().getOpenProjects();
73         synchronized (this) {
74             Set JavaDoc<Project> added = new HashSet JavaDoc<Project>();
75             added.addAll(Arrays.asList(arr));
76             added.removeAll(previous);
77             
78             previous.removeAll(Arrays.asList(arr));
79             
80             addedRec = createRecord("UI_OPEN_PROJECTS", added); // NOI18N
81
removedRec = createRecord("UI_CLOSED_PROJECTS", previous); // NOI18N
82

83             previous.clear();
84             previous.addAll(Arrays.asList(arr));
85         }
86         
87         log(addedRec);
88         log(removedRec);
89     }
90     
91     private LogRecord JavaDoc[] createRecord(String JavaDoc msg, Set JavaDoc<Project> projects) {
92         if (projects.isEmpty()) {
93             return null;
94         }
95         
96         Map JavaDoc<String JavaDoc,int[]> counts = new HashMap JavaDoc<String JavaDoc,int[]>();
97         for (Project p : projects) {
98             String JavaDoc n = p.getClass().getName();
99             int[] cnt = counts.get(n);
100             if (cnt == null) {
101                 cnt = new int[1];
102                 counts.put(n, cnt);
103             }
104             cnt[0]++;
105         }
106         
107         LogRecord JavaDoc[] arr = new LogRecord JavaDoc[counts.size()];
108         int i = 0;
109         for (Map.Entry JavaDoc<String JavaDoc,int[]> entry : counts.entrySet()) {
110             LogRecord JavaDoc rec = new LogRecord JavaDoc(Level.CONFIG, msg);
111             rec.setParameters(new Object JavaDoc[] { entry.getKey(), afterLastDot(entry.getKey()), entry.getValue()[0] });
112             rec.setLoggerName(logger.getName());
113             rec.setResourceBundle(NbBundle.getBundle(OpenPrjLogging.class));
114             rec.setResourceBundleName(OpenPrjLogging.class.getPackage().getName()+".Bundle");
115             
116             arr[i++] = rec;
117         }
118         
119         return arr;
120     }
121     
122     private void log(LogRecord JavaDoc[] arr) {
123         if (arr == null) {
124             return;
125         }
126         for (LogRecord JavaDoc r : arr) {
127             logger.log(r);
128         }
129     }
130     private static String JavaDoc afterLastDot(String JavaDoc s) {
131         int index = s.lastIndexOf('.');
132         if (index == -1) {
133             return s;
134         }
135         return s.substring(index + 1);
136     }
137 }
138
Popular Tags