1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.util.html;
35
36 import fr.paris.lutece.util.url.UrlItem;
37
38 import org.apache.commons.lang.StringUtils;
39
40 import java.io.Serializable;
41
42 import java.util.List;
43
44
45
46
47
48
49
50 public class ItemNavigator implements Serializable
51 {
52 private static final long serialVersionUID = -6240496590042563143L;
53 private List<String> _listItems;
54 private int _nCurrentItemId;
55 private String _strBaseUrl;
56 private String _strParameterName;
57
58
59
60
61
62
63
64
65 public ItemNavigator( List<String> listItems, int nCurrentItemId, String strBaseUrl, String strParameterName )
66 {
67 _listItems = listItems;
68 _nCurrentItemId = nCurrentItemId;
69 _strBaseUrl = strBaseUrl;
70 _strParameterName = strParameterName;
71 }
72
73
74
75
76
77 public void setCurrentItemId( String strItem )
78 {
79 int nCurrentItemId = _nCurrentItemId;
80
81 if ( ( strItem != null ) && ( _listItems != null ) && !_listItems.isEmpty( ) &&
82 !strItem.equals( _listItems.get( _nCurrentItemId ) ) )
83 {
84 int nPreviousItemId = _nCurrentItemId - 1;
85 int nNextItemId = _nCurrentItemId + 1;
86
87 if ( ( nPreviousItemId >= 0 ) && strItem.equals( _listItems.get( nPreviousItemId ) ) )
88 {
89
90 nCurrentItemId = nPreviousItemId;
91 }
92 else if ( ( nNextItemId < _listItems.size( ) ) && strItem.equals( _listItems.get( nNextItemId ) ) )
93 {
94
95 nCurrentItemId = nNextItemId;
96 }
97 else
98 {
99
100 for ( int nIndex = 0; nIndex < _listItems.size( ); nIndex++ )
101 {
102 if ( strItem.equals( _listItems.get( nIndex ) ) )
103 {
104 nCurrentItemId = nIndex;
105
106 break;
107 }
108 }
109 }
110 }
111
112 _nCurrentItemId = nCurrentItemId;
113 }
114
115
116
117
118
119 public String getPreviousPageLink( )
120 {
121 int nPreviousItemId = _nCurrentItemId - 1;
122
123 if ( ( _listItems != null ) && !_listItems.isEmpty( ) && ( nPreviousItemId >= 0 ) )
124 {
125 UrlItem url = new UrlItem( _strBaseUrl );
126 url.addParameter( _strParameterName, _listItems.get( nPreviousItemId ) );
127
128 return url.getUrl( );
129 }
130
131 return StringUtils.EMPTY;
132 }
133
134
135
136
137
138 public String getNextPageLink( )
139 {
140 int nNextItemId = _nCurrentItemId + 1;
141
142 if ( ( _listItems != null ) && !_listItems.isEmpty( ) && ( nNextItemId < _listItems.size( ) ) )
143 {
144 UrlItem url = new UrlItem( _strBaseUrl );
145 url.addParameter( _strParameterName, _listItems.get( nNextItemId ) );
146
147 return url.getUrl( );
148 }
149
150 return StringUtils.EMPTY;
151 }
152
153
154
155
156
157 public String getBaseUrl( )
158 {
159 return _strBaseUrl;
160 }
161
162
163
164
165
166 public void setBaseUrl( String strBaseUrl )
167 {
168 _strBaseUrl = strBaseUrl;
169 }
170
171
172
173
174
175 public int getListItemSize( )
176 {
177 if ( _listItems != null )
178 {
179 return _listItems.size( );
180 }
181
182 return 0;
183 }
184
185
186
187
188
189 public int getCurrentItemId( )
190 {
191 return _nCurrentItemId;
192 }
193 }