00001 /* 00002 * %W% %E% 00003 * 00004 * Copyright 1997, 1998 Sun Microsystems, Inc. All Rights Reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or 00007 * without modification, are permitted provided that the following 00008 * conditions are met: 00009 * 00010 * - Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * 00013 * - Redistribution in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials 00016 * provided with the distribution. 00017 * 00018 * Neither the name of Sun Microsystems, Inc. or the names of 00019 * contributors may be used to endorse or promote products derived 00020 * from this software without specific prior written permission. 00021 * 00022 * This software is provided "AS IS," without a warranty of any 00023 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND 00024 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, 00025 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY 00026 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY 00027 * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR 00028 * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR 00029 * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE 00030 * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, 00031 * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER 00032 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF 00033 * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS 00034 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 00035 * 00036 * You acknowledge that this software is not designed, licensed or 00037 * intended for use in the design, construction, operation or 00038 * maintenance of any nuclear facility. 00039 */ 00040 00041 package GuiComponents.Inspector.TreeTable; 00042 00043 import javax.swing.*; 00044 import javax.swing.tree.*; 00045 import javax.swing.event.*; 00046 import javax.swing.table.*; 00047 00048 import java.awt.Dimension; 00049 import java.awt.Component; 00050 import java.awt.Graphics; 00051 00063 public class JTreeTable extends JTable { 00064 protected TreeTableCellRenderer tree; 00065 00066 public JTreeTable(TreeTableModel treeTableModel) { 00067 super(); 00068 00069 // Create the tree. It will be used as a renderer and editor. 00070 tree = new TreeTableCellRenderer(treeTableModel); 00071 00072 // Install a tableModel representing the visible rows in the tree. 00073 super.setModel(new TreeTableModelAdapter(treeTableModel, tree)); 00074 00075 // Force the JTable and JTree to share their row selection models. 00076 tree.setSelectionModel(new DefaultTreeSelectionModel() { 00077 // Extend the implementation of the constructor, as if: 00078 /* public this() */ { 00079 setSelectionModel(listSelectionModel); 00080 } 00081 }); 00082 // Make the tree and table row heights the same. 00083 tree.setRowHeight(getRowHeight()); 00084 00085 // Install the tree editor renderer and editor. 00086 setDefaultRenderer(TreeTableModel.class, tree); 00087 setDefaultEditor(TreeTableModel.class, new TreeTableCellEditor()); 00088 00089 setShowGrid(false); 00090 setIntercellSpacing(new Dimension(0, 0)); 00091 } 00092 00093 /* Workaround for BasicTableUI anomaly. Make sure the UI never tries to 00094 * paint the editor. The UI currently uses different techniques to 00095 * paint the renderers and editors and overriding setBounds() below 00096 * is not the right thing to do for an editor. Returning -1 for the 00097 * editing row in this case, ensures the editor is never painted. 00098 */ 00099 public int getEditingRow() { 00100 return (getColumnClass(editingColumn) == TreeTableModel.class) ? -1 : editingRow; 00101 } 00102 00103 // 00104 // The renderer used to display the tree nodes, a JTree. 00105 // 00106 00107 public class TreeTableCellRenderer extends JTree implements TableCellRenderer { 00108 00109 protected int visibleRow; 00110 00111 public TreeTableCellRenderer(TreeModel model) { 00112 super(model); 00113 } 00114 00115 public void setBounds(int x, int y, int w, int h) { 00116 super.setBounds(x, 0, w, JTreeTable.this.getHeight()); 00117 } 00118 00119 public void paint(Graphics g) { 00120 g.translate(0, -visibleRow * getRowHeight()); 00121 super.paint(g); 00122 } 00123 00124 public Component getTableCellRendererComponent(JTable table, 00125 Object value, 00126 boolean isSelected, 00127 boolean hasFocus, 00128 int row, int column) { 00129 if(isSelected) 00130 setBackground(table.getSelectionBackground()); 00131 else 00132 setBackground(table.getBackground()); 00133 00134 visibleRow = row; 00135 return this; 00136 } 00137 } 00138 00139 // 00140 // The editor used to interact with tree nodes, a JTree. 00141 // 00142 00143 public class TreeTableCellEditor extends AbstractCellEditor implements TableCellEditor { 00144 public Component getTableCellEditorComponent(JTable table, Object value, 00145 boolean isSelected, int r, int c) { 00146 return tree; 00147 } 00148 00153 /*public Object getCellEditorValue() { 00154 return tree; 00155 }*/ 00156 00157 } 00158 00159 } 00160