View Javadoc
1   /***************************************************************************
2    * Copyright (C) 2003-2009 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   **************************************************************************/
18  package org.exoplatform.ecm.webui.component.admin.views;
19  
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.util.Comparator;
23  import java.util.List;
24  
25  import javax.jcr.Node;
26  import javax.jcr.NodeIterator;
27  
28  import org.exoplatform.commons.utils.LazyPageList;
29  import org.exoplatform.commons.utils.ListAccess;
30  import org.exoplatform.commons.utils.ListAccessImpl;
31  import org.exoplatform.ecm.webui.core.UIPagingGrid;
32  import org.exoplatform.services.cms.views.ManageViewService;
33  import org.exoplatform.services.cms.views.ViewConfig.Tab;
34  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
35  import org.exoplatform.webui.config.annotation.ComponentConfig;
36  import org.exoplatform.webui.config.annotation.EventConfig;
37  import org.exoplatform.webui.event.Event;
38  import org.exoplatform.webui.event.EventListener;
39  
40  /**
41   * Created by The eXo Platform SARL
42   * Author : Dang Van Minh
43   *          minh.dang@exoplatform.com
44   * Feb 18, 2013
45   * 12:15:50 PM  
46   */
47  
48  @ComponentConfig(
49          template = "system:/groovy/ecm/webui/UIGridWithButton.gtmpl",
50          events = {
51              @EventConfig(listeners = UITabList.DeleteActionListener.class, confirm = "UITabList.msg.confirm-delete"),
52              @EventConfig(listeners = UITabList.EditActionListener.class),
53              @EventConfig(listeners = UITabList.AddTabActionListener.class)
54          }
55      )
56  public class UITabList extends UIPagingGrid {
57    
58    final static public String   TAPFORM_POPUP         = "TabForm_Popup";
59    
60    public static String[] TAB_BEAN_FIELD = {"tabName", "localizeButtons"} ;
61    public static String TAB_LIST = "ECMTabList" ;
62    private String[] actions_ = new String[] {"AddTab"};
63    private boolean isView_ = false;
64    
65    private String viewName;
66    
67    public UITabList() throws Exception {
68      getUIPageIterator().setId("TabListPageIterator") ;
69      configure("tabName", UITabList.TAB_BEAN_FIELD, new String[] {"Edit","Delete"}) ;
70    }
71    
72    public String[] getActions() { 
73      return actions_; 
74    }
75    
76    public void setActions(String[] actions) {
77      actions_ = actions;
78    }
79    
80    public boolean isView() {
81      return isView_;
82    }
83    
84    public void view(boolean isView) {
85      isView_ = isView;
86    }  
87  
88    @Override
89    public void refresh(int currentPage) throws Exception {
90      List<Tab> tabList = new ArrayList<Tab>();
91      UITabContainer uiTabContainer = getParent();
92      UIViewFormTabPane uiTabPane = uiTabContainer.getParent();
93      UIViewForm uiViewForm = uiTabPane.getChild(UIViewForm.class);
94      if(isView_) {
95        ManageViewService viewService = WCMCoreUtils.getService(ManageViewService.class);
96        Node viewNode = viewService.getViewByName(viewName, WCMCoreUtils.getSystemSessionProvider());
97        NodeIterator nodeIter = viewNode.getNodes();
98        while(nodeIter.hasNext()) {
99          Node tabNode = nodeIter.nextNode();
100         Tab tab = new Tab();
101         tab.setTabName(tabNode.getName());
102         tab.setButtons(tabNode.getProperty("exo:buttons").getValue().getString());
103         tab.setLocalizeButtons(uiViewForm.getLocalizationButtons(tabNode.getProperty("exo:buttons").getValue().getString()));
104         tabList.add(tab);
105       }
106     } else {
107       tabList = uiViewForm.getTabs();
108     }
109     Collections.sort(tabList, new TabComparator());
110     ListAccess<Tab> tabBeanList = new ListAccessImpl<Tab>(Tab.class, tabList);
111     getUIPageIterator().setPageList(new LazyPageList<Tab>(tabBeanList, getUIPageIterator().getItemsPerPage()));
112     getUIPageIterator().setTotalItems(tabList.size());
113     if (currentPage > getUIPageIterator().getAvailablePage()) {
114       getUIPageIterator().setCurrentPage(getUIPageIterator().getAvailablePage());
115     } else {
116       getUIPageIterator().setCurrentPage(currentPage);
117     }
118   }
119   
120   public String getViewName() {
121     return viewName;
122   }
123   
124   public void setViewName(String name) {
125     viewName = name;
126   }
127   
128   static  public class AddTabActionListener extends EventListener<UITabList> {
129     public void execute(Event<UITabList> event) throws Exception {
130       UITabList uiTabList = event.getSource();
131       UITabContainer uiContainer = uiTabList.getParent();
132       UITabForm uiTabForm = uiContainer.createUIComponent(UITabForm.class, null, null);
133       uiContainer.initPopup(UITabList.TAPFORM_POPUP, uiTabForm, 760, 0);
134       UIViewFormTabPane uiTabPane = uiContainer.getParent();
135       uiTabPane.setSelectedTab(uiContainer.getId());
136       event.getRequestContext().addUIComponentToUpdateByAjax(uiContainer);
137     }
138   }
139   
140   static  public class DeleteActionListener extends EventListener<UITabList> {
141     public void execute(Event<UITabList> event) throws Exception {
142       UITabList uiTabList = event.getSource();
143       String tabName = event.getRequestContext().getRequestParameter(OBJECTID);
144       UITabContainer uiTabContainer = uiTabList.getParent();
145       UIViewFormTabPane uiTabPane = uiTabContainer.getParent();
146       UIViewForm uiViewForm = uiTabPane.getChild(UIViewForm.class);
147       uiViewForm.getTabMap().remove(tabName);
148       uiTabList.refresh(uiTabList.getUIPageIterator().getCurrentPage());
149       uiTabPane.setSelectedTab(uiTabList.getId());
150       event.getRequestContext().addUIComponentToUpdateByAjax(uiTabList.getParent());
151     }
152   }
153   
154   static  public class EditActionListener extends EventListener<UITabList> {
155     public void execute(Event<UITabList> event) throws Exception {
156       UITabList uiTabList = event.getSource();
157       UITabContainer uiContainer = uiTabList.getParent();
158       String tabName = event.getRequestContext().getRequestParameter(OBJECTID);
159       UIViewFormTabPane uiTabPane = uiContainer.getParent();
160       UIViewForm uiViewForm = uiTabPane.getChild(UIViewForm.class);
161       Tab tab = uiViewForm.getTabMap().get(tabName);
162       UITabForm uiTabForm = uiContainer.createUIComponent(UITabForm.class, null, null);
163       uiTabForm.update(tab, false);
164       uiContainer.initPopup(UITabList.TAPFORM_POPUP, uiTabForm, 760, 0);
165       uiTabPane.setSelectedTab(uiTabList.getId());
166       event.getRequestContext().addUIComponentToUpdateByAjax(uiContainer);
167     }
168   }  
169 
170   static public class TabComparator implements Comparator<Tab> {
171     public int compare(Tab o1, Tab o2) throws ClassCastException {
172       String name1 = o1.getTabName();
173       String name2 = o2.getTabName();
174       return name1.compareToIgnoreCase(name2);
175     }
176   }
177 }