KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > webapp > PageLink


1 /* ************************************************************************** *
2  * Copyright (C) 2004 NightLabs GmbH, Marco Schulze *
3  * All rights reserved. *
4  * http://www.NightLabs.de *
5  * *
6  * This program and the accompanying materials are free software; you can re- *
7  * distribute it and/or modify it under the terms of the GNU General Public *
8  * License as published by the Free Software Foundation; either ver 2 of the *
9  * License, or any later version. *
10  * *
11  * This module is distributed in the hope that it will be useful, but WITHOUT *
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FIT- *
13  * NESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more *
14  * details. *
15  * *
16  * You should have received a copy of the GNU General Public License along *
17  * with this module; if not, write to the Free Software Foundation, Inc.: *
18  * 59 Temple Place, Suite 330 *
19  * Boston MA 02111-1307 *
20  * USA *
21  * *
22  * Or get it online: *
23  * http://www.opensource.org/licenses/gpl-license.php *
24  * *
25  * In case, you want to use this module or parts of it in a proprietary pro- *
26  * ject, you can purchase it under the NightLabs Commercial License. Please *
27  * contact NightLabs GmbH under info AT nightlabs DOT com for more infos or *
28  * visit http://www.NightLabs.com *
29  * ************************************************************************** */

30
31 /*
32  * Created on 18.06.2004
33  */

34 package com.nightlabs.webapp;
35
36 import java.util.ArrayList JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.Map JavaDoc;
40
41 /**
42  * @author marco
43  * @author Marc Klinger - marc at nightlabs dot de (API documentation fixes)
44  */

45 public class PageLink
46 {
47
48     public static List JavaDoc createPageLinks(Map JavaDoc urlParams, MultiPageSearchResult searchResult)
49     {
50         int currentPageIndex = searchResult.getPageIndex();
51         
52         List JavaDoc pageLinks = new ArrayList JavaDoc();
53         int pageCount = searchResult.getItemsFound() / searchResult.getItemsPerPage();
54         if (searchResult.getItemsFound() % searchResult.getItemsPerPage() != 0)
55             ++pageCount;
56         
57         boolean haveDots = true;
58         for (int i = 0; i < pageCount; ++i) {
59             if (i <= 2 || i >= pageCount - 3 || (i >= currentPageIndex - 2 && i <= currentPageIndex + 2)) {
60                 PageLink pl = new PageLink();
61                 pl.setCaption(Integer.toString(i));
62                 pl.setPageIndex(i);
63
64                 if (i != currentPageIndex) {
65                     pl.setUrlParams(new HashMap JavaDoc(urlParams));
66                     pl.setUrlParam("pageIndex", Integer.toString(i));
67                     pl.setUrlParam("itemsPerPage", new Integer JavaDoc(searchResult.getItemsPerPage()));
68                 } // if (i != currentPageIndex) {
69
pageLinks.add(pl);
70                 haveDots = true;
71             }
72             else {
73                 if (haveDots) {
74                     PageLink pl = new PageLink();
75                     pl.setCaption("...");
76                     pageLinks.add(pl);
77                     haveDots = false;
78                 }
79             }
80         }
81
82         return pageLinks;
83     }
84     
85     private int pageIndex;
86     private String JavaDoc caption;
87     private Map JavaDoc urlParams = null;
88     
89     /**
90      * @return Returns the caption.
91      */

92     public String JavaDoc getCaption() {
93         return caption;
94     }
95     /**
96      * @param caption The caption to set.
97      */

98     public void setCaption(String JavaDoc caption) {
99         this.caption = caption;
100     }
101     /**
102      * @return Returns the pageIndex.
103      */

104     public int getPageIndex() {
105         return pageIndex;
106     }
107     /**
108      * @param pageIndex The pageIndex to set.
109      */

110     public void setPageIndex(int pageIndex) {
111         this.pageIndex = pageIndex;
112     }
113     /**
114      * @return Returns the urlParams.
115      */

116     public Map JavaDoc getUrlParams() {
117         return urlParams;
118     }
119     /**
120      * @param url The urlParams to set.
121      */

122     public void setUrlParams(Map JavaDoc url) {
123         this.urlParams = url;
124     }
125     
126     public void setUrlParam(String JavaDoc name, Object JavaDoc value)
127     {
128         if (urlParams == null)
129             urlParams = new HashMap JavaDoc();
130         
131         urlParams.put(name, value.toString());
132     }
133
134 }
135
Popular Tags