KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > sortheader > HtmlCommandSortHeader


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 package org.apache.myfaces.custom.sortheader;
17
18 import org.apache.myfaces.component.html.ext.HtmlCommandLink;
19 import org.apache.myfaces.component.html.ext.HtmlDataTable;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import javax.faces.component.UIComponent;
25 import javax.faces.context.FacesContext;
26 import javax.faces.el.ValueBinding;
27 import javax.faces.event.AbortProcessingException;
28 import javax.faces.event.ActionEvent;
29 import javax.faces.event.FacesEvent;
30
31 /**
32  * @author Manfred Geiler (latest modification by $Author: matze $)
33  * @version $Revision: 1.5 $ $Date: 2004/10/13 11:50:58 $
34  */

35 public class HtmlCommandSortHeader
36         extends HtmlCommandLink
37 {
38     private static final Log log = LogFactory.getLog(HtmlCommandSortHeader.class);
39
40     /*
41     public boolean isImmediate()
42     {
43         return true;
44     }
45     */

46
47     public void broadcast(FacesEvent event) throws AbortProcessingException
48     {
49         super.broadcast(event);
50
51         if (event instanceof ActionEvent)
52         {
53             HtmlDataTable dataTable = findParentDataTable();
54             if (dataTable == null)
55             {
56                 log.error("CommandSortHeader has no MyFacesHtmlDataTable parent");
57             }
58             else
59             {
60                 String JavaDoc colName = getColumnName();
61                 String JavaDoc currentSortColumn = dataTable.getSortColumn();
62                 boolean currentAscending = dataTable.isSortAscending();
63                 if (colName.equals(currentSortColumn))
64                 {
65                     dataTable.setSortColumn(getColumnName());
66                     dataTable.setSortAscending(!currentAscending);
67                 }
68                 else
69                 {
70                     dataTable.setSortColumn(getColumnName());
71                     dataTable.setSortAscending(true);
72                 }
73             }
74         }
75     }
76
77
78     public HtmlDataTable findParentDataTable()
79     {
80         UIComponent parent = getParent();
81         while (parent != null)
82         {
83             if (parent instanceof HtmlDataTable)
84             {
85                 return (HtmlDataTable)parent;
86             }
87             parent = parent.getParent();
88         }
89         return null;
90     }
91
92
93     public Object JavaDoc saveState(FacesContext context)
94     {
95         Object JavaDoc values[] = new Object JavaDoc[3];
96         values[0] = super.saveState(context);
97         values[1] = _columnName;
98         values[2] = _arrow;
99         return ((Object JavaDoc) (values));
100     }
101
102     public void restoreState(FacesContext context, Object JavaDoc state)
103     {
104         Object JavaDoc values[] = (Object JavaDoc[])state;
105         super.restoreState(context, values[0]);
106         _columnName = (String JavaDoc)values[1];
107         _arrow = (Boolean JavaDoc)values[2];
108     }
109
110     //------------------ GENERATED CODE BEGIN (do not modify!) --------------------
111

112     public static final String JavaDoc COMPONENT_TYPE = "org.apache.myfaces.HtmlCommandSortHeader";
113     public static final String JavaDoc COMPONENT_FAMILY = "javax.faces.Command";
114     private static final String JavaDoc DEFAULT_RENDERER_TYPE = "javax.faces.Link";
115
116     private String JavaDoc _columnName = null;
117     private Boolean JavaDoc _arrow = null;
118
119     public HtmlCommandSortHeader()
120     {
121         setRendererType(DEFAULT_RENDERER_TYPE);
122     }
123
124     public String JavaDoc getFamily()
125     {
126         return COMPONENT_FAMILY;
127     }
128
129     public void setColumnName(String JavaDoc columnName)
130     {
131         _columnName = columnName;
132     }
133
134     public String JavaDoc getColumnName()
135     {
136         if (_columnName != null) return _columnName;
137         ValueBinding vb = getValueBinding("columnName");
138         return vb != null ? (String JavaDoc)vb.getValue(getFacesContext()) : null;
139     }
140
141     public void setArrow(boolean arrow)
142     {
143         _arrow = Boolean.valueOf(arrow);
144     }
145
146     public boolean isArrow()
147     {
148         if (_arrow != null) return _arrow.booleanValue();
149         ValueBinding vb = getValueBinding("arrow");
150         Boolean JavaDoc v = vb != null ? (Boolean JavaDoc)vb.getValue(getFacesContext()) : null;
151         return v != null ? v.booleanValue() : false;
152     }
153
154
155     //------------------ GENERATED CODE END ---------------------------------------
156
}
157
Popular Tags