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