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 org.slf4j.Logger;
20  import org.slf4j.LoggerFactory;
21  import org.jdesktop.beansbinding.AutoBinding;
22  import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
23  import org.jdesktop.beansbinding.Property;
24  
25  import org.w3c.dom.Element;
26  
27  import com.silvermindsoftware.hitch.binding.PropertyUtil;
28  
29  import cookxml.core.DecodeEngine;
30  import cookxml.core.exception.CookXmlException;
31  import cookxml.core.interfaces.Creator;
32  
33  /**
34   * This class provides basic functionality for cookXML binding creators.
35   *
36   * @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/AbstractBindingCreator.java $
37   *
38   * @author Stefan Wismer (SWI)
39   */
40  public abstract class AbstractBindingCreator implements Creator {
41  	// some common attributes
42  	private static final String SOURCE = "src";
43  	protected static final String PROPERTY = "property";
44  	protected static final String VALIDATION = "validation";
45  	protected static final String UPDATE_STRATEGY = "updateStrategy";
46  	
47  	/**
48  	 * The logger.
49  	 */
50  	private static final Logger s_logger = LoggerFactory
51  		.getLogger(AbstractBindingCreator.class);
52  	
53  	/**
54  	 * @param elm    the current XML element
55  	 * @return       the updateStrategy attribute
56  	 */
57  	protected UpdateStrategy getUpdateStrategy(Element elm) {
58  		UpdateStrategy updateStrategy = UpdateStrategy.READ;
59  		String updateStrategyString = elm.getAttribute(UPDATE_STRATEGY);
60  		if (updateStrategyString.equals("read once")) {
61  			updateStrategy = UpdateStrategy.READ_ONCE;
62  		} else if (updateStrategyString.equals("read write")) {
63  			updateStrategy = UpdateStrategy.READ_WRITE;
64  		}
65  		return updateStrategy;
66  	}
67  	
68  	/**
69  	 * @param decodeEngine    the cookXML decodeEngine
70  	 * @param elm             the current XML element
71  	 * @return                the source object to bind
72  	 */
73  	@SuppressWarnings("unchecked")
74  	protected Object getSource(DecodeEngine decodeEngine, Element elm) {
75  		Object obj = null;
76  		String src = elm.getAttribute(SOURCE);
77  		if (!src.equals("")) {
78  			if (src.contains(".")) {
79  				Property prop = PropertyUtil.create(
80  					src.substring(src.indexOf(".") + 1));
81  				obj = prop.getValue(decodeEngine.getVariable(
82  					src.substring(0, src.indexOf("."))));
83  			} else {
84  				obj = decodeEngine.getVariable(elm.getAttribute(SOURCE));
85  			}
86  		} else {
87  			s_logger.error("Error processing XML element " + elm.getNodeName()
88  				+ ". Mandatory attribute '" + SOURCE + "' not found.");
89  		}
90  		return obj;
91  	}
92  	
93  	/**
94  	 * @param elm    the current XML element
95  	 * @return       <code>true</code> if values should be validated
96  	 */
97  	protected boolean getValidate(Element elm) {
98  		boolean validate = Boolean.parseBoolean(elm.getAttribute(VALIDATION));
99  		if (!elm.getAttribute(VALIDATION).equals("")) {
100 			validate = true;
101 		}
102 		return validate;
103 	}
104 	
105 	/**
106 	 * Adds binding to the class associated with this XML GUI description.
107 	 * @param decodeEngine    the cookXML decodeEngine
108 	 * @param binding         the beans binding to add
109 	 */
110 	@SuppressWarnings("unchecked")
111 	protected void addBinding(DecodeEngine decodeEngine, AutoBinding binding) {
112 		Object form = decodeEngine.getVariable("this");
113 		if (Bindable.class.isAssignableFrom(form.getClass())) {
114 			Bindable bindableForm = (Bindable) form;
115 			
116 			bindableForm.getBinder().addManualBinding(binding);
117 		} else {
118 			s_logger.warn("No class to bind found. " + form
119 				+ " does not implement interface Bindable");
120 		}
121 	}
122 	
123 	/** {@inheritDoc} */
124 	public Object editFinished(String parentNS, String parentTag, Element elm,
125 		Object parentObj, Object obj, DecodeEngine decodeEngine)
126 		throws CookXmlException {
127 
128 		return obj;
129 	}
130 }