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.palette.ui;
042:
043: import java.awt.*;
044: import javax.swing.*;
045: import javax.swing.tree.*;
046: import org.netbeans.modules.palette.DefaultSettings;
047: import org.openide.explorer.view.NodeRenderer;
048: import org.openide.explorer.view.Visualizer;
049: import org.openide.nodes.Node;
050:
051: /**
052: * @author Pavel Flaska, S. Aubrecht
053: */
054: class CheckRenderer extends JPanel implements TreeCellRenderer {
055:
056: protected JCheckBox check;
057: private NodeRenderer nodeRenderer;
058: private DefaultSettings settings;
059: private static Dimension checkDim;
060:
061: static Rectangle checkBounds;
062:
063: static {
064: Dimension old = new JCheckBox().getPreferredSize();
065: checkDim = new Dimension(old.width, old.height - 5);
066: }
067:
068: public CheckRenderer(DefaultSettings settings) {
069: this .nodeRenderer = new NodeRenderer();
070: this .settings = settings;
071: setLayout(null);
072: add(check = new JCheckBox());
073: Color c = UIManager.getColor("Tree.textBackground"); //NOI18N
074: if (c == null) {
075: //May be null on GTK L&F
076: c = Color.WHITE;
077: }
078: check.setBackground(c); // NOI18N
079: Dimension dim = check.getPreferredSize();
080: check.setPreferredSize(checkDim);
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: stringDisplayer = nodeRenderer.getTreeCellRendererComponent(
090: tree, value, isSelected, expanded, leaf, row, hasFocus);
091:
092: TreePath path = tree.getPathForRow(row);
093: if (null != path && 1 == path.getPathCount()) {
094: //do not show checkbox for the root node
095: return stringDisplayer;
096: }
097:
098: if (stringDisplayer instanceof JComponent) {
099: setToolTipText(((JComponent) stringDisplayer)
100: .getToolTipText());
101: }
102:
103: //HtmlRenderer does not tolerate null colors - real ones are needed to
104: //ensure fg/bg always diverge enough to be readable
105: if (stringDisplayer.getBackground() == null) {
106: stringDisplayer.setBackground(tree.getBackground());
107: }
108: if (stringDisplayer.getForeground() == null) {
109: stringDisplayer.setForeground(tree.getForeground());
110: }
111:
112: if (check != null) {
113: Node node;
114: if (value instanceof Node) {
115: node = (Node) value;
116: } else {
117: node = Visualizer.findNode(value);
118: }
119: check.setSelected(null == node
120: || settings.isNodeVisible(node));//node.isSelected());
121: check.setEnabled(true);//!node.isDisabled());
122: }
123: return this ;
124: }
125:
126: public void paintComponent(Graphics g) {
127: Dimension d_check = check == null ? new Dimension(0, 0) : check
128: .getSize();
129: Dimension d_label = stringDisplayer == null ? new Dimension(0,
130: 0) : stringDisplayer.getPreferredSize();
131:
132: int y_check = 0;
133: int y_label = 0;
134:
135: if (d_check.height >= d_label.height) {
136: y_label = (d_check.height - d_label.height) / 2;
137: }
138: if (check != null) {
139: check.setBounds(0, 0, d_check.width, d_check.height);
140: check.paint(g);
141: }
142: if (stringDisplayer != null) {
143: int y = y_label - 2;
144: stringDisplayer.setBounds(d_check.width, y, d_label.width,
145: getHeight() - 1);
146: g.translate(d_check.width, y_label);
147: stringDisplayer.paint(g);
148: g.translate(-d_check.width, -y_label);
149: }
150: }
151:
152: public Dimension getPreferredSize() {
153: if (stringDisplayer != null) {
154: stringDisplayer.setFont(getFont());
155: }
156: Dimension d_check = check == null ? new Dimension(0,
157: checkDim.height) : check.getPreferredSize();
158:
159: Dimension d_label = stringDisplayer != null ? stringDisplayer
160: .getPreferredSize() : new Dimension(0, 0);
161:
162: return new Dimension(d_check.width + d_label.width,
163: (d_check.height < d_label.height ? d_label.height
164: : d_check.height));
165: }
166:
167: public void doLayout() {
168: Dimension d_check = check == null ? new Dimension(0, 0) : check
169: .getPreferredSize();
170: Dimension d_label = stringDisplayer == null ? new Dimension(0,
171: 0) : stringDisplayer.getPreferredSize();
172: int y_check = 0;
173: int y_label = 0;
174:
175: if (d_check.height < d_label.height)
176: y_check = (d_label.height - d_check.height) / 2;
177: else
178: y_label = (d_check.height - d_label.height) / 2;
179:
180: if (check != null) {
181: check.setLocation(0, y_check);
182: check.setBounds(0, y_check, d_check.width, d_check.height);
183: if (checkBounds == null)
184: checkBounds = check.getBounds();
185: }
186: }
187:
188: public static Rectangle getCheckBoxRectangle() {
189: return (Rectangle) checkBounds.clone();
190: }
191: }
|