View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package com.silvermindsoftware.hitch.binding;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import javax.swing.JComponent;
23  
24  import org.jdesktop.beansbinding.AutoBinding;
25  import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
26  import org.jdesktop.beansbinding.BindingListener;
27  import org.jdesktop.beansbinding.Bindings;
28  import org.jdesktop.beansbinding.Property;
29  
30  
31  import com.silvermindsoftware.hitch.validation.ValidatingBindingListener;
32  import com.silvermindsoftware.hitch.validation.response.ValidationResponder;
33  
34  import ch.elca.el4j.services.gui.swing.GUIApplication;
35  import ch.elca.el4j.util.config.GenericConfig;
36  
37  /**
38   * This class is used for creating bindings.
39   *
40   * @svnLink $Revision: 3873 $;$Date: 2009-08-04 13:59:45 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/swing/src/main/java/com/silvermindsoftware/hitch/binding/BindingFactory.java $
41   *
42   * @author Stefan Wismer (SWI)
43   */
44  public class BindingFactory {
45  	/**
46  	 * An instance of the factory (used for singleton).
47  	 */
48  	private static BindingFactory s_bindingFactory;
49  	
50  	/**
51  	 * Default mapping of properties.
52  	 */
53  	private static final DefaultProperties DEFAULT_PROPERTIES
54  		= new DefaultProperties();
55  	
56  	/**
57  	 * Map containing user defined binding mappings.
58  	 */
59  	@SuppressWarnings("unchecked")
60  	private Map<JComponent, BindingCreator> m_specialBinding;
61  	
62  	/**
63  	 * Map containing user defined validation responder.
64  	 */
65  	@SuppressWarnings("unchecked")
66  	private Map<JComponent, ValidationResponder> m_specialValidationResponder;
67  	
68  	
69  	/**
70  	 * The hidden constructor.
71  	 */
72  	@SuppressWarnings("unchecked")
73  	protected BindingFactory() {
74  		m_specialBinding = new HashMap<JComponent, BindingCreator>();
75  		m_specialValidationResponder
76  			= new HashMap<JComponent, ValidationResponder>();
77  	}
78  	
79  	/**
80  	 * @return    an instance of this class
81  	 */
82  	public static BindingFactory getInstance() {
83  		if (s_bindingFactory == null) {
84  			s_bindingFactory = new BindingFactory();
85  		}
86  		return s_bindingFactory;
87  	}
88  	
89  	/**
90  	 * @return      the default properties
91  	 */
92  	public static DefaultProperties getDefaultProperties() {
93  		return DEFAULT_PROPERTIES;
94  	}
95  	
96  	/**
97  	 * Registers a custom binding strategy.
98  	 *
99  	 * @param component     the component to bind
100 	 * @param binding       the custom binding
101 	 */
102 	@SuppressWarnings("unchecked")
103 	public void register(JComponent component, BindingCreator binding) {
104 		m_specialBinding.put(component, binding);
105 	}
106 	
107 	/**
108 	 * Registers a custom validation responder.
109 	 *
110 	 * @param component     the bound component
111 	 * @param responder     the custom validation responder
112 	 */
113 	public void registerValidationResponder(JComponent component,
114 		ValidationResponder responder) {
115 		m_specialValidationResponder.put(component, responder);
116 	}
117 	
118 	/**
119 	 * @param strategy          the binding update strategy
120 	 * @param modelObject       the instance of the model
121 	 * @param modelProperty     the property of the model to bound
122 	 * @param formComponent     the form component to bound to
123 	 * @return                  a AutoBinding object representing the binding
124 	 */
125 	@SuppressWarnings("unchecked")
126 	public AutoBinding createBinding(UpdateStrategy strategy,
127 		Object modelObject, String modelProperty, JComponent formComponent) {
128 		
129 		Property modelPropertyName = PropertyUtil.create(modelProperty);
130 		
131 		AutoBinding binding = null;
132 		// is a special binding registered?
133 		if (m_specialBinding.containsKey(formComponent)) {
134 			binding = m_specialBinding.get(formComponent).createBinding(
135 				modelPropertyName.getValue(modelObject), formComponent);
136 		} else {
137 			// create a default binding
138 			Property fromPropertyName = DEFAULT_PROPERTIES.getDefaultProperty(
139 				formComponent);
140 			if (fromPropertyName != null) {
141 				binding = Bindings.createAutoBinding(strategy,
142 					modelObject, modelPropertyName,
143 					formComponent, fromPropertyName);
144 			}
145 		}
146 		return binding;
147 	}
148 	
149 	/**
150 	 * @param formComponent    the GUI component
151 	 * @return                 a {@link BindingListener} that performs the
152 	 *                         validation
153 	 */
154 	@SuppressWarnings("unchecked")
155 	public BindingListener createValidationListener(JComponent formComponent) {
156 		ValidationResponder responder;
157 		if (m_specialValidationResponder.containsKey(formComponent)) {
158 			responder = m_specialValidationResponder.get(formComponent);
159 		} else {
160 			GenericConfig config = GUIApplication.getInstance().getConfig();
161 			responder = (ValidationResponder) config.get(
162 				"validationResponder");
163 		}
164 		
165 		if (m_specialBinding.containsKey(formComponent)) {
166 			m_specialBinding.get(formComponent).addValidation(formComponent);
167 		}
168 		return new ValidatingBindingListener(responder);
169 	}
170 }