KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > component > commandsortheader > CommandSortHeader


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 /* Original Copyright
35  * Copyright 2004 The Apache Software Foundation.
36  *
37  * Licensed under the Apache License, Version 2.0 (the "License");
38  * you may not use this file except in compliance with the License.
39  * You may obtain a copy of the License at
40  *
41  * http://www.apache.org/licenses/LICENSE-2.0
42  *
43  * Unless required by applicable law or agreed to in writing, software
44  * distributed under the License is distributed on an "AS IS" BASIS,
45  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
46  * See the License for the specific language governing permissions and
47  * limitations under the License.
48  */

49
50 package com.icesoft.faces.component.commandsortheader;
51
52 import com.icesoft.faces.component.CSS_DEFAULT;
53 import com.icesoft.faces.component.ext.HtmlCommandLink;
54 import com.icesoft.faces.component.ext.HtmlDataTable;
55 import com.icesoft.faces.component.ext.taglib.Util;
56
57 import javax.faces.component.UIComponent;
58 import javax.faces.context.FacesContext;
59 import javax.faces.el.ValueBinding;
60 import javax.faces.event.AbortProcessingException;
61 import javax.faces.event.ActionEvent;
62 import javax.faces.event.FacesEvent;
63
64 /**
65  * This component is an extension of com.icesoft.faces.component.ext.HtmlCommandLink,
66  * works in conjunction with dataTable.
67  */

68 public class CommandSortHeader
69         extends HtmlCommandLink {
70     /* (non-Javadoc)
71      * @see javax.faces.component.UIComponent#broadcast(javax.faces.event.FacesEvent)
72      */

73     public void broadcast(FacesEvent event) throws AbortProcessingException {
74         super.broadcast(event);
75
76         if (event instanceof ActionEvent) {
77             HtmlDataTable dataTable = findParentDataTable();
78             if (dataTable == null) {
79                 //log.error("parent table not found");
80
} else {
81                 String JavaDoc colName = getColumnName();
82                 String JavaDoc currentSortColumn = dataTable.getSortColumn();
83                 boolean currentAscending = dataTable.isSortAscending();
84                 if (colName.equals(currentSortColumn)) {
85                     dataTable.setSortColumn(getColumnName());
86                     dataTable.setSortAscending(!currentAscending);
87                 } else {
88                     dataTable.setSortColumn(getColumnName());
89                     dataTable.setSortAscending(true);
90                 }
91             }
92         }
93     }
94
95
96     /**
97      * <p>Return the instance of the <code>parentDataTable</code> of this
98      * component.</p>
99      */

100     public HtmlDataTable findParentDataTable() {
101         UIComponent parent = getParent();
102         while (parent != null) {
103             if (parent instanceof HtmlDataTable) {
104                 return (HtmlDataTable) parent;
105             }
106             parent = parent.getParent();
107         }
108         return null;
109     }
110
111
112     /**
113      * <p>Gets the state of the instance as a <code>Serializable</code>
114      * Object.</p>
115      */

116     public Object JavaDoc saveState(FacesContext context) {
117         Object JavaDoc values[] = new Object JavaDoc[3];
118         values[0] = super.saveState(context);
119         values[1] = _columnName;
120         values[2] = _arrow;
121         return ((Object JavaDoc) (values));
122     }
123
124     /**
125      * <p>Perform any processing required to restore the state from the entries
126      * in the state Object.</p>
127      */

128     public void restoreState(FacesContext context, Object JavaDoc state) {
129         Object JavaDoc values[] = (Object JavaDoc[]) state;
130         super.restoreState(context, values[0]);
131         _columnName = (String JavaDoc) values[1];
132         _arrow = (Boolean JavaDoc) values[2];
133     }
134
135     private String JavaDoc styleClass;
136
137     /**
138      * <p>Set the value of the <code>styleClass</code> property.</p>
139      */

140     public void setStyleClass(String JavaDoc styleClass) {
141         this.styleClass = styleClass;
142     }
143
144     /**
145      * <p>Return the value of the <code>styleClass</code> property.</p>
146      */

147     public String JavaDoc getStyleClass() {
148         return Util.getQualifiedStyleClass(this, styleClass,
149                                              CSS_DEFAULT.COMMAND_SORT_HEADER_STYLE_CLASS
150                                              , "styleClass",
151                                              isDisabled());
152     }
153
154     public static final String JavaDoc COMPONENT_TYPE = "com.icesoft.faces.SortHeader";
155     public static final String JavaDoc COMPONENT_FAMILY = "javax.faces.Command";
156     public static final String JavaDoc DEFAULT_RENDERER_TYPE =
157             "com.icesoft.faces.SortHeader";
158
159     private String JavaDoc _columnName = null;
160     private Boolean JavaDoc _arrow = null;
161
162     public CommandSortHeader() {
163         setRendererType(DEFAULT_RENDERER_TYPE);
164     }
165
166     /**
167      * <p>Return the value of the <code>COMPONENT_FAMILY</code> of this
168      * component.</p>
169      */

170     public String JavaDoc getFamily() {
171         return COMPONENT_FAMILY;
172     }
173
174     /**
175      * <p>Set the value of the <code>columnName</code> property.</p>
176      */

177     public void setColumnName(String JavaDoc columnName) {
178         _columnName = columnName;
179     }
180
181     /**
182      * <p>Return the value of the <code>columnName</code> property.</p>
183      */

184     public String JavaDoc getColumnName() {
185         if (_columnName != null) {
186             return _columnName;
187         }
188         ValueBinding vb = getValueBinding("columnName");
189         return vb != null ? vb.getValue(getFacesContext()).toString() : null;
190     }
191
192     /**
193      * <p>Set the value of the <code>arrow</code> property.</p>
194      */

195     public void setArrow(boolean arrow) {
196         _arrow = Boolean.valueOf(arrow);
197     }
198
199     /**
200      * <p>Return the value of the <code>arrow</code> property.</p>
201      */

202     public boolean isArrow() {
203         if (_arrow != null) {
204             return _arrow.booleanValue();
205         }
206         ValueBinding vb = getValueBinding("arrow");
207         Boolean JavaDoc v =
208                 vb != null ? (Boolean JavaDoc) vb.getValue(getFacesContext()) : null;
209         return v != null ? v.booleanValue() : false;
210     }
211 }
212
Popular Tags