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.thumbnail;
35  
36  import info.magnolia.context.MgnlContext;
37  import info.magnolia.event.EventBus;
38  import info.magnolia.objectfactory.ComponentProvider;
39  import info.magnolia.ui.imageprovider.ImageProvider;
40  import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter;
41  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
42  import info.magnolia.ui.workbench.AbstractContentPresenter;
43  import info.magnolia.ui.workbench.ContentView;
44  import info.magnolia.ui.workbench.definition.WorkbenchDefinition;
45  import info.magnolia.ui.workbench.thumbnail.ThumbnailContainer.ThumbnailItem;
46  
47  import java.util.List;
48  import java.util.Set;
49  
50  import javax.inject.Inject;
51  import javax.jcr.Node;
52  import javax.jcr.RepositoryException;
53  import javax.jcr.Session;
54  
55  import org.slf4j.Logger;
56  import org.slf4j.LoggerFactory;
57  
58  import com.vaadin.data.Item;
59  import com.vaadin.event.Action.Container;
60  
61  /**
62   * The ThumbnailPresenter is responsible for creating, configuring and updating a thumbnail grid of items according to the workbench definition.
63   */
64  public class ThumbnailPresenter extends AbstractContentPresenter implements ThumbnailView.Listener {
65  
66      private static final Logger log = LoggerFactory.getLogger(ThumbnailPresenter.class);
67  
68      private final ThumbnailView view;
69  
70      private final ImageProvider imageProvider;
71  
72      private ThumbnailContainer container;
73  
74      @Inject
75      public ThumbnailPresenter(final ThumbnailView view, final ImageProvider imageProvider, ComponentProvider componentProvider) {
76          super(componentProvider);
77          this.view = view;
78          this.imageProvider = imageProvider;
79      }
80  
81      @Override
82      public ContentView start(WorkbenchDefinition workbench, EventBus eventBus, String viewTypeName, Container shortcutActionManager) {
83          super.start(workbench, eventBus, viewTypeName, shortcutActionManager);
84  
85          container = new ThumbnailContainer(workbench, imageProvider);
86          container.setWorkspaceName(workbench.getWorkspace());
87          container.setThumbnailHeight(73);
88          container.setThumbnailWidth(73);
89  
90          view.setListener(this);
91          view.setContainer(container);
92          view.setThumbnailSize(73, 73);
93  
94          return view;
95      }
96  
97      @Override
98      public void setSelectedItemIds(List<String> itemIds) {
99          super.setSelectedItemIds(itemIds);
100     }
101 
102     @Override
103     public void refresh() {
104         container.refresh();
105         view.refresh();
106     }
107 
108     @Override
109     public com.vaadin.data.Container getContainer() {
110         return container;
111     }
112 
113     @Override
114     public void onItemSelection(Set<String> items) {
115         super.onItemSelection(items);
116     }
117 
118     @Override
119     public void onDoubleClick(Item item) {
120         JcrItemAdapter jcrItem = getJcrItemByThumbnailItem(item);
121         super.onDoubleClick(jcrItem);
122     }
123 
124     @Override
125     public void onRightClick(Item item, int clickX, int clickY) {
126         JcrItemAdapter jcrItem = getJcrItemByThumbnailItem(item);
127         super.onRightClick(jcrItem, clickX, clickY);
128     }
129 
130     @Override
131     public void select(List<String> itemIds) {
132         view.select(itemIds);
133     }
134 
135     /**
136      * Thumbnail container uses specific Thumbnail items, so we have to convert those into JcrItemAdapters.
137      */
138     private JcrItemAdapter getJcrItemByThumbnailItem(final Item item) {
139         if (item instanceof ThumbnailItem) {
140             String itemId = ((ThumbnailItem) item).getItemId();
141             try {
142                 Session session = MgnlContext.getJCRSession(workbenchDefinition.getWorkspace());
143                 final Node imageNode = session.getNodeByIdentifier(itemId);
144                 return new JcrNodeAdapter(imageNode);
145             } catch (RepositoryException e) {
146                 log.error(e.getMessage());
147             }
148         }
149         return null;
150     }
151 
152 
153 }