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.ValueContext;
39  import info.magnolia.ui.contentapp.browser.actions.ContextAwareActionPopup;
40  import info.magnolia.ui.contentapp.browser.actions.ShortcutActionsExecutor;
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.Iterator;
46  import java.util.Objects;
47  import java.util.Optional;
48  import java.util.stream.Stream;
49  
50  import com.vaadin.data.provider.DataProvider;
51  import com.vaadin.shared.ui.grid.DropMode;
52  import com.vaadin.shared.ui.grid.ScrollDestination;
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<T> 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         Optional.ofNullable(definition.getDropConstraint())
103                 .ifPresent(dropConstraint -> create(RowDragger.class, grid, DropMode.BETWEEN, create(dropConstraint.getImplementationClass(), dropConstraint)));
104 
105         valueContext.observe(items -> {
106             if (grid.getSelectedItems() != items) {
107                 grid.deselectAll();
108                 items.forEach(grid::select);
109             }
110         });
111 
112         setCompositionRoot(create(GridWithShortcuts.class, grid, actionsExecutor));
113         grid.focus();
114         setSizeFull();
115         addAttachListener(event -> scrollToSelectedItem());
116     }
117 
118     @Override
119     public void setVisible(boolean visible) {
120         if (visible) {
121             // covers the case when selecting item in tree, then switching tree -> list -> tree without any further selection in between
122             // would be covered by GridScrollExtension.setRestorePosition but this has to be disabled for Grid#scrollTo to work for other cases
123             scrollToSelectedItem();
124         }
125         super.setVisible(visible);
126     }
127 
128     protected void scrollToSelectedItem() {
129         if (definition.isScrollToSelectedItem()) {
130             grid.getSelectedItems().stream().findFirst()
131                     .ifPresent(selectedItem -> {
132                         int index = 0;
133                         final DataProvider<T, ?> dataProvider = grid.getDataProvider();
134                         final Object selectedItemId = dataProvider.getId(selectedItem);
135                         for (Iterator<T> it = fetchItems().iterator(); it.hasNext(); ) {
136                             T t = it.next();
137                             if (dataProvider.getId(t).equals(selectedItemId)) {
138                                 break;
139                             } else {
140                                 index++;
141                             }
142                         }
143                         grid.scrollTo(index, ScrollDestination.MIDDLE); //org.vaadin.extension.gridscroll.GridScrollExtension.setRestorePosition has to be disabled
144                     });
145         }
146     }
147 
148     Stream<T> fetchItems() {
149         return grid.getDataCommunicator().fetchItemsWithRange(0, Integer.MAX_VALUE).stream();
150     }
151 
152     protected abstract P createPresenter();
153 
154     protected void select(Stream<T> values) {
155         if (this.definition.isMultiSelect()) {
156             this.grid.asMultiSelect().updateSelection(values.collect(toSet()), new HashSet<>());
157         } else {
158             values.findFirst().ifPresent(this.grid.asSingleSelect()::setValue);
159         }
160     }
161 
162     /**
163      * A workaround to a bug in GridScrollExtension. See https://github.com/TatuLund/grid-scroll-extension/issues/10
164      */
165     private void setLastColumnAsNotResizable(Grid<T> grid) {
166         if (grid.getColumns().size() > 0) {
167             grid.getColumns().get(grid.getColumns().size() - 1).setResizable(false);
168         }
169     }
170 }