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:
042: /*
043: * Created on Jun 9, 2003
044: *
045: */
046: package org.netbeans.modules.uml.ui.swing.treetable;
047:
048: import java.awt.Color;
049: import java.awt.Component;
050: import java.awt.Graphics;
051:
052: import javax.swing.BorderFactory;
053: import javax.swing.JTable;
054: import javax.swing.JTree;
055: import javax.swing.UIManager;
056: import javax.swing.table.TableCellRenderer;
057: import javax.swing.tree.DefaultTreeCellRenderer;
058: import javax.swing.tree.TreeCellRenderer;
059: import javax.swing.tree.TreeModel;
060:
061: /**
062: * @author sumitabhk
063: *
064: */
065: /**
066: * A TreeCellRenderer that displays a JTree.
067: */
068: public class TreeTableCellRenderer extends JTree implements
069: TableCellRenderer {
070:
071: /**
072: *
073: */
074: public TreeTableCellRenderer() {
075: super ();
076: }
077:
078: /** Last table/tree row asked to renderer. */
079: protected int visibleRow;
080:
081: public TreeTableCellRenderer(TreeModel model) {
082: super (model);
083: setBorder(BorderFactory.createLineBorder(Color.BLACK));
084: this .putClientProperty("JTree.lineStyle", "Angled");
085: setShowsRootHandles(true);
086:
087: /*this.addTreeSelectionListener( new TreeSelectionListener() {
088:
089: public void valueChanged(TreeSelectionEvent e)
090: {
091: // TODO Auto-generated method stub
092: ETSystem.out.println("Value changed for tree selection");
093: }
094: }
095: );*/
096:
097: // this.addTreeExpansionListener( new TreeExpansionListener() {
098: //
099: // public void treeExpanded(TreeExpansionEvent event)
100: // {
101: // // TODO Auto-generated method stub
102: // ETSystem.out.println("Tree expanded");
103: //
104: // }
105: //
106: // public void treeCollapsed(TreeExpansionEvent event)
107: // {
108: // // TODO Auto-generated method stub
109: // ETSystem.out.println("Tree collapsed");
110: //
111: // }
112: // }
113: // );
114: }
115:
116: /**
117: * updateUI is overridden to set the colors of the Tree's renderer
118: * to match that of the table.
119: */
120: public void updateUI() {
121: super .updateUI();
122: // Make the tree's cell renderer use the table's cell selection
123: // colors.
124: TreeCellRenderer tcr = getCellRenderer();
125: if (tcr instanceof DefaultTreeCellRenderer) {
126: DefaultTreeCellRenderer dtcr = ((DefaultTreeCellRenderer) tcr);
127: // For 1.1 uncomment this, 1.2 has a bug that will cause an
128: // exception to be thrown if the border selection color is
129: // null.
130: //dtcr.setBorderSelectionColor(null);
131: dtcr.setTextSelectionColor(UIManager
132: .getColor("Table.selectionForeground"));
133: dtcr.setBackgroundSelectionColor(UIManager
134: .getColor("Table.selectionBackground"));
135: }
136: }
137:
138: /**
139: * Sets the row height of the tree, and forwards the row height to
140: * the table.
141: */
142: public void setRowHeight(int rowHeight) {
143: if (rowHeight > 0) {
144: super .setRowHeight(rowHeight);
145: if (this != null && this .getRowHeight() != rowHeight) {
146: this .setRowHeight(getRowHeight());
147: }
148: }
149: }
150:
151: /**
152: * This is overridden to set the height to match that of the JTable.
153: */
154: public void setBounds(int x, int y, int w, int h) {
155: super .setBounds(x, 0, w, this .getHeight());
156: }
157:
158: /**
159: * Sublcassed to translate the graphics such that the last visible
160: * row will be drawn at 0,0.
161: */
162: public void paint(Graphics g) {
163: g.translate(0, -visibleRow * getRowHeight());
164:
165: super .paint(g);
166: }
167:
168: /**
169: * TreeCellRenderer method. Overridden to update the visible row.
170: */
171: public Component getTableCellRendererComponent(JTable table,
172: Object value, boolean isSelected, boolean hasFocus,
173: int row, int column) {
174: this .putClientProperty("JTree.lineStyle", "Angled");
175: setShowsRootHandles(true);
176: if (isSelected) {
177: setBackground(table.getSelectionBackground());
178:
179: } else
180: setBackground(table.getBackground());
181:
182: visibleRow = row;
183: return this;
184: }
185:
186: }
|