1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
85
86 public ResourcesTreePresenter(final TreeView view, ComponentProvider componentProvider, ModuleRegistry moduleRegistry) {
87 this(view, componentProvider, moduleRegistry, null);
88 }
89
90
91
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 }