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