KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > refactoring > ui > j > 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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.refactoring.ui.j.ui;
20
21 import java.awt.*;
22 import java.util.regex.Matcher JavaDoc;
23 import java.util.regex.Pattern JavaDoc;
24 import javax.swing.*;
25 import javax.swing.tree.*;
26 import org.netbeans.modules.xml.xam.Named;
27 import org.openide.awt.HtmlRenderer;
28 import org.openide.util.NbBundle;
29
30 /**
31  * @author Pavel Flaska
32  */

33 public class CheckRenderer extends JPanel implements TreeCellRenderer {
34     public static final long serialVersionUID = 1L;
35
36     protected JCheckBox check;
37     protected HtmlRenderer.Renderer renderer = HtmlRenderer.createRenderer();
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(boolean isQuery) {
48         setLayout(null);
49         if (isQuery) {
50             check = null;
51         } else {
52             add(check = new JCheckBox());
53             Color c = UIManager.getColor("Tree.textBackground"); //NOI18N
54
if (c == null) {
55                 //May be null on GTK L&F
56
c = Color.WHITE;
57             }
58             check.setBackground(c); // NOI18N
59
check.setPreferredSize(checkDim);
60         }
61     }
62     
63     /** The component returned by HtmlRenderer.Renderer.getTreeCellRendererComponent() */
64     private Component stringDisplayer = new JLabel(" "); //NOI18N
65

66     public Component getTreeCellRendererComponent(JTree tree, Object JavaDoc value,
67             boolean isSelected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
68         CheckNode node = (CheckNode) value;
69         stringDisplayer = renderer.getTreeCellRendererComponent(tree,
70                 getNodeText(node), isSelected, expanded, leaf, row, hasFocus);
71         
72         renderer.setIcon(node.getIcon());
73         stringDisplayer.setEnabled(!node.isDisabled());
74         setToolTipText(node.getToolTip());
75         
76         //HtmlRenderer does not tolerate null colors - real ones are needed to
77
//ensure fg/bg always diverge enough to be readable
78
if (stringDisplayer.getBackground() == null) {
79             stringDisplayer.setBackground(tree.getBackground());
80         }
81         if (stringDisplayer.getForeground() == null) {
82             stringDisplayer.setForeground(tree.getForeground());
83         }
84         
85         if (check != null) {
86             check.setSelected(node.isSelected());
87             check.setEnabled(!node.isDisabled());
88             // The checkbox for the target of the refactoring is always
89
// checked and disabled
90
if (!node.getCheckEnabled()){
91                 check.setEnabled(false);
92                 check.setSelected(true);
93             }
94             
95         }
96         return this;
97     }
98     
99     public void paintComponent(Graphics g) {
100         Dimension d_check = check == null ? new Dimension(0, 0) : check.getSize();
101         Dimension d_label = stringDisplayer == null ? new Dimension(0,0) :
102             stringDisplayer.getPreferredSize();
103         
104         int y_check = 0;
105         int y_label = 0;
106         
107         if (d_check.height >= d_label.height) {
108             y_label = (d_check.height - d_label.height) / 2;
109         }
110         if (check != null) {
111             check.setBounds(0, 0, d_check.width, d_check.height);
112             check.paint(g);
113         }
114         if (stringDisplayer != null) {
115             int y = y_label-2;
116             stringDisplayer.setBounds(d_check.width, y,
117                     d_label.width, getHeight()-1);
118             g.translate(d_check.width, y_label);
119             stringDisplayer.paint(g);
120             g.translate(-d_check.width, -y_label);
121         }
122     }
123     
124     
125     /**
126      * Replace XML angle brackets < and > with &lt and &gt
127      * and surround text with <html> tags
128      *
129      */

130     private String JavaDoc getNodeText(CheckNode node) {
131         String JavaDoc nodeLabel = node.getLabel() == null ? NbBundle.getMessage(CheckRenderer.class,"LBL_NotAvailable") : node.getLabel();
132         
133         
134         nodeLabel = "<html>" + nodeLabel; // NOI18N
135
if (node.needsRefresh()) {
136             nodeLabel += " - " + NbBundle.getMessage(RefactoringPanel.class, "LBL_NeedsRefresh");
137         }
138         nodeLabel += "</html>"; // NOI18N
139
return nodeLabel;
140     }
141     
142     public Dimension getPreferredSize() {
143         if (stringDisplayer != null) {
144             stringDisplayer.setFont(getFont());
145         }
146         Dimension d_check = check == null ? new Dimension(0, checkDim.height) :
147             check.getPreferredSize();
148         
149         Dimension d_label = stringDisplayer != null ?
150             stringDisplayer.getPreferredSize() : new Dimension(0,0);
151         
152         return new Dimension(d_check.width + d_label.width, (d_check.height < d_label.height ? d_label.height : d_check.height));
153     }
154     
155     public void doLayout() {
156         Dimension d_check = check == null ? new Dimension(0, 0) : check.getPreferredSize();
157         Dimension d_label = stringDisplayer == null ? new Dimension(0,0) : stringDisplayer.getPreferredSize();
158         int y_check = 0;
159         int y_label = 0;
160         
161         if (d_check.height < d_label.height)
162             y_check = (d_label.height - d_check.height) / 2;
163         else
164             y_label = (d_check.height - d_label.height) / 2;
165         
166         if (check != null) {
167             check.setLocation(0, y_check);
168             check.setBounds(0, y_check, d_check.width, d_check.height);
169             if (checkBounds == null)
170                 checkBounds = check.getBounds();
171         }
172     }
173     
174     public static Rectangle getCheckBoxRectangle() {
175         return (Rectangle) checkBounds.clone();
176     }
177 }
178
Popular Tags