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: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.refactoring.spi.impl;
042:
043: import java.awt.*;
044: import javax.swing.*;
045: import javax.swing.tree.*;
046: import org.openide.awt.HtmlRenderer;
047: import org.openide.util.NbBundle;
048:
049: /**
050: * @author Pavel Flaska
051: */
052: public class CheckRenderer extends JPanel implements TreeCellRenderer {
053:
054: protected JCheckBox check;
055: protected HtmlRenderer.Renderer renderer = HtmlRenderer
056: .createRenderer();
057: private static Dimension checkDim;
058:
059: static Rectangle checkBounds;
060:
061: static {
062: Dimension old = new JCheckBox().getPreferredSize();
063: checkDim = new Dimension(old.width, old.height - 5);
064: }
065:
066: public CheckRenderer(boolean isQuery) {
067: setLayout(null);
068: if (isQuery) {
069: check = null;
070: } else {
071: add(check = new JCheckBox());
072: Color c = UIManager.getColor("Tree.textBackground"); //NOI18N
073: if (c == null) {
074: //May be null on GTK L&F
075: c = Color.WHITE;
076: }
077: check.setBackground(c); // NOI18N
078: Dimension dim = check.getPreferredSize();
079: check.setPreferredSize(checkDim);
080: }
081: }
082:
083: /** The component returned by HtmlRenderer.Renderer.getTreeCellRendererComponent() */
084: private Component stringDisplayer = new JLabel(" "); //NOI18N
085:
086: public Component getTreeCellRendererComponent(JTree tree,
087: Object value, boolean isSelected, boolean expanded,
088: boolean leaf, int row, boolean hasFocus) {
089: CheckNode node = (CheckNode) value;
090: stringDisplayer = renderer.getTreeCellRendererComponent(tree,
091: getNodeText(node), isSelected, expanded, leaf, row,
092: hasFocus);
093:
094: renderer.setIcon(node.getIcon());
095: stringDisplayer.setEnabled(!node.isDisabled());
096: String toolTip = node.getToolTip();
097: if (toolTip != null)
098: setToolTipText("<html>" + node.getToolTip() + "</html>"); // NOI18N
099:
100: //HtmlRenderer does not tolerate null colors - real ones are needed to
101: //ensure fg/bg always diverge enough to be readable
102: if (stringDisplayer.getBackground() == null) {
103: stringDisplayer.setBackground(tree.getBackground());
104: }
105: if (stringDisplayer.getForeground() == null) {
106: stringDisplayer.setForeground(tree.getForeground());
107: }
108:
109: if (check != null) {
110: check.setSelected(node.isSelected());
111: check.setEnabled(!node.isDisabled());
112: }
113: return this ;
114: }
115:
116: public void paintComponent(Graphics g) {
117: Dimension d_check = check == null ? new Dimension(0, 0) : check
118: .getSize();
119: Dimension d_label = stringDisplayer == null ? new Dimension(0,
120: 0) : stringDisplayer.getPreferredSize();
121:
122: int y_check = 0;
123: int y_label = 0;
124:
125: if (d_check.height >= d_label.height) {
126: y_label = (d_check.height - d_label.height) / 2;
127: }
128: if (check != null) {
129: check.setBounds(0, 0, d_check.width, d_check.height);
130: check.paint(g);
131: }
132: if (stringDisplayer != null) {
133: int y = y_label - 2;
134: stringDisplayer.setBounds(d_check.width, y, d_label.width,
135: getHeight() - 1);
136: g.translate(d_check.width, y_label);
137: stringDisplayer.paint(g);
138: g.translate(-d_check.width, -y_label);
139: }
140: }
141:
142: private String getNodeText(CheckNode node) {
143: String nodeLabel = node.getLabel() == null ? NbBundle
144: .getMessage(CheckRenderer.class, "LBL_NotAvailable")
145: : node.getLabel();
146: nodeLabel = "<html>" + nodeLabel; // NOI18N
147: if (node.needsRefresh()) {
148: nodeLabel += " - "
149: + NbBundle.getMessage(RefactoringPanel.class,
150: "LBL_NeedsRefresh");
151: }
152: nodeLabel += "</html>"; // NOI18N
153: int i = nodeLabel.indexOf("<br>"); // NOI18N
154: if (i != -1) {
155: return nodeLabel.substring(0, i) + "</html>"; // NOI18N
156: } else {
157: return nodeLabel;
158: }
159: }
160:
161: public Dimension getPreferredSize() {
162: if (stringDisplayer != null) {
163: stringDisplayer.setFont(getFont());
164: }
165: Dimension d_check = check == null ? new Dimension(0,
166: checkDim.height) : check.getPreferredSize();
167:
168: Dimension d_label = stringDisplayer != null ? stringDisplayer
169: .getPreferredSize() : new Dimension(0, 0);
170:
171: return new Dimension(d_check.width + d_label.width,
172: (d_check.height < d_label.height ? d_label.height
173: : d_check.height));
174: }
175:
176: public void doLayout() {
177: Dimension d_check = check == null ? new Dimension(0, 0) : check
178: .getPreferredSize();
179: Dimension d_label = stringDisplayer == null ? new Dimension(0,
180: 0) : stringDisplayer.getPreferredSize();
181: int y_check = 0;
182: int y_label = 0;
183:
184: if (d_check.height < d_label.height)
185: y_check = (d_label.height - d_check.height) / 2;
186: else
187: y_label = (d_check.height - d_label.height) / 2;
188:
189: if (check != null) {
190: check.setLocation(0, y_check);
191: check.setBounds(0, y_check, d_check.width, d_check.height);
192: if (checkBounds == null)
193: checkBounds = check.getBounds();
194: }
195: }
196:
197: public static Rectangle getCheckBoxRectangle() {
198: return (Rectangle) checkBounds.clone();
199: }
200: }
|