001: /*
002: * @(#)CheckBoxTreeCellRenderer.java 8/11/2005
003: *
004: * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
005: */
006:
007: package com.jidesoft.swing;
008:
009: import javax.swing.*;
010: import javax.swing.border.Border;
011: import javax.swing.border.EmptyBorder;
012: import javax.swing.tree.TreeCellRenderer;
013: import javax.swing.tree.TreePath;
014: import java.awt.*;
015: import java.awt.event.MouseEvent;
016: import java.io.Serializable;
017:
018: /**
019: * Renderers an item in a tree using JCheckBox.
020: */
021: public class CheckBoxTreeCellRenderer extends NullPanel implements
022: TreeCellRenderer, Serializable {
023:
024: protected static Border noFocusBorder;
025:
026: /**
027: * The checkbox that is used to paint the check box in cell renderer
028: */
029: protected TristateCheckBox _checkBox = new NullTristateCheckBox();
030: protected JCheckBox PROTOTYPE = new TristateCheckBox();
031: protected JLabel _label = new NullLabel();
032:
033: /**
034: * The label which appears after the check box.
035: */
036: protected TreeCellRenderer _actualTreeRenderer;
037:
038: /**
039: * Constructs a default renderer object for an item
040: * in a list.
041: */
042: public CheckBoxTreeCellRenderer() {
043: this (null);
044: }
045:
046: public CheckBoxTreeCellRenderer(TreeCellRenderer renderer) {
047: if (noFocusBorder == null) {
048: noFocusBorder = new EmptyBorder(1, 1, 1, 1);
049: }
050: _checkBox.setOpaque(false);
051: setBorder(noFocusBorder);
052: setLayout(new BorderLayout(0, 0));
053: add(_checkBox, BorderLayout.BEFORE_LINE_BEGINS);
054: _actualTreeRenderer = renderer;
055: }
056:
057: public TreeCellRenderer getActualTreeRenderer() {
058: return _actualTreeRenderer;
059: }
060:
061: public void setActualTreeRenderer(
062: TreeCellRenderer actualTreeRenderer) {
063: _actualTreeRenderer = actualTreeRenderer;
064: }
065:
066: public Component getTreeCellRendererComponent(JTree tree,
067: Object value, boolean selected, boolean expanded,
068: boolean leaf, int row, boolean hasFocus) {
069: _checkBox.setPreferredSize(new Dimension(PROTOTYPE
070: .getPreferredSize().width, 0));
071: setComponentOrientation(tree.getComponentOrientation());
072:
073: TreePath path = tree.getPathForRow(row);
074: if (path != null && tree instanceof CheckBoxTree) {
075: CheckBoxTreeSelectionModel selectionModel = ((CheckBoxTree) tree)
076: .getCheckBoxTreeSelectionModel();
077: if (selectionModel != null) {
078: _checkBox.setEnabled(((CheckBoxTree) tree)
079: .isCheckBoxEnabled()
080: && ((CheckBoxTree) tree)
081: .isCheckBoxEnabled(path));
082: if (selectionModel.isPathSelected(path, selectionModel
083: .isDigIn()))
084: _checkBox.setState(TristateCheckBox.SELECTED);
085: else
086: _checkBox
087: .setState(selectionModel.isDigIn()
088: && selectionModel
089: .isPartiallySelected(path) ? null
090: : TristateCheckBox.NOT_SELECTED);
091: }
092: }
093:
094: if (_actualTreeRenderer != null) {
095: JComponent treeCellRendererComponent = (JComponent) _actualTreeRenderer
096: .getTreeCellRendererComponent(tree, value,
097: selected, expanded, leaf, row, hasFocus);
098: if (path != null && tree instanceof CheckBoxTree) {
099: if (!((CheckBoxTree) tree).isCheckBoxVisible(path)) {
100: return treeCellRendererComponent;
101: }
102: }
103: Border border = treeCellRendererComponent.getBorder();
104: setBorder(border);
105: treeCellRendererComponent.setBorder(BorderFactory
106: .createEmptyBorder());
107: if (getComponentCount() == 2) {
108: remove(1);
109: }
110: add(treeCellRendererComponent);
111: }
112:
113: return this ;
114: }
115:
116: @Override
117: public String getToolTipText(MouseEvent event) {
118: if (_actualTreeRenderer instanceof JComponent) {
119: Point p = event.getPoint();
120: p.translate(-_checkBox.getWidth(), 0);
121: MouseEvent newEvent = new MouseEvent(
122: ((JComponent) _actualTreeRenderer), event.getID(),
123: event.getWhen(), event.getModifiers(), p.x, p.y,
124: event.getClickCount(), event.isPopupTrigger());
125:
126: String tip = ((JComponent) _actualTreeRenderer)
127: .getToolTipText(newEvent);
128:
129: if (tip != null) {
130: return tip;
131: }
132: }
133: return super.getToolTipText(event);
134: }
135:
136: }
|