KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > exceptions > Collector


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
20 package org.netbeans.modules.exceptions;
21 import java.awt.EventQueue JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.logging.Handler JavaDoc;
25 import java.util.logging.Level JavaDoc;
26 import java.util.logging.LogRecord JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28 import org.netbeans.modules.exceptions.listeners.WinFocusListener;
29 import org.netbeans.modules.exceptions.listeners.TopComponentListener;
30 import org.netbeans.modules.exceptions.listeners.UIHandler;
31 import org.netbeans.modules.exceptions.listeners.WMPropertyListener;
32 import org.openide.windows.WindowManager;
33
34 /**
35  *
36  * @author jindra
37  */

38 public class Collector {
39     public static final int MAX_LOGS = 100;
40     private static LinkedList JavaDoc<LogRecord JavaDoc> queue = new LinkedList JavaDoc<LogRecord JavaDoc>();
41     private static TopComponentListener tcListener;
42     private static WMPropertyListener wmListener;
43     private static WinFocusListener focListener;
44     private static Handler JavaDoc uiHandler;
45     private static Collector myInstance;
46     private final static String JavaDoc prop_name = "netbeans.exception.report.min.level"; // NOI18N
47
private String JavaDoc old_property;
48     /** Creates a new instance of Collector */
49     private Collector() {
50
51     }
52     
53     public static synchronized Collector getDefault(){
54         if (myInstance == null){
55             myInstance = new Collector();
56         }
57         return myInstance;
58     }
59     
60     public void start(){
61         //disabling impicit error manager
62
old_property = System.getProperty(prop_name);
63         System.setProperty(prop_name, "1001"); // NOI18N
64

65         Logger.getLogger("").addHandler(ExceptionsHandler.getInstance()); // NOI18N
66

67         Logger JavaDoc log = Logger.getLogger("org.netbeans.ui"); // NOI18N
68
log.setLevel(Level.FINEST);
69         uiHandler = new UIHandler(this);
70         log.addHandler(uiHandler);
71         
72         // Lookup.Template/*GENERICS<Activated>GENERICS*/ temp = new Lookup.Template/*GENERICS<Activated>GENERICS*/(NbErrorManager.class);
73
// Lookup.Result/*GENERICS<Activated>GENERICS*/ res = Lookup.getDefault().lookup(temp);
74

75         // Top Component Listener
76
tcListener = new TopComponentListener();
77         try {
78             // WindowManager listener
79
wmListener = new WMPropertyListener();
80         } catch (IOException JavaDoc ex) {
81             java.util.logging.Logger.getLogger(Collector.class.getName()).log(java.util.logging.Level.SEVERE,
82                     ex.getMessage(), ex);
83         }
84         // Focus Listener
85
focListener = new WinFocusListener();
86         final WinFocusListener fListener = focListener;
87         final WindowManager wManager = WindowManager.getDefault();
88         
89         wManager.addPropertyChangeListener(wmListener);
90         wManager.getRegistry().addPropertyChangeListener(tcListener);
91         
92         EventQueue.invokeLater(new Runnable JavaDoc() {
93             
94             public void run() {
95                 wManager.getMainWindow().addWindowFocusListener(fListener);
96             }
97         });
98     }
99     
100     public synchronized void write(LogRecord JavaDoc rec){
101         if (queue.size() > MAX_LOGS) {
102             queue.remove();
103         }
104         queue.add(rec);
105     }
106     
107     public LinkedList JavaDoc<LogRecord JavaDoc> getQueue(){
108         return queue;
109     }
110     
111     public void close(){
112         
113         Logger.getLogger("org.netbeans.ui").removeHandler(uiHandler); // NOI18N
114

115         Logger.getLogger("").removeHandler(ExceptionsHandler.getInstance()); // NOI18N
116
if (old_property!= null) {
117             System.setProperty(prop_name, old_property);
118         }
119     }
120 }
121
Popular Tags