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 java.text.ParseException;
19  import java.text.SimpleDateFormat;
20  
21  import java.util.Collections;
22  import java.util.Date;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import org.apache.commons.lang.time.DateFormatUtils;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.extremecomponents.table.model.ParameterRegistry;
30  import org.extremecomponents.util.HtmlBuilder;
31  
32  
33  
34  /***
35   * A filter cell.
36   *
37   * @author Jeff Johnston
38   */
39  public class FilterCell extends BaseCell
40  {
41  	private static Log logger = LogFactory.getLog(FilterCell.class);
42  
43  	/***
44  	 * If the filter is specified the default is to use a <input type=text> tag.
45  	 */
46  	private String getInput()
47  	{
48  		HtmlBuilder html = new HtmlBuilder();
49  	
50  		html.input("text");
51  		html.styleClass("filterInput");
52  		html.name(model.getTableKey() + ParameterRegistry.FILTER + column.getProperty());
53  		html.value(column.getValue().toString());
54  		html.close();
55  
56  		return html.toString();
57  	}
58  
59  	/***
60  	 * If the filter is specified as a droplist type, then need to generate
61  	 * a <select> input type tag.
62  	 */
63  	private String dropList()
64  	{
65  		HtmlBuilder html = new HtmlBuilder();
66  
67  		List list = column.getFilterDroplist();
68  
69  		if ((list != null) && (list.size() > 0))
70  		{
71  			Collections.sort(list); //we should sort this
72  		}
73  
74  		html.newline();
75  		html.tabs(2);
76  		html.select().name(model.getTableKey() + ParameterRegistry.FILTER + column.getProperty()).styleClass("filterInput").close();
77  		
78  		html.newline();
79  		html.tabs(2);
80  		html.option().value("").close();
81  		html.optionEnd();
82  		
83  		for (Iterator iter = list.iterator(); iter.hasNext();)
84  		{
85  			Object value = (Object) iter.next();
86  
87  			if (column.isDate())
88  			{
89  				try
90  				{
91  					SimpleDateFormat format = new SimpleDateFormat(column.getParse());
92  					Date date = format.parse(value.toString());
93  					value = DateFormatUtils.format(date, column.getFormat());
94  				}
95  				catch (ParseException e)
96  				{
97  					e.printStackTrace();
98  				}
99  			}
100 
101 			html.newline();
102 			html.tabs(2);
103 			html.option().value(""+value);
104 			
105 			if (value.equals(column.getValue()))
106 			{
107 				html.append(" selected");
108 			}
109 
110 			html.close();
111 			html.append(value);
112 			html.optionEnd();
113 		}
114 
115 		html.newline();
116 		html.tabs(2);
117 		html.selectEnd();
118 
119 		return html.toString();
120 	}
121 
122 	public String html()
123 	{
124 		HtmlBuilder html = new HtmlBuilder();
125 
126 		if (column.getFilter().equals("false"))
127 		{
128 			html.append("");
129 		}
130 		else if (column.getFilter().equals("droplist"))
131 		{
132 			html.append(dropList());
133 		}
134 		else //the default is the input box
135 		{
136 			html.append(getInput());
137 		}
138 
139 		return html.toString();
140 	}
141 	
142 	public String value()
143 	{
144 		return "";
145 	}
146 }