001: package net.ar.webonswing.tutorial.tree;
002:
003: import java.awt.*;
004:
005: import javax.swing.*;
006: import javax.swing.plaf.*;
007: import javax.swing.tree.*;
008:
009: import net.ar.webonswing.*;
010: import net.ar.webonswing.helpers.*;
011: import net.ar.webonswing.render.templates.*;
012: import net.ar.webonswing.swing.components.*;
013: import net.ar.webonswing.swing.layouts.*;
014:
015: public class ComponentStructureCellRenderer implements TreeCellRenderer {
016: MyCell theLeafCell;
017: MyCell theExpandedCell;
018: MyCell theCollapsedCell;
019: MyCell theRootCell;
020: Template theTemplate;
021:
022: public ComponentStructureCellRenderer(JTree aTree) {
023: theTemplate = WosSwingHelper.getTemplateForComponent("JTree2",
024: aTree);
025:
026: theLeafCell = createCell("theLeafNode", new ImageIcon(
027: "resources/images/j.close.gif"));
028: theExpandedCell = createCell("theExpandedNode", new ImageIcon(
029: "resources/images/j.open.gif"));
030: theCollapsedCell = createCell("theCollapsedNode",
031: new ImageIcon("resources/images/j.close.gif"));
032: theRootCell = createCell("theRootNode", new ImageIcon(
033: "resources/images/j.close.gif"));
034: }
035:
036: private MyCell createCell(String theNodeType, ImageIcon aIcon) {
037: JCheckBox theCheck = new JCheckBox();
038: JLabel theLabel = new JLabel();
039:
040: theCheck.setLayout(new TemplateLayout(WosFramework
041: .getKeyPositionTemplateForSubTemplate(theTemplate,
042: theNodeType
043: + ".theTreeCell.theContent.theCheck")));
044: theLabel.setLayout(new TemplateLayout(WosFramework
045: .getKeyPositionTemplateForSubTemplate(theTemplate,
046: theNodeType
047: + ".theTreeCell.theContent.theLabel")));
048:
049: MyCell theCell = new MyCell(new TemplateLayout(WosFramework
050: .getKeyPositionTemplateForSubTemplate(theTemplate,
051: theNodeType + ".theTreeCell.theContent")),
052: theCheck, "theCheck", theLabel, "theLabel");
053: theCell.theLabel.setIcon(aIcon);
054:
055: return theCell;
056: }
057:
058: public Component getTreeCellRendererComponent(JTree aTree,
059: Object value, boolean isSelected, boolean expanded,
060: boolean leaf, int row, boolean hasFocus) {
061: MyCell theCell = null;
062:
063: if (value instanceof CheckNode) {
064: TreeNode theRootNode = (TreeNode) aTree.getModel()
065: .getRoot();
066: CheckNode theCheckNode = (CheckNode) value;
067: theCell = theRootNode == theCheckNode ? theRootCell
068: : leaf ? theLeafCell : expanded ? theExpandedCell
069: : theCollapsedCell;
070:
071: theCell.setEnabled(aTree.isEnabled());
072: theCell.theCheck.setSelected(theCheckNode.isSelected());
073: theCell.theLabel.setText(aTree.convertValueToText(value,
074: isSelected, expanded, leaf, row, hasFocus));
075:
076: Color theBgColor = isSelected ? new ColorUIResource(255,
077: 255, 200) : UIManager.getColor("Label.background");
078: theCell.theLabel.setBackground(theBgColor);
079: }
080:
081: return theCell;
082: }
083:
084: class MyCell extends JPanel {
085: JCheckBox theCheck;
086: JLabel theLabel;
087:
088: public MyCell(LayoutManager aLayout, JCheckBox aCheck,
089: String aCheckPosition, JLabel aLabel,
090: String aLabelPosition) {
091: super (aLayout);
092:
093: add(theCheck = aCheck, aCheckPosition);
094: add(theLabel = aLabel, aLabelPosition);
095: }
096:
097: public Dimension getPreferredSize() {
098: Dimension d_check = theCheck.getPreferredSize();
099: Dimension d_label = theLabel.getPreferredSize();
100: return new Dimension(d_check.width + d_label.width,
101: (d_check.height < d_label.height ? d_label.height
102: : d_check.height));
103: }
104:
105: public void setBackground(Color color) {
106: if (color instanceof ColorUIResource)
107: color = null;
108:
109: super .setBackground(color);
110: }
111:
112: public JCheckBox getCheck() {
113: return theCheck;
114: }
115:
116: public void setCheck(JCheckBox aCheck) {
117: theCheck = aCheck;
118: }
119:
120: public JLabel getLabel() {
121: return theLabel;
122: }
123:
124: public void setLabel(JLabel aLabel) {
125: theLabel = aLabel;
126: }
127: }
128: }
|