KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > properties > PropertyBundleSupport


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.properties;
21
22 import java.io.Serializable JavaDoc;
23 import javax.swing.event.EventListenerList JavaDoc;
24
25 /**
26  * Support for PropertyBundle events, registering listeners, firing events.
27  *
28  * @author Petr Jiricka
29  */

30 public class PropertyBundleSupport implements Serializable JavaDoc {
31
32     /** generated serial version UID */
33     static final long serialVersionUID = -655481419012858008L;
34
35     /** list of listeners */
36     protected EventListenerList JavaDoc listenerList = new EventListenerList JavaDoc();
37
38     /**
39      * object to be provided as a source for any generated events
40      *
41      * @serial
42      */

43     private Object JavaDoc source;
44
45     /**
46      * Constructs a <code>PropertyBundleSupport</code> object.
47      *
48      * @param sourceBean object to be given as a source of events
49      * @exception java.lang.NullPointerException
50      * if the parameter is <code>null</code>
51      */

52     public PropertyBundleSupport(Object JavaDoc source) {
53         if (source == null) {
54             throw new NullPointerException JavaDoc();
55         }
56         this.source = source;
57     }
58
59     /**
60      * Registers a given listener so that it will receive notifications
61      * about changes in a property bundle.
62      * If the given listener is already registered, a duplicite registration
63      * will be performed, so that it will get notifications multiple times.
64      *
65      * @param l listener to be registered
66      * @see #removePropertyBundleListener
67      */

68     public void addPropertyBundleListener(PropertyBundleListener l) {
69         listenerList.add(PropertyBundleListener.class, l);
70     }
71
72     /**
73      * Unregisters a given listener so that it will no more receive
74      * notifications about changes in a property bundle.
75      * If the given listener has been registered multiple times,
76      * only one registration item will be removed.
77      *
78      * @param l the PropertyBundleListener
79      * @see #addPropertyBundleListener
80      */

81     public void removePropertyBundleListener(PropertyBundleListener l) {
82         listenerList.remove(PropertyBundleListener.class, l);
83     }
84
85     /**
86      * Notifies all registered listeners about a possibly structural change
87      * in the property bundle.
88      *
89      * @see #addPropertyBundleListener
90      * @see #removePropertyBundleListener
91      */

92     public void fireBundleStructureChanged() {
93         fireBundleChanged(new PropertyBundleEvent(
94                 source,
95                 PropertyBundleEvent.CHANGE_STRUCT));
96     }
97
98     /**
99      * Notifies all registered listeners about a complex change
100      * in the property bundle.
101      *
102      * @see #addPropertyBundleListener
103      * @see #removePropertyBundleListener
104      */

105     public void fireBundleDataChanged() {
106         fireBundleChanged(new PropertyBundleEvent(
107                 source,
108                 PropertyBundleEvent.CHANGE_ALL));
109     }
110
111     /**
112      * Notifies all registered listeners about a change in a single entry
113      * of the property bundle.
114      *
115      * @param entryName name of the changed entry
116      * @see #addPropertyBundleListener
117      * @see #removePropertyBundleListener
118      */

119     public void fireFileChanged(String JavaDoc entryName) {
120         fireBundleChanged(new PropertyBundleEvent(source, entryName));
121     }
122
123     /**
124      * Notifies all registered listeners about a change of a single item
125      * in a single entry of the property bundle.
126      *
127      * @param entryName name of the changed entry
128      * @param itemName name of the changed item
129      * @see #addPropertyBundleListener
130      * @see #removePropertyBundleListener
131      */

132     public void fireItemChanged(String JavaDoc entryName, String JavaDoc itemName) {
133         fireBundleChanged(new PropertyBundleEvent(source, entryName, itemName));
134     }
135
136     /**
137      * Forwards the given notification event to all registered
138      * <code>PropertyBundleListener</code>s.
139      *
140      * @param e event to be forwarded to the registered listeners
141      * @see #addPropertyBundleListener
142      * @see #removePropertyBundleListener
143      */

144     public void fireBundleChanged(PropertyBundleEvent e) {
145         //System.out.println(e.toString());
146
// Guaranteed to return a non-null array
147
Object JavaDoc[] listeners = listenerList.getListenerList();
148         // Process the listeners last to first, notifying
149
// those that are interested in this event
150
for (int i = listeners.length - 2; i >= 0; i -= 2) {
151             if (listeners[i] == PropertyBundleListener.class) {
152                 ((PropertyBundleListener) listeners[i + 1]).bundleChanged(e);
153             }
154         }
155     }
156
157 } // End of class PropertyBundleSupport
158
Popular Tags