001: package com.jat.presentation.menu;
002:
003: import java.io.IOException;
004: import java.io.Writer;
005: import java.util.Enumeration;
006: import java.util.Vector;
007: import javax.servlet.http.HttpServletRequest;
008:
009: /**
010: * <p>Title: JAT</p>
011: * <p>Description: </p>
012: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
013: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
014: * @author maurizio cesari
015: * @version 1.1
016: * @since 1.2
017: */
018:
019: class HtmlNodeGenerator {
020:
021: private String separator, trElement, tdIcon, tdLabel, tableStart,
022: tableStop;
023: private Vector tdsStatic;
024: private String imgSrcTrasparent;
025: private char apiceOld = '\'';
026: private char apiceNew = '\"';
027: private static HttpServletRequest request;
028: private Writer out;
029: private TreeNode nodo;
030:
031: public HtmlNodeGenerator(TreeNode nodo, Writer out) {
032: this .out = out;
033: this .nodo = nodo;
034: this .request = nodo.getRequest();
035: this .imgSrcTrasparent = getImgSrc("static_image",
036: "static_image_trasparente");
037: this .tableStart = getTableStart();
038: this .tableStop = "</TABLE>";
039: this .separator = getSeparator(imgSrcTrasparent);
040: this .tdLabel = getTdLabel();
041: this .tdIcon = getTdIcon();
042: this .tdsStatic = getTdsStatic(imgSrcTrasparent);
043: this .trElement = getTrElement();
044: }
045:
046: public void printNode() throws IOException {
047: //out.write("<tr>" + tdSeparator);
048: out.write(trElement);
049: out.write(this .getSingleTdStatic(imgSrcTrasparent));
050: for (int i = 0; i < tdsStatic.size(); i++) {
051: out.write((String) tdsStatic.get(i));
052: }
053: out.write(tdIcon);
054: out.write(this .getSingleTdStatic(imgSrcTrasparent));
055: out.write(tdLabel);
056: out.write("</TR>");
057: out.write(separator);
058: }
059:
060: public void printChilds() throws IOException {
061: HtmlNodeGenerator htmlChild;
062: if (nodo.isWritable()) {
063: if (!nodo.isRoot())
064: printNode();
065: Enumeration ch = nodo.children();
066: while (ch.hasMoreElements()) {
067: TreeNode child = (TreeNode) ch.nextElement();
068: htmlChild = new HtmlNodeGenerator(child, out);
069: htmlChild.printChilds();
070: }
071: }
072: }
073:
074: public String getTableStart() {
075: return convert("<table border='0' cellspacing='0' cellpadding='0' width='100%'>");
076: }
077:
078: public String getTrElement() {
079: String app = nodo.getSection();
080: int pos = app.indexOf('_');
081: return convert("<tr class='MenuLiv" + nodo.getLevel()
082: + app.substring(pos + 1)
083: + "' align='left' valign='middle'>");
084: }
085:
086: /** L'icona associata all'item dipende dallo stato (selezionato o meno.
087: *
088: * Se 'item � stato selezionato, viene caricata l'icona espressa sul file
089: * di configurazione, altrimenti si carica un'altra icona:
090: *
091: * Se nome icona=nomexxxyyy.ext, allora il noem dell'icona ssociata all'item
092: * nello stato selezionato sar�:
093: * nomexxxyyy_sel.ext
094: *
095: * )*/
096:
097: public String getTdIcon() {
098: return convert("<td><img src='"
099: + (isSelected() ? this .request.getContextPath()
100: + nodo.getSelectedIcon() : this .request
101: .getContextPath()
102: + nodo.getIcon()) + "' alt='' border='0'></td>");
103: }
104:
105: /** Il link associato all'item dipende dallo stato (selezionato o meno).
106: *
107: * Se l'item � stato selezionato, il link non � attivo e la classe associata
108: * alla label � 'testo'.
109: *
110: */
111:
112: public String getTdLabel() {
113: String normal = "<FONT CLASS='testo'><a target='"
114: + nodo.getTarget() + "' href='"
115: + this .request.getContextPath() + nodo.getUrl() + "'>"
116: + nodo.getLabel() + "</a></FONT>";
117:
118: String selected = "<FONT CLASS='testoB'>" + nodo.getLabel()
119: + "</FONT>";
120:
121: return convert("<td colspan='" + nodo.getColSpanLabel()
122: + "' width='100%'>"
123: + (isSelected() ? selected : normal) + "</td>");
124:
125: }
126:
127: public String getSeparator(String imgSrc) {
128: return convert("<tr><td colspan='" + nodo.getColSpanSeparator()
129: + "' class='" + "MenuSeparatoreColorRight"
130: + "' ><img src='" + this .request.getContextPath()
131: + imgSrc
132: + "' border='0' width='100%' height='1'></td></tr>" +
133:
134: "<tr><td colspan='" + nodo.getColSpanSeparator()
135: + "' bgcolor='" + "white" + "' ><img src='"
136: + this .request.getContextPath() + imgSrc
137: + "' border='0' width='100%' height='1'></td></tr>");
138: }
139:
140: public Vector getTdsStatic(String imgSrc) {
141: Vector ris = new Vector();
142: int repeat = nodo.getLevel();
143: for (int i = 0; i < repeat; i++) {
144: ris.add(getSingleTdStatic(imgSrc));
145: }
146: return ris;
147: }
148:
149: public String getSingleTdStatic(String imgSrc) {
150:
151: return convert("<td><img src='" + this .request.getContextPath()
152: + imgSrc + "' border='0' width='"
153: + getVoiceVar(nodo.getSection(), "voce.tab")
154: + "' height='"
155: + getVoiceVar(nodo.getSection(), "voce.height")
156: + "'></td>");
157: }
158:
159: private String getImgSrc(String imgSectorId, String imgKey) {
160: String ris = null;
161: try {
162: ris = request.getContextPath()
163: + com.jat.core.config.Config.getCurrent().getValue(
164: imgSectorId, imgKey);
165: } catch (Exception ex) {
166: }
167: return ris;
168: }
169:
170: private String getVoiceVar(String menuId, String voiceKey) {
171: String ris = null;
172: try {
173: ris = com.jat.core.config.Config.getCurrent().getValue(
174: menuId, voiceKey);
175: //("menu_left","voce.height"|"voce.tab")
176: } catch (Exception ex) {
177: }
178: return ris;
179: }
180:
181: public String convert(String input) {
182: return input.replace(apiceOld, apiceNew);
183: }
184:
185: /** Il nodo � "selezionato" se il valore del parametro "menu_item"
186: * corrisponde alla label.
187: * */
188:
189: private boolean isSelected() {
190: String selectedItem = (String) this .request
191: .getParameter("menu_item");
192: if (selectedItem == null)
193: return false;
194: return nodo.getLabel().equals(selectedItem);
195: }
196:
197: }
|