BrightSide Workbench Full Report + Source Code
DaoNavigator.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.jpa.iterator;
20 
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import org.turro.elephant.impl.navigation.DefaultNavigatorItem;
24 import org.turro.html.HTMLHelper;
25 
30 public class DaoNavigator {
31 
32  private HTMLHelper html;
33  private DaoHtmlIterator iterator;
34  private int pageSpan = 4;
35  private ArrayList<DefaultNavigatorItem> buttons;
36 
37  public DaoNavigator(DaoHtmlIterator iterator) {
38  this.iterator = iterator;
39  this.html = iterator.html;
40  }
41 
42  public void setPageSpan(int pageSpan) {
43  this.pageSpan = pageSpan;
44  }
45 
46  public void render() {
47  int pages = iterator.pages(),
48  current = Math.max(iterator.current(), 0);
49  if(pages < current || pages == 1) return;
50  html.startTag("div", "class='dao-navigator'");
51  html.startTag("ul");
52 
53  if(current - pageSpan > 1) {
54  html.startTag("li");
55  html.startAnchor("?page=" + 1, null);
56  html.write("1");
57  html.endTag("li");
58  }
59 
60  if(current > 1) {
61  html.startTag("li");
62  html.startAnchor("?page=" + (current), null);
63  html.write("&lt&lt");
64  html.endTag("li");
65  }
66 
67  for(int i = current - pageSpan; i < current + pageSpan; i++) {
68  if(i >= 0 && i < pages) {
69  html.startTag("li", current == i ? "class='active'" : null);
70  html.startAnchor("?page=" + (i + 1), null);
71  html.write((i + 1) + "");
72  html.endTag("li");
73  }
74  }
75 
76  if(current != pages && pages > pageSpan) {
77  html.startTag("li");
78  html.startAnchor("?page=" + (current + 2), null);
79  html.write("&gt&gt");
80  html.endTag("li");
81  }
82 
83  if(current + pageSpan < pages) {
84  html.startTag("li");
85  html.startAnchor("?page=" + pages, null);
86  html.write(pages + "");
87  html.endTag("li");
88  }
89 
90  html.endTag("div");
91  }
92 
93  public Collection<DefaultNavigatorItem> pages() {
94  if(buttons == null) {
95  buttons = new ArrayList<>();
96  int pages = iterator.pages(),
97  current = Math.max(iterator.current(), 0);
98  if(pages < current || pages == 1) return buttons;
99 
100  boolean navbut = pages > pageSpan * 2;
101  int start = 0, limit = pageSpan * 2;
102 
103  if(navbut) {
104  buttons.add(new DefaultNavigatorItem("1", "?page=" + 1, null, current == 0));
105  buttons.add(new DefaultNavigatorItem("&lt;&lt;", "?page=" + (current), null));
106  limit -= 4;
107  start = Integer.max(1, current - (limit / 2));
108  start = Integer.min(start, pages - (limit + 1));
109  }
110 
111  for(int i = start; i < (start + limit) && i < pages; i++) {
112  buttons.add(new DefaultNavigatorItem((i + 1) + "", "?page=" + (i + 1), null, current == i));
113  }
114 
115  if(navbut) {
116  buttons.add(new DefaultNavigatorItem("&gt;&gt;", "?page=" + (current + 2), null));
117  buttons.add(new DefaultNavigatorItem(pages + "", "?page=" + (pages), null, current == pages - 1));
118  }
119 
120  }
121  return buttons;
122  }
123 
124 }
HTMLGenerator startTag(String tag)
HTMLGenerator startAnchor(String url, String hint)
HTMLGenerator write(String value)
Definition: HTMLHelper.java:69
Collection< DefaultNavigatorItem > pages()
DaoNavigator(DaoHtmlIterator iterator)