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.cms.gui.controlx.list;
35  
36  import info.magnolia.cms.gui.control.ContextMenu;
37  import info.magnolia.cms.gui.controlx.impl.AbstractControl;
38  
39  import java.util.ArrayList;
40  import java.util.List;
41  
42  import org.apache.commons.lang.StringUtils;
43  
44  
45  /**
46   * A list. Can sort or group data.
47   * @author Philipp Bracher
48   * @version $Revision: 41137 $ ($Author: gjoseph $)
49   */
50  public class ListControl extends AbstractControl {
51  
52      /**
53       * The type used for rendering
54       */
55      public static final String RENDER_TYPE = "listControl";
56  
57      /**
58       * The current itarotor.
59       */
60      private ListModelIterator iterator;
61  
62      /**
63       * The underlaying model
64       */
65      private ListModel model;
66  
67      /**
68       * The context menu used
69       */
70      private ContextMenu contextMenu;
71  
72      /**
73       * Fields on which you can sort
74       */
75      private List sortableFields = new ArrayList();
76  
77      /**
78       * Fields you can group
79       */
80      private List groupableFields = new ArrayList();
81  
82      /**
83       * Max rows shown per group
84       */
85      private int maxRowsPerGroup = 5;
86  
87      /**
88       * Constructor. Setting the render type.
89       */
90      public ListControl() {
91          this.setRenderType(RENDER_TYPE);
92      }
93  
94      public ListModel getModel() {
95          return this.model;
96      }
97  
98      public void setModel(ListModel model) {
99          this.model = model;
100     }
101 
102     /**
103      * @see info.magnolia.cms.gui.controlx.list.ListControl#addColumn(info.magnolia.cms.gui.controlx.list.ListColumn)
104      */
105     public void addColumn(ListColumn column) {
106         this.addChild(column);
107     }
108 
109     /**
110      * Layzy bound iterator.
111      * @return Returns the iterator.
112      */
113     public ListModelIterator getIterator() {
114         if (this.iterator == null) {
115             this.iterator = this.getModel().getListModelIterator();
116         }
117         return iterator;
118     }
119 
120     /**
121      * Get the value for a column in the current iterator.
122      * @param name
123      * @return the value
124      */
125     public Object getIteratorValue(String name) {
126         return this.getIterator().getValue(name);
127     }
128 
129     /**
130      * Get the current object (not the value) in the current iterator.
131      * @return the object. corresponds to a row.
132      */
133     public Object getIteratorValueObject() {
134         return this.getIterator().getValueObject();
135     }
136 
137     public String getIteratorId() {
138         return this.getIterator().getId();
139     }
140 
141 
142     /**
143      * Restart the iterator.
144      */
145     public void resetIterator() {
146         this.iterator = null;
147     }
148 
149     public ContextMenu getContextMenu() {
150         return this.contextMenu;
151     }
152 
153     public void setContextMenu(ContextMenu contextMenu) {
154         this.contextMenu = contextMenu;
155     }
156 
157     public List getGroupableFields() {
158         return this.groupableFields;
159     }
160 
161     public List getSortableFields() {
162         return this.sortableFields;
163     }
164 
165     public void addSortableField(String name) {
166         this.sortableFields.add(name);
167     }
168 
169     public void addGroupableField(String name) {
170         this.groupableFields.add(name);
171     }
172 
173     public int getMaxRowsPerGroup() {
174         return this.maxRowsPerGroup;
175     }
176 
177     public void setMaxRowsPerGroup(int maxRowsPerGroup) {
178         this.maxRowsPerGroup = maxRowsPerGroup;
179     }
180 
181     /**
182      * @see info.magnolia.cms.gui.controlx.list.ListModel#getGroupBy()
183      */
184     public String getGroupBy() {
185         return StringUtils.defaultString(this.model.getGroupBy());
186     }
187 
188     /**
189      * @see info.magnolia.cms.gui.controlx.list.ListModel#getGroupByOrder()
190      */
191     public String getGroupByOrder() {
192         return StringUtils.defaultIfEmpty(this.model.getGroupByOrder(), "asc");
193     }
194 
195     /**
196      * @see info.magnolia.cms.gui.controlx.list.ListModel#getSortBy()
197      */
198     public String getSortBy() {
199         return StringUtils.defaultString(this.model.getSortBy());
200     }
201 
202     /**
203      * @see info.magnolia.cms.gui.controlx.list.ListModel#getSortByOrder()
204      */
205     public String getSortByOrder() {
206         return StringUtils.defaultIfEmpty(this.model.getSortByOrder(), "asc");
207     }
208 
209     /**
210      * @see info.magnolia.cms.gui.controlx.list.ListModel#setGroupBy(java.lang.String)
211      */
212     public void setGroupBy(String name) {
213         this.model.setGroupBy(name, this.model.getGroupByOrder());
214     }
215 
216     /**
217      * @see info.magnolia.cms.gui.controlx.list.ListModel#setGroupBy(java.lang.String)
218      */
219     public void setGroupByOrder(String order) {
220         this.model.setGroupBy(this.model.getGroupBy(), order);
221     }
222 
223     /**
224      * Get the lable of a specific
225      * @param name
226      * @return
227      */
228     public String getColumnLabel(String name) {
229         ListColumn column = (ListColumn) this.getChild(name);
230         return column.getLabel();
231     }
232 
233     /**
234      * @see info.magnolia.cms.gui.controlx.list.ListModel#setSortBy(java.lang.String)
235      */
236     public void setSortBy(String name) {
237         this.model.setSortBy(name, this.model.getSortByOrder());
238     }
239 
240     public void setSortByOrder(String order) {
241         this.model.setSortBy(this.model.getSortBy(), order);
242     }
243 
244 }