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.annotation.deprecation.MgnlDeprecated;
39  import info.magnolia.i18nsystem.I18nText;
40  import info.magnolia.ui.workbench.column.AbstractColumnFormatter;
41  
42  import javax.inject.Inject;
43  
44  import org.apache.commons.lang3.StringUtils;
45  
46  import com.vaadin.v7.data.Property;
47  import com.vaadin.v7.ui.Table;
48  
49  /**
50   * Defines a column that displays the value of a boolean property with an icon or simple text.
51   * @deprecated since 6.2.3. No direct replacement.
52   */
53  @MgnlDeprecated(since = "6.2.3", description = "No direct replacement.")
54  public class BooleanPropertyColumnDefinition extends AbstractColumnDefinition {
55  
56      private DisplayMode displayMode = DisplayMode.ICON_ONLY;
57      private String falseIcon;
58      private String trueIcon = "icon-tick";
59      private String falseLabel;
60      private String trueLabel;
61  
62      public BooleanPropertyColumnDefinition() {
63          setFormatterClass(BooleanPropertyColumnFormatter.class);
64          setDisplayInChooseDialog(false);
65          setWidth(46);
66      }
67  
68      @Override
69      public Class<?> getType() {
70          return Boolean.class;
71      }
72  
73      /**
74       * Specifies the display mode for representing the value of the boolean property.
75       */
76      public static enum DisplayMode {
77  
78          /** Generate an icon based on configured properties {@link BooleanPropertyColumnDefinition#getTrueIcon() trueIcon} and {@link BooleanPropertyColumnDefinition#getFalseIcon() falseIcon}. If blank nothing is rendered. */
79          ICON_ONLY,
80  
81          /** Generate a label based on configured properties {@link BooleanPropertyColumnDefinition#getTrueLabel() trueLabel} and {@link BooleanPropertyColumnDefinition#getFalseLabel() falseLabel}. If blank nothing is rendered. */
82          TEXT_ONLY,
83  
84          /** Generate both an icon and a label based on {@link BooleanPropertyColumnDefinition}'s configured properties. */
85          ICON_AND_TEXT
86      }
87  
88      /**
89       * Generates xhtml cell content (icon and/or text) for the {@link BooleanPropertyColumnDefinition}.
90       */
91      public static class BooleanPropertyColumnFormatter extends AbstractColumnFormatter<BooleanPropertyColumnDefinition> {
92  
93          @Inject
94          public BooleanPropertyColumnFormatter(BooleanPropertyColumnDefinition definition) {
95              super(definition);
96          }
97  
98          @Override
99          public String generateCell(Table source, Object itemId, Object columnId) {
100             StringBuilder sb = new StringBuilder();
101 
102             // get property value
103             Property<?> property = source.getContainerProperty(itemId, columnId);
104             boolean value = false;
105             if (property == null) {
106                 // no property found or its value was null, i.e. keep it false here
107             } else if (Boolean.class.isAssignableFrom(property.getType())) {
108                 value = ((Boolean) property.getValue()).booleanValue();
109             } else if (String.class.isAssignableFrom(property.getType())) {
110                 value = Boolean.parseBoolean((String) property.getValue());
111             }
112 
113             // generate cell content
114             if (definition.getDisplayMode() != DisplayMode.TEXT_ONLY) {
115                 if (value && StringUtils.isNotBlank(definition.getTrueIcon())) {
116                     sb.append("<span class=\"").append(escapeHtml4(definition.getTrueIcon())).append("\"></span>");
117                 } else if (!value && StringUtils.isNotBlank(definition.getFalseIcon())) {
118                     sb.append("<span class=\"").append(escapeHtml4(definition.getFalseIcon())).append("\"></span>");
119                 }
120             }
121             if (definition.getDisplayMode() != DisplayMode.ICON_ONLY) {
122                 if (value && StringUtils.isNotBlank(definition.getTrueLabel())) {
123                     sb.append("<span>").append(escapeHtml4(definition.getTrueLabel())).append("</span>");
124                 } else if (!value && StringUtils.isNotBlank(definition.getFalseLabel())) {
125                     sb.append("<span>").append(escapeHtml4(definition.getFalseLabel())).append("</span>");
126                 }
127             }
128 
129             return StringUtils.isNotBlank(sb) ? sb.toString() : null;
130         }
131     }
132 
133     /**
134      * Defines the display mode for representing the value of the boolean property.
135      *
136      * @see DisplayMode
137      */
138     public DisplayMode getDisplayMode() {
139         return displayMode;
140     }
141 
142     public void setDisplayMode(DisplayMode displayMode) {
143         this.displayMode = displayMode;
144     }
145 
146     /**
147      * Defines the icon to display when the value of the boolean property is <code>false</code>.
148      */
149     public String getFalseIcon() {
150         return falseIcon;
151     }
152 
153     public void setFalseIcon(String falseIcon) {
154         this.falseIcon = falseIcon;
155     }
156 
157     /**
158      * Defines the icon to display when the value of the boolean property is <code>true</code>.
159      * Default value is <code>"icon-tick"</code>
160      */
161     public String getTrueIcon() {
162         return trueIcon;
163     }
164 
165     public void setTrueIcon(String trueIcon) {
166         this.trueIcon = trueIcon;
167     }
168 
169     /**
170      * Defines the text to display when the value of the boolean property is <code>false</code>.
171      */
172     @I18nText
173     public String getFalseLabel() {
174         return falseLabel;
175     }
176 
177     public void setFalseLabel(String falseLabel) {
178         this.falseLabel = falseLabel;
179     }
180 
181     /**
182      * Defines the text to display when the value of the boolean property is <code>true</code>.
183      */
184     @I18nText
185     public String getTrueLabel() {
186         return trueLabel;
187     }
188 
189     public void setTrueLabel(String trueLabel) {
190         this.trueLabel = trueLabel;
191     }
192 }