Source Code Cross Referenced for LoggingUtils.java in  » Swing-Library » jgoodies-data-binding » com » jgoodies » binding » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Swing Library » jgoodies data binding » com.jgoodies.binding.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 2002-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003:         *
004:         * Redistribution and use in source and binary forms, with or without
005:         * modification, are permitted provided that the following conditions are met:
006:         *
007:         *  o Redistributions of source code must retain the above copyright notice,
008:         *    this list of conditions and the following disclaimer.
009:         *
010:         *  o Redistributions in binary form must reproduce the above copyright notice,
011:         *    this list of conditions and the following disclaimer in the documentation
012:         *    and/or other materials provided with the distribution.
013:         *
014:         *  o Neither the name of JGoodies Karsten Lentzsch nor the names of
015:         *    its contributors may be used to endorse or promote products derived
016:         *    from this software without specific prior written permission.
017:         *
018:         * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019:         * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020:         * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021:         * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022:         * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023:         * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024:         * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025:         * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026:         * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027:         * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028:         * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029:         */
030:
031:        package com.jgoodies.binding.util;
032:
033:        import java.beans.PropertyChangeEvent;
034:        import java.beans.PropertyChangeListener;
035:        import java.util.logging.Level;
036:        import java.util.logging.Logger;
037:
038:        import com.jgoodies.binding.beans.BeanUtils;
039:
040:        /**
041:         * Assists in logging changes in bound bean properties.
042:         *
043:         * @author Andrej Golovnin
044:         * @author Karsten Lentzsch
045:         * @version $Revision: 1.5 $
046:         *
047:         * @see Logger
048:         */
049:        public final class LoggingUtils {
050:
051:            private static final Logger LOGGER = Logger
052:                    .getLogger(LoggingUtils.class.getName());
053:
054:            private static Level defaultLevel = Level.FINE;
055:
056:            private LoggingUtils() {
057:                // Override default constructor; prevents instantiation.
058:            }
059:
060:            /**
061:             * Sets the default log level to be used when logging PropertyChangeEvents.
062:             * The initial default level is {@link Level#FINE}.
063:             *
064:             * @param level   the default level to be used if no custom level
065:             *     has been provided
066:             *
067:             * @throws NullPointerException   if the new defaultLevel is null
068:             */
069:            public static void setDefaultLevel(Level level) {
070:                if (level == null) {
071:                    throw new NullPointerException(
072:                            "The log level must not be null.");
073:                }
074:                LoggingUtils.defaultLevel = level;
075:            }
076:
077:            /**
078:             * Registers a PropertyChangeListener with the specified bean
079:             * that logs all PropertyChangeEvents fired by this bean
080:             * using the default Logger and default log level.
081:             *
082:             * @param bean   the bean to log PropertyChangeEvents from
083:             *
084:             * @throws NullPointerException if the bean is null
085:             */
086:            public static void logPropertyChanges(Object bean) {
087:                logPropertyChanges(bean, LOGGER);
088:            }
089:
090:            /**
091:             * Registers a PropertyChangeListener with the specified bean, which logs
092:             * all PropertyChangeEvents fired by the given bean using the specified
093:             * Logger and the default log level.
094:             *
095:             * @param bean   the bean to log PropertyChangeEvents from
096:             * @param logger the Logger to be used to log PropertyChangeEvents
097:             *
098:             * @throws NullPointerException if the bean or logger is null
099:             */
100:            public static void logPropertyChanges(Object bean, Logger logger) {
101:                logPropertyChanges(bean, logger, defaultLevel);
102:            }
103:
104:            /**
105:             * Registers a PropertyChangeListener with the specified bean, which logs
106:             * all PropertyChangeEvents fired by the given bean using the specified
107:             * Logger and log level.
108:             *
109:             * @param bean   the bean to log PropertyChangeEvents from
110:             * @param logger the Logger to be used to log PropertyChangeEvents
111:             * @param level  the log level
112:             *
113:             * @throws NullPointerException if the bean, logger, or level is null
114:             */
115:            public static void logPropertyChanges(Object bean, Logger logger,
116:                    Level level) {
117:                BeanUtils.addPropertyChangeListener(bean, new LogHandler(
118:                        logger, level));
119:            }
120:
121:            /**
122:             * Registers a named PropertyChangeListener with the specified bean,
123:             * which logs all PropertyChangeEvents of the given property using
124:             * the default Logger and default log level.
125:             *
126:             * @param bean          the bean to log PropertyChangeEvents from
127:             * @param propertyName  the name of the property which PropertyChangeEvents
128:             *                      should be logged
129:             *
130:             * @throws NullPointerException if the bean or propertyName is null
131:             */
132:            public static void logPropertyChanges(Object bean,
133:                    String propertyName) {
134:                logPropertyChanges(bean, propertyName, LOGGER);
135:            }
136:
137:            /**
138:             * Registers a named PropertyChangeListener with the specified bean,
139:             * which logs all PropertyChangeEvents of the given property using
140:             * the specified Logger and the default log level.
141:             *
142:             * @param bean          the bean to log PropertyChangeEvents from
143:             * @param propertyName  the name of the property which PropertyChangeEvents
144:             *                      should be logged
145:             * @param logger        the Logger to be used to log PropertyChangeEvents
146:             *
147:             * @throws NullPointerException if the bean, propertyName, or logger is null
148:             */
149:            public static void logPropertyChanges(Object bean,
150:                    String propertyName, Logger logger) {
151:                logPropertyChanges(bean, propertyName, logger, defaultLevel);
152:            }
153:
154:            /**
155:             * Registers a named PropertyChangeListener with the specified bean,
156:             * which logs all PropertyChangeEvents of the given property, Logger,
157:             * and log level.
158:             *
159:             * @param bean          the bean to log PropertyChangeEvents from
160:             * @param propertyName  the name of the property which PropertyChangeEvents
161:             *                      should be logged
162:             * @param logger        the Logger to be used to log PropertyChangeEvents
163:             * @param level         the log level
164:             *
165:             * @throws NullPointerException if the bean, propertyName, logger,
166:             *     or level is null
167:             */
168:            public static void logPropertyChanges(Object bean,
169:                    String propertyName, Logger logger, Level level) {
170:                BeanUtils.addPropertyChangeListener(bean, propertyName,
171:                        new LogHandler(logger, level));
172:            }
173:
174:            // Helper code ************************************************************
175:
176:            /**
177:             * A listener which logs PropertyChangeEvents.
178:             */
179:            private static final class LogHandler implements 
180:                    PropertyChangeListener {
181:
182:                /**
183:                 * Logger to be used to log PropertyChangeEvents.
184:                 */
185:                private final Logger logger;
186:
187:                /**
188:                 * The log level used for the logging.
189:                 */
190:                private final Level level;
191:
192:                /**
193:                 * Creates and returns LoggingListener, which uses the given Logger
194:                 * to log PropertyChangeEvents.
195:                 *
196:                 * @param logger the logger to be used to log PropertyChangeEvents
197:                 * @param level   the level used for the logging
198:                 */
199:                LogHandler(Logger logger, Level level) {
200:                    if (logger == null) {
201:                        throw new NullPointerException(
202:                                "The logger must not be null.");
203:                    }
204:                    if (level == null) {
205:                        throw new NullPointerException(
206:                                "The level must not be null.");
207:                    }
208:                    this .logger = logger;
209:                    this .level = level;
210:                }
211:
212:                /**
213:                 * Logs the given event.
214:                 */
215:                public void propertyChange(PropertyChangeEvent e) {
216:                    if (!logger.isLoggable(level))
217:                        return;
218:
219:                    Object newValue = e.getNewValue();
220:                    Object oldValue = e.getOldValue();
221:                    StringBuilder builder = new StringBuilder(e.getSource()
222:                            .toString());
223:                    builder.append(" [propertyName=");
224:                    builder.append(e.getPropertyName());
225:                    builder.append(", oldValue=");
226:                    builder.append(oldValue);
227:                    if (oldValue != null) {
228:                        builder.append(", oldValueType=");
229:                        builder.append(oldValue.getClass());
230:                    }
231:                    builder.append(", newValue=");
232:                    builder.append(newValue);
233:                    if (newValue != null) {
234:                        builder.append(", newValueType=");
235:                        builder.append(newValue.getClass());
236:                    }
237:                    logger.log(level, builder.toString());
238:                }
239:
240:            }
241:
242:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.