1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
38
39
40
41
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 }