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-2007 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.sql.framework.ui.view;
042:
043: import java.awt.Color;
044: import java.awt.Component;
045: import java.awt.GridBagConstraints;
046: import java.awt.GridBagLayout;
047: import java.awt.Point;
048:
049: import javax.swing.BorderFactory;
050: import javax.swing.ImageIcon;
051: import javax.swing.JCheckBox;
052: import javax.swing.JLabel;
053: import javax.swing.JPanel;
054: import javax.swing.JTree;
055: import javax.swing.UIManager;
056: import javax.swing.tree.DefaultTreeCellRenderer;
057: import javax.swing.tree.TreeCellRenderer;
058:
059: /**
060: * @author Jonathan Giron
061: * @version $Revision$
062: */
063: public class TableColumnTreeCellRenderer extends JPanel implements
064: TreeCellRenderer {
065:
066: /** Color to use for the background when the node isn't selected. */
067: protected Color backgroundNonSelectionColor;
068:
069: /** Color to use for the background when a node is selected. */
070: protected Color backgroundSelectionColor;
071:
072: /** Color to use for the focus indicator when the node has focus. */
073: protected Color borderSelectionColor;
074:
075: /** True if has focus. */
076: protected boolean hasFocus;
077:
078: /** Is the value currently selected. */
079: protected boolean selected;
080:
081: /** Color to use for the foreground for non-selected nodes. */
082: protected Color textNonSelectionColor;
083:
084: /** Color to use for the foreground for selected nodes. */
085: protected Color textSelectionColor;
086:
087: /** Checkbox indicating selection state */
088: private JCheckBox checkBox;
089:
090: private JTree tree;
091:
092: /**
093: * Constructor TableColumnTreeCellRenderer constructs this object.
094: */
095: public TableColumnTreeCellRenderer() {
096: super ();
097:
098: setTextSelectionColor(UIManager
099: .getColor("Tree.selectionForeground"));
100: setTextNonSelectionColor(UIManager
101: .getColor("Tree.textForeground"));
102: setBackgroundSelectionColor(UIManager
103: .getColor("Tree.selectionBackground"));
104: setBackgroundNonSelectionColor(UIManager
105: .getColor("Tree.textBackground"));
106: setBorderSelectionColor(UIManager
107: .getColor("Tree.selectionBorderColor"));
108:
109: checkBox = new JCheckBox();
110: checkBox.setBorder(BorderFactory.createEmptyBorder(2, 1, 2, 2));
111: }
112:
113: /**
114: * Returns the background color to be used for non selected nodes.
115: *
116: * @return Color for the nonselection color
117: */
118: public Color getBackgroundNonSelectionColor() {
119: return backgroundNonSelectionColor;
120: }
121:
122: /**
123: * Returns the color to use for the background if node is selected.
124: *
125: * @return Color is the new background selection color.
126: */
127: public Color getBackgroundSelectionColor() {
128: return backgroundSelectionColor;
129: }
130:
131: /**
132: * Returns the color the border is drawn.
133: *
134: * @return Color of the border selection
135: */
136: public Color getBorderSelectionColor() {
137: return borderSelectionColor;
138: }
139:
140: /**
141: * Returns the color the text is drawn with when the node isn't selected.
142: *
143: * @return Color of the text non selection.
144: */
145: public Color getTextNonSelectionColor() {
146: return textNonSelectionColor;
147: }
148:
149: /**
150: * Returns the color the text is drawn with when the node is selected.
151: *
152: * @return Color of text selection
153: */
154: public Color getTextSelectionColor() {
155: return textSelectionColor;
156: }
157:
158: /**
159: * Sets the value of the current tree cell to <code>value</code>. If
160: * <code>selected</code> is true, the cell will be drawn as if selected. If
161: * <code>expanded</code> is true the node is currently expanded and if
162: * <code>leaf</code> is true the node represets a leaf and if <code>hasFocus</code>
163: * is true the node currently has focus. <code>tree</code> is the <code>JTree</code>
164: * the receiver is being configured for. Returns the <code>Component</code> that the
165: * renderer uses to draw the value.
166: *
167: * @param theTree is the tree whose renderer component is requested
168: * @param value is the object that is to be in focus
169: * @param selected1 is true if the object is selected
170: * @param expanded is true if the object is expanded
171: * @param leaf is true if the object is a leaf node
172: * @param row is the row of the object
173: * @param hasFocus1 is true if the object has focus
174: * @return the <code>Component</code> that the renderer uses to draw the value
175: */
176: public Component getTreeCellRendererComponent(JTree theTree,
177: Object value, boolean selected1, boolean expanded,
178: boolean leaf, int row, boolean hasFocus1) {
179: setTree(theTree);
180: this .selected = selected1;
181: this .hasFocus = hasFocus1;
182:
183: DefaultTreeCellRenderer dtc = new DefaultTreeCellRenderer();
184: dtc.setComponentOrientation(tree.getComponentOrientation());
185:
186: JLabel label = (JLabel) dtc.getTreeCellRendererComponent(
187: theTree, value, selected1, expanded, leaf, row,
188: hasFocus1);
189:
190: TableColumnNode node = null;
191: if (value instanceof TableColumnNode) {
192: node = (TableColumnNode) value;
193: }
194: if (node == null) {
195: return label;
196: }
197:
198: this .removeAll();
199: this .setLayout(new GridBagLayout());
200: this .setBackground(label.getBackground());
201: checkBox.setBackground(label.getBackground());
202:
203: GridBagConstraints gc = new GridBagConstraints();
204: gc.weightx = 0.0;
205: gc.weighty = 0.0;
206: gc.anchor = GridBagConstraints.LINE_START;
207: gc.fill = GridBagConstraints.NONE;
208: gc.gridwidth = 1;
209: this .add(checkBox, gc);
210:
211: gc = new GridBagConstraints();
212: gc.weightx = 1.0;
213: gc.weighty = 0.0;
214: gc.anchor = GridBagConstraints.LINE_START;
215: gc.fill = GridBagConstraints.HORIZONTAL;
216: gc.gridwidth = 1;
217: this .add(label, gc);
218:
219: label.setText(node.getName());
220: label.setIcon(new ImageIcon(node.getIcon(0)));
221: checkBox.setSelected(node.isSelected());
222:
223: if (!node.isEnabled()) {
224: checkBox.setEnabled(false);
225: checkBox.setFocusable(false);
226: label.setEnabled(false);
227: label.setFocusable(false);
228: } else {
229: checkBox.setEnabled(true);
230: checkBox.setFocusable(true);
231: }
232:
233: this .setToolTipText(node.getToolTip());
234: return this ;
235: }
236:
237: /**
238: * method isMouseClickOnCheckBox is used to determine if the user has clicked the
239: * mouse on the checkbox.
240: *
241: * @param p is the point to check
242: * @return boolean true if the user has clicked on the checkbox.
243: */
244: public boolean isMouseClickOnCheckBox(Point p) {
245: return checkBox.contains(p);
246: }
247:
248: /**
249: * Sets the background color to be used for non selected nodes.
250: *
251: * @param newColor is the new color for background nonselection
252: */
253: public void setBackgroundNonSelectionColor(Color newColor) {
254: backgroundNonSelectionColor = newColor;
255: }
256:
257: /**
258: * Sets the color to use for the background if node is selected.
259: *
260: * @param newColor is the new background selection color to use.
261: */
262: public void setBackgroundSelectionColor(Color newColor) {
263: backgroundSelectionColor = newColor;
264: }
265:
266: /**
267: * Sets the color to use for the border.
268: *
269: * @param newColor is the new color for border selection
270: */
271: public void setBorderSelectionColor(Color newColor) {
272: borderSelectionColor = newColor;
273: }
274:
275: /**
276: * Sets the color the text is drawn with when the node isn't selected.
277: *
278: * @param newColor is the new color for text nonselection
279: */
280: public void setTextNonSelectionColor(Color newColor) {
281: textNonSelectionColor = newColor;
282: }
283:
284: /**
285: * Sets the color the text is drawn with when the node is selected.
286: *
287: * @param newColor is the new color for text selection
288: */
289: public void setTextSelectionColor(Color newColor) {
290: textSelectionColor = newColor;
291: }
292:
293: private void setTree(JTree tree) {
294: this.tree = tree;
295: }
296: }
|