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.admin.templates;
18  
19  import org.exoplatform.ecm.webui.component.admin.UIECMAdminPortlet;
20  import org.exoplatform.ecm.webui.selector.UIPermissionSelector;
21  import org.exoplatform.webui.application.WebuiRequestContext;
22  import org.exoplatform.webui.config.annotation.ComponentConfig;
23  import org.exoplatform.webui.config.annotation.ComponentConfigs;
24  import org.exoplatform.webui.config.annotation.EventConfig;
25  import org.exoplatform.webui.core.UIComponent;
26  import org.exoplatform.webui.core.UIPopupContainer;
27  import org.exoplatform.webui.core.UIPopupWindow;
28  import org.exoplatform.webui.event.Event;
29  import org.exoplatform.webui.event.EventListener;
30  import org.exoplatform.webui.ext.manager.UIAbstractManager;
31  
32  
33  /**
34   * Created by The eXo Platform SARL
35   * Author : pham tuan
36   *          phamtuanchip@yahoo.de
37   * Oct 03, 2006
38   * 9:43:23 AM
39   */
40  @ComponentConfigs( {
41    @ComponentConfig(template = "app:/groovy/webui/component/admin/template/UITemplatesManager.gtmpl", 
42        events = { @EventConfig(listeners = UITemplatesManager.SelectTabActionListener.class) })
43  })
44  
45  public class UITemplatesManager extends UIAbstractManager {
46    final static public String POPUP_TEMPLATE_ID = "TemplateContainerPopup";
47    final static public String ACTIONS_TEMPLATE_ID = "UIActionsTemplateContainer";
48    final static public String OTHERS_TEMPLATE_ID = "UIOthersTemplateContainer";
49  
50    final static public String ACTIONS_TEMPLATE_LIST_ID = "UIActionsTemplateList";
51    final static public String OTHERS_TEMPLATE_LIST_ID  = "UIOthersTemplateList";
52    final static public String ACTIONS_ITERATOR_ID = "ActionsNodeTypeListIterator";
53    final static public String OTHERS_ITERATOR_ID = "OthersNodeTypeListIterator";
54  
55  
56    private String selectedTabId = "UITemplateContainer";
57  
58    public String getSelectedTabId()
59    {
60      return selectedTabId;
61    }
62  
63    public void setSelectedTab(String renderTabId)
64    {
65      selectedTabId = renderTabId;
66    }
67  
68    public void setSelectedTab(int index)
69    {
70      selectedTabId = getChild(index - 1).getId();
71    }
72  
73    public UITemplatesManager() throws Exception {    
74      UITemplateContainer uiTemp = addChild(UITemplateContainer.class, null, null) ;
75      uiTemp.getChild(UITemplateList.class).setTemplateFilter(UITemplateList.DOCUMENTS_TEMPLATE_TYPE);
76  
77      UITemplateContainer uiActionsTemp = addChild(UITemplateContainer.class, null, ACTIONS_TEMPLATE_ID) ;
78      uiActionsTemp.getChild(UITemplateList.class).setTemplateFilter(UITemplateList.ACTIONS_TEMPLATE_TYPE);
79      uiActionsTemp.getChild(UITemplateList.class).setId(ACTIONS_TEMPLATE_LIST_ID);
80  
81      UITemplateContainer uiOthersTemp = addChild(UITemplateContainer.class, null, OTHERS_TEMPLATE_ID) ;
82      uiOthersTemp.getChild(UITemplateList.class).setTemplateFilter(UITemplateList.OTHERS_TEMPLATE_TYPE);
83      uiOthersTemp.getChild(UITemplateList.class).setId(OTHERS_TEMPLATE_LIST_ID);
84  
85      addChild(UIPopupWindow.class, null, POPUP_TEMPLATE_ID) ;    
86      addChild(UIPopupWindow.class, null, UITemplateContent.TEMPLATE_PERMISSION) ;
87  
88      setSelectedTab("UITemplateContainer");
89    }
90  
91    public void initPopup(UIComponent uiComponent) throws Exception {
92      UIPopupWindow uiPopup = getChildById(POPUP_TEMPLATE_ID);
93      uiPopup.setRendered(true);
94      uiPopup.setShowMask(true);    
95      uiPopup.setWindowSize(600,300) ;
96      uiPopup.setUIComponent(uiComponent) ;
97      uiPopup.setShow(true) ;
98      uiPopup.setResizable(true) ;
99    }
100 
101   public void initPopupPermission(String id, String membership) throws Exception {
102     UIPopupWindow uiPopup = getChildById(UITemplateContent.TEMPLATE_PERMISSION);
103     uiPopup.setWindowSize(560, 300);
104     UIPermissionSelector uiECMPermission = createUIComponent(UIPermissionSelector.class, null, null);
105     uiECMPermission.setSelectedMembership(true);
106     if (membership != null && membership.indexOf(":/") > -1) {
107       String[] arrMember = membership.split(":/");
108       uiECMPermission.setCurrentPermission("/" + arrMember[1]);
109     }
110     if (id.equals("AddNew")) {
111       UITemplateForm uiForm = findFirstComponentOfType(UITemplateForm.class);
112       uiECMPermission.setSourceComponent(uiForm, null);
113     } else {
114       UITemplateContent uiTemContent = findComponentById(id);
115       uiECMPermission.setSourceComponent(uiTemContent, null);
116     }
117     uiPopup.setUIComponent(uiECMPermission);
118     uiPopup.setRendered(true);
119     uiPopup.setShow(true);
120     uiPopup.setResizable(true);
121   }
122 
123   public boolean isEditingTemplate() {
124     UIECMAdminPortlet adminPortlet = this.getAncestorOfType(UIECMAdminPortlet.class);
125     UIPopupContainer popupContainer = adminPortlet.getChild(UIPopupContainer.class);
126     UIPopupWindow uiPopup = popupContainer.getChild(UIPopupWindow.class);
127     uiPopup.setId(POPUP_TEMPLATE_ID);
128     return (uiPopup != null && uiPopup.isShow() && uiPopup.isRendered());
129   }  
130 
131   public void refresh() throws Exception {	
132     UITemplateContainer templateContainer = ((UITemplateContainer)getChildById("UITemplateContainer"));
133     UITemplateContainer templateActionsContainer = ((UITemplateContainer)getChildById(ACTIONS_TEMPLATE_ID));
134     UITemplateContainer templateOthersContainer = ((UITemplateContainer)getChildById(OTHERS_TEMPLATE_ID));
135     templateContainer.getChild(UITemplateList.class).getUIPageIterator().setId(ACTIONS_ITERATOR_ID);
136     templateOthersContainer.getChild(UITemplateList.class).getUIPageIterator().setId(OTHERS_ITERATOR_ID);
137     templateContainer.update();    
138     templateActionsContainer.update();   
139     templateOthersContainer.update();    
140   }
141 
142   static public class SelectTabActionListener extends EventListener<UITemplatesManager>
143   {
144     public void execute(Event<UITemplatesManager> event) throws Exception
145     {
146       WebuiRequestContext context = event.getRequestContext();
147       String renderTab = context.getRequestParameter(UIComponent.OBJECTID);
148       if (renderTab == null)
149         return;
150       event.getSource().setSelectedTab(renderTab);
151       WebuiRequestContext parentContext = (WebuiRequestContext)context.getParentAppRequestContext();
152       if (parentContext != null)
153       {
154         parentContext.setResponseComplete(true);
155       }
156       else
157       {
158         context.setResponseComplete(true);
159       }
160     }
161   }
162 
163   public static class CloseActionListener extends EventListener<UIPopupWindow> {
164     public void execute(Event<UIPopupWindow> event) throws Exception {
165       UITemplatesManager uiManager = event.getSource().getAncestorOfType(UITemplatesManager.class) ;
166       UIPopupWindow uiPopupWindow = uiManager.getChild(UIPopupWindow.class) ;
167       uiPopupWindow.setRendered(false) ;
168       event.getRequestContext().addUIComponentToUpdateByAjax(uiManager) ;
169     }
170   }
171 
172 }