View Javadoc

1   /*
2    * Copyright 2004 Jeff Johnston
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.extremecomponents.table.cell;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.extremecomponents.table.bean.Column;
22  import org.extremecomponents.table.model.TableModel;
23  import org.extremecomponents.table.model.TableProperties;
24  import org.extremecomponents.table.model.ParameterRegistry;
25  import org.extremecomponents.table.model.SortHandler;
26  import org.extremecomponents.util.HtmlBuilder;
27  
28  
29  
30  /***
31   * A header cell.
32   *
33   * @author Jeff Johnston
34   */
35  public class HeaderCell extends BaseCell
36  {
37  	private static Log logger = LogFactory.getLog(HeaderCell.class);
38  	private String headerClass;
39  	private String sortImage;
40  	private String sortOrder; //by default it cannot link
41  
42  	/***
43  	 * Account for the fact that we have to sort.
44  	 */
45  	public void init(TableModel model, Column column, Integer rowcount)
46  	{
47  		super.init(model, column, rowcount);
48  
49  		headerClass = column.getHeaderClass();
50  
51  		SortHandler sortHandler = model.getSortHandler();
52  
53  		if (sortHandler.canSort(column.getProperty()))
54  		{
55  			sortOrder = model.getRegistry().getParameter(ParameterRegistry.SORT + column.getProperty());
56  
57  			if (sortOrder.equals(SortHandler.SORT_DEFAULT_))
58  			{
59  				sortOrder = SortHandler.SORT_ASC_;
60  			}
61  			else if (sortOrder.equals(SortHandler.SORT_ASC_))
62  			{
63  				headerClass = model.getProperties().getProperty(TableProperties.HEADER_SORT_CLASS);
64  				sortImage = model.getTable().getImage(SortHandler.SORT_ASC_IMAGE) ;
65  				sortOrder = SortHandler.SORT_DESC_;
66  			}
67  			else if (sortOrder.equals(SortHandler.SORT_DESC_))
68  			{
69  				headerClass = model.getProperties().getProperty(TableProperties.HEADER_SORT_CLASS);
70  				sortImage = model.getTable().getImage(SortHandler.SORT_DESC_IMAGE) ;
71  				sortOrder = SortHandler.SORT_DEFAULT_;
72  			}
73  		}
74  		else
75  		{
76  			sortOrder = SortHandler.SORT_ASC_; //the default
77  		}
78  	}
79  	
80  	public void destroy() 
81  	{
82  		super.destroy();
83  		headerClass = null;
84  		sortImage = null;
85  		sortOrder = null; 
86  	}
87  	
88  	public String html()
89  	{
90  		HtmlBuilder html = new HtmlBuilder();
91  
92  		html.th(2);
93  		
94  		if (StringUtils.isNotEmpty(headerClass))
95  		{
96  			html.styleClass(headerClass);
97  		}
98  
99  		html.close();
100 
101 		if (model.getTable().canSort() && column.canSort())
102 		{
103 			html.a().quote();
104 
105 			String action = model.getTable().getAction();
106 
107 			if (StringUtils.isNotEmpty(action))
108 			{
109 				html.append(action);
110 			}
111 
112 			html.question().append(model.getTableKey()).append(ParameterRegistry.SORT).append(column.getProperty()).equals().append(sortOrder);
113 			html.append(model.getRegistry().getURLParameterString(true, false, false));
114 			html.append(model.getRegistry().getParameterString(ParameterRegistry.OPEN));
115 			html.quote().title("Sort By " + column.getTitle()).close();
116 			
117 			//put in the name
118 			html.append(column.getTitle());
119 
120 			//put in the image
121 			if (StringUtils.isNotEmpty(sortImage))
122 			{
123 				html.append(" ").img(sortImage);
124 			}
125 
126 			html.aEnd();
127 		}
128 		else
129 		{
130 			html.append(column.getTitle());
131 		}
132 
133 		html.thEnd();
134 
135 		return html.toString();
136 	}
137 	
138 	public String value()
139 	{
140 		return column.getTitle();
141 	}	
142 }