Source Code Cross Referenced for VetoableChangeSupport.java in  » Apache-Harmony-Java-SE » java-package » java » beans » 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 » Apache Harmony Java SE » java package » java.beans 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:
018:        package java.beans;
019:
020:        import java.io.IOException;
021:        import java.io.ObjectInputStream;
022:        import java.io.ObjectOutputStream;
023:        import java.io.Serializable;
024:        import java.util.ArrayList;
025:        import java.util.Hashtable;
026:        import java.util.Iterator;
027:        import java.util.List;
028:
029:        // FIXME: obviously need synchronization, when access listeners
030:
031:        public class VetoableChangeSupport implements  Serializable {
032:
033:            private static final long serialVersionUID = -5090210921595982017l;
034:
035:            private Hashtable<String, VetoableChangeSupport> children = new Hashtable<String, VetoableChangeSupport>();
036:
037:            private transient ArrayList<VetoableChangeListener> globalListeners = new ArrayList<VetoableChangeListener>();
038:
039:            private Object source;
040:
041:            @SuppressWarnings("unused")
042:            // for serialization
043:            private int vetoableChangeSupportSerializedDataVersion = 2;
044:
045:            public VetoableChangeSupport(Object sourceBean) {
046:                if (sourceBean == null) {
047:                    throw new NullPointerException();
048:                }
049:                this .source = sourceBean;
050:            }
051:
052:            public synchronized void removeVetoableChangeListener(
053:                    String propertyName, VetoableChangeListener listener) {
054:                if ((propertyName != null) && (listener != null)) {
055:                    VetoableChangeSupport listeners = children
056:                            .get(propertyName);
057:
058:                    if (listeners != null) {
059:                        listeners.removeVetoableChangeListener(listener);
060:                    }
061:                }
062:            }
063:
064:            public synchronized void addVetoableChangeListener(
065:                    String propertyName, VetoableChangeListener listener) {
066:                if (propertyName != null && listener != null) {
067:                    VetoableChangeSupport listeners = children
068:                            .get(propertyName);
069:
070:                    if (listeners == null) {
071:                        listeners = new VetoableChangeSupport(source);
072:                        children.put(propertyName, listeners);
073:                    }
074:                    listeners.addVetoableChangeListener(listener);
075:                }
076:            }
077:
078:            public synchronized VetoableChangeListener[] getVetoableChangeListeners(
079:                    String propertyName) {
080:                VetoableChangeSupport listeners = null;
081:
082:                if (propertyName != null) {
083:                    listeners = children.get(propertyName);
084:                }
085:                return (listeners == null) ? new VetoableChangeListener[] {}
086:                        : getAsVetoableChangeListenerArray(listeners);
087:            }
088:
089:            public synchronized boolean hasListeners(String propertyName) {
090:                boolean result = globalListeners.size() > 0;
091:                if (!result && propertyName != null) {
092:                    VetoableChangeSupport listeners = children
093:                            .get(propertyName);
094:                    if (listeners != null) {
095:                        result = listeners.globalListeners.size() > 0;
096:                    }
097:                }
098:                return result;
099:            }
100:
101:            public synchronized void removeVetoableChangeListener(
102:                    VetoableChangeListener listener) {
103:                if (listener != null) {
104:                    globalListeners.remove(listener);
105:                }
106:            }
107:
108:            public synchronized void addVetoableChangeListener(
109:                    VetoableChangeListener listener) {
110:                if (listener != null) {
111:                    if (listener instanceof  VetoableChangeListenerProxy) {
112:                        VetoableChangeListenerProxy proxy = (VetoableChangeListenerProxy) listener;
113:                        addVetoableChangeListener(proxy.getPropertyName(),
114:                                (VetoableChangeListener) proxy.getListener());
115:                    } else {
116:                        globalListeners.add(listener);
117:                    }
118:                }
119:            }
120:
121:            public synchronized VetoableChangeListener[] getVetoableChangeListeners() {
122:                List<VetoableChangeListener> result = new ArrayList<VetoableChangeListener>();
123:                if (globalListeners != null) {
124:                    result.addAll(globalListeners);
125:                }
126:
127:                for (Iterator<String> iterator = children.keySet().iterator(); iterator
128:                        .hasNext();) {
129:                    String propertyName = iterator.next();
130:                    VetoableChangeSupport namedListener = children
131:                            .get(propertyName);
132:                    VetoableChangeListener[] childListeners = namedListener
133:                            .getVetoableChangeListeners();
134:                    for (int i = 0; i < childListeners.length; i++) {
135:                        result.add(new VetoableChangeListenerProxy(
136:                                propertyName, childListeners[i]));
137:                    }
138:                }
139:                return (result
140:                        .toArray(new VetoableChangeListener[result.size()]));
141:            }
142:
143:            private void writeObject(ObjectOutputStream oos) throws IOException {
144:                oos.defaultWriteObject();
145:                VetoableChangeListener[] copy = new VetoableChangeListener[globalListeners
146:                        .size()];
147:                globalListeners.toArray(copy);
148:                for (VetoableChangeListener listener : copy) {
149:                    if (listener instanceof  Serializable) {
150:                        oos.writeObject(listener);
151:                    }
152:                }
153:                // Denotes end of list
154:                oos.writeObject(null);
155:
156:            }
157:
158:            private void readObject(ObjectInputStream ois) throws IOException,
159:                    ClassNotFoundException {
160:                ois.defaultReadObject();
161:                this .globalListeners = new ArrayList<VetoableChangeListener>();
162:                if (null == this .children) {
163:                    this .children = new Hashtable<String, VetoableChangeSupport>();
164:                }
165:                Object listener;
166:                do {
167:                    // Reads a listener _or_ proxy
168:                    listener = ois.readObject();
169:                    addVetoableChangeListener((VetoableChangeListener) listener);
170:                } while (listener != null);
171:            }
172:
173:            @SuppressWarnings("boxing")
174:            public void fireVetoableChange(String propertyName,
175:                    boolean oldValue, boolean newValue)
176:                    throws PropertyVetoException {
177:                PropertyChangeEvent event = createPropertyChangeEvent(
178:                        propertyName, oldValue, newValue);
179:                doFirePropertyChange(event);
180:            }
181:
182:            @SuppressWarnings("boxing")
183:            public void fireVetoableChange(String propertyName, int oldValue,
184:                    int newValue) throws PropertyVetoException {
185:                PropertyChangeEvent event = createPropertyChangeEvent(
186:                        propertyName, oldValue, newValue);
187:                doFirePropertyChange(event);
188:            }
189:
190:            public void fireVetoableChange(String propertyName,
191:                    Object oldValue, Object newValue)
192:                    throws PropertyVetoException {
193:                PropertyChangeEvent event = createPropertyChangeEvent(
194:                        propertyName, oldValue, newValue);
195:                doFirePropertyChange(event);
196:            }
197:
198:            public void fireVetoableChange(PropertyChangeEvent event)
199:                    throws PropertyVetoException {
200:                doFirePropertyChange(event);
201:            }
202:
203:            private PropertyChangeEvent createPropertyChangeEvent(
204:                    String propertyName, Object oldValue, Object newValue) {
205:                return new PropertyChangeEvent(source, propertyName, oldValue,
206:                        newValue);
207:            }
208:
209:            private void doFirePropertyChange(PropertyChangeEvent event)
210:                    throws PropertyVetoException {
211:                String propName = event.getPropertyName();
212:                Object oldValue = event.getOldValue();
213:                Object newValue = event.getNewValue();
214:
215:                if (newValue != null && oldValue != null
216:                        && newValue.equals(oldValue)) {
217:                    return;
218:                }
219:
220:                /* Take note of who we are going to notify (and potentially un-notify) */
221:
222:                VetoableChangeListener[] listensToAll;
223:                VetoableChangeSupport listeners = null;
224:                // property change
225:                synchronized (this ) {
226:                    listensToAll = globalListeners
227:                            .toArray(new VetoableChangeListener[0]);
228:                    String propertyName = event.getPropertyName();
229:                    if (propertyName != null) {
230:                        listeners = children.get(propertyName);
231:                    }
232:                }
233:
234:                try {
235:                    for (VetoableChangeListener listener : listensToAll) {
236:                        listener.vetoableChange(event);
237:                    }
238:                } catch (PropertyVetoException pve) {
239:                    // Tell them we have changed it back
240:                    PropertyChangeEvent revertEvent = createPropertyChangeEvent(
241:                            propName, newValue, oldValue);
242:                    for (VetoableChangeListener listener : listensToAll) {
243:                        try {
244:                            listener.vetoableChange(revertEvent);
245:                        } catch (PropertyVetoException ignored) {
246:                            // expected
247:                        }
248:                    }
249:                    throw pve;
250:                }
251:                if (listeners != null) {
252:                    listeners.fireVetoableChange(event);
253:                }
254:            }
255:
256:            private static VetoableChangeListener[] getAsVetoableChangeListenerArray(
257:                    VetoableChangeSupport listeners) {
258:                return listeners.globalListeners
259:                        .toArray(new VetoableChangeListener[0]);
260:            }
261:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.