001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: /* Original Copyright
035: * Copyright 2004 The Apache Software Foundation.
036: *
037: * Licensed under the Apache License, Version 2.0 (the "License");
038: * you may not use this file except in compliance with the License.
039: * You may obtain a copy of the License at
040: *
041: * http://www.apache.org/licenses/LICENSE-2.0
042: *
043: * Unless required by applicable law or agreed to in writing, software
044: * distributed under the License is distributed on an "AS IS" BASIS,
045: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
046: * See the License for the specific language governing permissions and
047: * limitations under the License.
048: */
049:
050: package com.icesoft.faces.component.commandsortheader;
051:
052: import com.icesoft.faces.component.CSS_DEFAULT;
053: import com.icesoft.faces.component.ext.HtmlCommandLink;
054: import com.icesoft.faces.component.ext.HtmlDataTable;
055: import com.icesoft.faces.component.ext.taglib.Util;
056:
057: import javax.faces.component.UIComponent;
058: import javax.faces.context.FacesContext;
059: import javax.faces.el.ValueBinding;
060: import javax.faces.event.AbortProcessingException;
061: import javax.faces.event.ActionEvent;
062: import javax.faces.event.FacesEvent;
063:
064: /**
065: * This component is an extension of com.icesoft.faces.component.ext.HtmlCommandLink,
066: * works in conjunction with dataTable.
067: */
068: public class CommandSortHeader extends HtmlCommandLink {
069: /* (non-Javadoc)
070: * @see javax.faces.component.UIComponent#broadcast(javax.faces.event.FacesEvent)
071: */
072: public void broadcast(FacesEvent event)
073: throws AbortProcessingException {
074: super .broadcast(event);
075:
076: if (event instanceof ActionEvent) {
077: HtmlDataTable dataTable = findParentDataTable();
078: if (dataTable == null) {
079: //log.error("parent table not found");
080: } else {
081: String colName = getColumnName();
082: String currentSortColumn = dataTable.getSortColumn();
083: boolean currentAscending = dataTable.isSortAscending();
084: if (colName.equals(currentSortColumn)) {
085: dataTable.setSortColumn(getColumnName());
086: dataTable.setSortAscending(!currentAscending);
087: } else {
088: dataTable.setSortColumn(getColumnName());
089: dataTable.setSortAscending(true);
090: }
091: }
092: }
093: }
094:
095: /**
096: * <p>Return the instance of the <code>parentDataTable</code> of this
097: * component.</p>
098: */
099: public HtmlDataTable findParentDataTable() {
100: UIComponent parent = getParent();
101: while (parent != null) {
102: if (parent instanceof HtmlDataTable) {
103: return (HtmlDataTable) parent;
104: }
105: parent = parent.getParent();
106: }
107: return null;
108: }
109:
110: /**
111: * <p>Gets the state of the instance as a <code>Serializable</code>
112: * Object.</p>
113: */
114: public Object saveState(FacesContext context) {
115: Object values[] = new Object[3];
116: values[0] = super .saveState(context);
117: values[1] = _columnName;
118: values[2] = _arrow;
119: return ((Object) (values));
120: }
121:
122: /**
123: * <p>Perform any processing required to restore the state from the entries
124: * in the state Object.</p>
125: */
126: public void restoreState(FacesContext context, Object state) {
127: Object values[] = (Object[]) state;
128: super .restoreState(context, values[0]);
129: _columnName = (String) values[1];
130: _arrow = (Boolean) values[2];
131: }
132:
133: private String styleClass;
134:
135: /**
136: * <p>Set the value of the <code>styleClass</code> property.</p>
137: */
138: public void setStyleClass(String styleClass) {
139: this .styleClass = styleClass;
140: }
141:
142: /**
143: * <p>Return the value of the <code>styleClass</code> property.</p>
144: */
145: public String getStyleClass() {
146: return Util.getQualifiedStyleClass(this , styleClass,
147: CSS_DEFAULT.COMMAND_SORT_HEADER_STYLE_CLASS,
148: "styleClass", isDisabled());
149: }
150:
151: public static final String COMPONENT_TYPE = "com.icesoft.faces.SortHeader";
152: public static final String COMPONENT_FAMILY = "javax.faces.Command";
153: public static final String DEFAULT_RENDERER_TYPE = "com.icesoft.faces.SortHeader";
154:
155: private String _columnName = null;
156: private Boolean _arrow = null;
157:
158: public CommandSortHeader() {
159: setRendererType(DEFAULT_RENDERER_TYPE);
160: }
161:
162: /**
163: * <p>Return the value of the <code>COMPONENT_FAMILY</code> of this
164: * component.</p>
165: */
166: public String getFamily() {
167: return COMPONENT_FAMILY;
168: }
169:
170: /**
171: * <p>Set the value of the <code>columnName</code> property.</p>
172: */
173: public void setColumnName(String columnName) {
174: _columnName = columnName;
175: }
176:
177: /**
178: * <p>Return the value of the <code>columnName</code> property.</p>
179: */
180: public String getColumnName() {
181: if (_columnName != null) {
182: return _columnName;
183: }
184: ValueBinding vb = getValueBinding("columnName");
185: return vb != null ? vb.getValue(getFacesContext()).toString()
186: : null;
187: }
188:
189: /**
190: * <p>Set the value of the <code>arrow</code> property.</p>
191: */
192: public void setArrow(boolean arrow) {
193: _arrow = Boolean.valueOf(arrow);
194: }
195:
196: /**
197: * <p>Return the value of the <code>arrow</code> property.</p>
198: */
199: public boolean isArrow() {
200: if (_arrow != null) {
201: return _arrow.booleanValue();
202: }
203: ValueBinding vb = getValueBinding("arrow");
204: Boolean v = vb != null ? (Boolean) vb
205: .getValue(getFacesContext()) : null;
206: return v != null ? v.booleanValue() : false;
207: }
208: }
|