KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > web > NavigationStack


1 /*
2  * $Id: NavigationStack.java,v 1.4 2005/01/07 01:46:35 michelson Exp $
3  *
4  * Copyright (c) 2004 j2biz Group, http://www.j2biz.com Koeln / Duesseldorf ,
5  * Germany
6  *
7  * @author Max Kalina
8  *
9  *
10  * This program is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU General Public License as published by the Free Software
12  * Foundation; either version 2 of the License, or (at your option) any later
13  * version.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License along with
21  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22  * Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  */

25
26 package com.j2biz.blogunity.web;
27
28 import java.util.Enumeration JavaDoc;
29 import java.util.Locale JavaDoc;
30 import java.util.Stack JavaDoc;
31
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36
37 import com.j2biz.blogunity.BlogunityManager;
38 import com.j2biz.blogunity.IConstants;
39 import com.j2biz.blogunity.i18n.I18NMessageManager;
40
41 /**
42  * This class describes the stack of pages, that was visited by the user.
43  * Elements of this stack will be shown as "navigation-steps" at the website.
44  *
45  */

46 public class NavigationStack {
47     /**
48      * Logger for this class
49      */

50     private static final Log log = LogFactory.getLog(NavigationStack.class);
51
52     private Stack JavaDoc stack;
53
54     /**
55      *
56      */

57     public NavigationStack() {
58
59         if (log.isDebugEnabled()) {
60             log.debug("new navigation stack created.");
61         }
62         stack = new Stack JavaDoc();
63     }
64
65     public IActionResult push(IActionResult item) {
66
67         if (log.isDebugEnabled()) {
68             log.debug("Pushing to stack: " + item.getPath());
69         }
70
71         // check, if the last item is not equals to the current item
72
int len = size();
73         if (len == 0) stack.push(item);
74         else {
75             IActionResult last = peek();
76             int indx1 = item.getPath().indexOf("?");
77             if (indx1 < 0) indx1 = item.getPath().length();
78             String JavaDoc p1 = item.getPath().substring(0, indx1);
79
80             int indx2 = last.getPath().indexOf("?");
81             if (indx2 < 0) indx2 = last.getPath().length();
82             String JavaDoc p2 = last.getPath().substring(0, indx2);
83
84             if (!p1.equals(p2)) {
85                 if (len >= 2) {
86                     IActionResult r = (IActionResult) stack.elementAt(len - 2);
87                     if (r.getPath().equals(item.getPath())) {
88                         pop();
89                     } else {
90                         stack.push(item);
91                     }
92                 } else
93                     stack.push(item);
94             }
95
96         }
97
98         return item;
99     }
100
101     public synchronized IActionResult pop() {
102         return (IActionResult) stack.pop();
103     }
104
105     public synchronized void popAllExceptFirst() {
106         if (size() > 1) {
107             // remove all stack-entries until size() = 1
108
while (size() != 1)
109                 pop();
110         }
111     }
112
113     public synchronized IActionResult peek() {
114         return (IActionResult) stack.peek();
115     }
116
117     public synchronized IActionResult peekNextToLast() {
118         int len = size();
119         if (len < 2) return null;
120         return (IActionResult) stack.elementAt(len - 2);
121     }
122
123     public boolean empty() {
124         return stack.size() == 0;
125     }
126
127     public int size() {
128         return stack.size();
129     }
130
131     public void clear() {
132         stack.clear();
133     }
134
135     public Enumeration JavaDoc elements() {
136         return stack.elements();
137     }
138
139     /*
140      * (non-Javadoc)
141      *
142      * @see java.lang.Object#toString()
143      */

144     public String JavaDoc toString() {
145         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
146
147         if (empty()) {
148             out.append("Navigation stack is empty");
149         } else {
150             Enumeration JavaDoc e = elements();
151             while (e.hasMoreElements()) {
152                 IActionResult r = (IActionResult) e.nextElement();
153                 out.append("[");
154                 out.append(r.getKey());
155                 out.append(":");
156                 out.append(r.getPath());
157                 out.append("]&nbsp;-&nbsp;");
158             }
159         }
160         return out.toString();
161     }
162
163     public String JavaDoc toHTML(HttpServletRequest JavaDoc request) {
164
165         Locale JavaDoc loc = Locale.ENGLISH;
166         loc = (Locale JavaDoc) request.getAttribute(IConstants.Request.LOCALE);
167         if (loc == null) loc = BlogunityManager.getSystemConfiguration().getSystemLocale();
168
169         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
170
171         if (!empty()) {
172             String JavaDoc ctx = request.getContextPath();
173             Enumeration JavaDoc e = elements();
174             while (e.hasMoreElements()) {
175
176                 IActionResult r = (IActionResult) e.nextElement();
177
178                 out.append("<a class=\"naviLink\" HREF=\"");
179                 out.append(ctx);
180                 out.append(r.getPath());
181                 out.append("\">");
182                 out.append(I18NMessageManager.getInstance().getNavigationText(r.getKey(), loc));
183                 out.append("</a>\n");
184
185             }
186         }
187         return out.toString();
188     }
189
190 }
Popular Tags