001: package com.jat.presentation.controller.navigation;
002:
003: import java.util.Enumeration;
004: import java.util.Vector;
005: import javax.servlet.http.HttpServletRequest;
006: import javax.servlet.http.HttpSession;
007:
008: import com.jat.core.config.Config;
009: import com.jat.core.log.LogManager;
010: import com.jat.presentation.controller.Action;
011:
012: /**
013: * <p>Title: JAT</p>
014: * <p>Description: </p>
015: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
016: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
017: * @author stf
018: * @version 1.0
019: * @since 1.2
020: */
021:
022: public class Navigation {
023:
024: public static String NAVIGATION = "JAT_NAVIGATION";
025: public static String BACK = "JAT_BACK";
026: public static String FORWARD = "JAT_FORWARD";
027:
028: public static Navigation getDefault(HttpSession session) {
029: Navigation nav = (Navigation) session.getAttribute(NAVIGATION);
030: if (nav == null) {
031: nav = new Navigation();
032: session.setAttribute(NAVIGATION, nav);
033: }
034: return nav;
035: }
036:
037: public void addAction(HttpServletRequest request, Action action) {
038: try {
039: boolean enabled = Config.getCurrent().getValue(
040: "navigation", "enabled").equalsIgnoreCase("true");
041: if (!enabled)
042: return;
043: } catch (Exception ex) {
044: LogManager.sendDebug(this .getClass().getName()
045: + "::addAction: exception: " + ex);
046: }
047: if (this .isBackAction(request)) {
048: this .addForwardList(current);
049: this .current = this .removeBackList();
050: } else if (this .isForwardAction(request)) {
051: this .addBackList(current);
052: this .current = this .removeForwardList();
053: } else {
054: this .addBackList(current);
055: if (action.isNavigable())
056: this .current = new NavigationAction(request);
057: this .forwardList = new Vector();
058: }
059: }
060:
061: public boolean hasBack() {
062: return this .backList.size() > 0;
063: }
064:
065: public NavigationAction getBack() {
066: NavigationAction back = null;
067: if (this .hasBack())
068: back = (NavigationAction) this .backList.lastElement();
069: return back;
070: }
071:
072: public boolean hasForward() {
073: return this .forwardList.size() > 0;
074: }
075:
076: public NavigationAction getForward() {
077: NavigationAction forward = null;
078: if (hasForward())
079: forward = (NavigationAction) this .forwardList.lastElement();
080: return forward;
081: }
082:
083: public String toString() {
084: String ret = this .getClass().getName() + ":{";
085: ret += "CURRENT=["
086: + (current != null ? this .current.getURI() : "null")
087: + "]";
088: ret += "}, BACK_LIST(" + this .backList.size() + ")={";
089: for (Enumeration e = this .backList.elements(); e
090: .hasMoreElements();) {
091: ret += "[" + ((NavigationAction) e.nextElement()).getURI()
092: + "]";
093: }
094: ret += " }, FORWARD_LIST(" + this .forwardList.size() + ")={";
095: for (Enumeration e = this .forwardList.elements(); e
096: .hasMoreElements();) {
097: ret += "[" + ((NavigationAction) e.nextElement()).getURI()
098: + "]";
099: }
100: ret += " }";
101: return ret;
102: }
103:
104: protected boolean isBackAction(HttpServletRequest request) {
105: return request.getParameter(BACK) != null;
106: }
107:
108: protected boolean isForwardAction(HttpServletRequest request) {
109: return request.getParameter(FORWARD) != null;
110: }
111:
112: protected void addBackList(NavigationAction action) {
113: if (action == null)
114: return;
115: if (this .getBack() != null && this .getBack().equals(action))
116: this .removeBackList();
117: if (this .backList.size() > 10)
118: this .backList.remove(0);
119: this .backList.addElement(action);
120: }
121:
122: protected void addForwardList(NavigationAction action) {
123: if (action == null)
124: return;
125: if (this .getForward() != null
126: && this .getForward().equals(action))
127: this .removeForwardList();
128: this .forwardList.addElement(action);
129: }
130:
131: protected NavigationAction removeBackList() {
132: if (backList.size() > 0)
133: return (NavigationAction) this .backList
134: .remove(this .backList.size() - 1);
135: return null;
136: }
137:
138: protected NavigationAction removeForwardList() {
139: if (forwardList.size() > 0)
140: return (NavigationAction) this .forwardList
141: .remove(this .forwardList.size() - 1);
142: return null;
143: }
144:
145: private Navigation() {
146: }
147:
148: private Vector backList = new Vector();
149: private Vector forwardList = new Vector();
150: private NavigationAction current = null;
151: }
|