View Javadoc

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