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.list;
35  
36  import info.magnolia.ui.vaadin.grid.MagnoliaTable;
37  import info.magnolia.ui.workbench.column.definition.ColumnFormatter;
38  
39  import java.util.ArrayList;
40  import java.util.Arrays;
41  import java.util.Collections;
42  import java.util.HashSet;
43  import java.util.LinkedHashSet;
44  import java.util.List;
45  import java.util.Set;
46  
47  import org.apache.commons.lang3.ObjectUtils;
48  import org.slf4j.Logger;
49  import org.slf4j.LoggerFactory;
50  
51  import com.vaadin.shared.MouseEventDetails.MouseButton;
52  import com.vaadin.ui.Component;
53  import com.vaadin.v7.data.Container;
54  import com.vaadin.v7.data.Item;
55  import com.vaadin.v7.data.Property;
56  import com.vaadin.v7.data.Property.ValueChangeEvent;
57  import com.vaadin.v7.event.ItemClickEvent;
58  import com.vaadin.v7.event.ItemClickEvent.ItemClickListener;
59  import com.vaadin.v7.ui.Table;
60  import com.vaadin.v7.ui.Table.ColumnResizeEvent;
61  import com.vaadin.v7.ui.Table.TableDragMode;
62  
63  /**
64   * Default Vaadin implementation of the list view.
65   */
66  public class ListViewImpl implements ListView {
67  
68      private static final Logger log = LoggerFactory.getLogger(ListViewImpl.class);
69  
70      private static final int MINIMUM_COLUMN_WIDTH = 46;
71      private static final String DELETED_ROW_STYLENAME = "deleted";
72  
73      private Table table;
74  
75      private Listener listener;
76  
77      protected void initializeTable(Table table) {
78          table.setSizeFull();
79  
80          table.setSelectable(true);
81          table.setMultiSelect(true);
82          table.setNullSelectionAllowed(true);
83  
84          table.setDragMode(TableDragMode.NONE);
85          table.setEditable(false);
86          table.setColumnCollapsingAllowed(false);
87          table.setColumnReorderingAllowed(false);
88          table.setSortEnabled(true);
89  
90          table.setCellStyleGenerator(new Table.CellStyleGenerator() {
91  
92  
93              @Override
94              public String getStyle(Table source, Object itemId, Object propertyId) {
95                  // icon style is expected on the whole table row, not on a column matching a specific propertyId
96                  if (propertyId == null && itemId != null) {
97                      final Item item = source.getContainerDataSource().getItem(itemId);
98                      if (item == null) {
99                          return DELETED_ROW_STYLENAME;
100                     } else {
101                         return listener.getIcon(item);
102                     }
103                 }
104                 return null;
105             }
106         });
107 
108         // this one was re-added since the check-all worked for tree only but nor for list- and search-view, see MGNLUI-1958
109         // TODO fgrilli: a workaround for MGNLUI-1651
110         table.addStyleName("no-header-checkbox");
111         this.table = table;
112         bindHandlers();
113     }
114 
115     protected void bindHandlers() {
116         table.addValueChangeListener(new Property.ValueChangeListener() {
117             @Override
118             public void valueChange(ValueChangeEvent event) {
119                 Object value = event.getProperty().getValue();
120 
121                 if (listener != null) {
122                     Set<Object> items;
123                     if (value instanceof Set) {
124                         items = (Set) value;
125                         // Container roots are expected to have null parent itemId.
126                         // Then, we remove it from table selection, so next multi-selection won't add to it, and let valueChangeListener kick in again
127                         if (items.size() == 1 && items.contains(null)) {
128                             table.setValue(null);
129                             return;
130                         }
131                     } else if (value == null) {
132                         items = Collections.emptySet();
133                     } else {
134                         items = new LinkedHashSet<>();
135                         items.add(value);
136                     }
137                     listener.onItemSelection(items);
138                 }
139             }
140         });
141 
142         table.addItemClickListener(new ItemClickListener() {
143 
144             @Override
145             public void itemClick(ItemClickEvent event) {
146 
147                 if (event.getButton() == MouseButton.RIGHT) {
148                     if (listener != null) {
149                         listener.onRightClick(event.getItemId(), event.getClientX(), event.getClientY());
150                     }
151                 } else if (event.isDoubleClick()) {
152                     if (listener != null) {
153                         listener.onDoubleClick(event.getItemId());
154                     }
155                 } else {
156                     Object value = table.getValue();
157                     if (value != null) {
158                         Set<Object> items;
159                         if (value instanceof Set) {
160                             items = (Set<Object>) value;
161                         } else {
162                             items = new LinkedHashSet<>();
163                             items.add(value);
164                         }
165                         if (items.size() == 1 && ObjectUtils.equals(items.iterator().next(), event.getItemId())) {
166                             table.setValue(null);
167                         }
168                     }
169                 }
170             }
171         });
172 
173         table.addColumnResizeListener(new Table.ColumnResizeListener() {
174 
175             @Override
176             public void columnResize(ColumnResizeEvent event) {
177                 if (event.getCurrentWidth() < MINIMUM_COLUMN_WIDTH) {
178                     table.setColumnWidth(event.getPropertyId(), MINIMUM_COLUMN_WIDTH);
179                 }
180             }
181         });
182     }
183 
184     protected Listener getListener() {
185         return listener;
186     }
187 
188     @Override
189     public void setListener(Listener listener) {
190         this.listener = listener;
191     }
192 
193     @Override
194     public void setContainer(Container container) {
195         table = createTable(container);
196         initializeTable(table);
197     }
198 
199     protected Table createTable(Container container) {
200         return new MagnoliaTable(container);
201     }
202 
203     @Override
204     public void addColumn(String propertyId, String title) {
205         if (!table.getContainerPropertyIds().contains(propertyId)) {
206             log.warn("Ignoring column '{}', container does not support this propertyId.", propertyId);
207             return;
208         }
209         table.setColumnHeader(propertyId, title);
210         List<Object> visibleColumns = new ArrayList<Object>(Arrays.asList(table.getVisibleColumns()));
211         if (!visibleColumns.contains(propertyId)) {
212             visibleColumns.add(propertyId);
213         }
214         table.setVisibleColumns(visibleColumns.toArray());
215     }
216 
217     @Override
218     public void addColumn(String propertyId, String title, int width) {
219         addColumn(propertyId, title);
220         table.setColumnWidth(propertyId, width);
221     }
222 
223     @Override
224     public void addColumn(String propertyId, String title, float expandRatio) {
225         addColumn(propertyId, title);
226         table.setColumnExpandRatio(propertyId, expandRatio);
227     }
228 
229     @Override
230     public void setColumnFormatter(String propertyId, ColumnFormatter formatter) {
231         table.addGeneratedColumn(propertyId, formatter);
232     }
233 
234     @Override
235     public void clearColumns() {
236         table.setVisibleColumns(new Object[] {});
237     }
238 
239     @Override
240     public void select(List<Object> itemIds) {
241         if (itemIds == null) {
242             table.setValue(null);
243             return;
244         }
245 
246         // convert to set before comparing with actual table selection (which is also a set underneath)
247         Set<Object> uniqueItemIds = new HashSet<>(itemIds);
248         if (uniqueItemIds.equals(table.getValue())) {
249             // selection already in sync, nothing to do
250             return;
251         }
252 
253         table.setValue(uniqueItemIds);
254         // do not #setCurrentPageFirstItemId because AbstractJcrContainer's index resolution is super slow.
255     }
256 
257     @Override
258     public void expand(Object itemId) {
259     }
260 
261     @Override
262     public Component asVaadinComponent() {
263         return table;
264     }
265 
266     @Override
267     public void setMultiselect(boolean multiselect) {
268         table.setMultiSelect(multiselect);
269     }
270 
271     @Override
272     public void onShortcutKey(int keyCode, int[] modifierKeys) {
273         if (listener != null) {
274             listener.onShortcutKey(keyCode, modifierKeys);
275         }
276     }
277 
278 }