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 ch.elca.el4j.services.gui.swing.cookswing.binding;
18  
19  import java.util.List;
20  
21  import javax.swing.JComboBox;
22  
23  import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
24  import org.jdesktop.swingbinding.JComboBoxBinding;
25  import org.jdesktop.swingbinding.SwingBindings;
26  import org.springframework.context.ApplicationContext;
27  import org.w3c.dom.Element;
28  
29  import com.silvermindsoftware.hitch.validation.response.ComboBoxRenderer;
30  import com.silvermindsoftware.hitch.validation.response.DefaultValidatingComboBoxRenderer;
31  
32  import ch.elca.el4j.services.gui.swing.GUIApplication;
33  import ch.elca.el4j.util.config.GenericConfig;
34  
35  import cookxml.core.DecodeEngine;
36  import cookxml.core.exception.CreatorException;
37  
38  /**
39   * The cookSwing creator for general purpose <comboboxbinding>s.
40   *
41   * @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/ch/elca/el4j/services/gui/swing/cookswing/binding/ComboBoxBindingCreator.java $
42   *
43   * @author Stefan Wismer (SWI)
44   */
45  public class ComboBoxBindingCreator extends AbstractBindingCreator {
46  	// <comboboxbinding> specific attributes
47  	protected static final String RENDERER = "rendererBean";
48  
49  	/** {@inheritDoc} */
50  	@SuppressWarnings("unchecked")
51  	public Object create(String parentNS, String parentTag, Element elm,
52  		Object parentObj, DecodeEngine decodeEngine) throws CreatorException {
53  		
54  		// read attributes
55  		UpdateStrategy updateStrategy = getUpdateStrategy(elm);
56  		List listSource = (List) getSource(decodeEngine, elm);
57  		if (listSource == null) {
58  			return null;
59  		}
60  		JComboBox comboBox = (JComboBox) parentObj;
61  		
62  		// renderer and validation
63  		String renderer = elm.getAttribute(RENDERER);
64  		ComboBoxRenderer cr = null;
65  		if (renderer.equals("")) {
66  			GenericConfig config = GUIApplication.getInstance().getConfig();
67  			DefaultValidatingComboBoxRenderer validatingRenderer
68  				= (DefaultValidatingComboBoxRenderer)
69  				config.get("comboBoxRenderer");
70  			validatingRenderer.setValidate(getValidate(elm));
71  			cr = validatingRenderer;
72  		} else {
73  			ApplicationContext ctx
74  				= GUIApplication.getInstance().getSpringContext();
75  			cr = (ComboBoxRenderer) ctx.getBean(renderer);
76  		}
77  		
78  		cr.setProperty(elm.getAttribute(PROPERTY));
79  		comboBox.setRenderer(cr);
80  		
81  		// create binding
82  		JComboBoxBinding cb = SwingBindings.createJComboBoxBinding(
83  			updateStrategy, listSource, comboBox);
84  		
85  		addBinding(decodeEngine, cb);
86  		
87  		return new NoAddValueHolder<JComboBoxBinding>(cb);
88  	}
89  }