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 static java.util.stream.Collectors.toSet;
37  
38  import info.magnolia.ui.contentapp.browser.actions.ContextAwareActionPopup;
39  import info.magnolia.ui.contentapp.browser.actions.ShortcutActionsExecutor;
40  import info.magnolia.ui.ValueContext;
41  import info.magnolia.ui.contentapp.browser.drop.RowDragger;
42  import info.magnolia.ui.contentapp.configuration.GridViewDefinition;
43  
44  import java.util.HashSet;
45  import java.util.Objects;
46  import java.util.Optional;
47  import java.util.stream.Stream;
48  
49  import org.vaadin.extension.gridscroll.GridScrollExtension;
50  import org.vaadin.extension.gridscroll.shared.ColumnResizeCompensationMode;
51  
52  import com.vaadin.shared.ui.grid.DropMode;
53  import com.vaadin.ui.Composite;
54  import com.vaadin.ui.Grid;
55  import com.vaadin.ui.components.grid.Editor;
56  
57  /**
58   * Base abstract implementation of Grid-based browsing views.
59   *
60   * @see info.magnolia.ui.contentapp.browser.ListView
61   * @see info.magnolia.ui.contentapp.browser.TreeView
62   */
63  public abstract class GridView<T, P extends GridViewPresenter<T>> extends Composite implements ContentView {
64  
65      protected final ValueContext<T> valueContext;
66      protected final GridViewDefinition<T> definition;
67      protected final Grid<T> grid;
68  
69      protected final P presenter;
70      protected final Editor<T> editor;
71  
72      protected GridView(ValueContext<T> valueContext, GridViewDefinition<T> definition) {
73          this.valueContext = valueContext;
74          this.definition = definition;
75          this.presenter = createPresenter();
76          this.grid = presenter.grid();
77          this.editor = grid.getEditor();
78  
79          ContextAwareActionPopup actionPopup = null;
80          if (!definition.isReadOnly()) {
81              actionPopup = create(ContextAwareActionPopup.class, grid);
82          }
83          final ShortcutActionsExecutor actionsExecutor =
84                  actionPopup != null ? actionPopup : ShortcutActionsExecutor.NOOP;
85  
86          grid.setSizeFull();
87          grid.setSelectionMode(definition.isMultiSelect() ? Grid.SelectionMode.MULTI : Grid.SelectionMode.SINGLE);
88          grid.addSelectionListener(event -> valueContext.set(event.getAllSelectedItems()));
89  
90          editor.setEnabled(false);
91          grid.getColumns().stream()
92                  .map(Grid.Column::getEditorBinding)
93                  .filter(Objects::nonNull)
94                  .findFirst()
95                  .ifPresent(any -> {
96                      editor.setBuffered(false);
97                      editor.setEnabled(true);
98                  });
99  
100         setLastColumnAsNotResizable(grid);
101 
102         new GridScrollExtension<>(grid).setColumnResizeCompensationMode(ColumnResizeCompensationMode.RESIZE_COLUMN);
103 
104         // MGNLUI-5349 the following column resize listener will over-rule the programmatically
105         // set max/min column width configuration if so is decided by the user
106         // (i.e. the resize originates from the client-side event).
107         //
108         // We apply such measure in order to mitigate the UI glitches caused by the quirks and conflicts
109         // between the used GridScollExtension and in-build Grid’s column resize logic.
110         //
111         grid.addColumnResizeListener(event -> {
112             if (event.isUserOriginated()) {
113                 Grid.Column<?, ?> column = event.getColumn();
114                 double width = column.getWidth();
115                 double minimumWidth = column.getMinimumWidth();
116                 double maximumWidth = column.getMaximumWidth();
117 
118                 column.setMinimumWidth(minimumWidth > 0 && width <= minimumWidth ? width : minimumWidth);
119                 column.setMaximumWidth(maximumWidth > 0 && width >= maximumWidth ? width : maximumWidth);
120             }
121         });
122 
123         Optional.ofNullable(definition.getDropConstraint())
124                 .ifPresent(dropConstraint -> create(RowDragger.class, grid, DropMode.BETWEEN, create(dropConstraint.getImplementationClass(), dropConstraint)));
125 
126         valueContext.observe(items -> {
127             if (grid.getSelectedItems() != items) {
128                 grid.deselectAll();
129                 items.forEach(grid::select);
130             }
131         });
132 
133         setCompositionRoot(create(GridWithShortcuts.class, grid, actionsExecutor));
134         grid.focus();
135         setSizeFull();
136     }
137 
138     protected abstract P createPresenter();
139 
140     protected void select(Stream<T> values) {
141         if (this.definition.isMultiSelect()) {
142             this.grid.asMultiSelect().updateSelection(values.collect(toSet()), new HashSet<>());
143         } else {
144             values.findFirst().ifPresent(this.grid.asSingleSelect()::setValue);
145         }
146     }
147 
148     /**
149      * A workaround to a bug in GridScrollExtension. See https://github.com/TatuLund/grid-scroll-extension/issues/10
150      */
151     private void setLastColumnAsNotResizable(Grid<T> grid) {
152         if (grid.getColumns().size() > 0) {
153             grid.getColumns().get(grid.getColumns().size() - 1).setResizable(false);
154         }
155     }
156 }