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  
22  import org.jdesktop.application.ApplicationContext;
23  import org.jdesktop.beansbinding.Property;
24  import org.jdesktop.swingbinding.JTableBinding;
25  import org.jdesktop.swingbinding.JTableBinding.ColumnBinding;
26  import org.w3c.dom.Element;
27  
28  import com.silvermindsoftware.hitch.binding.PropertyUtil;
29  
30  import ch.elca.el4j.services.gui.swing.GUIApplication;
31  
32  import cookxml.core.DecodeEngine;
33  import cookxml.core.exception.CreatorException;
34  
35  /**
36   * The cookSwing creator for general purpose <columnbinding>s inside
37   * <tablebinding>s.
38   *
39   * @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/ColumnBindingCreator.java $
40   *
41   * @author Stefan Wismer (SWI)
42   */
43  public class ColumnBindingCreator extends AbstractBindingCreator {
44  	/**
45  	 * The logger.
46  	 */
47  	private static Logger s_logger = LoggerFactory.getLogger(ColumnBindingCreator.class);
48  	
49  	// <column> specific attributes
50  	protected static final String LABEL = "label";
51  	protected static final String EDITABLE = "editable";
52  	protected static final String CLASS = "class";
53  	
54  	/** {@inheritDoc} */
55  	@SuppressWarnings("unchecked")
56  	public Object create(String parentNS, String parentTag, Element elm,
57  		Object parentObj, DecodeEngine decodeEngine) throws CreatorException {
58  		
59  		// read attributes
60  		JTableBinding tb = ((NoAddValueHolder<JTableBinding>)
61  			parentObj).getObject();
62  		Property prop = PropertyUtil.create(elm.getAttribute(PROPERTY));
63  		
64  		// create binding
65  		ColumnBinding cb = tb.addColumnBinding(prop);
66  		
67  		// get localized string
68  		String columnName = elm.getAttribute(LABEL);
69  		if (columnName.startsWith("@")) {
70  			GUIApplication app = GUIApplication.getInstance();
71  			ApplicationContext appContext = app.getContext();
72  			columnName = appContext.getResourceMap(
73  				decodeEngine.getVariable("this").getClass())
74  				.getString(columnName.substring(1));
75  			if (columnName == null) {
76  				// no string found -> restore
77  				columnName = elm.getAttribute(LABEL).substring(1);
78  			}
79  		}
80  		cb.setColumnName(columnName);
81  		
82  		// is editable?
83  		cb.setEditable(elm.getAttribute(EDITABLE).equalsIgnoreCase("true"));
84  		
85  		// set class
86  		try {
87  			if (!elm.getAttribute(CLASS).equals("")) {
88  				cb.setColumnClass(Class.forName(elm.getAttribute(CLASS)));
89  			}
90  		} catch (ClassNotFoundException e) {
91  			s_logger.warn("Attribute '" + CLASS + "' of tag '"
92  				+ elm.getNodeName() + "' contains invalid class.");
93  		}
94  		
95  		return new NoAddValueHolder<ColumnBinding>(cb);
96  	}
97  }