1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.ui.workbench.column.definition;
35
36 import static org.apache.commons.lang3.StringEscapeUtils.escapeHtml4;
37
38 import info.magnolia.i18nsystem.I18nText;
39 import info.magnolia.ui.workbench.column.AbstractColumnFormatter;
40
41 import javax.inject.Inject;
42
43 import org.apache.commons.lang3.StringUtils;
44
45 import com.vaadin.v7.data.Property;
46 import com.vaadin.v7.ui.Table;
47
48
49
50
51 public class BooleanPropertyColumnDefinition extends AbstractColumnDefinition {
52
53 private DisplayMode displayMode = DisplayMode.ICON_ONLY;
54 private String falseIcon;
55 private String trueIcon = "icon-tick";
56 private String falseLabel;
57 private String trueLabel;
58
59 public BooleanPropertyColumnDefinition() {
60 setFormatterClass(BooleanPropertyColumnFormatter.class);
61 setDisplayInChooseDialog(false);
62 setWidth(46);
63 }
64
65 @Override
66 public Class<?> getType() {
67 return Boolean.class;
68 }
69
70
71
72
73 public static enum DisplayMode {
74
75
76 ICON_ONLY,
77
78
79 TEXT_ONLY,
80
81
82 ICON_AND_TEXT
83 }
84
85
86
87
88 public static class BooleanPropertyColumnFormatter extends AbstractColumnFormatter<BooleanPropertyColumnDefinition> {
89
90 @Inject
91 public BooleanPropertyColumnFormatter(BooleanPropertyColumnDefinition definition) {
92 super(definition);
93 }
94
95 @Override
96 public String generateCell(Table source, Object itemId, Object columnId) {
97 StringBuilder sb = new StringBuilder();
98
99
100 Property<?> property = source.getContainerProperty(itemId, columnId);
101 boolean value = false;
102 if (property == null) {
103
104 } else if (Boolean.class.isAssignableFrom(property.getType())) {
105 value = ((Boolean) property.getValue()).booleanValue();
106 } else if (String.class.isAssignableFrom(property.getType())) {
107 value = Boolean.parseBoolean((String) property.getValue());
108 }
109
110
111 if (definition.getDisplayMode() != DisplayMode.TEXT_ONLY) {
112 if (value && StringUtils.isNotBlank(definition.getTrueIcon())) {
113 sb.append("<span class=\"").append(escapeHtml4(definition.getTrueIcon())).append("\"></span>");
114 } else if (!value && StringUtils.isNotBlank(definition.getFalseIcon())) {
115 sb.append("<span class=\"").append(escapeHtml4(definition.getFalseIcon())).append("\"></span>");
116 }
117 }
118 if (definition.getDisplayMode() != DisplayMode.ICON_ONLY) {
119 if (value && StringUtils.isNotBlank(definition.getTrueLabel())) {
120 sb.append("<span>").append(escapeHtml4(definition.getTrueLabel())).append("</span>");
121 } else if (!value && StringUtils.isNotBlank(definition.getFalseLabel())) {
122 sb.append("<span>").append(escapeHtml4(definition.getFalseLabel())).append("</span>");
123 }
124 }
125
126 return StringUtils.isNotBlank(sb) ? sb.toString() : null;
127 }
128 }
129
130
131
132
133
134
135 public DisplayMode getDisplayMode() {
136 return displayMode;
137 }
138
139 public void setDisplayMode(DisplayMode displayMode) {
140 this.displayMode = displayMode;
141 }
142
143
144
145
146 public String getFalseIcon() {
147 return falseIcon;
148 }
149
150 public void setFalseIcon(String falseIcon) {
151 this.falseIcon = falseIcon;
152 }
153
154
155
156
157
158 public String getTrueIcon() {
159 return trueIcon;
160 }
161
162 public void setTrueIcon(String trueIcon) {
163 this.trueIcon = trueIcon;
164 }
165
166
167
168
169 @I18nText
170 public String getFalseLabel() {
171 return falseLabel;
172 }
173
174 public void setFalseLabel(String falseLabel) {
175 this.falseLabel = falseLabel;
176 }
177
178
179
180
181 @I18nText
182 public String getTrueLabel() {
183 return trueLabel;
184 }
185
186 public void setTrueLabel(String trueLabel) {
187 this.trueLabel = trueLabel;
188 }
189 }