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.validation.response;
18  
19  import java.awt.Color;
20  import java.awt.Component;
21  
22  import javax.swing.DefaultListCellRenderer;
23  import javax.swing.JList;
24  
25  import org.jdesktop.swingbinding.validation.ValidationCapability;
26  
27  import com.silvermindsoftware.hitch.binding.PropertyUtil;
28  
29  import ch.elca.el4j.services.gui.swing.GUIApplication;
30  
31  /**
32   * A validating cell renderer for comboboxes.
33   *
34   * @svnLink $Revision: 3874 $;$Date: 2009-08-04 14:25:40 +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/validation/response/DefaultValidatingComboBoxRenderer.java $
35   *
36   * @author Stefan Wismer (SWI)
37   */
38  public class DefaultValidatingComboBoxRenderer
39  	extends DefaultListCellRenderer implements ComboBoxRenderer {
40  	
41  	/**
42  	 * Color to mark value as invalid.
43  	 */
44  	protected final Color m_invalidColor;
45  	
46  	/**
47  	 * The property to render.
48  	 */
49  	protected String m_property;
50  	
51  	/**
52  	 * Should invalid property values marked with different color?
53  	 */
54  	protected boolean m_validate;
55  	
56  	
57  	/**
58  	 * The default contructor reading the invalidColor from Spring config.
59  	 */
60  	public DefaultValidatingComboBoxRenderer() {
61  		this(null, null, false);
62  	}
63  	
64  	/**
65  	 * @param property    the property to render
66  	 * @param validate    validate property value?
67  	 */
68  	public DefaultValidatingComboBoxRenderer(String property,
69  		boolean validate) {
70  		this(null, property, validate);
71  	}
72  	
73  	/**
74  	 * @param color       the color to mark value as invalid
75  	 * @param property    the property to render
76  	 * @param validate    validate property value?
77  	 */
78  	public DefaultValidatingComboBoxRenderer(Color color, String property,
79  		boolean validate) {
80  		m_invalidColor = color;
81  		m_property = property;
82  		m_validate = validate;
83  	}
84  	
85  	/** {@inheritDoc} */
86  	public void setProperty(String property) {
87  		m_property = property;
88  	}
89  	
90  	/**
91  	 * @param validate   validate property value?
92  	 */
93  	public void setValidate(boolean validate) {
94  		m_validate = validate;
95  	}
96  	
97  	/** {@inheritDoc} */
98  	@Override
99  	public Component getListCellRendererComponent(JList list,
100 		Object value, int index, boolean isSelected, boolean cellHasFocus) {
101 		
102 		Component renderer;
103 		if (m_property != null) {
104 			Object propValue = PropertyUtil.create(m_property).getValue(value);
105 			renderer = super.getListCellRendererComponent(list,
106 					propValue, index, isSelected, cellHasFocus);
107 			
108 			if (m_validate) {
109 				if (value instanceof ValidationCapability) {
110 					ValidationCapability v = (ValidationCapability) value;
111 					if (!v.isValid(m_property)) {
112 						renderer.setBackground(getInvalidColor());
113 					}
114 				}
115 			}
116 		} else {
117 			renderer = super.getListCellRendererComponent(list,
118 				value, index, isSelected,
119 				cellHasFocus);
120 		}
121 		
122 		return renderer;
123 	}
124 	
125 	/**
126 	 * @return    the color to mark a value as invalid.
127 	 */
128 	private Color getInvalidColor() {
129 		if (m_invalidColor != null) {
130 			return m_invalidColor;
131 		} else {
132 			return (Color) GUIApplication.getInstance().getConfig()
133 			.get("invalidColor");
134 		}
135 	}
136 }