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.ActionManager;
39  import info.magnolia.ui.contentapp.browser.context.ValueContext;
40  import info.magnolia.ui.contentapp.browser.drop.RowDragger;
41  import info.magnolia.ui.contentapp.configuration.GridViewDefinition;
42  
43  import java.util.HashSet;
44  import java.util.Objects;
45  import java.util.Optional;
46  import java.util.stream.Stream;
47  
48  import org.apache.commons.lang3.BooleanUtils;
49  import org.vaadin.extension.gridscroll.GridScrollExtension;
50  import org.vaadin.extension.gridscroll.shared.ColumnResizeCompensationMode;
51  
52  import com.vaadin.event.ShortcutAction;
53  import com.vaadin.event.ShortcutListener;
54  import com.vaadin.shared.MouseEventDetails;
55  import com.vaadin.shared.ui.grid.DropMode;
56  import com.vaadin.ui.Composite;
57  import com.vaadin.ui.Grid;
58  
59  /**
60   * Base abstract implementation of Grid-based browsing views.
61   *
62   * @see info.magnolia.ui.contentapp.browser.ListView
63   * @see info.magnolia.ui.contentapp.browser.TreeView
64   */
65  public abstract class GridView<T, P extends GridViewPresenter<T>> extends Composite implements ContentView {
66  
67      protected final ValueContext<T> valueContext;
68      protected final GridViewDefinition<T> definition;
69      protected final Grid<T> grid;
70  
71      protected final P presenter;
72  
73      protected GridView(ValueContext<T> valueContext, GridViewDefinition<T> definition) {
74          this.valueContext = valueContext;
75          this.definition = definition;
76          this.presenter = createPresenter();
77  
78          grid = presenter.grid();
79          grid.setSizeFull();
80          grid.setSelectionMode(definition.isMultiSelect() ? Grid.SelectionMode.MULTI : Grid.SelectionMode.SINGLE);
81          grid.addSelectionListener(event -> valueContext.set(event.getAllSelectedItems()));
82          grid.addItemClickListener(event -> {
83              if (isSingleLeftClick(event.getMouseEventDetails())) {
84                  grid.deselectAll();
85                  grid.select(event.getItem());
86              }
87          });
88  
89          if (!definition.isReadOnly()) { // grid with actions
90              ActionManager actionManager = create(ActionManager.class, grid);
91              grid.addShortcutListener(new ShortcutListener("Default action", ShortcutAction.KeyCode.ENTER, new int[]{}) {
92                  @Override
93                  public void handleAction(Object sender, Object target) {
94                      actionManager.fireDefaultAction();
95                  }
96              });
97              grid.addItemClickListener(event -> {
98                  if (event.getMouseEventDetails().isDoubleClick()) {
99                      actionManager.fireDefaultAction();
100                 }
101             });
102         }
103 
104         grid.getEditor().setEnabled(false);
105         grid.getColumns().stream()
106                 .map(Grid.Column::getEditorBinding)
107                 .filter(Objects::nonNull)
108                 .findFirst()
109                 .ifPresent(any -> {
110                     grid.getEditor().setBuffered(true);
111                     grid.getEditor().setEnabled(true);
112                 });
113 
114         setLastColumnAsNotResizable(grid);
115 
116         new GridScrollExtension<>(grid).setColumnResizeCompensationMode(ColumnResizeCompensationMode.RESIZE_COLUMN);
117 
118 //        FastNavigation<T> navigation = new FastNavigation<>(grid, true, true);
119 //        navigation.setOpenEditorWithSingleClick(false);
120 //        navigation.setOpenEditorOnTyping(true);
121 //        navigation.addEditorOpenShortcut(13);
122 
123         Optional.ofNullable(definition.getDropConstraint()).ifPresent(dropConstraint -> create(RowDragger.class, grid, DropMode.BETWEEN, dropConstraint));
124 
125         valueContext.observe(value -> {
126             if (grid.getSelectedItems() != value) {
127                 grid.deselectAll();
128                 value.forEach(grid::select);
129             }
130         });
131         setCompositionRoot(grid);
132         setSizeFull();
133     }
134 
135     private boolean isSingleLeftClick(MouseEventDetails mouseEventDetails) {
136         return BooleanUtils.and(
137                 Stream.of(mouseEventDetails.getButton() == MouseEventDetails.MouseButton.LEFT,
138                         !mouseEventDetails.isDoubleClick(),
139                         !mouseEventDetails.isShiftKey(),
140                         !mouseEventDetails.isCtrlKey(),
141                         !mouseEventDetails.isAltKey())
142                         .toArray(Boolean[]::new));
143     }
144 
145     protected abstract P createPresenter();
146 
147     protected void select(Stream<T> values) {
148         if (this.definition.isMultiSelect()) {
149             this.grid.asMultiSelect().updateSelection(values.collect(toSet()), new HashSet<>());
150         } else {
151             values.findFirst().ifPresent(this.grid.asSingleSelect()::setValue);
152         }
153     }
154 
155     /**
156      * A workaround to a bug in GridScrollExtension. See https://github.com/TatuLund/grid-scroll-extension/issues/10
157      */
158     private void setLastColumnAsNotResizable(Grid<T> grid) {
159         if (grid.getColumns().size() > 0) {
160             grid.getColumns().get(grid.getColumns().size() - 1).setResizable(false);
161         }
162     }
163 }