001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.tree;
022:
023: import java.util.Map;
024:
025: import javax.swing.tree.TreeNode;
026:
027: public class ClientTreeRenderer extends TreeRenderer {
028:
029: // constructors /////////////////////////////////////////////////////////////
030:
031: // constants ////////////////////////////////////////////////////////////////
032:
033: // classes //////////////////////////////////////////////////////////////////
034:
035: // methods //////////////////////////////////////////////////////////////////
036:
037: /**
038: * Renders the header of the tree: a <tt>table</tt> tag.
039: */
040: protected void renderHeader(StringBuffer buf) throws TreeException {
041:
042: buf.append("<table>\n");
043: }
044:
045: /**
046: * Renders the footer of the tree.
047: */
048: protected void renderFooter(StringBuffer buf) throws TreeException {
049:
050: buf.append("</table>\n");
051: }
052:
053: protected void renderScript(StringBuffer buf) {
054:
055: String openedHandleImage = getOpenedHandleImage();
056: String closedHandleImage = getClosedHandleImage();
057:
058: buf
059: .append("<script> "
060: + " "
061: + " function getNextTr( el ) { "
062: + " for ( var s = el.nextSibling; s != null; s = s.nextSibling ) "
063: + " if ( s.nodeName.toLowerCase() == \"tr\" ) "
064: + " return s; "
065: + " return null; "
066: + " } "
067: + " "
068: + " function getFirstNode( el ) { "
069: + " for ( var i = 0; i < el.childNodes.length; i++ ) "
070: + " if ( el.childNodes[ i ] && "
071: + " ( el.childNodes[ i ].nodeType == 1 ) ) "
072: + " return el.childNodes[ i ]; "
073: + " return null; "
074: + " } "
075: + " "
076: + " function getFirstText( el ) { "
077: + " for ( var i = 0; i < el.childNodes.length; i++ ) "
078: + " if ( el.childNodes[ i ] && "
079: + " ( el.childNodes[ i ].nodeType == 3 ) ) "
080: + " return el.childNodes[ i ]; "
081: + " return null; "
082: + " } "
083: + " "
084: + " function toggleNode( el ) { "
085: + " var r = getNextTr( el.parentNode.parentNode ); "
086: + " var n = getFirstNode( el ); "
087: + " var t = getFirstText( el ); "
088: + " "
089: + " if ( r.style.display != \"none\" ) { "
090: + " r.style.display = \"none\"; "
091: + " "
092: + " if ( ( n == null ) && ( t != null ) ) "
093: + " t.nodeValue = \"+\"; "
094: + " else "
095: + " n.src = \""
096: + closedHandleImage
097: + "\"; "
098: + " } "
099: + " else { "
100: + " r.style.display = \"\"; "
101: + " "
102: + " if ( ( n == null ) && ( t != null ) ) "
103: + " t.nodeValue=\"-\"; " + " else "
104: + " n.src = \"" + openedHandleImage
105: + "\"; " + " } " + " } " + "</script>\n");
106: }
107:
108: protected void renderNode(StringBuffer buf, FoldingTreeNode node) {
109:
110: String s = null;
111: Map iconImages = getIconImages();
112:
113: //
114: // render node
115: //
116: buf.append("<tr>");
117:
118: if (node.isLeaf()) {
119: if (iconImages == null) {
120: buf.append("<td> </td>");
121: } else {
122: s = (String) iconImages.get(SPACER_ICON);
123:
124: if (s == null)
125: buf.append("<td> </td>");
126: else
127: buf
128: .append("<td><img src=\"" + s
129: + "\"></img></td>");
130: }
131: } else {
132:
133: //
134: // render handle
135: //
136: buf
137: .append("<td><a onclick=\"toggleNode(this)\" href=\"javascript:;\">");
138:
139: if (node.getOpened()) {
140: if (getOpenedHandleImage() == null)
141: buf.append("-");
142: else
143: buf.append("<img border=\"0\" src=\""
144: + getOpenedHandleImage() + "\"></img>");
145: } else {
146: if (getClosedHandleImage() == null)
147: buf.append("+");
148: else
149: buf.append("<img border=\"0\" src=\""
150: + getClosedHandleImage() + "\"></img>");
151: }
152:
153: buf.append("</a></td>");
154: }
155:
156: //
157: // render icon
158: //
159: if (iconImages != null) {
160:
161: s = (String) iconImages.get(node.getIconHint());
162:
163: if (s == null)
164: s = (String) iconImages.get(DEFAULT_ICON);
165:
166: if (s == null) {
167: buf.append("<td> </td>");
168: } else {
169: if (node.getUrl() == null)
170: buf.append("<td><img src=\"" + s + "\"></td>");
171: else
172: buf.append("<td><a href=\"" + node.getUrl()
173: + "\"><img border=\"0\" src=\"" + s
174: + "\"></a></td>");
175: }
176: }
177:
178: //
179: // render label
180: //
181: if (node.getUrl() == null)
182: buf.append("<td width=\"100%\">" + node.getLabel()
183: + "</td>");
184: else
185: buf.append("<td width=\"100%\"><a href=\"" + node.getUrl()
186: + "\">" + node.getLabel() + "</a></td>");
187:
188: buf.append("</tr>\n");
189:
190: if (!node.isLeaf()) {
191:
192: //
193: // render children
194: //
195: buf.append("<tr style=\"display:");
196:
197: if (node.getOpened())
198: buf.append("normal");
199: else
200: buf.append("none");
201:
202: buf.append("\"><td> </td>");
203:
204: buf.append("<td colspan=\"2\"><table>");
205:
206: for (int i = 0; i < node.getChildCount(); i++)
207: renderNode(buf, (FoldingTreeNode) node.getChildAt(i));
208:
209: buf.append("</table></td></tr>\n");
210: }
211: }
212:
213: public String renderTree(FoldingTreeNode root) throws TreeException {
214:
215: StringBuffer buf = new StringBuffer();
216:
217: if (!scriptRendered_) {
218: scriptRendered_ = true;
219: renderScript(buf);
220: }
221:
222: renderHeader(buf);
223:
224: if (isRootHidden()) {
225: for (int i = 0; i < root.getChildCount(); i++) {
226: renderNode(buf, (FoldingTreeNode) root.getChildAt(i));
227: }
228: } else {
229: renderNode(buf, root);
230: }
231:
232: renderFooter(buf);
233:
234: return buf.toString();
235: }
236:
237: // properties ///////////////////////////////////////////////////////////////
238:
239: // attributes ///////////////////////////////////////////////////////////////
240:
241: private boolean scriptRendered_ = false;
242: }
|