001: /*
002: * Copyright Javelin Software, All rights reserved.
003: */
004:
005: package com.javelin.swinglets.plaf.html;
006:
007: import java.awt.*;
008: import java.util.*;
009: import java.io.*;
010:
011: import javax.swing.tree.*;
012:
013: import com.javelin.swinglets.*;
014: import com.javelin.swinglets.tree.*;
015: import com.javelin.swinglets.plaf.*;
016: import com.javelin.swinglets.theme.*;
017:
018: /**
019: * HTMLTreeUI defines a look and feel for default HTML.
020: *
021: * @author Robin Sharp
022: */
023:
024: public class HTMLTreeUI extends HTMLComponentUI {
025: /**
026: * Render the UI on the PrintWriter
027: */
028: public void update(PrintWriter out, SComponent c) {
029: if (!c.isVisible())
030: return;
031:
032: STree tree = (STree) c;
033:
034: if (tree.getModel() == null)
035: return;
036:
037: TreePath path = new TreePath(tree.getModel().getRoot());
038: row = 0;
039:
040: treeCellRenderer = tree.getTreeCellRenderer(getLookAndFeel()
041: .getClass().getName());
042:
043: //Write the table
044: out.print("<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0");
045:
046: Object cssClass = c.getClientProperty(CSS_CLASS);
047: if (cssClass != null) {
048: out.print(" class=\"");
049: out.print(cssClass);
050: }
051:
052: if (tree.getBackgroundIcon() != null) {
053: out.print(" BACKGROUND=\"");
054: out.print(tree.getBackgroundIcon().getUrl());
055: out.print("\"");
056: } else if (!tree.isOpaque()) {
057: //Dont do anything
058: } else if (tree.getBackground() != null) {
059: out.print(" BGCOLOR=\"#");
060: out.print(SColor.toHexString(tree.getBackground()));
061: out.print("\"");
062: } else {
063: out.print(" BGCOLOR=\"#");
064: out.print(SColor.toHexString(getTheme()
065: .getWindowBackground()));
066: out.print("\"");
067: }
068:
069: out.println(" >");
070:
071: /*
072: //WRITE THE FORM INSIDE THE TABLE
073: out.println();
074: out.print( "<FORM" );
075: out.print( " METHOD=\"GET\" " );
076: HTMLUtility.setName( out, tree );
077: out.println( ">" );
078:
079: //WRITE OUT THE FRAME AND COMPONENT AS HIDDEN FIELDS
080: HTMLUtility.setFrameName( out, tree );
081: HTMLUtility.setComponentName( out, tree );
082: */
083: update(out, tree, path);
084:
085: //out.println( "</FORM>" );
086: out.println("</TABLE>");
087: }
088:
089: /**
090: * Depth first update of all expanded nodes and their descendents.
091: */
092: public void update(PrintWriter out, STree tree, TreePath path) {
093: boolean expanded = tree.isExpanded(path);
094:
095: //System.out.println( "VALUE=" + path.getLastPathComponent() + ",expanded=" + expanded + ",row=" + row );
096:
097: if (tree.getModel().getRoot() == path.getLastPathComponent()) {
098: //May be display the root
099: if (tree.isRootVisible()) {
100: updateNode(out, tree, path, expanded);
101: }
102: } else {
103: //else display the node
104: updateNode(out, tree, path, expanded);
105: }
106:
107: if (row > tree.getRowCount())
108: return;
109: ;
110:
111: //Display all the children
112: if (expanded) {
113: depth++;
114: TreeNode node = (TreeNode) path.getLastPathComponent();
115: for (int index = 0; index < node.getChildCount(); index++) {
116: //Recursively display the children
117: update(out, tree, path.pathByAddingChild(node
118: .getChildAt(index)));
119: }
120: depth--;
121: }
122: }
123:
124: /**
125: * Render a node in HTML as a TABLE;
126: */
127: public void updateNode(PrintWriter out, STree tree, TreePath path,
128: boolean expanded) {
129: TreeNode node = (TreeNode) path.getLastPathComponent();
130: out.println("<TR><TD>");
131:
132: for (int index = 0; index < depth; index++) {
133: out.print(HTMLCharacterUI
134: .getSpecialCharacter(SCharacter.SPACE));
135: out.print(HTMLCharacterUI
136: .getSpecialCharacter(SCharacter.SPACE));
137: out.print(HTMLCharacterUI
138: .getSpecialCharacter(SCharacter.SPACE));
139: out.print(HTMLCharacterUI
140: .getSpecialCharacter(SCharacter.SPACE));
141: out.print(HTMLCharacterUI
142: .getSpecialCharacter(SCharacter.SPACE));
143: }
144:
145: if (treeCellRenderer != null) {
146: SComponent component = treeCellRenderer
147: .getTreeCellRendererComponent(tree, node, expanded,
148: node.isLeaf(), row, tree
149: .isPathSelected(path));
150:
151: if (component != null) {
152: //Change the look and feel to that of the tree.
153: component.setLookAndFeel(tree.getLookAndFeel());
154:
155: if (component.getFont() == null) {
156: if (tree.getFont() != null) {
157: component.setFont(tree.getFont());
158: } else {
159: component.setFont(getTheme()
160: .getSystemTextFont());
161: }
162: }
163:
164: component.paint(out);
165: }
166: } else {
167: out.println(node.toString());
168: }
169:
170: out.println("</TD></TR>");
171:
172: row++;
173:
174: }
175:
176: // PRIVATE //////////////////////////////////////////////////////////////////////////
177:
178: protected int row;
179: protected int depth;
180:
181: private STreeCellRenderer treeCellRenderer;
182: }
|