001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.lib.profiler.ui.components.tree;
042:
043: import java.awt.BorderLayout;
044: import java.awt.Component;
045: import java.awt.Dimension;
046: import javax.swing.ButtonModel;
047: import javax.swing.Icon;
048: import javax.swing.JCheckBox;
049: import javax.swing.JPanel;
050: import javax.swing.JTree;
051: import javax.swing.tree.DefaultTreeCellRenderer;
052: import javax.swing.tree.TreePath;
053:
054: /**
055: *
056: * @author Jiri Sedlacek
057: */
058: public class CheckTreeCellRenderer extends JPanel implements
059: TreeCellRendererPersistent {
060: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
061:
062: private static Dimension checkBoxDimension = new JCheckBox()
063: .getPreferredSize();
064:
065: //~ Instance fields ----------------------------------------------------------------------------------------------------------
066:
067: private JCheckBox checkBox = new JCheckBox();
068: private ButtonModel checkBoxModel = checkBox.getModel();
069: private Component treeRendererComponent;
070: private DefaultTreeCellRenderer treeRenderer = new DefaultTreeCellRenderer();
071: private boolean persistentRenderer = false;
072:
073: //~ Constructors -------------------------------------------------------------------------------------------------------------
074:
075: public CheckTreeCellRenderer() {
076: setLayout(new BorderLayout());
077: setOpaque(false);
078: checkBox.setOpaque(false);
079: add(checkBox, BorderLayout.WEST);
080: }
081:
082: //~ Methods ------------------------------------------------------------------------------------------------------------------
083:
084: public static Dimension getCheckBoxDimension() {
085: return checkBoxDimension;
086: }
087:
088: public Component getTreeCellRendererComponent(JTree tree,
089: Object value, boolean selected, boolean expanded,
090: boolean leaf, int row, boolean hasFocus) {
091: // Get CheckTreeNode from current Node
092: CheckTreeNode treeNode = (value instanceof CheckTreeNode) ? (CheckTreeNode) value
093: : null;
094:
095: // Update UI
096: if (treeRendererComponent != null) {
097: remove(treeRendererComponent);
098: }
099:
100: if ((treeNode != null)
101: && treeRenderer instanceof DefaultTreeCellRenderer) {
102: checkBox.setVisible(!persistentRenderer);
103: setupCellRendererIcon(
104: (DefaultTreeCellRenderer) treeRenderer, treeNode
105: .getIcon());
106: } else {
107: checkBox.setVisible(false);
108: setupCellRendererIcon(
109: (DefaultTreeCellRenderer) treeRenderer, null);
110: }
111:
112: treeRendererComponent = treeRenderer
113: .getTreeCellRendererComponent(tree, value, selected,
114: expanded, leaf, row, hasFocus);
115: add(treeRendererComponent, BorderLayout.CENTER);
116:
117: // Return if no path or not a CheckTreeNode
118: if (treeNode == null) {
119: return this ;
120: }
121:
122: // If tree model supports checking (uses CheckTreeNodes), setup CheckBox
123: if (treeNode.isFullyChecked()) {
124: setupCheckBox(Boolean.TRUE);
125: } else {
126: setupCheckBox(treeNode.isPartiallyChecked() ? null
127: : Boolean.FALSE);
128: }
129:
130: return this ;
131: }
132:
133: public Component getTreeCellRendererComponentPersistent(JTree tree,
134: Object value, boolean selected, boolean expanded,
135: boolean leaf, int row, boolean hasFocus) {
136: CheckTreeCellRenderer ctcr = new CheckTreeCellRenderer();
137: ctcr.persistentRenderer = true;
138:
139: return ctcr.getTreeCellRendererComponent(tree, value, selected,
140: expanded, leaf, row, hasFocus);
141: }
142:
143: private void setupCellRendererIcon(
144: DefaultTreeCellRenderer renderer, Icon icon) {
145: renderer.setLeafIcon(icon);
146: renderer.setOpenIcon(icon);
147: renderer.setClosedIcon(icon);
148: }
149:
150: private void setupCheckBox(Boolean state) {
151: if (state == Boolean.TRUE) {
152: // Fully checked
153: checkBoxModel.setArmed(false);
154: checkBoxModel.setPressed(false);
155: checkBoxModel.setSelected(true);
156: } else if (state == Boolean.FALSE) {
157: // Fully unchecked
158: checkBoxModel.setArmed(false);
159: checkBoxModel.setPressed(false);
160: checkBoxModel.setSelected(false);
161: } else {
162: // Partially checked
163: checkBoxModel.setArmed(true);
164: checkBoxModel.setPressed(true);
165: checkBoxModel.setSelected(true);
166: }
167: }
168: }
|