View Javadoc
1   /*
2    * Copyright (C) 2003-2007 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.ecm.webui.component.explorer.sidebar ;
18  
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import javax.jcr.Node;
26  
27  import org.exoplatform.ecm.webui.component.explorer.UIJCRExplorer;
28  import org.exoplatform.ecm.webui.component.explorer.UIJCRExplorerPortlet;
29  import org.exoplatform.ecm.webui.component.explorer.UIJcrExplorerContainer;
30  import org.exoplatform.ecm.webui.component.explorer.UIWorkingArea;
31  import org.exoplatform.ecm.webui.component.explorer.control.UIAddressBar;
32  import org.exoplatform.ecm.webui.component.explorer.sidebar.action.ExplorerActionComponent;
33  import org.exoplatform.services.cms.views.ManageViewService;
34  import org.exoplatform.services.log.ExoLogger;
35  import org.exoplatform.services.log.Log;
36  import org.exoplatform.services.wcm.core.NodetypeConstant;
37  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
38  import org.exoplatform.webui.config.annotation.ComponentConfig;
39  import org.exoplatform.webui.config.annotation.EventConfig;
40  import org.exoplatform.webui.core.UIComponent;
41  import org.exoplatform.webui.core.UIContainer;
42  import org.exoplatform.webui.event.Event;
43  import org.exoplatform.webui.event.EventListener;
44  import org.exoplatform.webui.ext.UIExtension;
45  import org.exoplatform.webui.ext.UIExtensionManager;
46  import org.exoplatform.webui.ext.manager.UIAbstractManagerComponent;
47  
48  /**
49   * Created by The eXo Platform SARL
50   * Author : Hung Nguyen
51   *          nguyenkequanghung@yahoo.com
52   * oct 5, 2006
53   */
54  @ComponentConfig(
55      template = "app:/groovy/webui/component/explorer/sidebar/UISideBar.gtmpl",
56      events = {
57          @EventConfig(listeners = UISideBar.CloseActionListener.class)
58      }
59  )
60  public class UISideBar extends UIContainer {
61    public static final String UI_TAG_EXPLORER = "TagExplorer";
62  
63    private String                           currentComp;
64  
65    private static final Log                 LOG            = ExoLogger.getLogger(UISideBar.class.getName());
66  
67    public static final String               EXTENSION_TYPE = "org.exoplatform.ecm.dms.UISideBar";
68  
69    public static final int VISIBLE_COMPONENT_SIZE = 5;
70  
71  
72    private List<UIAbstractManagerComponent> managers
73        = Collections.synchronizedList(new ArrayList<UIAbstractManagerComponent>());
74  
75    private String selectedComp;
76  
77    private List<UIAbstractManagerComponent> lstVisibleComp;
78  
79    private List<UIAbstractManagerComponent> lstHiddenComp;
80  
81  
82    public UISideBar() throws Exception {
83      addChild(UITreeExplorer.class, null, null).setRendered(false);
84      addChild(UIViewRelationList.class, null, null).setRendered(false);
85      addChild(UITagExplorer.class, null, null).setRendered(false);
86      addChild(UIClipboard.class, null, null).setRendered(false);
87      addChild(UISavedSearches.class, null, null).setRendered(false);
88      addChild(UIAllItems.class, null, null);
89      addChild(UIAllItemsByType.class, null, null);
90    }
91  
92    public List<UIAbstractManagerComponent> getLstVisibleComp() {
93      return lstVisibleComp;
94    }
95  
96    public void setLstVisibleComp(List<UIAbstractManagerComponent> lstVisibleComp) {
97      this.lstVisibleComp = lstVisibleComp;
98    }
99  
100   public List<UIAbstractManagerComponent> getLstHiddenComp() {
101     return lstHiddenComp;
102   }
103 
104   public void setLstHiddenComp(List<UIAbstractManagerComponent> lstHiddenComp) {
105     this.lstHiddenComp = lstHiddenComp;
106   }
107 
108   public void setSelectedComp(String componentName) {
109     selectedComp = componentName;
110   }
111 
112   public void initComponents() throws Exception {
113     lstVisibleComp = new ArrayList<UIAbstractManagerComponent>(VISIBLE_COMPONENT_SIZE);
114     lstHiddenComp = new ArrayList<UIAbstractManagerComponent>();
115     List<UIAbstractManagerComponent> managers = getManagers();
116     for (int i = 0; i < managers.size(); i++) {
117       UIAbstractManagerComponent component = managers.get(i);
118 
119       if (lstVisibleComp.size() < VISIBLE_COMPONENT_SIZE) {
120         if (!isHideExplorerPanel() || !(component instanceof ExplorerActionComponent)) {
121           lstVisibleComp.add(component);
122         }
123       } else {
124         lstHiddenComp.add(component);
125       }
126     }
127   }
128 
129   public String getCurrentComp() throws Exception {
130     if(currentComp == null || currentComp.length() == 0) {
131       currentComp = getChild(UITreeExplorer.class).getId();
132     }
133     if (isHideExplorerPanel() && getChild(UITreeExplorer.class).getId().equals(currentComp)) {
134       currentComp = getChild(UITagExplorer.class).getId();
135       this.getAncestorOfType(UIJCRExplorer.class).setCurrentState();
136       this.getChild(UITagExplorer.class).updateTagList();
137     }
138     return currentComp;
139   }
140 
141   public String getSelectedComp() throws Exception {
142     if(selectedComp == null || selectedComp.length() == 0) {
143       selectedComp = "Explorer";
144     }
145     if (isHideExplorerPanel() && "Explorer".equals(selectedComp)) {
146       selectedComp = UI_TAG_EXPLORER;
147     }
148     return selectedComp;
149   }
150 
151   public void updateSideBarView() throws Exception {
152     boolean showFilterBar = getAncestorOfType(UIJCRExplorerPortlet.class).isShowFilterBar();
153     getChild(UIAllItems.class).setRendered(showFilterBar);
154     getChild(UIAllItemsByType.class).setRendered(showFilterBar);
155   }
156 
157 
158   public void setCurrentComp(String currentComp) {
159     this.currentComp = currentComp;
160   }
161 
162   public void renderSideBarChild(String[] arrId) throws Exception {
163     for(String id : arrId) {
164       setRenderedChild(id); // Need to remove this because we've already called updateSideBarView() but need Checking
165       renderChild(id);
166     }
167   }
168 
169   public String getRepository() {
170     return getAncestorOfType(UIJCRExplorer.class).getRepositoryName();
171   }
172 
173   static public class CloseActionListener extends EventListener<UISideBar> {
174     public void execute(Event<UISideBar> event) throws Exception {
175       UIWorkingArea uiWorkingArea = event.getSource().getParent();
176       uiWorkingArea.setShowSideBar(false);
177       UIJCRExplorerPortlet explorerPorltet = uiWorkingArea.getAncestorOfType(UIJCRExplorerPortlet.class);
178       UIJCRExplorer uiExplorer = explorerPorltet.findFirstComponentOfType(UIJCRExplorer.class);
179       UIJcrExplorerContainer uiJcrExplorerContainer= explorerPorltet.getChild(UIJcrExplorerContainer.class);
180       uiExplorer.refreshExplorer();
181       uiJcrExplorerContainer.setRenderedChild(UIJCRExplorer.class);
182       event.getRequestContext().addUIComponentToUpdateByAjax(uiExplorer);
183     }
184   }
185 
186   private List<UIExtension> getUIExtensionList() {
187     UIExtensionManager manager = getApplicationComponent(UIExtensionManager.class);
188     return manager.getUIExtensions(EXTENSION_TYPE);
189   }
190 
191   public synchronized void initialize() throws Exception {
192     List<UIExtension> extensions = getUIExtensionList();
193     if (extensions == null) {
194       return;
195     }
196     managers.clear();
197     Map<String, Object> context = new HashMap<String, Object>();
198     UIJCRExplorer uiExplorer = this.getAncestorOfType(UIJCRExplorer.class);
199     context.put(UIJCRExplorer.class.getName(), uiExplorer);
200     for (UIExtension extension : extensions) {
201       UIComponent component = addUIExtension(extension, context);
202       if (component != null && !managers.contains(component)) {
203         managers.add((UIAbstractManagerComponent) component);
204       }
205     }
206     initComponents();
207   }
208 
209   private synchronized UIComponent addUIExtension(UIExtension extension, Map<String, Object> context) throws Exception {
210     UIExtensionManager manager = getApplicationComponent(UIExtensionManager.class);
211     UIComponent component = manager.addUIExtension(extension, context, this);
212     if (component instanceof UIAbstractManagerComponent) {
213       // You can access to the given extension and the extension is valid
214       UIAbstractManagerComponent uiAbstractManagerComponent = (UIAbstractManagerComponent) component;
215       uiAbstractManagerComponent.setUIExtensionName(extension.getName());
216       uiAbstractManagerComponent.setUIExtensionCategory(extension.getCategory());
217       return component;
218     } else if (component != null) {
219       // You can access to the given extension but the extension is not valid
220       if (LOG.isWarnEnabled()) {
221         LOG.warn("All the extension '" + extension.getName() + "' of type '" + EXTENSION_TYPE
222           + "' must be associated to a component of type " + UIAbstractManagerComponent.class);
223       }
224       removeChild(component.getClass());
225     }
226     return null;
227   }
228 
229   public List<UIAbstractManagerComponent> getManagers() {
230     List<UIAbstractManagerComponent> managers = new ArrayList<UIAbstractManagerComponent>();
231     managers.addAll(this.managers);
232     return managers;
233   }
234 
235   public void unregister(UIAbstractManagerComponent component) {
236     managers.remove(component);
237   }
238   
239   public boolean isRenderComponent(String actionName) throws Exception {
240     if ("Explorer".equals(actionName)) {
241       return !isHideExplorerPanel();
242     }
243     return true;
244   }
245   
246   private boolean isHideExplorerPanel() throws Exception {
247     UIAddressBar uiAddress = this.getAncestorOfType(UIJCRExplorer.class).
248     findFirstComponentOfType(UIAddressBar.class);
249     String viewName = uiAddress.getSelectedViewName();
250     Node viewNode = WCMCoreUtils.getService(ManageViewService.class).getViewByName(
251                                  viewName, WCMCoreUtils.getSystemSessionProvider());
252     return viewNode.getProperty(NodetypeConstant.EXO_HIDE_EXPLORER_PANEL).getBoolean();
253   }
254 
255 }