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  
21  import javax.swing.JComponent;
22  
23  import ch.elca.el4j.services.gui.swing.GUIApplication;
24  
25  /**
26   * A default ValidationResponder that makes the background of the corresponding
27   * GUI element red.
28   *
29   * @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/DefaultValidationResponder.java $
30   *
31   * @author Stefan Wismer (SWI)
32   */
33  public class DefaultValidationResponder implements ValidationResponder {
34  	/**
35  	 * Identifier for valid color in component's client property.
36  	 */
37  	protected static final String VALID_COLOR = "Valid Color";
38  	
39  	/**
40  	 * Color to mark value as invalid.
41  	 */
42  	protected final Color m_invalidColor;
43  
44  	/**
45  	 * The default constructor reading the invalidColor from Spring config.
46  	 */
47  	public DefaultValidationResponder() {
48  		m_invalidColor = null;
49  	}
50  	
51  	/**
52  	 * @param color    the background color if value is invalid
53  	 */
54  	public DefaultValidationResponder(Color color) {
55  		m_invalidColor = color;
56  	}
57  	
58  	/** {@inheritDoc} */
59  	public void setValid(Object object, JComponent component, boolean valid) {
60  		if (valid) {
61  			setValid(object, component);
62  		} else {
63  			setInvalid(object, component, null);
64  		}
65  	}
66  	
67  	/** {@inheritDoc} */
68  	public void setValid(Object object, JComponent component) {
69  		if (component != null) {
70  			if (component.getBackground().equals(getInvalidColor())) {
71  				component.setBackground(
72  					(Color) component.getClientProperty(VALID_COLOR));
73  			}
74  		}
75  	}
76  	
77  	/** {@inheritDoc} */
78  	public void setInvalid(Object object, JComponent component,
79  		String message) {
80  		
81  		if (component != null) {
82  			if (!component.getBackground().equals(getInvalidColor())) {
83  				component.putClientProperty(
84  					VALID_COLOR, component.getBackground());
85  				component.setBackground(getInvalidColor());
86  			}
87  		}
88  	}
89  	
90  	/**
91  	 * @return    the color to mark a value as invalid.
92  	 */
93  	private Color getInvalidColor() {
94  		if (m_invalidColor != null) {
95  			return m_invalidColor;
96  		} else {
97  			return (Color) GUIApplication.getInstance().getConfig()
98  			.get("invalidColor");
99  		}
100 	}
101 }