1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
33
34
35
36
37
38 public class DefaultValidatingComboBoxRenderer
39 extends DefaultListCellRenderer implements ComboBoxRenderer {
40
41
42
43
44 protected final Color m_invalidColor;
45
46
47
48
49 protected String m_property;
50
51
52
53
54 protected boolean m_validate;
55
56
57
58
59
60 public DefaultValidatingComboBoxRenderer() {
61 this(null, null, false);
62 }
63
64
65
66
67
68 public DefaultValidatingComboBoxRenderer(String property,
69 boolean validate) {
70 this(null, property, validate);
71 }
72
73
74
75
76
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
86 public void setProperty(String property) {
87 m_property = property;
88 }
89
90
91
92
93 public void setValidate(boolean validate) {
94 m_validate = validate;
95 }
96
97
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
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 }