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.model;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.extremecomponents.table.bean.TableSection;
24  import org.extremecomponents.table.cell.Cell;
25  import org.extremecomponents.table.cell.PageCell;
26  
27  
28  
29  /***
30   * Keep track of the page information
31   *
32   * @author Jeff Johnston
33   */
34  public class PageHandler
35  {
36  	private static Log logger = LogFactory.getLog(PageHandler.class);
37  	public final static String FIRST = "First";
38  	public final static String LAST = "Last";
39  	public final static String PREV = "Prev";
40  	public final static String NEXT = "Next";
41  	public final static String LAST_PAGE = "lastPage";
42  	public final static String FIRST_PAGE = "firstPage";
43  	public final static String PREV_PAGE = "prevPage";
44  	public final static String NEXT_PAGE = "nextPage";
45  	
46  	private TableModel model;
47  	
48  	public PageHandler(TableModel model)
49  	{
50  		this.model = model;
51  	}
52  
53  	/***
54  	 * Build up the pagination cells.
55  	 */
56  	public List getPagination(TableModel model)
57  	{
58  		List pagination = new ArrayList();
59  
60  		if (!model.getTable().doPagination())
61  		{
62  			return pagination;
63  		}
64  
65  		TableSection tableSection = model.getTableSection();
66  
67  		int maxRows = Integer.parseInt(model.getTable().getMaxRows());
68  
69  		if (maxRows == 0)
70  		{
71  			maxRows = tableSection.getTotalRows();
72  		}
73  
74  		int totalRows = tableSection.getTotalRows();
75  
76  		int sections = 1;
77  
78  		if (maxRows != 0)
79  		{
80  			sections = totalRows / maxRows; //split the sections up
81  		}
82  
83  		if ((maxRows != 0) && ((totalRows % maxRows) > 0)) //have a few straglers
84  		{
85  			sections++;
86  		}
87  
88  		if (sections > 1)
89  		{
90  			buildPagination(model, pagination, tableSection, sections);
91  		}
92  
93  		return pagination;
94  	}
95  
96  	private void buildPagination(TableModel model, List pagination, TableSection tableSections, int sections)
97  	{
98  		int page = tableSections.getPage();
99  
100 		//FIRST
101 		int startFirst = 1;
102 
103 		if (page == 1)
104 		{
105 			startFirst = -1;
106 		}
107 
108 		pagination.add(buildPaginationCell(model, FIRST, startFirst, false));
109 
110 		//PREV
111 		int startPrev = page - 1;
112 
113 		if (page == 1)
114 		{
115 			startPrev = -1;
116 		}
117 
118 		pagination.add(buildPaginationCell(model, PREV, startPrev, false));
119 
120 		//start of body
121 		int begin = 1;
122 		int end = sections;
123 
124 		if (sections > 9)
125 		{
126 			end = 9;
127 		}
128 
129 		if ((sections > 9) && (page > 5)) //put in the middle if have more than 9 sections and not in the middle
130 		{
131 			begin = page - 4;
132 			end = page + 4;
133 
134 			if (end > sections)
135 			{
136 				begin = sections - 8;
137 				end = sections;
138 			}
139 		}
140 
141 		//now go through the body
142 		for (int i = begin; i <= end; i++)
143 		{
144 			if (i == page)
145 			{
146 				pagination.add(buildPaginationCell(model, "" + i, i, true));
147 			}
148 			else
149 			{
150 				pagination.add(buildPaginationCell(model, "" + i, i, false));
151 			}
152 		}
153 
154 		//end of body
155 		//NEXT
156 		int startNext = page + 1;
157 
158 		if (page == sections)
159 		{
160 			startNext = -1;
161 		}
162 
163 		pagination.add(buildPaginationCell(model, NEXT, startNext, false));
164 
165 		//Last
166 		int startLast = sections;
167 
168 		if (page == sections)
169 		{
170 			startLast = -1;
171 		}
172 
173 		pagination.add(buildPaginationCell(model, LAST, startLast, false));
174 	}
175 
176 	public String buildPaginationCell(TableModel model, String name, int page, boolean selected)
177 	{
178 		Cell cell = model.getCachedCell(PageCell.class);
179 		cell.init(model, null, null);
180 		((PageCell)cell).initCustom(name, page, selected); //not the normal case...
181 		String html = cell.html();
182 		cell.destroy();
183 		return html;
184 	}
185 }