001: package com.jat.presentation.menu;
002:
003: import java.util.Vector;
004: import javax.servlet.http.HttpServletRequest;
005:
006: import javax.swing.tree.DefaultMutableTreeNode;
007: import com.jat.business.JatUser;
008: import java.util.Enumeration;
009: import com.jat.business.*;
010: import com.jat.core.log.LogManager;
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 maurizio cesari
018: * @version 1.1
019: * @since 1.2
020: */
021:
022: public class TreeNode extends DefaultMutableTreeNode {
023: private String url = "";
024: private String icon = "";
025: private String label = "";
026: private String key = "";
027: private String target = "";
028: private String description = "";
029: private Vector privileges = null;
030: private boolean completed = false;
031: private String section;
032: private HttpServletRequest request;
033:
034: public static Vector getAllNodes(String section, String key,
035: HttpServletRequest request) throws Exception {
036: TreeNode tree = new TreeNode(section, key, request);
037: Vector ret = new Vector();
038: ret = ((TreeNode) tree.getRoot()).getChilds(ret);
039: return ret;
040: }
041:
042: private Vector getChilds(Vector childs) {
043: for (Enumeration e = this .children(); e.hasMoreElements();) {
044: TreeNode node = (TreeNode) e.nextElement();
045: if (node.isWritable()) {
046: childs.add(node);
047: childs = node.getChilds(childs);
048: }
049: }
050: return childs;
051: }
052:
053: protected TreeNode(String section, String key,
054: HttpServletRequest request) throws Exception {
055: super ();
056: this .section = section;
057: if (this .section == null)
058: throw new Exception("Section not found");
059: this .request = request;
060: this .key = key;
061: this .init();
062: }
063:
064: private TreeNode(String section, String key, String url,
065: String icon, String label, String target,
066: Vector privileges, HttpServletRequest request)
067: throws Exception {
068: super ();
069: this .section = section;
070: this .key = key;
071: this .url = url;
072: this .icon = icon;
073: this .label = label;
074: this .target = target;
075: this .privileges = privileges;
076: this .request = request;
077: loadChilds();
078: this .completed = true;
079: }
080:
081: private void init() throws Exception {
082: Vector menuVector = com.jat.core.config.Config.getCurrent()
083: .getSubKeys(section, key);
084: for (Enumeration e = menuVector.elements(); e.hasMoreElements();) {
085: String childKey = (String) e.nextElement();
086: this .add(TreeNode.getTreeNode(section, childKey,
087: this .request));
088: }
089: }
090:
091: private static TreeNode getTreeNode(String section, String key,
092: HttpServletRequest request) throws Exception {
093: String label = com.jat.core.config.Config.getCurrent()
094: .getValue(section, key + ".label");
095: String url = com.jat.core.config.Config.getCurrent().getValue(
096: section, key + ".url");
097: if (url.indexOf("?") > 0)
098: url += "&menu_item=" + label;
099: else
100: url += "?menu_item=" + label;
101: String icon = com.jat.core.config.Config.getCurrent().getValue(
102: section, key + ".icon");
103: String target = com.jat.core.config.Config.getCurrent()
104: .getValue(section, key + ".target");
105: Vector privileges = new Vector();
106: try {
107: privileges = com.jat.core.config.Config.getCurrent()
108: .getSubValues(section, key + ".privilege");
109: } catch (Exception ignored) {
110: }
111: return new TreeNode(section, key, url, icon, label, target,
112: privileges, request);
113: }
114:
115: public String getKey() {
116: return key;
117: }
118:
119: public String getTarget() {
120: return target;
121: }
122:
123: public HttpServletRequest getRequest() {
124: return request;
125: }
126:
127: public String getUrl() {
128: return url;
129: }
130:
131: public String getIcon() {
132: return icon;
133: }
134:
135: /** Per la selezione dell'icona associata all'item quando selezionato.
136: * */
137:
138: public String getSelectedIcon() {
139: String app = getIcon();
140: return app.substring(0, app.indexOf(".")) + "_sel"
141: + app.substring(app.indexOf("."), app.length());
142: }
143:
144: public String getLabel() {
145: return label;
146: }
147:
148: public String getSection() {
149: return section;
150: }
151:
152: public boolean isWritable() {
153: if (this .isRoot())
154: return true;
155: if (this .getLevel() > 1) {
156: if (!isSelected())
157: return false;
158: }
159: return this .hasPrivilege();
160: }
161:
162: private boolean isSelected() {
163: String selectedItem = (String) this .request.getSession()
164: .getAttribute("MENU_ITEM_SELECTED");
165: if (selectedItem == null)
166: return false;
167: return this .getParent(1).hasChildSelected(selectedItem);
168: }
169:
170: private TreeNode getParent(int level) {
171: if (this .getLevel() == level)
172: return this ;
173: return ((com.jat.presentation.menu.TreeNode) this .getParent())
174: .getParent(level);
175: }
176:
177: private boolean hasChildSelected(String selectedItem) {
178: if (this .getLabel().equals(selectedItem))
179: return true;
180: if (this .isLeaf())
181: return false;
182: for (Enumeration e = this .children(); e.hasMoreElements();) {
183: com.jat.presentation.menu.TreeNode child = (com.jat.presentation.menu.TreeNode) e
184: .nextElement();
185: if (child.hasChildSelected(selectedItem))
186: return true;
187: }
188: return false;
189: }
190:
191: public boolean hasPrivilege() {
192: JatUser user = (JatUser) this .request.getSession()
193: .getAttribute(JatUser.JAT_USER);
194: if (user == null)
195: return this .privileges == null
196: || this .privileges.size() < 1;
197: else if (this .privileges != null && privileges.size() > 0) {
198: for (Enumeration e = this .privileges.elements(); e
199: .hasMoreElements();) {
200: try {
201: if (user.hasPrivilege((String) e.nextElement(),
202: null))
203: return true;
204: } catch (BusinessException ex) {
205: LogManager.sendError(this .getClass().getName()
206: + "::hasPrivilege: exception: " + ex);
207: }
208: }
209: return false;
210: }
211: return true;
212: }
213:
214: public String getMenuClass(String position) {
215: return "MenuLiv" + super .getLevel() + position; // esempio: "Left"
216: }
217:
218: public int getColSpanLabel() {
219: return getLevels() + 1 - getLevel();
220: }
221:
222: public int getColSpanSeparator() {
223: return getLevels() + 4;
224: }
225:
226: private void loadChilds() throws Exception {
227: Vector childKeys = com.jat.core.config.Config.getCurrent()
228: .getSubKeys(section, key + ".child");
229: for (Enumeration e = childKeys.elements(); e.hasMoreElements();) {
230: TreeNode node = TreeNode.getTreeNode(section, (String) e
231: .nextElement(), this .request);
232: add(node);
233: }
234: }
235:
236: public int getLevels() {
237: return ((TreeNode) super .getRoot()).getDepth();
238: }
239:
240: public String toString() {
241: description = "Nodo:[url=" + url + "],[icon=" + icon
242: + "],[label=" + label + "],[key=" + key + "],[level="
243: + super .getLevel() + "][depth=" + this .getDepth() + "]";
244: return description;
245: }
246:
247: public String toStringSimple() {
248: description = "<p>[key=" + key + "],[level=" + super .getLevel()
249: + "][depth=" + this .getDepth() + "]</p>";
250: return description;
251: }
252: }
|