View Javadoc
1   /**
2    * This file Copyright (c) 2003-2014 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$ ($Author$)
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      @Override
90      protected String getTemplateName(String viewName) {
91          return FreemarkerUtil.createTemplateName(AbstractList.class, "html");
92      }
93  
94      @Override
95      public String show() {
96          String view = super.show();
97          ListControl list = this.getList();
98          initList(list);
99          configureList(list);
100         return view;
101     }
102 
103     /**
104      * @param list
105      */
106     public abstract void configureList(ListControl list);
107 
108     /**
109      * @param list
110      */
111     public void initList(ListControl list) {
112         list.setName("list");
113         list.setRenderKit(this.getRenderKit());
114         list.setContextMenu(this.getContextMenu());
115         list.setModel(this.getModel());
116         list.setSortBy(this.getSortBy());
117         list.setSortByOrder(this.getSortByOrder());
118         list.setGroupBy(this.getGroupBy());
119         list.setGroupByOrder(this.getGroupByOrder());
120     }
121 
122     public String getLanguage(){
123         return MgnlContext.getUser().getLanguage();
124     }
125 
126     /**
127      * Returns the model used by this list
128      */
129     public abstract ListModel getModel();
130 
131     /**
132      * @param list The list to set.
133      */
134     public void setList(ListControl list) {
135         this.list = list;
136     }
137 
138     /**
139      * @return Returns the list.
140      */
141     public ListControl getList() {
142         if (list == null) {
143             list = new ListControl();
144         }
145         return list;
146     }
147 
148     public ContextMenu getContextMenu() {
149         if (this.contextMenu == null) {
150             this.contextMenu = new ContextMenu("mgnlContextMenu");
151             configureContextMenu(this.contextMenu);
152         }
153         return this.contextMenu;
154     }
155 
156     /**
157      * Override to configure the menu
158      */
159     protected void configureContextMenu(ContextMenu menu) {
160     }
161 
162     /**
163      * Helper method to creat menu items for the list
164      */
165     protected void addContextMenuItem(ContextMenu menu, String name, String label, String iconName, String methodName, String isActiveMethodName) {
166         final ContextMenuItem showInTree = new ContextMenuItem(name);
167         showInTree.setLabel(getMsgs().get(label));
168         showInTree.setIcon(MgnlContext.getContextPath() + "/.resources/icons/16/" + iconName+".gif");
169         showInTree.setOnclick(this.getList().getName() + "." + methodName + "();");
170         showInTree.addJavascriptCondition("function(){return " + this.getList().getName() + "." + isActiveMethodName + "()}");
171         menu.addMenuItem(showInTree);
172     }
173 
174 
175     /**
176      * Returns the default admin interface render kit.
177      */
178     protected RenderKit getRenderKit() {
179         return RenderKitFactory.getRenderKit(RenderKitFactory.ADMIN_INTERFACE_RENDER_KIT);
180     }
181 
182     /**
183      * @return Returns the functionBar.
184      */
185     public FunctionBar getFunctionBar() {
186         if (this.functionBar == null) {
187             this.functionBar = new FunctionBar("functionBar");
188             configureFunctionBar(this.functionBar);
189         }
190         return this.functionBar;
191     }
192 
193     /**
194      * Override to configure the bar
195      */
196     protected void configureFunctionBar(FunctionBar bar) {
197     }
198 
199     /**
200      * @param functionBar The functionBar to set.
201      */
202     public void setFunctionBar(FunctionBar functionBar) {
203         this.functionBar = functionBar;
204     }
205 
206     /**
207      * @return Returns the groupBy.
208      */
209     public String getGroupBy() {
210         return this.groupBy;
211     }
212 
213     /**
214      * @param groupBy The groupBy to set.
215      */
216     public void setGroupBy(String groupBy) {
217         this.groupBy = groupBy;
218     }
219 
220     /**
221      * @return Returns the groupByOrder.
222      */
223     public String getGroupByOrder() {
224         return this.groupByOrder;
225     }
226 
227     /**
228      * @param groupByOrder The groupByOrder to set.
229      */
230     public void setGroupByOrder(String groupByOrder) {
231         this.groupByOrder = groupByOrder;
232     }
233 
234     /**
235      * @return Returns the sortBy.
236      */
237     public String getSortBy() {
238         return this.sortBy;
239     }
240 
241     /**
242      * @param sortBy The sortBy to set.
243      */
244     public void setSortBy(String sortBy) {
245         this.sortBy = sortBy;
246     }
247 
248     /**
249      * @return Returns the sortByOrder.
250      */
251     public String getSortByOrder() {
252         return this.sortByOrder;
253     }
254 
255     /**
256      * @param sortByOrder The sortByOrder to set.
257      */
258     public void setSortByOrder(String sortByOrder) {
259         this.sortByOrder = sortByOrder;
260     }
261 
262     /**
263      * Do some additional rendering in the subclass
264      */
265     public String onRender() {
266         return "";
267     }
268 
269     /**
270      * Do some additional rendering in the subclass
271      */
272     public String onRenderHeader() {
273         return "";
274     }
275 
276     public String getURI() {
277         return MgnlContext.getAggregationState().getCurrentURI();
278     }
279 
280 }