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.namespace;
18  
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.Comparator;
22  import java.util.List;
23  
24  import javax.jcr.NamespaceRegistry;
25  
26  import org.exoplatform.commons.utils.LazyPageList;
27  import org.exoplatform.commons.utils.ListAccess;
28  import org.exoplatform.commons.utils.ListAccessImpl;
29  import org.exoplatform.ecm.webui.core.UIPagingGrid;
30  import org.exoplatform.services.jcr.RepositoryService;
31  import org.exoplatform.webui.config.annotation.ComponentConfig;
32  import org.exoplatform.webui.config.annotation.EventConfig;
33  import org.exoplatform.webui.event.Event;
34  import org.exoplatform.webui.event.EventListener;
35  
36  /**
37   * Created by The eXo Platform SARL
38   * Author : pham tuan
39   *          phamtuanchip@yahoo.de
40   * September 20, 2006
41   * 16:37:15
42   */
43  @ComponentConfig(
44      template = "system:/groovy/ecm/webui/UIGridWithButton.gtmpl",
45      events = {@EventConfig(listeners = UINamespaceList.AddNamespaceActionListener.class)}
46  )
47  
48  public class UINamespaceList extends UIPagingGrid {
49  
50    private static String[] NAMESPACE_BEAN_FIELD = {"prefix", "uri"} ;
51  
52    public UINamespaceList() throws Exception {
53      getUIPageIterator().setId("NamespaceListIterator") ;
54      configure("prefix", NAMESPACE_BEAN_FIELD, null) ;
55    }
56  
57    public String[] getActions() { return new String[] {"AddNamespace"} ;}
58  
59    @SuppressWarnings("unchecked")
60    public void refresh(int currentPage) throws Exception {
61      NamespaceRegistry namespaceRegistry = getApplicationComponent(RepositoryService.class).getCurrentRepository()
62                                                                                            .getNamespaceRegistry();
63      List<NamespaceBean> nspBeans = new ArrayList<NamespaceBean>();
64      String[] prefixs = namespaceRegistry.getPrefixes();
65      for (int i = 0; i < prefixs.length; i++) {
66        String prefix = prefixs[i];
67        if (prefix == null || prefix.trim().length() == 0) {
68          continue;
69        }
70        NamespaceBean bean = new NamespaceBean(prefix, namespaceRegistry.getURI(prefix));
71        nspBeans.add(bean);
72      }
73      Collections.sort(nspBeans, new NameSpaceComparator());
74      ListAccess<NamespaceBean> namespaceList = new ListAccessImpl<NamespaceBean>(NamespaceBean.class,
75                                                                                  nspBeans);
76      LazyPageList<NamespaceBean> dataPageList = new LazyPageList<NamespaceBean>(namespaceList,
77                                                                                 getUIPageIterator().getItemsPerPage());
78      getUIPageIterator().setTotalItems(nspBeans.size());
79      getUIPageIterator().setPageList(dataPageList);
80      if (currentPage > getUIPageIterator().getAvailablePage())
81        getUIPageIterator().setCurrentPage(getUIPageIterator().getAvailablePage());
82      else
83        getUIPageIterator().setCurrentPage(currentPage);
84    }
85  
86    static public class NameSpaceComparator implements Comparator<NamespaceBean> {
87      public int compare(NamespaceBean o1, NamespaceBean o2) throws ClassCastException {
88        String name1 = o1.getPrefix();
89        String name2 = o2.getPrefix();
90        return name1.compareToIgnoreCase(name2);
91      }
92    }
93  
94    static public class AddNamespaceActionListener extends EventListener<UINamespaceList> {
95      public void execute(Event<UINamespaceList> event) throws Exception {
96        UINamespaceManager uiManager = event.getSource().getParent() ;
97        uiManager.initPopup() ;
98        UINamespaceForm uiForm = uiManager.findFirstComponentOfType(UINamespaceForm.class) ;
99        uiForm.reset() ;
100       event.getRequestContext().addUIComponentToUpdateByAjax(uiManager) ;
101     }
102   }
103 
104   public static class NamespaceBean {
105     private String prefix ;
106     private String uri ;
107 
108     public NamespaceBean(String prefix, String uri){
109       this.prefix = prefix ;
110       this.uri = uri ;
111     }
112 
113     public String getPrefix () { return prefix ;}
114     public String getUri () { return uri ;}
115   }
116 }