001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package com.sun.rave.web.ui.faces;
042:
043: import java.util.Map;
044: import javax.faces.context.FacesContext;
045: import javax.faces.el.ValueBinding;
046: import com.sun.data.provider.RowKey;
047: import com.sun.data.provider.SortCriteria;
048: import com.sun.data.provider.TableDataProvider;
049: import com.sun.data.provider.impl.TableRowDataProvider;
050:
051: /**
052: * <p>The ValueExpressionSortCriteria class is an implementation of SortCriteria
053: * that simply retrieves the sort value from a {@link ValueBinding} which is
054: * created using the specified value expression.</p>
055: *
056: * @author Joe Nuxoll
057: */
058: public class ValueExpressionSortCriteria extends SortCriteria {
059:
060: /**
061: * Constructs a ValueExpressionSortCriteria with no value expression
062: */
063: public ValueExpressionSortCriteria() {
064: }
065:
066: /**
067: * Constructs a ValueExpressionSortCriteria with the specified value
068: * expression.
069: *
070: * @param valueExpression The desired value expression
071: */
072: public ValueExpressionSortCriteria(String valueExpression) {
073: this .valueExpression = valueExpression;
074: }
075:
076: /**
077: * Constructs a ValueExpressionSortCriteria with the specified value
078: * expression and ascending state.
079: *
080: * @param valueExpression The desired value expression
081: * @param ascending The desired boolean state for the ascending property
082: */
083: public ValueExpressionSortCriteria(String valueExpression,
084: boolean ascending) {
085: this .valueExpression = valueExpression;
086: this .setAscending(ascending);
087: }
088:
089: /**
090: * Returns the value expression to use for this sort criteria.
091: *
092: * @return The currently set value expression for this sort criteria
093: */
094: public String getValueExpression() {
095: return valueExpression;
096: }
097:
098: /**
099: * Sets the value expression for this sort criteria.
100: *
101: * @param valueExpression The desired value expression for this sort criteria
102: */
103: public void setValueExpression(String valueExpression) {
104: this .valueExpression = valueExpression;
105: }
106:
107: /**
108: * Returns the request map variable key that will be used to store the
109: * {@link TableRowDataProvider} for the current row being sorted. This
110: * allows value expressions to refer to the "current" row during the sort
111: * operation.
112: *
113: * @return String key to use for the {@link TableRowDataProvider}
114: */
115: public String getRequestMapKey() {
116: return requestMapKey;
117: }
118:
119: /**
120: * Sets the request map variable key that will be used to store the
121: * {@link TableRowDataProvider} for the current row being sorted. This
122: * allows value expressions to refer to the "current" row during the sort
123: * operation.
124: *
125: * @param requestMapKey String key to use for the {@link TableRowDataProvider}
126: */
127: public void setRequestMapKey(String requestMapKey) {
128: this .requestMapKey = requestMapKey;
129: }
130:
131: /**
132: * <p>If no display name is set, this returns the value expression.</p>
133: *
134: * {@inheritDoc}
135: */
136: public String getDisplayName() {
137: String name = super .getDisplayName();
138: if ((name == null || "".equals(name))
139: && valueExpression != null
140: && !"".equals(valueExpression)) {
141: return valueExpression;
142: }
143: return name;
144: }
145:
146: /**
147: * Returns the value expression.
148: *
149: * {@inheritDoc}
150: */
151: public String getCriteriaKey() {
152: return valueExpression != null ? valueExpression : ""; // NOI18N
153: }
154:
155: /**
156: * <p>Returns the value from a {@link ValueBinding} created using the value
157: * expression. The passed arguments are ignored.</p>
158: *
159: * {@inheritDoc}
160: */
161: public Object getSortValue(TableDataProvider provider, RowKey row) {
162: if (valueExpression == null || "".equals(valueExpression)) {
163: return null;
164: }
165:
166: FacesContext facesContext = FacesContext.getCurrentInstance();
167: ValueBinding valueBinding = facesContext.getApplication()
168: .createValueBinding(valueExpression);
169:
170: if (valueBinding == null) {
171: return null;
172: }
173:
174: Map requestMap = facesContext.getExternalContext()
175: .getRequestMap();
176: Object value = null;
177:
178: synchronized (rowProviderLock) {
179:
180: Object storedRequestMapValue = null;
181: if (requestMapKey != null && !"".equals(requestMapKey)) {
182: storedRequestMapValue = requestMap.get(requestMapKey);
183: if (rowProvider == null) {
184: rowProvider = new TableRowDataProvider();
185: }
186: rowProvider.setTableDataProvider(provider);
187: rowProvider.setTableRow(row);
188: requestMap.put(requestMapKey, rowProvider);
189: }
190:
191: value = valueBinding.getValue(facesContext);
192:
193: if (requestMapKey != null && !"".equals(requestMapKey)) {
194: if (rowProvider != null) {
195: rowProvider.setTableDataProvider(null);
196: rowProvider.setTableRow(null);
197: }
198: requestMap.put(requestMapKey, storedRequestMapValue);
199: }
200: }
201:
202: return value;
203: }
204:
205: private String valueExpression;
206: private String requestMapKey = "currentRow"; // NOI18N
207: private transient TableRowDataProvider rowProvider;
208: private String rowProviderLock = "rowProviderLock"; // this is a monitor lock for rowProvider
209: }
|