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