View Javadoc
1   /**
2    * This file Copyright (c) 2013-2018 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.workbench;
35  
36  import info.magnolia.event.EventBus;
37  import info.magnolia.objectfactory.ComponentProvider;
38  import info.magnolia.ui.vaadin.integration.contentconnector.ContentConnector;
39  import info.magnolia.ui.workbench.column.definition.ColumnDefinition;
40  import info.magnolia.ui.workbench.definition.ContentPresenterDefinition;
41  import info.magnolia.ui.workbench.definition.WorkbenchDefinition;
42  import info.magnolia.ui.workbench.event.ItemDoubleClickedEvent;
43  import info.magnolia.ui.workbench.event.ItemRightClickedEvent;
44  import info.magnolia.ui.workbench.event.ItemShortcutKeyEvent;
45  import info.magnolia.ui.workbench.event.SelectionChangedEvent;
46  
47  import java.util.ArrayList;
48  import java.util.Collections;
49  import java.util.HashSet;
50  import java.util.Iterator;
51  import java.util.List;
52  import java.util.Set;
53  
54  import org.slf4j.Logger;
55  import org.slf4j.LoggerFactory;
56  
57  import com.google.common.collect.Lists;
58  import com.vaadin.v7.data.Container;
59  import com.vaadin.v7.data.Item;
60  
61  /**
62   * Abstract JCR-agnostic {@link ContentPresenter} implementation.
63   */
64  public abstract class AbstractContentPresenterBase implements ContentPresenter, ContentView.Listener {
65  
66      private static final Logger log = LoggerFactory.getLogger(AbstractContentPresenter.class);
67  
68      protected static final String ICON_PROPERTY = "icon-node-data";
69  
70      protected static final String ICON_TRASH = "icon-trash";
71  
72      protected EventBus eventBus;
73  
74      protected WorkbenchDefinition workbenchDefinition;
75  
76      private List<Object> selectedItemIds = new ArrayList<>();
77  
78      protected String viewTypeName;
79  
80      protected ContentConnector contentConnector;
81  
82      private final ComponentProvider componentProvider;
83  
84      protected Container container;
85  
86      public AbstractContentPresenterBase(ComponentProvider componentProvider) {
87          this.componentProvider = componentProvider;
88      }
89  
90      protected ComponentProvider getComponentProvider() {
91          return componentProvider;
92      }
93  
94      @Override
95      public ContentView start(WorkbenchDefinition workbenchDefinition, EventBus eventBus, String viewTypeName, ContentConnector contentConnector) {
96          this.workbenchDefinition = workbenchDefinition;
97          this.eventBus = eventBus;
98          this.viewTypeName = viewTypeName;
99          this.contentConnector = contentConnector;
100         return null;
101     }
102 
103     @Override
104     public List<Object> getSelectedItemIds() {
105         return this.selectedItemIds;
106     }
107 
108     public Object getSelectedItemId() {
109         return selectedItemIds.isEmpty() ? null : selectedItemIds.get(0);
110     }
111 
112     @Override
113     public void setSelectedItemIds(List<Object> selectedItemIds) {
114         this.selectedItemIds = selectedItemIds;
115     }
116 
117     // CONTENT VIEW LISTENER
118 
119     @Override
120     public void onItemSelection(Set<Object> itemIds) {
121         Object rootItemId = contentConnector.getDefaultItemId();
122         if (itemIds == null || itemIds.isEmpty()) {
123             log.debug("Got null com.vaadin.v7.data.Item. ItemSelectedEvent will be fired with null path.");
124             setSelectedItemIds(Lists.newArrayList(rootItemId));
125         } else {
126             List<Object> selectedIds = new ArrayList<>(itemIds.size());
127             boolean isMultipleSelection = itemIds.size() > 1;
128 
129             for (Object id : itemIds) {
130                 if (isMultipleSelection && rootItemId.equals(id)) {
131                     // in a multiple selection done via checkbox the root path is always added to the selection, just skip it
132                     continue;
133                 }
134                 selectedIds.add(id);
135             }
136 
137             setSelectedItemIds(selectedIds);
138             log.debug("com.vaadin.v7.data.Item at {} was selected. Firing ItemSelectedEvent...", selectedIds);
139         }
140         eventBus.fireEvent(new SelectionChangedEvent(Collections.unmodifiableSet(new HashSet<>(selectedItemIds))));
141 
142     }
143 
144     @Override
145     public void onDoubleClick(Object itemId) {
146         if (itemId != null) {
147             try {
148                 setSelectedItemIds(Lists.newArrayList(itemId));
149                 log.debug("com.vaadin.data.Item at {} was double clicked. Firing ItemDoubleClickedEvent...", getSelectedItemId());
150                 eventBus.fireEvent(new ItemDoubleClickedEvent(getSelectedItemId()));
151             } catch (Exception e) {
152                 log.error("An error occurred while double clicking on a row in the data grid", e);
153             }
154         } else {
155             log.warn("Got null com.vaadin.v7.data.Item. No event will be fired.");
156         }
157     }
158 
159     @Override
160     public void onRightClick(Object itemId, int clickX, int clickY) {
161         if (itemId != null) {
162             try {
163                 // if the right-clicket item is not yet selected
164                 if (!selectedItemIds.contains(itemId)) {
165                     List<Object> ids = Lists.newArrayList(itemId);
166                     setSelectedItemIds(ids);
167                     select(ids);
168                 }
169                 log.debug("com.vaadin.v7.data.Item at {} was right clicked. Firing ItemRightClickedEvent...", itemId);
170                 eventBus.fireEvent(new ItemRightClickedEvent(itemId, clickX, clickY));
171             } catch (Exception e) {
172                 log.error("An error occurred while right clicking on a row in the data grid", e);
173             }
174         } else {
175             log.warn("Got null com.vaadin.v7.data.Item. No event will be fired.");
176         }
177     }
178 
179     @Override
180     public void onShortcutKey(int keyCode, int[] modifierKeys) {
181         if (selectedItemIds.size() >= 1) {
182             try {
183                 log.debug("com.vaadin.data.Item at {} was keyboard clicked. Firing ItemShortcutKeyEvent...", getSelectedItemId());
184                 eventBus.fireEvent(new ItemShortcutKeyEvent(selectedItemIds, keyCode, modifierKeys));
185             } catch (Exception e) {
186                 log.error("An error occurred while a key was pressed with a selected row in the data grid", e);
187             }
188         } else {
189             log.warn("Got null com.vaadin.v7.data.Item. No event will be fired.");
190         }
191     }
192 
193     protected Iterator<ColumnDefinition> getColumnsIterator() {
194         Iterator<ContentPresenterDefinition> viewsIterator = workbenchDefinition.getContentViews().iterator();
195         while (viewsIterator.hasNext()) {
196             ContentPresenterDefinition contentView = viewsIterator.next();
197             String viewType = contentView.getViewType();
198             if (viewType != null && viewType.equals(viewTypeName)) {
199                 return getAvailableColumns(contentView.getColumns()).iterator();
200             }
201         }
202         return null;
203     }
204 
205     @Override
206     public abstract String getIcon(Item item);
207 
208     protected List<ColumnDefinition> getAvailableColumns(final List<ColumnDefinition> allColumns) {
209         final List<ColumnDefinition> availableColumns = new ArrayList<ColumnDefinition>();
210         Iterator<ColumnDefinition> it = allColumns.iterator();
211         while (it.hasNext()) {
212             ColumnDefinition column = it.next();
213             if (column.isEnabled() && (column.getRuleClass() == null || componentProvider.newInstance(column.getRuleClass(), column).isAvailable())) {
214                 availableColumns.add(column);
215             }
216         }
217         return availableColumns;
218     }
219 
220     @Override
221     public void select(List<Object> itemIds) {
222     }
223 
224     @Override
225     public void expand(Object itemId) {
226     }
227 
228     protected abstract Container initializeContainer();
229 
230     protected ContentPresenterDefinition getPresenterDefinition() {
231         Iterator<ContentPresenterDefinition> viewsIterator = workbenchDefinition.getContentViews().iterator();
232         while (viewsIterator.hasNext()) {
233             ContentPresenterDefinition contentPresenterDefinition = viewsIterator.next();
234             String viewType = contentPresenterDefinition.getViewType();
235             if (viewType != null && viewType.equals(viewTypeName)) {
236                 return contentPresenterDefinition;
237             }
238         }
239         return null;
240     }
241 }