KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > support > RefreshablePagedListHolder


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.beans.support;
18
19 import java.util.Locale JavaDoc;
20
21 import org.springframework.beans.BeanUtils;
22
23 /**
24  * RefreshablePagedListHolder is a PagedListHolder subclass with reloading capabilities.
25  * It automatically re-requests the List from the source provider, in case of Locale or
26  * filter changes.
27  *
28  * <p>Data binding works just like with PagedListHolder. The locale can be specified in
29  * Locale's toString syntax, e.g. "locale=en_US". The filter object can be of any
30  * custom class, preferably a bean for easy data binding from a request. An instance
31  * will simply get passed through to PagedListSourceProvider.loadList. A filter property
32  * can be specified via "filter.myFilterProperty", for example.
33  *
34  * <p>The scenario in the controller could be:
35  * <code>
36  * RefreshablePagedListHolder holder = request.getSession("mySessionAttr");<br>
37  * if (holder == null) {<br>
38  * holder = new RefreshablePagedListHolder();<br>
39  * holder.setSourceProvider(new MyAnonymousOrEmbeddedSourceProvider());<br>
40  * holder.setFilter(new MyAnonymousOrEmbeddedFilter());<br>
41  * request.getSession().setAttribute("mySessionAttr", holder);<br>
42  * }<br>
43  * holder.refresh(false);
44  * BindException ex = BindUtils.bind(request, listHolder, "myModelAttr");<br>
45  * return ModelAndView("myViewName", ex.getModel());<br>
46  * <br>
47  * ...<br>
48  * <br>
49  * private class MyAnonymousOrEmbeddedSourceProvider implements PagedListSourceProvider {<br>
50  * public List loadList(Locale locale, Object filter) {<br>
51  * MyAnonymousOrEmbeddedFilter filter = (MyAnonymousOrEmbeddedFilter) filter;<br<
52  * // an empty name mask should lead to all objects being loaded
53  * return myBusinessService.loadMyObjectsByNameMask(filter.getName());<br>
54  * }<br>
55  * <br>
56  * private class MyAnonymousOrEmbeddedFilter {<br>
57  * private String name = "";<br>
58  * public String getName() {<br>
59  * return name;<br<
60  * }<br>
61  * public void setName(String name) {<br>
62  * this.name = name;<br>
63  * }<br>
64  * }<br>
65  * </code>
66  *
67  * @author Jean-Pierre Pawlak
68  * @author Juergen Hoeller
69  * @since 24.05.2003
70  * @see org.springframework.beans.support.PagedListSourceProvider
71  * @see org.springframework.beans.propertyeditors.LocaleEditor
72  */

73 public class RefreshablePagedListHolder extends PagedListHolder {
74
75     private PagedListSourceProvider sourceProvider;
76
77     private Locale JavaDoc locale;
78
79     private Locale JavaDoc localeUsed;
80
81     private Object JavaDoc filter;
82
83     private Object JavaDoc filterUsed;
84
85     /**
86      * Create a new list holder.
87      * You'll need to set a source provider to be able to use the holder.
88      * @see #setSourceProvider
89      */

90     public RefreshablePagedListHolder() {
91         super();
92     }
93
94     /**
95      * Create a new list holder with the given source provider.
96      */

97     public RefreshablePagedListHolder(PagedListSourceProvider sourceProvider) {
98         super();
99         this.sourceProvider = sourceProvider;
100     }
101
102     /**
103      * Set the callback class for reloading the List when necessary.
104      * If the list is definitely not modifiable, i.e. not locale aware
105      * and no filtering, use PagedListHolder.
106      * @see org.springframework.beans.support.PagedListHolder
107      */

108     public void setSourceProvider(PagedListSourceProvider sourceProvider) {
109         this.sourceProvider = sourceProvider;
110     }
111
112     /**
113      * Return the callback class for reloading the List when necessary.
114      */

115     public PagedListSourceProvider getSourceProvider() {
116         return sourceProvider;
117     }
118
119     /**
120      * Set the Locale that the source provider should use for loading the list.
121      * This can either be populated programmatically (e.g. with the request locale),
122      * or via binding (using Locale's toString syntax, e.g. "locale=en_US").
123      * @param locale the current Locale, or <code>null</code>
124      */

125     public void setLocale(Locale JavaDoc locale) {
126         this.locale = locale;
127     }
128
129     /**
130      * Return the Locale that the source provider should use for loading the list.
131      * @return the current Locale, or <code>null</code>
132      */

133     public Locale JavaDoc getLocale() {
134         return locale;
135     }
136
137     /**
138      * Set the filter object that the source provider should use for loading the list.
139      * This will typically be a bean, for easy data binding.
140      * @param filter the filter object, or <code>null</code>
141      */

142     public void setFilter(Object JavaDoc filter) {
143         this.filter = filter;
144     }
145
146     /**
147      * Return the filter that the source provider should use for loading the list.
148      * @return the current filter, or <code>null</code>
149      */

150     public Object JavaDoc getFilter() {
151         return filter;
152     }
153
154     /**
155      * Reload the underlying list from the source provider if necessary
156      * (i.e. if the locale and/or the filter has changed), and resort it.
157      * @param force whether a reload should be performed in any case
158      */

159     public void refresh(boolean force) {
160         if (this.sourceProvider != null && (force ||
161             (this.locale != null && !this.locale.equals(this.localeUsed)) ||
162             (this.filter != null && !this.filter.equals(this.filterUsed)))) {
163             setSource(this.sourceProvider.loadList(this.locale, this.filter));
164             if (this.filter != null && !this.filter.equals(this.filterUsed)) {
165                 this.setPage(0);
166             }
167             this.localeUsed = this.locale;
168             if (null != this.filter) {
169                 this.filterUsed = BeanUtils.instantiateClass(this.filter.getClass());
170                 BeanUtils.copyProperties(this.filter, this.filterUsed);
171             }
172         }
173         resort();
174     }
175
176 }
177
Popular Tags