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.JTable;
22  import javax.swing.table.TableCellEditor;
23  import javax.swing.table.TableCellRenderer;
24  
25  import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
26  import org.jdesktop.swingbinding.JTableBinding;
27  import org.jdesktop.swingbinding.SwingBindings;
28  import org.jdesktop.swingbinding.validation.ValidatedProperty;
29  import org.springframework.context.ApplicationContext;
30  import org.w3c.dom.Element;
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 <tablebinding>s.
40   *
41   * @svnLink $Revision: 3884 $;$Date: 2009-08-04 15:48:31 +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/TableBindingCreator.java $
42   *
43   * @author Stefan Wismer (SWI)
44   */
45  public class TableBindingCreator extends AbstractBindingCreator {
46  	// <tablebinding> specific attributes
47  	protected static final String RENDERER = "rendererBean";
48  	protected static final String EDITOR = "editorBean";
49  	
50  	/** {@inheritDoc} */
51  	@SuppressWarnings("unchecked")
52  	public Object create(String parentNS, String parentTag, Element elm,
53  		Object parentObj, DecodeEngine decodeEngine) throws CreatorException {
54  
55  		// read properties
56  		UpdateStrategy updateStrategy = getUpdateStrategy(elm);
57  		List listSource = (List) getSource(decodeEngine, elm);
58  		if (listSource == null) {
59  			return null;
60  		}
61  		JTable table = (JTable) parentObj;
62  
63  		// renderer and validation
64  		String renderer = elm.getAttribute(RENDERER);
65  		if (renderer.equals("")) {
66  			if (getValidate(elm)) {
67  				GenericConfig config = GUIApplication.getInstance().getConfig();
68  				table.setDefaultRenderer(ValidatedProperty.class,
69  					(TableCellRenderer) config.get("tableCellRenderer"));
70  			}
71  		} else {
72  			ApplicationContext ctx
73  				= GUIApplication.getInstance().getSpringContext();
74  			table.setDefaultRenderer(ValidatedProperty.class,
75  				(TableCellRenderer) ctx.getBean(renderer));
76  		}
77  		
78  		String editor = elm.getAttribute(EDITOR);
79  		if (editor.equals("")) {
80  			if (getValidate(elm)) {
81  				GenericConfig config = GUIApplication.getInstance().getConfig();
82  				table.setDefaultEditor(ValidatedProperty.class,
83  					(TableCellEditor) config.get("tableCellEditor"));
84  			}
85  		} else {
86  			ApplicationContext ctx
87  				= GUIApplication.getInstance().getSpringContext();
88  			table.setDefaultEditor(ValidatedProperty.class,
89  				(TableCellEditor) ctx.getBean(editor));
90  		}
91  
92  		// create binding
93  		JTableBinding tb = SwingBindings.createJTableBinding(
94  			updateStrategy, listSource, table);
95  		addBinding(decodeEngine, tb);
96  
97  		return new NoAddValueHolder<JTableBinding>(tb);
98  	}
99  }