001: /*
002: * Copyright Javelin Software, All rights reserved.3
003: */
004:
005: package com.javelin.swinglets.plaf.wml;
006:
007: import java.awt.*;
008:
009: import javax.swing.tree.*;
010:
011: import com.javelin.swinglets.*;
012: import com.javelin.swinglets.tree.*;
013: import com.javelin.swinglets.theme.*;
014:
015: /**
016: * Defines the requirements for an object that displays a tree node in WML.
017: * <P>
018: * The renderer reloads the whole web page when a leaf node is selected.
019: *
020: * @author Robin Sharp
021: */
022:
023: public class WMLDefaultTreeCellRenderer extends SPanel implements
024: STreeCellRenderer {
025: /**
026: * Create a new WMLDefaultTreeCellRenderer
027: */
028: public WMLDefaultTreeCellRenderer() {
029: add(button);
030: add(label);
031: }
032:
033: /**
034: * Sets the value of the current tree cell to <code>value</code>.
035: * Currently component.setName( "" + row ) MUST be called.
036: */
037: public SComponent getTreeCellRendererComponent(STree tree,
038: Object value, boolean expanded, boolean leaf, int row,
039: boolean isSelected) {
040: if (theme == null
041: || theme != tree.getSwingletManager().getTheme()) {
042: theme = tree.getSwingletManager().getTheme();
043:
044: open = theme.getIcon(STheme.TREE_OPEN_ICON);
045: closed = theme.getIcon(STheme.TREE_CLOSED_ICON);
046: doc = theme.getIcon(STheme.TREE_LEAF_ICON);
047: }
048:
049: button.setName("" + row);
050:
051: if (leaf) {
052: button.setIcon(doc);
053: //label.setLink( getLink( tree, row ) );
054: } else if (expanded) {
055: button.setIcon(open);
056: //label.setLink( getLink( tree, row ) );
057: } else {
058: button.setIcon(closed);
059: //label.setLink( getLink( tree, row ) );
060: }
061:
062: if (isSelected) {
063: label.setFont(bold10);
064: } else {
065: label.setFont(plain10);
066: }
067:
068: if (value != null) {
069: if (value instanceof DefaultMutableTreeNode
070: && ((DefaultMutableTreeNode) value).getUserObject() != null
071: && ((DefaultMutableTreeNode) value).getUserObject() instanceof SComponent) {
072: return (SComponent) ((DefaultMutableTreeNode) value)
073: .getUserObject();
074: }
075:
076: label.setText(value.toString());
077: } else {
078: label.setText("");
079: }
080:
081: return this ;
082: }
083:
084: /**
085: * Get the link for this row. This caches the link string.
086: */
087: protected SLink getLink(STree tree, int row) {
088: //Gauge the length of the buffer
089: if (linkBuffer == null) //Only update buffer once
090: {
091: SContainer container = tree.getTopLevelAncestor();
092:
093: //If the table or container have changed reload the buffer
094: if (!container.getName().equals(containerName)
095: || !tree.getName().equals(treeName)) {
096: containerName = container.getName();
097: treeName = tree.getName();
098:
099: linkBuffer = new StringBuffer(60);
100:
101: linkBuffer.append("?");
102: linkBuffer.append(SConstants.SOURCE_CONTAINER);
103: linkBuffer.append("=");
104: linkBuffer.append(container.getName());
105:
106: if (container instanceof SFrame
107: && ((SFrame) container).getParentFrame() != null) {
108: linkBuffer.append("&");
109: linkBuffer.append(SConstants.TARGET_COMPONENT);
110: linkBuffer.append("=");
111: linkBuffer.append(((SFrame) container)
112: .getParentFrame().getName());
113: } else {
114: linkBuffer.append("&");
115: linkBuffer.append(SConstants.TARGET_COMPONENT);
116: linkBuffer.append("=");
117: linkBuffer.append(container.getName());
118: }
119:
120: linkBuffer.append("&");
121: linkBuffer.append(SConstants.SOURCE_COMPONENT);
122: linkBuffer.append("=");
123: linkBuffer.append(tree.getName());
124:
125: linkBuffer.append("&");
126:
127: linkLength = linkBuffer.length();
128:
129: //link.setTarget( SFrame.SELF );
130: }
131: }
132:
133: //Remove the old column from the end of the buffer
134: linkBuffer.setLength(linkLength);
135:
136: //Now append the new row
137: linkBuffer.append(row);
138: linkBuffer.append("=");
139: linkBuffer.append(STree.LABEL);
140:
141: link.setUrl(linkBuffer.toString());
142: return link;
143: }
144:
145: protected SButton button = new SButton("Submit");
146: protected SLabel label = new SLabel();
147:
148: protected STheme theme;
149: protected SIcon open;
150: protected SIcon closed;
151: protected SIcon doc;
152:
153: protected SLink link = new SLink(null, SFrame.TOP);
154:
155: //This is for cached links
156: private StringBuffer linkBuffer;
157: private int linkLength;
158: private String treeName;
159: private String containerName;
160:
161: private SFont plain10 = SFont.getFont("Arial,Helvetica",
162: SFont.PLAIN, 8);
163: private SFont bold10 = SFont.getFont("Arial,Helvetica", SFont.BOLD,
164: 8);
165:
166: }
|