KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > databinding > datagrid > api > DataGridStateFactory


1 /*
2  * Copyright 2004 The Apache Software Foundation.
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  * $Header:$
17  */

18 package org.apache.beehive.netui.databinding.datagrid.api;
19
20 import java.util.HashMap JavaDoc;
21 import javax.servlet.jsp.PageContext JavaDoc;
22 import javax.servlet.jsp.JspContext JavaDoc;
23 import javax.servlet.ServletRequest JavaDoc;
24
25 import org.apache.beehive.netui.util.Bundle;
26
27 /**
28  * <p>
29  * Factory class used to construct instances of {@link DataGridState} objects. This
30  * class is used by the data grid and other clients to obtain the state for a data grid
31  * with a given name.
32  * </p>
33  * <p>
34  * Data grid state information is scoped by a unique data grid String name. This name
35  * should be unique for a particular scope in a request. For all factory methods that
36  * take a data grid name as a parameter, the value of the <code>name</code> attribute
37  * should match the value of the
38  * {@link org.apache.beehive.netui.tags.databinding.datagrid.DataGrid#setName(String)}
39  * attribute for the data grid whose state to lookup.
40  * </p>
41  */

42 public final class DataGridStateFactory {
43
44     private static final String JavaDoc KEY = DataGridStateFactory.class.getName() + "REQUEST_KEY";
45     private static final DataGridConfig DEFAULT_DATA_GRID_CONFIG = DataGridConfigFactory.getInstance();
46
47     /**
48      * Get an instance of a DataGridStateFactory given a {@link JspContext}.
49      *
50      * @param jspContext the current {@link JspContext}
51      * @return an instance of the factory
52      */

53     public static final DataGridStateFactory getInstance(JspContext JavaDoc jspContext) {
54         assert jspContext instanceof PageContext JavaDoc;
55         return getInstance(((PageContext JavaDoc)jspContext).getRequest());
56     }
57
58     /**
59      * Get an instance of a DataGridStateFactory given a {@link ServletRequest}.
60      *
61      * @param request the current {@link ServletRequest}
62      * @return an instance of the factory
63      */

64     public static final DataGridStateFactory getInstance(ServletRequest JavaDoc request) {
65         Object JavaDoc obj = request.getAttribute(KEY);
66         if(obj != null) {
67             assert obj instanceof DataGridStateFactory;
68             return (DataGridStateFactory)obj;
69         }
70         else {
71             DataGridStateFactory factory = new DataGridStateFactory(request);
72             request.setAttribute(KEY, factory);
73             return factory;
74         }
75     }
76
77     private final ServletRequest JavaDoc _request;
78     private final HashMap JavaDoc/*<String, DataGridStateCodec>*/ _cache;
79
80     private DataGridStateFactory(ServletRequest JavaDoc request) {
81         _request = request;
82         _cache = new HashMap JavaDoc/*<String, DataGridStateCodec>*/();
83     }
84
85     /**
86      * <p>
87      * Lookup a {@link DataGridState} object given a data grid identifier.
88      * </p>
89      * <p>
90      * This method will use the default {@link DataGridConfig} object when returning a data grid specific
91      * implementation of the {@link DataGridState} object. In order to specify a {@link DataGridConfig},
92      * the {@link DataGridStateFactory#getDataGridState(String, DataGridConfig)} can be supplied
93      * with a specific data grid configuration.
94      * </p>
95      *
96      * @param name the name of a data grid.
97      * @return the {@link DataGridState} for the data grid with the given name
98      */

99     public final DataGridState getDataGridState(String JavaDoc name) {
100         return getDataGridState(name, DEFAULT_DATA_GRID_CONFIG);
101     }
102
103     /**
104      * <p>
105      * Lookup a {@link DataGridState} object given a data grid identifier and a specific
106      * {@link DataGridConfig} object.
107      * </p>
108      *
109      * @param name the name of the data grid
110      * @param config the {@link DataGridConfig} object to use when creating the
111      * grid's {@link DataGridState} object.
112      * @return the data grid state object
113      */

114     public final DataGridState getDataGridState(String JavaDoc name, DataGridConfig config) {
115         if(config == null)
116             throw new IllegalArgumentException JavaDoc(Bundle.getErrorString("DataGridStateFactory_nullDataGridConfig"));
117
118         DataGridStateCodec codec = lookupCodec(name, config);
119         DataGridState state = codec.getDataGridState();
120         return state;
121     }
122
123     /**
124      * <p>
125      * Lookup a {@link DataGridURLBuilder} object given a data grid identifier.
126      * </p>
127      * <p>
128      * This method will use the default {@link DataGridConfig} object when returning a data grid specific
129      * implementation of the {@link DataGridURLBuilder} object. In order to specify a {@link DataGridConfig},
130      * the {@link DataGridStateFactory#getDataGridURLBuilder(String, DataGridConfig)} can be supplied
131      * with a specific data grid configuration.
132      * </p>
133      *
134      * @param name the name of the data grid
135      * @return the {@link DataGridURLBuilder} for the data grid with the given name
136      */

137     public final DataGridURLBuilder getDataGridURLBuilder(String JavaDoc name) {
138         return getDataGridURLBuilder(name, DEFAULT_DATA_GRID_CONFIG);
139     }
140
141     /**
142      * <p>
143      * Lookup a {@link DataGridURLBuilder} object given a data grid identifier and a specific
144      * {@link DataGridConfig} object.
145      * </p>
146      *
147      * @param name the name of the data grid
148      * @param config the {@link DataGridConfig} object to use when creating the
149      * grid's {@link DataGridURLBuilder} object.
150      * @return the URL builder for a data grid's state object
151      */

152     public final DataGridURLBuilder getDataGridURLBuilder(String JavaDoc name, DataGridConfig config) {
153         if(config == null)
154             throw new IllegalArgumentException JavaDoc(Bundle.getErrorString("DataGridStateFactory_nullDataGridConfig"));
155
156         DataGridStateCodec codec = lookupCodec(name, config);
157         DataGridURLBuilder builder = codec.getDataGridURLBuilder();
158         return builder;
159     }
160
161     /**
162      * <p>
163      * Convenience method that allows a {@link DataGridState} object from a client to
164      * be attached to the factory. This allows subsequent calls to retrieve this same {@link DataGridState}
165      * instance.
166      * </p>
167      *
168      * @param name the name of the data grid
169      * @param state the {@link DataGridState} object to attach
170      */

171     public final void attachDataGridState(String JavaDoc name, DataGridState state) {
172         DataGridStateCodec codec = lookupCodec(name, DEFAULT_DATA_GRID_CONFIG);
173         codec.setDataGridState(state);
174     }
175
176     private final DataGridStateCodec lookupCodec(String JavaDoc name, DataGridConfig config) {
177         DataGridStateCodec codec = null;
178         if(_cache.containsKey(name))
179             codec = (DataGridStateCodec)_cache.get(name);
180         else {
181             assert config != null;
182             codec = config.createStateCodec(_request, name);
183             _cache.put(name, codec);
184         }
185
186         return codec;
187     }
188 }
189
Popular Tags