View Javadoc
1   /**
2    * This file Copyright (c) 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.resources.app.workbench.tree;
35  
36  import info.magnolia.context.Context;
37  import info.magnolia.event.EventBus;
38  import info.magnolia.jcr.util.NodeTypes;
39  import info.magnolia.jcr.util.NodeUtil;
40  import info.magnolia.module.ModuleRegistry;
41  import info.magnolia.objectfactory.ComponentProvider;
42  import info.magnolia.resourceloader.ResourceOrigin;
43  import info.magnolia.resources.app.ResourcesContentConnector;
44  import info.magnolia.resources.app.availability.IsResourcePresentInJcrRule;
45  import info.magnolia.resources.app.utils.ResourceUtils;
46  import info.magnolia.resources.app.workbench.ResourcesContainer;
47  import info.magnolia.resources.app.workbench.tools.FilterClasspathResourcesEvent;
48  import info.magnolia.ui.vaadin.integration.contentconnector.ContentConnector;
49  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
50  import info.magnolia.ui.workbench.tree.TreePresenter;
51  import info.magnolia.ui.workbench.tree.TreeView;
52  
53  import java.util.List;
54  
55  import javax.inject.Inject;
56  import javax.jcr.Node;
57  import javax.jcr.RepositoryException;
58  
59  import com.google.common.base.Optional;
60  import com.google.common.collect.Lists;
61  import com.vaadin.data.Container;
62  import com.vaadin.data.Item;
63  import com.vaadin.data.Property;
64  
65  /**
66   * Specialized {@link TreePresenter} which creates an origin-based Vaadin container.
67   */
68  public class ResourcesTreePresenter extends TreePresenter {
69  
70      private static final String ICON_DIRECTORY = "icon-folder-l";
71      private static final String ICON_FILE = "icon-file-text";
72  
73      private final ModuleRegistry moduleRegistry;
74      private final Optional<Context> context;
75  
76      @Inject
77      public ResourcesTreePresenter(final TreeView view, ComponentProvider componentProvider, ModuleRegistry moduleRegistry, Context context) {
78          super(view, componentProvider);
79          this.moduleRegistry = moduleRegistry;
80          this.context = Optional.fromNullable(context);
81      }
82  
83      /**
84       * @deprecated since 2.4.2 - use {@link #ResourcesTreePresenter(TreeView, ComponentProvider, ModuleRegistry, Context)} instead.
85       */
86      public ResourcesTreePresenter(final TreeView view, ComponentProvider componentProvider, ModuleRegistry moduleRegistry) {
87          this(view, componentProvider, moduleRegistry, null);
88      }
89  
90      /**
91       * @deprecated since 2.4.1 - use {@link #ResourcesTreePresenter(TreeView, ComponentProvider, ModuleRegistry)} instead.
92       */
93      @Deprecated
94      public ResourcesTreePresenter(final TreeView view, ComponentProvider componentProvider, ContentConnector contentConnector, ModuleRegistry moduleRegistry, EventBus eventBus) {
95          this(view, componentProvider, moduleRegistry, null);
96      }
97  
98      @Override
99      protected Container.Hierarchical createContainer() {
100         final ResourceOrigin<?> origin = resourcesContentConnector().getOrigin();
101         final List<String> moduleNames = Lists.newArrayList(moduleRegistry.getModuleNames());
102         final ResourcesContainer resourcesContainer = new ResourcesContainer(origin, moduleNames);
103 
104         eventBus.addHandler(FilterClasspathResourcesEvent.class, new FilterClasspathResourcesEvent.Handler() {
105             @Override
106             public void onResourceFilterChange(FilterClasspathResourcesEvent event) {
107                 resourcesContainer.setClasspathResourcesFiltered(event.isFilteringClasspathResources());
108             }
109         });
110 
111         return resourcesContainer;
112     }
113 
114     protected ResourcesContentConnector resourcesContentConnector() {
115         return (ResourcesContentConnector)contentConnector;
116     }
117 
118     @Override
119     public String getIcon(Item item) {
120         String resourcePath = item.getItemProperty(ResourcesContainer.RESOURCE_PATH).getValue().toString();
121         boolean isResourcePresentInJcr = new IsResourcePresentInJcrRule(resourcesContentConnector().getOrigin()).isAvailable(Lists.newArrayList(resourcePath));
122 
123         if (context.isPresent() && isResourcePresentInJcr) {
124             JcrNodeAdapter jcrNodeAdapter = ResourceUtils.getJcrNodeAdapterFor(context.get(), item);
125             Node node = jcrNodeAdapter.getJcrItem();
126 
127             boolean isDeleted;
128             try {
129                 isDeleted = NodeUtil.hasMixin(node, NodeTypes.Deleted.NAME);
130             } catch (RepositoryException ignored) {
131                 isDeleted = false;
132             }
133             if (isDeleted) {
134                 return ICON_TRASH;
135             }
136         }
137 
138         Property<Boolean> isDirectory = item.getItemProperty(ResourcesContainer.DIRECTORY_PROPERTY_ID);
139         if (isDirectory != null) {
140             return isDirectory.getValue() ? ICON_DIRECTORY : ICON_FILE;
141         }
142         return null;
143     }
144 
145 }