KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > table > forms > AbstractPagerForm


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
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 General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.table.forms;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24
25 import javax.servlet.http.HttpSession JavaDoc;
26
27 import org.apache.struts.action.ActionMapping;
28
29 import com.sslexplorer.core.forms.CoreForm;
30 import com.sslexplorer.navigation.AbstractFavoriteItem;
31 import com.sslexplorer.properties.PersistentSettings;
32 import com.sslexplorer.security.LogonControllerFactory;
33 import com.sslexplorer.security.SessionInfo;
34 import com.sslexplorer.table.Pager;
35 import com.sslexplorer.table.TableItemModel;
36
37 public class AbstractPagerForm extends CoreForm {
38
39     ArrayList JavaDoc accounts = new ArrayList JavaDoc();
40     boolean isEditable;
41     TableItemModel model;
42     Pager pager;
43     int startRow;
44     String JavaDoc sortName;
45     boolean sortReverse;
46     int pageSize;
47     HttpSession JavaDoc session;
48     String JavaDoc filterText;
49     String JavaDoc selectedItem;
50     boolean filterMatchedNothing;
51     List JavaDoc userFavorites;
52     List JavaDoc globalFavorites;
53     String JavaDoc defaultSortColumnId;
54
55     public AbstractPagerForm(TableItemModel model) {
56         this.model = model;
57         pager = new Pager(model);
58     }
59     
60     public List JavaDoc getUserFavorites() {
61         return userFavorites;
62     }
63     
64     public void setUserFavorites(List JavaDoc userFavorites) {
65         this.userFavorites = userFavorites;
66     }
67     
68     public List JavaDoc getGlobalFavorites() {
69         return globalFavorites;
70     }
71     
72     public void setGlobalFavorites(List JavaDoc policyFavorites) {
73         this.globalFavorites = policyFavorites;
74     }
75     
76     public String JavaDoc getFavoriteType(int resourceId) {
77         if(globalFavorites != null && globalFavorites.contains(new Integer JavaDoc(resourceId))) {
78             return AbstractFavoriteItem.GLOBAL_FAVORITE;
79         }
80         else if(userFavorites != null && userFavorites.contains(new Integer JavaDoc(resourceId))) {
81             return AbstractFavoriteItem.USER_FAVORITE;
82         }
83         else {
84             return AbstractFavoriteItem.NO_FAVORITE;
85         }
86     }
87     
88     public TableItemModel getModel() {
89         return model;
90     }
91     
92     public String JavaDoc getFilterText() {
93         return filterText;
94     }
95     
96     public void setFilterText(String JavaDoc filterText) {
97         this.filterText = filterText;
98     }
99
100     public void initialize(HttpSession JavaDoc session, String JavaDoc defaultSortColumnId) {
101         this.session = session;
102         this.defaultSortColumnId = defaultSortColumnId;
103         model.clear();
104         
105         SessionInfo info = LogonControllerFactory.getInstance().getSessionInfo(session);
106         
107         if(pageSize != -1) {
108             pager.setPageSize(pageSize);
109             PersistentSettings.setIntValue(info, "pager." + model.getId() + ".pageSize", pageSize, 10);
110         }
111         else {
112             pager.setPageSize(PersistentSettings.getIntValue(info, "pager." + model.getId() + ".pageSize", 10));
113         }
114         
115         if(startRow != -1) {
116             pager.setStartRow(startRow);
117             session.setAttribute("pager." + model.getId() + ".startRow", new Integer JavaDoc(startRow));
118         }
119         else {
120             Integer JavaDoc startRowAttr = (Integer JavaDoc)session.getAttribute("pager." + model.getId() + ".startRow");
121             if(startRowAttr != null) {
122                 pager.setStartRow(startRowAttr.intValue());
123             }
124             else {
125                 pager.setStartRow(0);
126             }
127         }
128         checkSort();
129     }
130     
131     public String JavaDoc getSortName() {
132         return sortName;
133     }
134     
135     public void setSortName(String JavaDoc sortName) {
136         this.sortName = sortName;
137     }
138     
139     public boolean getSortReverse() {
140         return sortReverse;
141     }
142     
143     public void setSortReverse(boolean sortReverse) {
144         this.sortReverse = sortReverse;
145     }
146     
147     public int getStartRow() {
148         return startRow;
149     }
150     
151     public void setStartRow(int startRow) {
152         this.startRow = startRow;
153     }
154     
155     public int getPageSize() {
156         return pageSize;
157     }
158     
159     public void setPageSize(int pageSize) {
160         this.pageSize = pageSize;
161     }
162     
163     public Pager getPager() {
164         return pager;
165     }
166     
167     public String JavaDoc getSelectedItem() {
168         return selectedItem;
169     }
170     
171     public void setSelectedItem(String JavaDoc selectedItem) {
172         this.selectedItem = selectedItem;
173     }
174
175     public void reset(ActionMapping mapping, javax.servlet.http.HttpServletRequest JavaDoc request) {
176         startRow = -1;
177         sortName = null;
178         sortReverse = false;
179         pageSize = -1;
180     }
181     
182     protected void checkSort() {
183         if(getSortName() != null) {
184             String JavaDoc sortId = getSortName().substring(getSortName().lastIndexOf(".") + 1);
185             getPager().setSortName(sortId);
186             getPager().setSortReverse(getSortReverse());
187             session.setAttribute("pager." + model.getId() + ".sortName", sortId);
188             session.setAttribute("pager." + model.getId() + ".sortReverse", Boolean.valueOf(getSortReverse()));
189         }
190         else {
191             /* If no sort name has been supplied then use whatever the last sort for this table
192              * id.
193              */

194             String JavaDoc lastSortName = (String JavaDoc)session.getAttribute("pager." + model.getId() + ".sortName");
195             if(lastSortName != null) {
196                 getPager().setSortName(lastSortName);
197                 getPager().setSortReverse(((Boolean JavaDoc)session.getAttribute("pager." + model.getId() + ".sortReverse")).booleanValue());
198             }
199             else {
200                 String JavaDoc colId = defaultSortColumnId == null ? getModel().getColumnName(0) : defaultSortColumnId;
201                 getPager().setSortName(colId);
202                 getPager().setSortReverse(false);
203             }
204             setSortName(getPager().getSortName());
205             setSortReverse(getPager().getSortReverse());
206         }
207     }
208 }
Popular Tags