View Javadoc

1   /**
2    * This file Copyright (c) 2012 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.admincentral.thumbnail.view;
35  
36  import info.magnolia.context.MgnlContext;
37  import info.magnolia.ui.model.imageprovider.definition.ImageProvider;
38  import info.magnolia.ui.model.workbench.definition.WorkbenchDefinition;
39  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
40  import info.magnolia.ui.vaadin.integration.jcr.container.AbstractJcrContainer;
41  import info.magnolia.ui.vaadin.layout.LazyThumbnailLayout;
42  import info.magnolia.ui.vaadin.layout.LazyThumbnailLayout.ThumbnailDblClickListener;
43  import info.magnolia.ui.vaadin.layout.LazyThumbnailLayout.ThumbnailSelectionListener;
44  
45  import javax.jcr.Node;
46  import javax.jcr.RepositoryException;
47  import javax.jcr.Session;
48  
49  import org.slf4j.Logger;
50  import org.slf4j.LoggerFactory;
51  
52  import com.vaadin.ui.Component;
53  import com.vaadin.ui.CssLayout;
54  
55  
56  /**
57   * LazyThumbnailViewImpl.
58   */
59  public class LazyThumbnailViewImpl implements ThumbnailView {
60  
61      private static final Logger log = LoggerFactory.getLogger(LazyThumbnailViewImpl.class);
62  
63      private ThumbnailContainer container;
64  
65      private final WorkbenchDefinition workbenchDefinition;
66  
67      private Listener listener;
68  
69      private final LazyThumbnailLayout layout;
70  
71      private final CssLayout margin = new CssLayout();
72  
73      private final ImageProvider imageProvider;
74  
75      public LazyThumbnailViewImpl(final WorkbenchDefinition definition, final ThumbnailContainer container) {
76          this.workbenchDefinition = definition;
77          this.imageProvider = container.getImageProvider();
78          this.layout = new LazyThumbnailLayout();
79          this.container = container;
80          layout.setSizeFull();
81          layout.addStyleName("mgnl-workbench-thumbnail-view");
82  
83          layout.addThumbnailSelectionListener(new ThumbnailSelectionListener() {
84  
85              @Override
86              public void onThumbnailSelected(final String thumbnailId) {
87                  JcrNodeAdapter node = getThumbnailNodeAdapterByIdentifier(thumbnailId);
88                  listener.onItemSelection(node);
89              }
90          });
91  
92          layout.addDoubleClickListener(new ThumbnailDblClickListener() {
93  
94              @Override
95              public void onThumbnailDblClicked(final String thumbnailId) {
96                  JcrNodeAdapter node = getThumbnailNodeAdapterByIdentifier(thumbnailId);
97                  listener.onDoubleClick(node);
98              }
99          });
100         margin.setSizeFull();
101         margin.setStyleName("mgnl-content-view");
102         margin.addComponent(layout);
103     }
104 
105     @Override
106     public void setListener(Listener listener) {
107         this.listener = listener;
108     }
109 
110     @Override
111     public void select(String path) {
112         // do something?
113     }
114 
115     @Override
116     public final void refresh() {
117         this.container = new ThumbnailContainer(workbenchDefinition, imageProvider);
118         container.setWorkspaceName(workbenchDefinition.getWorkspace());
119         container.setThumbnailHeight(73);
120         container.setThumbnailWidth(73);
121         layout.setContainerDataSource(container);
122         layout.setThumbnailSize(73, 73);
123     }
124 
125     @Override
126     public AbstractJcrContainer getContainer() {
127         throw new UnsupportedOperationException();
128     }
129 
130     @Override
131     public Component asVaadinComponent() {
132         return margin;
133     }
134 
135     private String prepareJcrSQL2Query() {
136         final String itemType = workbenchDefinition.getMainItemType().getItemType();
137         return "select * from [" + itemType + "] as t order by name(t)";
138 
139     }
140 
141     private JcrNodeAdapter getThumbnailNodeAdapterByIdentifier(final String thumbnailId) {
142         try {
143             Session session = MgnlContext.getJCRSession(workbenchDefinition.getWorkspace());
144             final Node imageNode = session.getNodeByIdentifier(thumbnailId);
145             return new JcrNodeAdapter(imageNode);
146         } catch (RepositoryException e) {
147             log.error(e.getMessage());
148         }
149         return null;
150     }
151 
152     @Override
153     public ViewType getViewType() {
154         return ViewType.THUMBNAIL;
155     }
156 }