View Javadoc
1   /**
2    * This file Copyright (c) 2013-2015 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  
60  /**
61   * The ThumbnailPresenter is responsible for creating, configuring and updating a thumbnail grid of items according to the workbench definition.
62   */
63  public class ThumbnailPresenter extends AbstractContentPresenter implements ThumbnailView.Listener {
64  
65      private static final Logger log = LoggerFactory.getLogger(ThumbnailPresenter.class);
66  
67      private final ThumbnailView view;
68  
69      private final ImageProvider imageProvider;
70  
71      private ThumbnailContainer container;
72  
73      @Inject
74      public ThumbnailPresenter(final ThumbnailView view, final ImageProvider imageProvider, ComponentProvider componentProvider) {
75          super(componentProvider);
76          this.view = view;
77          this.imageProvider = imageProvider;
78      }
79  
80      @Override
81      public ContentView start(WorkbenchDefinition workbench, EventBus eventBus, String viewTypeName) {
82          super.start(workbench, eventBus, viewTypeName);
83  
84          container = new ThumbnailContainer(workbench, imageProvider);
85          container.setWorkspaceName(workbench.getWorkspace());
86          container.setThumbnailHeight(73);
87          container.setThumbnailWidth(73);
88  
89          view.setListener(this);
90          view.setContainer(container);
91          view.setThumbnailSize(73, 73);
92  
93          return view;
94      }
95  
96      @Override
97      public void refresh() {
98          container.refresh();
99          view.refresh();
100     }
101 
102     protected com.vaadin.data.Container getContainer() {
103         return container;
104     }
105 
106     @Override
107     public void onItemSelection(Set<String> items) {
108         super.onItemSelection(items);
109     }
110 
111     @Override
112     public void onDoubleClick(Item item) {
113         JcrItemAdapter jcrItem = getJcrItemByThumbnailItem(item);
114         super.onDoubleClick(jcrItem);
115     }
116 
117     @Override
118     public void onRightClick(Item item, int clickX, int clickY) {
119         JcrItemAdapter jcrItem = getJcrItemByThumbnailItem(item);
120         super.onRightClick(jcrItem, clickX, clickY);
121     }
122 
123     @Override
124     public void select(List<String> itemIds) {
125         view.select(itemIds);
126     }
127 
128     /**
129      * Thumbnail container uses specific Thumbnail items, so we have to convert those into JcrItemAdapters.
130      */
131     private JcrItemAdapter getJcrItemByThumbnailItem(final Item item) {
132         if (item instanceof ThumbnailItem) {
133             String itemId = ((ThumbnailItem) item).getItemId();
134             try {
135                 Session session = MgnlContext.getJCRSession(workbenchDefinition.getWorkspace());
136                 final Node imageNode = session.getNodeByIdentifier(itemId);
137                 return new JcrNodeAdapter(imageNode);
138             } catch (RepositoryException e) {
139                 log.error(e.getMessage());
140             }
141         }
142         return null;
143     }
144 
145 }