001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.databinding.datagrid.runtime.config;
020:
021: import java.util.List;
022:
023: import javax.servlet.ServletRequest;
024:
025: import org.apache.beehive.netui.databinding.datagrid.api.sort.Sort;
026: import org.apache.beehive.netui.databinding.datagrid.api.sort.SortModel;
027: import org.apache.beehive.netui.databinding.datagrid.api.sort.SortStrategy;
028: import org.apache.beehive.netui.databinding.datagrid.api.filter.FilterModel;
029: import org.apache.beehive.netui.databinding.datagrid.api.filter.Filter;
030: import org.apache.beehive.netui.databinding.datagrid.api.pager.PagerModel;
031: import org.apache.beehive.netui.databinding.datagrid.api.rendering.PagerRenderer;
032: import org.apache.beehive.netui.databinding.datagrid.api.rendering.StyleModel;
033: import org.apache.beehive.netui.databinding.datagrid.api.DataGridConfig;
034: import org.apache.beehive.netui.databinding.datagrid.api.DataGridResourceProvider;
035: import org.apache.beehive.netui.databinding.datagrid.api.DataGridState;
036: import org.apache.beehive.netui.databinding.datagrid.api.DataGridStateCodec;
037: import org.apache.beehive.netui.databinding.datagrid.runtime.rendering.pager.PreviousNextPagerRenderer;
038: import org.apache.beehive.netui.databinding.datagrid.runtime.model.style.DefaultStyleModel;
039: import org.apache.beehive.netui.databinding.datagrid.runtime.model.style.EmptyStyleModel;
040:
041: /**
042: * <p>
043: * Default implementation of the {@link DataGridConfig} base class. This class is used to provide concrete
044: * implementations of state containers and service providers for the data grid.
045: * </p>
046: */
047: public class DefaultDataGridConfig extends DataGridConfig {
048:
049: private static final String STYLE_POLICY_NAME_EMPTY = "empty";
050: private static final String STYLE_POLICY_NAME_DEFAULT = "default";
051: private static final String STYLE_PREFIX_DEFAULT = "datagrid";
052:
053: private static final StyleModel DEFAULT_STYLE_POLICY = new DefaultStyleModel(
054: STYLE_PREFIX_DEFAULT);
055: private static final StyleModel EMPTY_STYLE_POLICY = new EmptyStyleModel();
056: private static final SortStrategy SORT_STRATEGY = new DefaultSortStrategy();
057:
058: /**
059: * Create a {@link DataGridState} object. The default implementation returned is
060: * {@link DataGridState}.
061: * @return a data grid state implementation
062: */
063: public DataGridState createDataGridState() {
064: return new DataGridState();
065: }
066:
067: /**
068: * Create a {@link Sort} object. The default implementation returned is {@link Sort}.
069: * @return a sort
070: */
071: public Sort createSort() {
072: return new Sort();
073: }
074:
075: /**
076: * Create a {@link Filter} object. The default implementation returned is {@link Filter}.
077: * @return a filter
078: */
079: public Filter createFilter() {
080: return new Filter();
081: }
082:
083: /**
084: * Create a {@link SortModel} object. The default implementation returned is {@link SortModel} with a
085: * {@link SortStrategy} of {@link org.apache.beehive.netui.databinding.datagrid.runtime.config.DefaultSortStrategy}.
086: * @param sorts the list of sorts for a data grid
087: * @return a sort model
088: */
089: public SortModel createSortModel(List/*<Sort>*/sorts) {
090: SortModel sortModel = new SortModel(sorts);
091: sortModel.setSortStrategy(SORT_STRATEGY);
092: return sortModel;
093: }
094:
095: /**
096: * Create a {@link FilterModel} object. The default implementation returned is {@link FilterModel}.
097: * @param filters the list of filters for a data grid
098: * @return a filter model
099: */
100: public FilterModel createFilterModel(List/*<Filter>*/filters) {
101: return new FilterModel(filters);
102: }
103:
104: /**
105: * Create a {@link PagerModel} object. The default implementation returned is {@link PagerModel}.
106: * @return a pager model
107: */
108: public PagerModel createPagerModel() {
109: return new PagerModel();
110: }
111:
112: /**
113: * Create a {@link DataGridStateCodec} for a grid with the given name for the given {@link ServletRequest}.
114: * @param request the current request
115: * @param gridName a data grid's name
116: * @return the state encoder / decoder for a data grid's request state
117: */
118: public DataGridStateCodec createStateCodec(ServletRequest request,
119: String gridName) {
120: DefaultDataGridStateCodec codec = new DefaultDataGridStateCodec(
121: this );
122: codec.setServletRequest(request);
123: codec.setGridName(gridName);
124: return codec;
125: }
126:
127: /**
128: * Get the default {@link PagerRenderer}. The default pager renderer will display a pager with previous / next
129: * page links via the implementation class {@link PreviousNextPagerRenderer}.
130: * @return the pager renderer
131: */
132: public PagerRenderer getDefaultPagerRenderer() {
133: return new PreviousNextPagerRenderer();
134: }
135:
136: /**
137: * Get a {@link DataGridResourceProvider}. The default implementation class is
138: * {@link org.apache.beehive.netui.databinding.datagrid.runtime.config.DefaultDataGridResourceProvider}
139: * and provides a basic implementation that reads messages from the default .properties file.
140: * @return the resource provider
141: */
142: public DataGridResourceProvider getDefaultResourceProvider() {
143: return new DefaultDataGridResourceProvider();
144: }
145:
146: /**
147: * Get a {@link DataGridResourceProvider} for the given resource bundle path. The default implementation
148: * will set this resource bundle but does not enable message chaining.
149: * @param resourceBundle a resource bundle specifically requested by a data grid
150: * @return the resource provider
151: */
152: public DataGridResourceProvider getResourceProvider(
153: String resourceBundle) {
154: DataGridResourceProvider resourceProvider = new DefaultDataGridResourceProvider();
155: resourceProvider.setResourceBundlePath(resourceBundle);
156: return resourceProvider;
157: }
158:
159: /**
160: * Get a {@link StyleModel} given a model name and a style class prefix. This class exposes two available
161: * style names:
162: * <table>
163: * <tr><td>Name</td><td>Description</td><td>Implementation Class</td></tr>
164: * <tr><td><code>empty</code></td>
165: * <td>Renders CSS style classes that are non-prefixed and generally empty.</td>
166: * <td>{@link EmptyStyleModel}</td>
167: * </tr>
168: * <tr><td><code>default</code></td>
169: * <td>Renders CSS style classes with names using a default prefix of <code>datagrid</code></td>
170: * <td>{@link DefaultStyleModel}</td>
171: * </tr>
172: * </table>
173: * When using the <code>empty</code> style model, styles rendered on the <table> element will
174: * be empty; the same style rendered wtih the <code>default</code> style model will render as
175: * <code>class="datagrid"</code>. If the style prefix "foo" is provided for the <code>default</code> style policy
176: * the style name will be rendered as <code>class="foo"</code>.x
177: * @param name the name of a {@link StyleModel} implementation to use
178: * @param classPrefix the prefix for a style name
179: * @return the style model
180: */
181: public StyleModel getStyleModel(String name, String classPrefix) {
182: if (name == null || name.equals(STYLE_POLICY_NAME_DEFAULT)) {
183: if (classPrefix != null)
184: return new DefaultStyleModel(classPrefix);
185: else
186: return DEFAULT_STYLE_POLICY;
187: } else if (name != null && name.equals(STYLE_POLICY_NAME_EMPTY))
188: return EMPTY_STYLE_POLICY;
189: else
190: return DEFAULT_STYLE_POLICY;
191: }
192: }
|