View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.jetspeed.portlet.webcontent;
19  
20  import java.io.Serializable;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  
25  /***
26   * A history of content navigations in the WebContentPortlet
27   *
28   * @author <a href="mailto:dyoung@phase2systems.com">David L Young</a>
29   * @version $Id: $ 
30   */
31  
32  public class WebContentHistoryList extends Object
33      implements Serializable
34  {
35      int maxLength;
36      List history;
37      int currentIndex;
38      
39      // Constructors
40  
41      public WebContentHistoryList()
42      {
43          this( -1 );
44      }
45      public WebContentHistoryList( int maxLength )
46      {
47          super();
48          
49          this.maxLength = maxLength;
50          this.history = new ArrayList();
51          this.currentIndex = -1;
52      }
53      
54      // Methods
55      
56      public boolean isEmpty()
57      {
58          return this.history.isEmpty();
59      }
60      public boolean hasCurrentPage()
61      {
62          return this.currentIndex >= 0;
63      }
64      public boolean hasPreviousPage()
65      {
66          return !isEmpty() && this.currentIndex-1 >= 0;
67      }
68      public boolean hasNextPage()
69      {
70          return !isEmpty() && this.currentIndex+1 < this.history.size();
71      }
72      
73      public WebContentHistoryPage getCurrentPage()
74      {
75          if (!hasCurrentPage())
76              return null ;
77          return (WebContentHistoryPage)this.history.get(this.currentIndex);
78      }
79      public WebContentHistoryPage getPreviousPage()
80      {
81          if (!hasPreviousPage())
82              return null;
83          this.currentIndex = this.currentIndex-1;
84          return getCurrentPage();
85      }
86      public WebContentHistoryPage getNextPage()
87      {
88          if (!hasNextPage())
89              return null;
90          this.currentIndex = this.currentIndex+1;
91          return getCurrentPage();
92      }
93      
94      public void visitPage(WebContentHistoryPage page)
95      {
96          if (page==null)
97              throw new IllegalArgumentException("WebContentHistoryList.addPage() - non-null page required.");
98          
99          int i = this.history.indexOf(page);
100         if (i >= 0 && i == this.currentIndex) 
101         {
102             // just visiting the current page
103             return;
104         }
105         
106         // otherwise - new page...
107         while (hasNextPage())
108         {
109             // ...visiting a page discards any pages we have visited by going "back"
110             this.history.remove(this.currentIndex+1);
111         }
112         if (i >= 0 && i < history.size())
113         {
114             // ...actually, new visit to an old page, only keep one reference to it
115             this.history.remove(i);
116         }
117         
118         // add in the new page, at the end
119         this.history.add(page);
120         this.currentIndex = this.history.size()-1;
121         
122         // System.out.println("WebContentHistoryList.visitPage() - current index is: "+this.currentIndex+"\nhistory list..."+ArrayUtils.toString(this.history));
123     }
124 }