001: /*
002: * Copyright Javelin Software, All rights reserved.
003: */
004:
005: package com.javelin.swinglets.plaf.wml;
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:
017: /**
018: * WMLTreeUI defines a look and feel for default WML.
019: *
020: * @author Robin Sharp
021: */
022:
023: public class WMLTreeUI extends WMLComponentUI {
024: /**
025: * Render the UI on the PrintWriter
026: */
027: public void update(PrintWriter out, SComponent c) {
028: if (!c.isVisible())
029: return;
030:
031: STree tree = (STree) c;
032:
033: TreePath path = tree.getSelectedPath();
034: if (path == null) {
035: path = new TreePath(tree.getModel().getRoot());
036: }
037:
038: TreeNode node = (TreeNode) path.getLastPathComponent();
039:
040: //Wind back when the leaf is selected
041: if (node.getChildCount() == 0) {
042: path = new TreePath(tree.getModel().getRoot());
043: node = (TreeNode) path.getLastPathComponent();
044: }
045:
046: int row = tree.getRowForPath(path);
047:
048: //Display the root/parent node.
049: if (path.getPathCount() > 0) //1
050: {
051: writeTreeNode(out, tree, node, true, row);
052: }
053:
054: //Display the children
055: for (int index = 0; index < node.getChildCount(); index++) {
056: writeTreeNode(out, tree, (TreeNode) node.getChildAt(index),
057: false, ++row);
058: }
059:
060: }
061:
062: protected void writeTreeNode(PrintWriter out, STree tree,
063: TreeNode treeNode, boolean parent, int row) {
064: out.print("<anchor>");
065:
066: out.println("<br/>");
067: //TEXT
068: if (parent) {
069: out.print(".. ");
070: } else if (treeNode.getChildCount() > 0) {
071: //folder
072: out.print("+ ");
073: } else {
074: //leaf
075: out.print("- ");
076: }
077:
078: out.print(treeNode.toString());
079:
080: out.print("<go method=\"post\"");
081: out.print(" href=\"");
082: out.print(tree.getSwingletManager().getUrl());
083: out.println("\">");
084:
085: out.print("<postfield name=\"");
086: out.print(row);
087: out.print("\" value=\"");
088: if (treeNode.getChildCount() > 0) {
089: out.print(STree.NODE);
090: } else {
091: out.print(STree.LABEL);
092: }
093: out.println("\"/>");
094:
095: out.print("<postfield name=\"");
096: out.print(STree.VIRTUAL_TREE);
097: out.print("\" value=\"true\"/>");
098:
099: out.print("<postfield name=\"");
100: out.print(SConstants.SOURCE_CONTAINER);
101: out.print("\" value=\"");
102: SContainer container = tree.getTopLevelAncestor();
103: if (container == null)
104: out.print(tree.getName());
105: else
106: out.print(container.getName());
107: out.print("\"/>");
108:
109: out.print("<postfield name=\"");
110: out.print(SConstants.SOURCE_COMPONENT);
111: out.print("\" value=\"");
112: out.print(tree.getName());
113: out.println("\"/>");
114:
115: out.print("</go>");
116: out.println("</anchor>");
117: }
118:
119: // PRIVATE //////////////////////////////////////////////////////////////////////////
120:
121: protected int row;
122: protected int depth;
123:
124: private STreeCellRenderer treeCellRenderer; //Not used at the moment
125:
126: }
127:
128: /* OLD STUFF FOR - use on SMenus
129: {
130: if( !c.isVisible() ) return;
131:
132: STree tree = (STree)c;
133:
134: TreePath path = new TreePath( tree.getModel().getRoot() );
135: out.print( "<select name=\"" );
136: out.print( tree.getName() );
137: out.println( "\" >" );
138:
139: if( tree.getModel().getRoot() == path.getLastPathComponent() &&
140: tree.isRootVisible() )
141: {
142: out.print( "<optgroup title=\"" );
143: out.print( path.getLastPathComponent().toString() );
144: out.println( "\">" );
145: }
146:
147: update( out, tree, path );
148:
149: if( tree.getModel().getRoot() == path.getLastPathComponent() &&
150: tree.isRootVisible() )
151: {
152: out.println( "</optgroup>" );
153: }
154:
155: out.println( "</select>" );
156:
157: }
158: */
159: /**
160: * Depth first update of all nodes and their descendents.
161: */
162: /*
163: public void update( PrintWriter out, STree tree, TreePath path )
164: {
165: TreeNode node = (TreeNode)path.getLastPathComponent();
166:
167: if( node.isLeaf() )
168: {
169: out.print( "<option value=\"" );
170: out.print( row );
171: out.print( "\">" );
172: out.print( node.toString() );
173: out.println( "</option>" );
174: }
175: else
176: {
177: out.print( "<optgroup title=\"" );
178: out.print( node.toString() );
179: out.println( "\">" );
180:
181: depth++;
182: for( int index = 0; index < node.getChildCount(); index++ )
183: {
184: update( out, tree, path.pathByAddingChild( node.getChildAt( index ) ) );
185: }
186: depth--;
187:
188: out.println( "</optgroup>" );
189: }
190:
191: row++;
192: }
193:
194: */
195:
196: /*
197: <select name="country_name" tabindex="2">
198:
199: <optgroup title="Scandanavia">
200: <option value="Denmark">Denmark</option>
201: <option value="Finland">Finland</option>
202: <option value="Norway">Norway</option>
203: <option value="Sweden">Sweden</option>
204: </optgroup>
205:
206: <optgroup title="Europe">
207: <option value="France">France</option>
208: <option value="Germany">Germany</option>
209: <option value="Italy">Italy</option>
210: <option value="Spain">Spain</option>
211: </optgroup>
212: <option value="England">England</option>
213: </select>
214: */
|