View Javadoc
1   /**
2    * This file Copyright (c) 2019 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.contentapp.browser;
35  
36  import info.magnolia.objectfactory.ComponentProvider;
37  import info.magnolia.ui.contentapp.configuration.GridViewDefinition;
38  import info.magnolia.ui.framework.ioc.Destructible;
39  
40  import java.util.stream.Stream;
41  
42  import org.apache.commons.lang3.StringUtils;
43  
44  import com.vaadin.data.PropertySet;
45  import com.vaadin.data.provider.DataProvider;
46  import com.vaadin.icons.VaadinIcons;
47  import com.vaadin.server.FontIcon;
48  import com.vaadin.shared.data.sort.SortDirection;
49  import com.vaadin.ui.Grid;
50  import com.vaadin.ui.components.grid.HeaderCell;
51  import com.vaadin.ui.components.grid.HeaderRow;
52  
53  /**
54   * Base presenter of {@link GridView}.
55   *
56   * @param <T>
57   *     item type
58   */
59  public abstract class GridViewPresenter<T> implements Destructible {
60  
61      protected final GridViewDefinition<T> definition;
62  
63      protected final ComponentProvider componentProvider;
64  
65      private final ActionExecutionService actionExecutionService;
66  
67      private PropertySet<T> propertySet = null;
68  
69      protected DataProvider<T,?> dataProvider = null;
70  
71      protected Grid<T> grid = null;
72  
73      protected GridViewPresenter(GridViewDefinition<T> viewDefinition, ComponentProvider componentProvider, ActionExecutionService actionExecutionService) {
74          this.definition = viewDefinition;
75          this.componentProvider = componentProvider;
76          this.actionExecutionService = actionExecutionService;
77      }
78  
79      protected abstract DataProvider<T, ?> createDataProvider();
80  
81      protected abstract PropertySet<T> createPropertySet();
82  
83      public Grid<T> grid() {
84          if (this.grid == null) {
85              this.grid = createGrid();
86  
87              GridColumnConfigurer<T> columnConfigurer = componentProvider.newInstance(GridColumnConfigurer.class, definition, propertySet());
88              grid.setDataProvider(dataProvider());
89              columnConfigurer.configureColumns(grid);
90  
91              grid.addSortListener(event -> event.getSortOrder()
92                      .forEach(sortOrder -> {
93                          HeaderRow headerRow = grid.getDefaultHeaderRow();
94  
95                          grid.getColumns().stream()
96                                  .map(headerRow::getCell)
97                                  .forEach(headerCell -> headerCell.setHtml(headerCell.getText())); //remove existing arrows
98  
99                          HeaderCell headerCell = headerRow.getCell(sortOrder.getSorted());
100                         FontIcon icon = sortOrder.getDirection() == SortDirection.ASCENDING ? VaadinIcons.ARROWS_LONG_UP : VaadinIcons.ARROW_LONG_DOWN;
101                         headerCell.setHtml(headerCell.getText() + " " + icon.getHtml());
102                     })
103             );
104         }
105 
106         return grid;
107     }
108 
109     protected boolean isPropertyEditable(Grid.Column property, T item) {
110         return definition.getColumns().stream()
111                 .filter(columnDefinition -> StringUtils.equals(columnDefinition.getName(), property.getId()))
112                 .findFirst()
113                 .map(columnDefinition -> actionExecutionService.getAvailabilityFilter(Stream.of(item)).test(columnDefinition.getEditor().getAvailability()))
114                 .orElse(false);
115     }
116 
117     protected Grid<T> createGrid() {
118         return new MagnoliaGrid<>(
119                 propertySet(),
120                 this::isItemAvailable,
121                 this::isPropertyEditable);
122     }
123 
124     protected DataProvider<T, ?> dataProvider() {
125         if (this.dataProvider == null) {
126             this.dataProvider = createDataProvider();
127         }
128         return this.dataProvider;
129     }
130 
131     protected PropertySet<T> propertySet() {
132         if (this.propertySet == null) {
133             this.propertySet = createPropertySet();
134         }
135         return this.propertySet;
136     }
137 
138     public boolean isItemAvailable(T item) {
139         return true;
140     }
141 }