View Javadoc
1   /**
2    * This file Copyright (c) 2011-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * Defines a column that displays the value of a boolean property with an icon or simple text.
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       * Specifies the display mode for representing the value of the boolean property.
72       */
73      public static enum DisplayMode {
74  
75          /** Generate an icon based on configured properties {@link BooleanPropertyColumnDefinition#getTrueIcon() trueIcon} and {@link BooleanPropertyColumnDefinition#getFalseIcon() falseIcon}. If blank nothing is rendered. */
76          ICON_ONLY,
77  
78          /** Generate a label based on configured properties {@link BooleanPropertyColumnDefinition#getTrueLabel() trueLabel} and {@link BooleanPropertyColumnDefinition#getFalseLabel() falseLabel}. If blank nothing is rendered. */
79          TEXT_ONLY,
80  
81          /** Generate both an icon and a label based on {@link BooleanPropertyColumnDefinition}'s configured properties. */
82          ICON_AND_TEXT
83      }
84  
85      /**
86       * Generates xhtml cell content (icon and/or text) for the {@link BooleanPropertyColumnDefinition}.
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              // get property value
100             Property<?> property = source.getContainerProperty(itemId, columnId);
101             boolean value = false;
102             if (property == null) {
103                 // no property found or its value was null, i.e. keep it false here
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             // generate cell content
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      * Defines the display mode for representing the value of the boolean property.
132      *
133      * @see DisplayMode
134      */
135     public DisplayMode getDisplayMode() {
136         return displayMode;
137     }
138 
139     public void setDisplayMode(DisplayMode displayMode) {
140         this.displayMode = displayMode;
141     }
142 
143     /**
144      * Defines the icon to display when the value of the boolean property is <code>false</code>.
145      */
146     public String getFalseIcon() {
147         return falseIcon;
148     }
149 
150     public void setFalseIcon(String falseIcon) {
151         this.falseIcon = falseIcon;
152     }
153 
154     /**
155      * Defines the icon to display when the value of the boolean property is <code>true</code>.
156      * Default value is <code>"icon-tick"</code>
157      */
158     public String getTrueIcon() {
159         return trueIcon;
160     }
161 
162     public void setTrueIcon(String trueIcon) {
163         this.trueIcon = trueIcon;
164     }
165 
166     /**
167      * Defines the text to display when the value of the boolean property is <code>false</code>.
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      * Defines the text to display when the value of the boolean property is <code>true</code>.
180      */
181     @I18nText
182     public String getTrueLabel() {
183         return trueLabel;
184     }
185 
186     public void setTrueLabel(String trueLabel) {
187         this.trueLabel = trueLabel;
188     }
189 }