View Javadoc

1   
2   /**
3    * This file Copyright (c) 2003-2011 Magnolia International
4    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
5    *
6    *
7    * This file is dual-licensed under both the Magnolia
8    * Network Agreement and the GNU General Public License.
9    * You may elect to use one or the other of these licenses.
10   *
11   * This file is distributed in the hope that it will be
12   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
13   * implied warranty of MERCHANTABILITY or FITNESS FOR A
14   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
15   * Redistribution, except as permitted by whichever of the GPL
16   * or MNA you select, is prohibited.
17   *
18   * 1. For the GPL license (GPL), you can redistribute and/or
19   * modify this file under the terms of the GNU General
20   * Public License, Version 3, as published by the Free Software
21   * Foundation.  You should have received a copy of the GNU
22   * General Public License, Version 3 along with this program;
23   * if not, write to the Free Software Foundation, Inc., 51
24   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
25   *
26   * 2. For the Magnolia Network Agreement (MNA), this file
27   * and the accompanying materials are made available under the
28   * terms of the MNA which accompanies this distribution, and
29   * is available at http://www.magnolia-cms.com/mna.html
30   *
31   * Any modifications to this file must keep this entire header
32   * intact.
33   *
34   */
35  package info.magnolia.module.admininterface.lists;
36  
37  import info.magnolia.cms.gui.control.ContextMenu;
38  import info.magnolia.cms.gui.control.ContextMenuItem;
39  import info.magnolia.cms.gui.control.FunctionBar;
40  import info.magnolia.cms.gui.controlx.RenderKit;
41  import info.magnolia.cms.gui.controlx.RenderKitFactory;
42  import info.magnolia.cms.gui.controlx.list.ListControl;
43  import info.magnolia.cms.gui.controlx.list.ListModel;
44  import info.magnolia.context.MgnlContext;
45  import info.magnolia.freemarker.FreemarkerUtil;
46  import info.magnolia.module.admininterface.TemplatedMVCHandler;
47  
48  import javax.servlet.http.HttpServletRequest;
49  import javax.servlet.http.HttpServletResponse;
50  
51  
52  /**
53   * @author Philipp Bracher
54   * @version $Revision$ ($Author$)
55   */
56  public abstract class AbstractList extends TemplatedMVCHandler {
57  
58      private String sortBy = "";
59  
60      private String sortByOrder = "asc";
61  
62      private String groupBy = "";
63  
64      private String groupByOrder = "asc";
65  
66      /**
67       * Control used.
68       */
69      private ListControl list;
70  
71      /**
72       * The function bar shown at the bottom
73       */
74      private FunctionBar functionBar;
75  
76      private ContextMenu contextMenu;
77  
78      /**
79       * @param name
80       * @param request
81       * @param response
82       */
83      public AbstractList(String name, HttpServletRequest request, HttpServletResponse response) {
84          super(name, request, response);
85      }
86  
87      /**
88       * Don't use the class name.
89       */
90      protected String getTemplateName(String viewName) {
91          return FreemarkerUtil.createTemplateName(AbstractList.class, "html");
92      }
93  
94      public String show() {
95          String view = super.show();
96          ListControl list = this.getList();
97          initList(list);
98          configureList(list);
99          return view;
100     }
101 
102     /**
103      * @param list
104      */
105     public abstract void configureList(ListControl list);
106 
107     /**
108      * @param list
109      */
110     public void initList(ListControl list) {
111         list.setName("list");
112         list.setRenderKit(this.getRenderKit());
113         list.setContextMenu(this.getContextMenu());
114         list.setModel(this.getModel());
115         list.setSortBy(this.getSortBy());
116         list.setSortByOrder(this.getSortByOrder());
117         list.setGroupBy(this.getGroupBy());
118         list.setGroupByOrder(this.getGroupByOrder());
119     }
120 
121     public String getLanguage(){
122         return MgnlContext.getUser().getLanguage();
123     }
124 
125     /**
126      * Returns the model used by this list
127      */
128     public abstract ListModel getModel();
129 
130     /**
131      * @param list The list to set.
132      */
133     public void setList(ListControl list) {
134         this.list = list;
135     }
136 
137     /**
138      * @return Returns the list.
139      */
140     public ListControl getList() {
141         if (list == null) {
142             list = new ListControl();
143         }
144         return list;
145     }
146 
147     public ContextMenu getContextMenu() {
148         if (this.contextMenu == null) {
149             this.contextMenu = new ContextMenu("mgnlContextMenu");
150             configureContextMenu(this.contextMenu);
151         }
152         return this.contextMenu;
153     }
154 
155     /**
156      * Override to configure the menu
157      */
158     protected void configureContextMenu(ContextMenu menu) {
159     }
160 
161     /**
162      * Helper method to creat menu items for the list
163      */
164     protected void addContextMenuItem(ContextMenu menu, String name, String label, String iconName, String methodName, String isActiveMethodName) {
165         final ContextMenuItem showInTree = new ContextMenuItem(name);
166         showInTree.setLabel(getMsgs().get(label));
167         showInTree.setIcon(MgnlContext.getContextPath() + "/.resources/icons/16/" + iconName+".gif");
168         showInTree.setOnclick(this.getList().getName() + "." + methodName + "();");
169         showInTree.addJavascriptCondition("function(){return " + this.getList().getName() + "." + isActiveMethodName + "()}");
170         menu.addMenuItem(showInTree);
171     }
172 
173 
174     /**
175      * Returns the default admin interface render kit.
176      */
177     protected RenderKit getRenderKit() {
178         return RenderKitFactory.getRenderKit(RenderKitFactory.ADMIN_INTERFACE_RENDER_KIT);
179     }
180 
181     /**
182      * @return Returns the functionBar.
183      */
184     public FunctionBar getFunctionBar() {
185         if (this.functionBar == null) {
186             this.functionBar = new FunctionBar("functionBar");
187             configureFunctionBar(this.functionBar);
188         }
189         return this.functionBar;
190     }
191 
192     /**
193      * Override to configure the bar
194      */
195     protected void configureFunctionBar(FunctionBar bar) {
196     }
197 
198     /**
199      * @param functionBar The functionBar to set.
200      */
201     public void setFunctionBar(FunctionBar functionBar) {
202         this.functionBar = functionBar;
203     }
204 
205     /**
206      * @return Returns the groupBy.
207      */
208     public String getGroupBy() {
209         return this.groupBy;
210     }
211 
212     /**
213      * @param groupBy The groupBy to set.
214      */
215     public void setGroupBy(String groupBy) {
216         this.groupBy = groupBy;
217     }
218 
219     /**
220      * @return Returns the groupByOrder.
221      */
222     public String getGroupByOrder() {
223         return this.groupByOrder;
224     }
225 
226     /**
227      * @param groupByOrder The groupByOrder to set.
228      */
229     public void setGroupByOrder(String groupByOrder) {
230         this.groupByOrder = groupByOrder;
231     }
232 
233     /**
234      * @return Returns the sortBy.
235      */
236     public String getSortBy() {
237         return this.sortBy;
238     }
239 
240     /**
241      * @param sortBy The sortBy to set.
242      */
243     public void setSortBy(String sortBy) {
244         this.sortBy = sortBy;
245     }
246 
247     /**
248      * @return Returns the sortByOrder.
249      */
250     public String getSortByOrder() {
251         return this.sortByOrder;
252     }
253 
254     /**
255      * @param sortByOrder The sortByOrder to set.
256      */
257     public void setSortByOrder(String sortByOrder) {
258         this.sortByOrder = sortByOrder;
259     }
260 
261     /**
262      * Do some additional rendering in the subclass
263      */
264     public String onRender() {
265         return "";
266     }
267 
268     /**
269      * Do some additional rendering in the subclass
270      */
271     public String onRenderHeader() {
272         return "";
273     }
274 
275     public String getURI() {
276         return MgnlContext.getAggregationState().getCurrentURI();
277     }
278 
279 }