View Javadoc
1   /**
2    * This file Copyright (c) 2012-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.vaadin.grid;
35  
36  import java.util.Collection;
37  import java.util.Map;
38  
39  import org.apache.commons.lang3.StringEscapeUtils;
40  import org.slf4j.Logger;
41  import org.slf4j.LoggerFactory;
42  
43  import com.vaadin.ui.Component;
44  import com.vaadin.v7.data.Container;
45  import com.vaadin.v7.data.Property;
46  import com.vaadin.v7.data.Validator;
47  import com.vaadin.v7.data.util.HierarchicalContainer;
48  import com.vaadin.v7.shared.ui.treetable.TreeTableState;
49  import com.vaadin.v7.ui.TreeTable;
50  
51  /**
52   * VMagnoliaTreeTable.
53   */
54  public class MagnoliaTreeTable extends TreeTable {
55  
56      private static Logger log = LoggerFactory.getLogger(MagnoliaTreeTable.class);
57  
58      public MagnoliaTreeTable(Container dataSource) {
59          super(null, dataSource);
60          addStyleName("v-magnolia-table");
61          setCacheRate(4);
62      }
63  
64      public MagnoliaTreeTable() {
65          this(new HierarchicalContainer());
66      }
67  
68      /**
69       * This method is made public in order to be able to delegate the call to
70       * tree tables' {@link HierarchicalStrategy} instead of the actual container
71       * which can be very slow.
72       */
73      @Override
74      public int indexOfId(Object itemId) {
75          return super.indexOfId(itemId);
76      }
77  
78      @Override
79      protected String formatPropertyValue(Object rowId, Object colId, Property<?> property) {
80          String result = super.formatPropertyValue(rowId, colId, property);
81          return StringEscapeUtils.escapeHtml4(result);
82      }
83  
84      @Override
85      public void changeVariables(Object source, Map<String, Object> variables) {
86          super.changeVariables(source, variables);
87          if (variables.containsKey("selectAll")) {
88              boolean selectAll = (Boolean) variables.get("selectAll");
89              if (selectAll) {
90                  Collection<?> ids = getItemIds();
91                  for (final Object id : ids) {
92                      select(id);
93                  }
94              } else {
95                  setValue(null);
96              }
97          }
98  
99          if (variables.containsKey("toggleSelection")) {
100             boolean selected = (Boolean) variables.get("toggleSelection");
101             String key = String.valueOf(variables.get("toggledRowId"));
102             final Object id = itemIdMapper.get(key);
103             if (selected) {
104                 select(id);
105             } else {
106                 unselect(id);
107             }
108         }
109     }
110 
111     /**
112      * Make registerComponent public so that inplace-editing fields can be added to table.
113      */
114     @Override
115     public void registerComponent(Component component) {
116         super.registerComponent(component);
117     }
118 
119     /**
120      * MGNLUI-729 Overridden so that table is not marked as dirty without changes. This was made to prevent selection from being sent over again.
121      * Beware, this breaks the normal state update mechanism and might require that you mark it as dirty explicitly.
122      */
123     @Override
124     protected TreeTableState getState() {
125         return (TreeTableState) getState(false);
126     }
127 
128     /**
129      * MGNLUI-962 Overridden to fulfill AbstractField's repaintIsNotNeeded, super impl returns empty collection instead.
130      */
131     @Override
132     public Collection<Validator> getValidators() {
133         return null;
134     }
135 
136     /**
137      * @deprecated since 5.4.6, use #isDescendantOf on the TreeView itself, not on the TreeTable.
138      */
139     @Deprecated
140     public boolean isDescendantOf(final Object itemId, final Object parentId) {
141         Hierarchical container = getContainerDataSource();
142         Object id = itemId;
143         while (!container.isRoot(id)) {
144             id = container.getParent(id);
145             if (id.equals(parentId)) {
146                 return true;
147             }
148         }
149         return false;
150     }
151 }