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:
042: package org.netbeans.modules.mobility.project.ui.customizer.regex;
043:
044: import org.openide.explorer.view.Visualizer;
045: import org.openide.nodes.Node;
046:
047: import javax.swing.*;
048: import javax.swing.tree.TreeCellRenderer;
049: import java.awt.*;
050: import java.awt.event.ItemListener;
051: import java.beans.BeanInfo;
052:
053: /**
054: * User: suchys
055: * Date: Dec 20, 2003
056: * Time: 12:30:14 PM
057: */
058: public class CheckedNodeRenderer implements TreeCellRenderer {
059: private RendererComponent customRenderer = null;
060: private Object defaultRenderer = null;
061: private CheckedTreeBeanView storage;
062: private Color selectionForeground, selectionBackground,
063: textForeground;//, textBackground;
064:
065: protected RendererComponent getRenderer() {
066: return customRenderer;
067: }
068:
069: public CheckedNodeRenderer(Object defaultRenderer) {
070: this .defaultRenderer = defaultRenderer;
071: selectionForeground = UIManager
072: .getColor("Tree.selectionForeground"); //NOI18N
073: selectionBackground = UIManager
074: .getColor("Tree.selectionBackground"); //NOI18N
075: textForeground = UIManager.getColor("Tree.textForeground"); //NOI18N
076: // textBackground = UIManager.getColor("Tree.textBackground");
077: customRenderer = new RendererComponent();
078: Font fontValue = UIManager.getFont("Tree.font"); //NOI18N
079: if (fontValue != null) {
080: customRenderer.setFont(fontValue);
081: }
082: Boolean booleanValue = (Boolean) UIManager
083: .get("Tree.drawsFocusBorderAroundIcon"); //NOI18N
084: customRenderer.setFocusPainted((booleanValue != null)
085: && (booleanValue.booleanValue()));
086: }
087:
088: public CheckedNodeRenderer() {
089: this (null);
090: }
091:
092: public void setContentStorage(final CheckedTreeBeanView storage) {
093: this .storage = storage;
094: }
095:
096: public Component getTreeCellRendererComponent(final JTree tree,
097: final Object value, final boolean selected,
098: final boolean expanded, final boolean leaf, final int row,
099: final boolean hasFocus) {
100: Component returnValue = null;
101: final Node node = Visualizer.findNode(value);
102: final FileObjectCookie doj = (FileObjectCookie) node
103: .getCookie(FileObjectCookie.class);
104: if (doj != null) {
105: //String stringValue = tree.convertValueToText(value, selected, expanded, leaf, row, false);
106: customRenderer.setEnabled(tree.isEnabled());
107:
108: if (selected) {
109: customRenderer.setForeground(selectionForeground,
110: textForeground);
111: customRenderer.setBackground(selectionBackground, tree
112: .getBackground());
113: } else {
114: customRenderer.setForeground(textForeground);
115: customRenderer.setBackground(tree.getBackground());
116: }
117: customRenderer.setText(node.getDisplayName());
118: if (doj != null) {
119: if (storage == null)
120: customRenderer
121: .setState(CheckedTreeBeanView.UNSELECTED);
122: else {
123: final Object state = storage.getState(doj
124: .getFileObject());
125: customRenderer
126: .setState(state == null ? CheckedTreeBeanView.UNSELECTED
127: : state);
128: }
129: }
130:
131: customRenderer.setIcon(new ImageIcon(node
132: .getIcon(BeanInfo.ICON_COLOR_16x16)));
133: return customRenderer;
134: }
135: if (defaultRenderer != null)
136: returnValue = ((TreeCellRenderer) defaultRenderer)
137: .getTreeCellRendererComponent(tree, value,
138: selected, expanded, leaf, row, hasFocus);
139: return returnValue;
140: }
141:
142: static class RendererComponent extends JPanel {
143: MultiStateCheckBox jCheckBox1;
144: final private JLabel jLabel1;
145: final private JLabel jLabel2;
146:
147: public RendererComponent() {
148: jCheckBox1 = new MultiStateCheckBox();
149: jCheckBox1.setBorder(null);
150: jLabel1 = new JLabel();
151: jLabel2 = new JLabel();
152: //todo badges around icons !!!
153: setLayout(new BorderLayout(5, 0));
154: add(jCheckBox1, BorderLayout.WEST);
155: add(jLabel1, BorderLayout.CENTER);
156: add(jLabel2, BorderLayout.EAST);
157: }
158:
159: public void setForeground(final Color fg) {
160: setForeground(fg, fg);
161: }
162:
163: public void setBackground(final Color bg) {
164: setBackground(bg, bg);
165: }
166:
167: public void setForeground(final Color selection,
168: final Color text) {
169: if (jCheckBox1 == null || jLabel1 == null
170: || jLabel2 == null)
171: return;
172: jCheckBox1.setForeground(text);
173: jLabel1.setForeground(text);
174: jLabel2.setForeground(selection);
175: super .setForeground(selection);
176: }
177:
178: public void setBackground(final Color selection,
179: final Color text) {
180: if (jCheckBox1 == null || jLabel1 == null
181: || jLabel2 == null)
182: return;
183: //System.err.println(jCheckBox1);
184: jCheckBox1.setBackground(text);
185: jLabel1.setBackground(text);
186: jLabel2.setBackground(selection);
187: super .setBackground(selection);
188: }
189:
190: public void setFocusPainted(final boolean painted) {
191: jCheckBox1.setFocusPainted(painted);
192: }
193:
194: public void setText(final String text) {
195: jLabel2.setText(text);
196: }
197:
198: public String getText() {
199: return jLabel2.getText();
200: }
201:
202: public void setState(final Object state) {
203: jCheckBox1.setState(state);
204: }
205:
206: public void setIcon(final Icon icon) {
207: //System.err.println(icon);
208: jLabel1.setIcon(icon);
209: }
210:
211: public void addItemListener(final ItemListener itemListener) {
212: jCheckBox1.addItemListener(itemListener);
213: }
214:
215: public void removeItemListener(final ItemListener itemListener) {
216: jCheckBox1.removeItemListener(itemListener);
217: }
218: }
219:
220: }
|