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.PageHandler;
24  import org.extremecomponents.table.model.ParameterRegistry;
25  import org.extremecomponents.util.HtmlBuilder;
26  
27  
28  
29  /***
30   * A pagination cell.
31   *
32   * @author Jeff Johnston
33   */
34  public class PageCell extends BaseCell
35  {
36  	private static Log logger = LogFactory.getLog(PageCell.class);
37  	private String name;
38  	private int page = -1; //by default it cannot link
39  	private String additionalParameters;
40  	private boolean selected = false; //defaulted...set explicitly to make clear the intent
41  
42  	/***
43  	 * Need to get the additional parameters that will be passed when paging through the pages.
44  	 */
45  	public void init(TableModel model, Column column, Integer rowcount)
46  	{
47  		super.init(model, column, rowcount);
48  		additionalParameters = model.getRegistry().getURLParameterString(true, true, false);
49  	}
50  
51  	public void initCustom(String name, int page, boolean selected)
52  	{
53  		this.name = name;
54  		this.page = page;
55  		this.selected = selected;
56  	}
57  
58  	public String html()
59  	{
60  		HtmlBuilder html = new HtmlBuilder();
61  
62  		html.td(4);
63  		if (name.equals(PageHandler.FIRST))
64  		{
65  			html.styleClass("paginationFirst");
66  		}
67  		else if (name.equals(PageHandler.LAST))
68  		{
69  			html.styleClass("paginationLast");
70  		}
71  		html.close();
72  		
73  		if (name.equals(PageHandler.FIRST))
74  		{
75  			String firstPage = model.getTable().getImage(PageHandler.FIRST_PAGE);
76  			if (StringUtils.isNotEmpty(firstPage))
77  			{
78  				name = HtmlBuilder.getImage(firstPage, "First Page");
79  			}
80  			else
81  			{
82  				name = "|<< ";
83  			}
84  		}
85  		if (name.equals(PageHandler.PREV))
86  		{
87  			String prevPage = model.getTable().getImage(PageHandler.PREV_PAGE);
88  			if (StringUtils.isNotEmpty(prevPage))
89  			{
90  				name = HtmlBuilder.getImage(prevPage, "Previous Page");
91  			}
92  			else
93  			{
94  				name = " << ";
95  			}
96  		}
97  
98  		if (name.equals(PageHandler.NEXT))
99  		{
100 			String nextPage = model.getTable().getImage(PageHandler.NEXT_PAGE);
101 			if (StringUtils.isNotEmpty(nextPage))
102 			{
103 				name = HtmlBuilder.getImage(nextPage, "Next Page");
104 			}
105 			else
106 			{
107 				name = " >> ";
108 			}
109 		}
110 		if (name.equals(PageHandler.LAST))
111 		{
112 			String lastPage = model.getTable().getImage(PageHandler.LAST_PAGE);
113 			if (StringUtils.isNotEmpty(lastPage))
114 			{
115 				name = HtmlBuilder.getImage(lastPage, "Last Page");
116 			}
117 			else
118 			{
119 				name = " >>|";
120 			}
121 		}
122 
123 		//now go through the paging
124 		if (page == -1)
125 		{
126 			html.append(name);
127 		}
128 		else
129 		{
130 			html.a();
131 			html.quote();
132 
133 			String action = model.getTable().getAction();
134 
135 			if (StringUtils.isNotEmpty(action))
136 			{
137 				html.append(action);
138 			}
139 
140 			html.question().append(model.getTableKey()).append(ParameterRegistry.PAGE).equals().append(""+page);
141 			html.append(additionalParameters);
142 			html.quote().close();
143 			
144 			if (selected)
145 			{
146 				html.bold().append("[").append(name).append("]").boldEnd();
147 			}
148 			else
149 			{
150 				html.append(name); //<b>1</b>
151 			}
152 
153 			html.aEnd();
154 		}
155 
156 		html.tdEnd();
157 
158 		return html.toString();
159 	}
160 	
161 	public String value()
162 	{
163 		return "";
164 	}	
165 }