View Javadoc

1   /**
2    * This file Copyright (c) 2011-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.context.MgnlContext;
37  import info.magnolia.event.EventBus;
38  import info.magnolia.jcr.util.NodeTypes;
39  import info.magnolia.jcr.util.NodeUtil;
40  import info.magnolia.objectfactory.ComponentProvider;
41  import info.magnolia.ui.vaadin.integration.jcr.AbstractJcrNodeAdapter;
42  import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter;
43  import info.magnolia.ui.vaadin.integration.jcr.JcrItemUtil;
44  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
45  import info.magnolia.ui.vaadin.integration.jcr.JcrPropertyAdapter;
46  import info.magnolia.ui.workbench.column.definition.ColumnDefinition;
47  import info.magnolia.ui.workbench.definition.ContentPresenterDefinition;
48  import info.magnolia.ui.workbench.definition.NodeTypeDefinition;
49  import info.magnolia.ui.workbench.definition.WorkbenchDefinition;
50  import info.magnolia.ui.workbench.event.ItemDoubleClickedEvent;
51  import info.magnolia.ui.workbench.event.ItemRightClickedEvent;
52  import info.magnolia.ui.workbench.event.SelectionChangedEvent;
53  
54  import java.util.ArrayList;
55  import java.util.LinkedHashSet;
56  import java.util.List;
57  import java.util.Set;
58  
59  import java.util.Iterator;
60  import javax.inject.Inject;
61  import javax.jcr.Node;
62  import javax.jcr.Property;
63  import javax.jcr.RepositoryException;
64  
65  import org.slf4j.Logger;
66  import org.slf4j.LoggerFactory;
67  
68  import com.vaadin.data.Item;
69  
70  /**
71   * Abstract generic logic for content presenters.
72   */
73  public abstract class AbstractContentPresenter implements ContentPresenter, ContentView.Listener {
74  
75      private static final Logger log = LoggerFactory.getLogger(AbstractContentPresenter.class);
76  
77      private static final String ICON_PROPERTY = "icon-node-data";
78  
79      private static final String ICON_TRASH = "icon-trash";
80  
81      protected EventBus eventBus;
82  
83      protected WorkbenchDefinition workbenchDefinition;
84  
85      private List<String> selectedItemIds = new ArrayList<String>();
86  
87      protected String viewTypeName;
88  
89      private final ComponentProvider componentProvider;
90  
91      @Inject
92      public AbstractContentPresenter(ComponentProvider componentProvider) {
93          this.componentProvider = componentProvider;
94      }
95  
96      protected ComponentProvider getComponentProvider() {
97          return componentProvider;
98      }
99  
100     @Override
101     public ContentView start(WorkbenchDefinition workbenchDefinition, EventBus eventBus, String viewTypeName) {
102         this.workbenchDefinition = workbenchDefinition;
103         this.eventBus = eventBus;
104         this.viewTypeName = viewTypeName;
105         return null;
106     }
107 
108     @Override
109     public List<String> getSelectedItemIds() {
110         return this.selectedItemIds;
111     }
112 
113     public String getSelectedItemId() {
114         return selectedItemIds.isEmpty() ? null : selectedItemIds.get(0);
115     }
116 
117     @Override
118     public void setSelectedItemIds(List<String> selectedItemIds) {
119         this.selectedItemIds = selectedItemIds;
120     }
121 
122     // CONTENT VIEW LISTENER
123 
124     @Override
125     public void onItemSelection(Set<String> items) {
126         try {
127             Set<JcrItemAdapter> jcrItems = new LinkedHashSet<JcrItemAdapter>();
128             if (items == null || items.isEmpty()) {
129                 log.debug("Got null com.vaadin.data.Item. ItemSelectedEvent will be fired with null path.");
130                 List<String> ids = new ArrayList<String>(1);
131                 ids.add(JcrItemUtil.getItemId(getWorkbenchRoot()));
132                 setSelectedItemIds(ids);
133                 jcrItems.add(toJcrItemAdapter(getWorkbenchRoot()));
134             } else {
135                 List<String> itemIds = new ArrayList<String>(items.size());
136                 for (String item : items) {
137                     // if the selection is done by clicking the checkbox, the root item is added to the set - so it has to be ignored
138                     // but only if there is any other item in the set
139                     // TODO MGNLUI-1521
140                     if (JcrItemUtil.getItemId(getWorkbenchRoot()).equals(item) && items.size() > 1) {
141                         continue;
142                     }
143                     itemIds.add(item);
144                     jcrItems.add(toJcrItemAdapter(JcrItemUtil.getJcrItem(workbenchDefinition.getWorkspace(), item)));
145                 }
146                 setSelectedItemIds(itemIds);
147                 log.debug("com.vaadin.data.Item at {} was selected. Firing ItemSelectedEvent...", itemIds.toArray());
148             }
149             eventBus.fireEvent(new SelectionChangedEvent(workbenchDefinition.getWorkspace(), jcrItems));
150         } catch (Exception e) {
151             log.error("An error occurred while selecting a row in the data grid", e);
152         }
153     }
154 
155     private Node getWorkbenchRoot() {
156         try {
157             return MgnlContext.getJCRSession(workbenchDefinition.getWorkspace()).getNode(workbenchDefinition.getPath());
158         } catch (RepositoryException e) {
159             log.debug("Cannot find workbench root node for workspace=[" + workbenchDefinition.getWorkspace() + "] and path=[" + workbenchDefinition.getPath() + "]. Error: " + e.getMessage());
160             return null;
161         }
162     }
163 
164     private JcrItemAdapter toJcrItemAdapter(javax.jcr.Item item) {
165         if (item == null) {
166             return null;
167         }
168         JcrItemAdapter adapter = null;
169         if (item.isNode()) {
170             adapter = new JcrNodeAdapter((Node) item);
171         } else {
172             adapter = new JcrPropertyAdapter((Property) item);
173         }
174         return adapter;
175     }
176 
177     @Override
178     public void onDoubleClick(Item item) {
179         if (item != null) {
180             try {
181                 List<String> ids = new ArrayList<String>(1);
182                 ids.add(((JcrItemAdapter) item).getItemId());
183                 setSelectedItemIds(ids);
184                 log.debug("com.vaadin.data.Item at {} was double clicked. Firing ItemDoubleClickedEvent...", getSelectedItemId());
185                 eventBus.fireEvent(new ItemDoubleClickedEvent(workbenchDefinition.getWorkspace(), getSelectedItemId()));
186             } catch (Exception e) {
187                 log.error("An error occurred while double clicking on a row in the data grid", e);
188             }
189         } else {
190             log.warn("Got null com.vaadin.data.Item. No event will be fired.");
191         }
192     }
193 
194     @Override
195     public void onRightClick(Item item, int clickX, int clickY) {
196         if (item != null) {
197             try {
198                 // if the right-clicket item is not yet selected
199                 if (!selectedItemIds.contains(((JcrItemAdapter) item).getItemId())) {
200                     List<String> ids = new ArrayList<String>(1);
201                     ids.add(((JcrItemAdapter) item).getItemId());
202                     setSelectedItemIds(ids);
203                     select(ids);
204                 }
205                 String clickedItemId = ((JcrItemAdapter) item).getItemId();
206                 log.debug("com.vaadin.data.Item at {} was right clicked. Firing ItemRightClickedEvent...", clickedItemId);
207                 eventBus.fireEvent(new ItemRightClickedEvent(workbenchDefinition.getWorkspace(), (JcrItemAdapter) item, clickX, clickY));
208             } catch (Exception e) {
209                 log.error("An error occurred while right clicking on a row in the data grid", e);
210             }
211         } else {
212             log.warn("Got null com.vaadin.data.Item. No event will be fired.");
213         }
214     }
215 
216     protected Iterator<ColumnDefinition> getColumnsIterator() {
217         Iterator<ContentPresenterDefinition> viewsIterator = workbenchDefinition.getContentViews().iterator();
218         while (viewsIterator.hasNext()) {
219             ContentPresenterDefinition contentView = viewsIterator.next();
220             if (contentView.getViewType().getText().equals(viewTypeName)) {
221                 return getAvailableColumns(contentView.getColumns()).iterator();
222             }
223         }
224         return null;
225     }
226 
227     @Override
228     public String getIcon(Item item) {
229         try {
230             if (item instanceof JcrPropertyAdapter) {
231                 return ICON_PROPERTY;
232             } else if (item instanceof JcrNodeAdapter) {
233                 Node node = ((AbstractJcrNodeAdapter)item).getJcrItem();
234                 if (NodeUtil.hasMixin(node, NodeTypes.Deleted.NAME)) {
235                     return ICON_TRASH;
236                 }
237 
238                 NodeTypeDefinition nodeTypeDefinition = getNodeTypeDefinitionForNode(node);
239                 if (nodeTypeDefinition != null) {
240                     return nodeTypeDefinition.getIcon();
241                 }
242             }
243 
244         } catch (RepositoryException e) {
245             log.warn("Unable to resolve icon", e);
246         }
247         return null;
248     }
249 
250     private NodeTypeDefinition getNodeTypeDefinitionForNode(Node node) throws RepositoryException {
251         String primaryNodeTypeName = node.getPrimaryNodeType().getName();
252         for (NodeTypeDefinition nodeTypeDefinition : workbenchDefinition.getNodeTypes()) {
253             if (nodeTypeDefinition.isStrict()) {
254                 if (primaryNodeTypeName.equals(nodeTypeDefinition.getName())) {
255                     return nodeTypeDefinition;
256                 }
257             } else if (NodeUtil.isNodeType(node, nodeTypeDefinition.getName())) {
258                 return nodeTypeDefinition;
259             }
260         }
261         return null;
262     }
263 
264     protected List<ColumnDefinition> getAvailableColumns(final List<ColumnDefinition> allColumns) {
265         final List<ColumnDefinition> availableColumns = new ArrayList<ColumnDefinition>();
266         Iterator<ColumnDefinition> it = allColumns.iterator();
267         while (it.hasNext()) {
268             ColumnDefinition column = it.next();
269             if (column.isEnabled() && (column.getRuleClass() == null || componentProvider.newInstance(column.getRuleClass(), column).isAvailable())) {
270                 availableColumns.add(column);
271             }
272         }
273         return availableColumns;
274     }
275 
276     @Override
277     public void select(List<String> itemIds) {
278     }
279 }