KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > palette > ui > CheckRenderer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.palette.ui;
20
21 import java.awt.*;
22 import javax.swing.*;
23 import javax.swing.tree.*;
24 import org.netbeans.modules.palette.DefaultSettings;
25 import org.netbeans.modules.palette.Settings;
26 import org.openide.explorer.view.NodeRenderer;
27 import org.openide.explorer.view.Visualizer;
28 import org.openide.nodes.Node;
29
30 /**
31  * @author Pavel Flaska, S. Aubrecht
32  */

33 class CheckRenderer extends JPanel implements TreeCellRenderer {
34
35     protected JCheckBox check;
36     private NodeRenderer nodeRenderer;
37     private DefaultSettings settings;
38     private static Dimension checkDim;
39
40     static Rectangle checkBounds;
41     
42     static {
43         Dimension old = new JCheckBox().getPreferredSize();
44         checkDim = new Dimension(old.width, old.height - 5);
45     }
46     
47     public CheckRenderer( DefaultSettings settings ) {
48         this.nodeRenderer = new NodeRenderer();
49         this.settings = settings;
50         setLayout(null);
51         add(check = new JCheckBox());
52         Color c = UIManager.getColor("Tree.textBackground"); //NOI18N
53
if (c == null) {
54             //May be null on GTK L&F
55
c = Color.WHITE;
56         }
57         check.setBackground(c); // NOI18N
58
Dimension dim = check.getPreferredSize();
59         check.setPreferredSize(checkDim);
60     }
61     
62     /** The component returned by HtmlRenderer.Renderer.getTreeCellRendererComponent() */
63     private Component stringDisplayer = new JLabel(" "); //NOI18N
64

65     public Component getTreeCellRendererComponent(JTree tree, Object JavaDoc value,
66     boolean isSelected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
67         stringDisplayer = nodeRenderer.getTreeCellRendererComponent(tree,
68             value, isSelected, expanded, leaf, row, hasFocus);
69
70         TreePath path = tree.getPathForRow( row );
71         if( null != path && 1 == path.getPathCount() ) {
72             //do not show checkbox for the root node
73
return stringDisplayer;
74         }
75         
76         if( stringDisplayer instanceof JComponent ) {
77             setToolTipText( ((JComponent)stringDisplayer).getToolTipText() );
78         }
79         
80         //HtmlRenderer does not tolerate null colors - real ones are needed to
81
//ensure fg/bg always diverge enough to be readable
82
if (stringDisplayer.getBackground() == null) {
83             stringDisplayer.setBackground (tree.getBackground());
84         }
85         if (stringDisplayer.getForeground() == null) {
86             stringDisplayer.setForeground (tree.getForeground());
87         }
88
89         if( check != null ) {
90             Node node;
91             if( value instanceof Node ) {
92                 node = (Node)value;
93             } else {
94                 node = Visualizer.findNode( value );
95             }
96             check.setSelected( null == node || settings.isNodeVisible( node ) );//node.isSelected());
97
check.setEnabled( true );//!node.isDisabled());
98
}
99         return this;
100     }
101     
102     public void paintComponent (Graphics g) {
103         Dimension d_check = check == null ? new Dimension(0, 0) : check.getSize();
104         Dimension d_label = stringDisplayer == null ? new Dimension(0,0) :
105             stringDisplayer.getPreferredSize();
106             
107         int y_check = 0;
108         int y_label = 0;
109         
110         if (d_check.height >= d_label.height) {
111             y_label = (d_check.height - d_label.height) / 2;
112         }
113         if (check != null) {
114             check.setBounds (0, 0, d_check.width, d_check.height);
115             check.paint(g);
116         }
117         if (stringDisplayer != null) {
118             int y = y_label-2;
119             stringDisplayer.setBounds (d_check.width, y,
120                 d_label.width, getHeight()-1);
121             g.translate (d_check.width, y_label);
122             stringDisplayer.paint(g);
123             g.translate (-d_check.width, -y_label);
124         }
125     }
126     
127     public Dimension getPreferredSize() {
128         if (stringDisplayer != null) {
129             stringDisplayer.setFont(getFont());
130         }
131         Dimension d_check = check == null ? new Dimension(0, checkDim.height) :
132             check.getPreferredSize();
133             
134         Dimension d_label = stringDisplayer != null ?
135             stringDisplayer.getPreferredSize() : new Dimension(0,0);
136             
137         return new Dimension(d_check.width + d_label.width, (d_check.height < d_label.height ? d_label.height : d_check.height));
138     }
139     
140     public void doLayout() {
141         Dimension d_check = check == null ? new Dimension(0, 0) : check.getPreferredSize();
142         Dimension d_label = stringDisplayer == null ? new Dimension (0,0) : stringDisplayer.getPreferredSize();
143         int y_check = 0;
144         int y_label = 0;
145         
146         if (d_check.height < d_label.height)
147             y_check = (d_label.height - d_check.height) / 2;
148         else
149             y_label = (d_check.height - d_label.height) / 2;
150
151         if (check != null) {
152             check.setLocation(0, y_check);
153             check.setBounds(0, y_check, d_check.width, d_check.height);
154             if (checkBounds == null)
155                 checkBounds = check.getBounds();
156         }
157     }
158
159     public static Rectangle getCheckBoxRectangle() {
160         return (Rectangle) checkBounds.clone();
161     }
162 }
163
Popular Tags