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.ValidatedProperty;
26
27 import ch.elca.el4j.services.gui.swing.GUIApplication;
28
29 /**
30 * A validating cell renderer for lists.
31 *
32 * @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/DefaultValidatingCellRenderer.java $
33 *
34 * @author Stefan Wismer (SWI)
35 */
36 public class DefaultValidatingCellRenderer extends DefaultListCellRenderer {
37 /**
38 * Color to mark value as invalid.
39 */
40 protected final Color m_invalidColor;
41
42 /**
43 * The default contructor reading the invalidColor from Spring config.
44 */
45 public DefaultValidatingCellRenderer() {
46 m_invalidColor = null;
47 }
48
49 /**
50 * @param color the color to mark value as invalid
51 */
52 public DefaultValidatingCellRenderer(Color color) {
53 m_invalidColor = color;
54 }
55
56 /** {@inheritDoc} */
57 @Override
58 public Component getListCellRendererComponent(JList list,
59 Object value, int index, boolean isSelected, boolean cellHasFocus) {
60
61 // check if values is validateable
62 if (value == null || !ValidatedProperty.class.isAssignableFrom(
63 value.getClass())) {
64
65 return super.getListCellRendererComponent(
66 list, value, index, isSelected, cellHasFocus);
67 }
68
69 Component renderer = super.getListCellRendererComponent(list,
70 ((ValidatedProperty) value).getValue(), index, isSelected,
71 cellHasFocus);
72
73 if (!((ValidatedProperty) value).isValid()) {
74 renderer.setBackground(getInvalidColor());
75 }
76
77 return renderer;
78 }
79
80 /**
81 * @return the color to mark a value as invalid.
82 */
83 private Color getInvalidColor() {
84 if (m_invalidColor != null) {
85 return m_invalidColor;
86 } else {
87 return (Color) GUIApplication.getInstance().getConfig()
88 .get("invalidColor");
89 }
90 }
91 }