1 19 package org.netbeans.modules.uihandlerforprojects; 20 21 import java.beans.PropertyChangeEvent ; 22 import java.beans.PropertyChangeListener ; 23 import java.util.Arrays ; 24 import java.util.HashMap ; 25 import java.util.HashSet ; 26 import java.util.Map ; 27 import java.util.Set ; 28 import java.util.logging.Level ; 29 import java.util.logging.LogRecord ; 30 import java.util.logging.Logger ; 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 41 public class OpenPrjLogging implements Activated, PropertyChangeListener { 42 43 private static OpenPrjLogging INSTANCE; 44 45 private Set <Project> previous = new WeakSet<Project>(); 46 private Logger logger; 47 48 public OpenPrjLogging() { 49 INSTANCE = this; 50 } 51 52 public void activated(Logger uiLogger) { 53 logger = Logger.getLogger(uiLogger.getName() + ".projects"); 55 LogRecord [] 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); } 65 66 log(rec); 67 } 68 69 public void propertyChange(PropertyChangeEvent evt) { 70 LogRecord [] addedRec, removedRec; 71 72 Project[] arr = OpenProjects.getDefault().getOpenProjects(); 73 synchronized (this) { 74 Set <Project> added = new HashSet <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); removedRec = createRecord("UI_CLOSED_PROJECTS", previous); 83 previous.clear(); 84 previous.addAll(Arrays.asList(arr)); 85 } 86 87 log(addedRec); 88 log(removedRec); 89 } 90 91 private LogRecord [] createRecord(String msg, Set <Project> projects) { 92 if (projects.isEmpty()) { 93 return null; 94 } 95 96 Map <String ,int[]> counts = new HashMap <String ,int[]>(); 97 for (Project p : projects) { 98 String 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 [] arr = new LogRecord [counts.size()]; 108 int i = 0; 109 for (Map.Entry <String ,int[]> entry : counts.entrySet()) { 110 LogRecord rec = new LogRecord (Level.CONFIG, msg); 111 rec.setParameters(new Object [] { 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 [] arr) { 123 if (arr == null) { 124 return; 125 } 126 for (LogRecord r : arr) { 127 logger.log(r); 128 } 129 } 130 private static String afterLastDot(String 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 |