1 /***
2 * Copyright (C) 2006 Philipp Mpalampanis
3 *
4 * License: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Alternatively, the contents of this file may be used under the terms of
17 * either the GNU General Public License Version 2 or later (the "GPL"), or
18 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
19 * in which case the provisions of the GPL or the LGPL are applicable instead
20 * of those above. If you wish to allow use of your version of this file only
21 * under the terms of either the GPL or the LGPL, and not to allow others to
22 * use your version of this file under the terms of the MPL, indicate your
23 * decision by deleting the provisions above and replace them with the notice
24 * and other provisions required by the GPL or the LGPL. If you do not delete
25 * the provisions above, a recipient may use your version of this file under
26 * the terms of any one of the MPL, the GPL or the LGPL.
27 */
28 package net.sf.echobinding;
29
30 import java.beans.PropertyChangeEvent;
31 import java.beans.PropertyChangeListener;
32 import java.util.*;
33
34 import net.sf.echobinding.binding.BindingContext;
35
36 /***
37 * The <code>BindingConfiguration</code> is the root node in the data binding
38 * context tree. It manages all underlaying <code>BindingContext</code>s and
39 * is usally the access point for the application to the data binding facility.
40 *
41 * @see net.sf.echobinding.binding.BindingContext
42 */
43 public class BindingConfiguration implements PropertyChangeListener {
44
45 private static Map<String, BindingContext> _contexts = new HashMap<String, BindingContext>();
46
47 /***
48 * @param contextId
49 * @return the binding context for that contextId
50 */
51 public static BindingContext getBindingContext(String contextId) {
52 BindingContext ctx = _contexts.get( contextId );
53 if (ctx == null)
54 throw new RuntimeException( "unknown binding context: " + contextId );
55 return ctx;
56 }
57
58 private ArrayList<PropertyChangeListener> _propertyChangeListeners = new ArrayList<PropertyChangeListener>();
59
60 /***
61 * @return the binding contexts as a map
62 */
63 public Map<String, BindingContext> getContexts() {
64 return _contexts;
65 }
66
67 /***
68 * @param contexts
69 */
70 public void setContexts(Map<String, BindingContext> contexts) {
71 for (String contextName : contexts.keySet()) {
72 addContext( contextName, _contexts.get( contextName ) );
73 }
74 }
75
76 /***
77 * @param contextName
78 * @param context
79 * @return the configuration
80 */
81 public BindingConfiguration addContext(String contextName, BindingContext context) {
82 if(_contexts == null)
83 _contexts = new HashMap<String, BindingContext>();
84
85 registerContext(context);
86 _contexts.put(contextName, context);
87
88 return this;
89 }
90
91 /***
92 * Registers a new binding context. This is necessary for
93 * receive/sending property change events from/to other contexts within this
94 * configuration.
95 *
96 * @param context
97 */
98 public void registerContext(BindingContext context) {
99
100
101 context.removePropertyChangeListener( this );
102 context.addPropertyChangeListener( this );
103
104
105 addPropertyChangeListener( context );
106
107
108 }
109
110
111
112
113
114
115 public void propertyChange(PropertyChangeEvent event) {
116
117 PropertyChangeEvent newEvent = new PropertyChangeEvent( this, event
118 .getPropertyName(), event.getOldValue(), event.getNewValue() );
119 for (PropertyChangeListener listener : _propertyChangeListeners) {
120 if (!listener.equals( event.getSource() ))
121 listener.propertyChange( newEvent );
122 }
123 }
124
125 /***
126 * @param listener
127 */
128 public void addPropertyChangeListener(PropertyChangeListener listener) {
129
130 _propertyChangeListeners.add( listener );
131 }
132
133 /***
134 * @param listener
135 */
136 public void removePropertyChangeListener(PropertyChangeListener listener) {
137 _propertyChangeListeners.remove( listener );
138 }
139
140 }