KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > spi > impl > 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.refactoring.spi.impl;
20
21 import java.awt.*;
22 import javax.swing.*;
23 import javax.swing.tree.*;
24 import org.openide.awt.HtmlRenderer;
25 import org.openide.util.NbBundle;
26
27 /**
28  * @author Pavel Flaska
29  */

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

63     public Component getTreeCellRendererComponent(JTree tree, Object JavaDoc value,
64     boolean isSelected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
65         CheckNode node = (CheckNode) value;
66         stringDisplayer = renderer.getTreeCellRendererComponent(tree,
67             getNodeText(node), isSelected, expanded, leaf, row, hasFocus);
68
69         renderer.setIcon (node.getIcon());
70         stringDisplayer.setEnabled(!node.isDisabled());
71         setToolTipText("<html>"+node.getToolTip()+"</html>");
72         
73         //HtmlRenderer does not tolerate null colors - real ones are needed to
74
//ensure fg/bg always diverge enough to be readable
75
if (stringDisplayer.getBackground() == null) {
76             stringDisplayer.setBackground (tree.getBackground());
77         }
78         if (stringDisplayer.getForeground() == null) {
79             stringDisplayer.setForeground (tree.getForeground());
80         }
81
82         if (check != null) {
83             check.setSelected(node.isSelected());
84             check.setEnabled(!node.isDisabled());
85         }
86         return this;
87     }
88     
89     public void paintComponent (Graphics g) {
90         Dimension d_check = check == null ? new Dimension(0, 0) : check.getSize();
91         Dimension d_label = stringDisplayer == null ? new Dimension(0,0) :
92             stringDisplayer.getPreferredSize();
93             
94         int y_check = 0;
95         int y_label = 0;
96         
97         if (d_check.height >= d_label.height) {
98             y_label = (d_check.height - d_label.height) / 2;
99         }
100         if (check != null) {
101             check.setBounds (0, 0, d_check.width, d_check.height);
102             check.paint(g);
103         }
104         if (stringDisplayer != null) {
105             int y = y_label-2;
106             stringDisplayer.setBounds (d_check.width, y,
107                 d_label.width, getHeight()-1);
108             g.translate (d_check.width, y_label);
109             stringDisplayer.paint(g);
110             g.translate (-d_check.width, -y_label);
111         }
112     }
113     
114     private String JavaDoc getNodeText(CheckNode node) {
115         String JavaDoc nodeLabel = node.getLabel() == null ? NbBundle.getMessage(CheckRenderer.class,"LBL_NotAvailable") : node.getLabel();
116         nodeLabel = "<html>" + nodeLabel; // NOI18N
117
if (node.needsRefresh()) {
118             nodeLabel += " - " + NbBundle.getMessage(RefactoringPanel.class, "LBL_NeedsRefresh");
119         }
120         nodeLabel += "</html>"; // NOI18N
121
int i = nodeLabel.indexOf("<br>");
122         if (i!=-1) {
123             return nodeLabel.substring(0,i) +"</html>";
124         } else {
125             return nodeLabel;
126         }
127     }
128     
129     public Dimension getPreferredSize() {
130         if (stringDisplayer != null) {
131             stringDisplayer.setFont(getFont());
132         }
133         Dimension d_check = check == null ? new Dimension(0, checkDim.height) :
134             check.getPreferredSize();
135             
136         Dimension d_label = stringDisplayer != null ?
137             stringDisplayer.getPreferredSize() : new Dimension(0,0);
138             
139         return new Dimension(d_check.width + d_label.width, (d_check.height < d_label.height ? d_label.height : d_check.height));
140     }
141     
142     public void doLayout() {
143         Dimension d_check = check == null ? new Dimension(0, 0) : check.getPreferredSize();
144         Dimension d_label = stringDisplayer == null ? new Dimension (0,0) : stringDisplayer.getPreferredSize();
145         int y_check = 0;
146         int y_label = 0;
147         
148         if (d_check.height < d_label.height)
149             y_check = (d_label.height - d_check.height) / 2;
150         else
151             y_label = (d_check.height - d_label.height) / 2;
152
153         if (check != null) {
154             check.setLocation(0, y_check);
155             check.setBounds(0, y_check, d_check.width, d_check.height);
156             if (checkBounds == null)
157                 checkBounds = check.getBounds();
158         }
159     }
160
161     public static Rectangle getCheckBoxRectangle() {
162         return (Rectangle) checkBounds.clone();
163     }
164 }
165
Popular Tags