Source Code Cross Referenced for WeakPropertyChangeListener.java in  » Swing-Library » jide-common » com » jidesoft » swing » 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 » jide common » com.jidesoft.swing 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


01:        package com.jidesoft.swing;
02:
03:        import javax.swing.event.ChangeEvent;
04:        import java.awt.event.ItemEvent;
05:        import java.beans.PropertyChangeEvent;
06:        import java.beans.PropertyChangeListener;
07:        import java.lang.ref.WeakReference;
08:        import java.lang.reflect.Method;
09:
10:        /**
11:         * How to use this:
12:         * <code><pre>
13:         * KeyboardFocusManager focusManager =
14:         * KeyboardFocusManager.getCurrentKeyboardFocusManager();
15:         * <p/>
16:         * // instead of registering direclty use weak listener
17:         * // focusManager.addPropertyChangeListener(focusOwnerListener);
18:         * <p/>
19:         * focusManager.addPropertyChangeListener(
20:         * new WeakPropertyChangeListener(focusOwnerListener, focusManager));
21:         * </pre></code>
22:         * <p/>
23:         * How does this work:
24:         * <p/>
25:         * Instead of registering propertyChangeListener directly to keyboardFocusManager, we wrap it inside WeakPropertyChangeListener and register this weak listener to keyboardFocusManager. This weak listener acts a delegate.
26:         * It receives the propertyChangeEvents from keyboardFocusManager and delegates it the wrapped listener.
27:         * <p/>
28:         * The interesting part of this weak listener, it hold a weakReference to the original propertyChangeListener. so this delegate is eligible for garbage collection which it is no longer reachable via references. When it gets garbage
29:         * collection, the weakReference will be pointing to null. On next propertyChangeEvent notification from keyboardFocusManager, it find that the weakReference is pointing to null, and unregisters itself from
30:         * keyboardFocusManager. Thus the weak listener will also become eligible for garbage collection in next gc cycle.
31:         * <p/>
32:         * This concept is not something new. If you have a habit of looking into swing sources, you will find that AbstractButton actually adds a weak listener to its action. The weak listener class used for this is :
33:         * javax.swing.AbstractActionPropertyChangeListener; This class is package-private, so you don't find it in javadoc.
34:         * <p/>
35:         * The full-fledged, generic implementation of weak listeners is available in Netbeans OpenAPI: WeakListeners.java . It is worth to have a look at it.
36:         *
37:         * @author Santhosh Kumar T - santhosh@in.fiorano.com
38:         */
39:        public class WeakPropertyChangeListener implements 
40:                PropertyChangeListener {
41:            private WeakReference<PropertyChangeListener> _listenerRef;
42:            private Object _src;
43:
44:            public WeakPropertyChangeListener(PropertyChangeListener listener,
45:                    Object src) {
46:                _listenerRef = new WeakReference(listener);
47:                _src = src;
48:            }
49:
50:            public void propertyChange(PropertyChangeEvent evt) {
51:                PropertyChangeListener listener = _listenerRef.get();
52:                if (listener == null) {
53:                    removeListener();
54:                } else
55:                    listener.propertyChange(evt);
56:            }
57:
58:            public void itemStateChanged(ItemEvent e) {
59:
60:            }
61:
62:            public void stateChanged(ChangeEvent e) {
63:
64:            }
65:
66:            private void removeListener() {
67:                try {
68:                    Method method = _src.getClass().getMethod(
69:                            "removePropertyChangeListener",
70:                            new Class[] { PropertyChangeListener.class });
71:                    method.invoke(_src, new Object[] { this  });
72:                } catch (Exception e) {
73:                    e.printStackTrace();
74:                }
75:            }
76:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.