1 package net.logAnalyzer.utils.gui.treetable;
2
3 /*
4 * Copyright 1997, 1998 Sun Microsystems, Inc. All Rights Reserved. Redistribution and use in source
5 * and binary forms, with or without modification, are permitted provided that the following
6 * conditions are met: - Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer. - Redistribution in binary form must reproduce
8 * the above copyright notice, this list of conditions and the following disclaimer in the
9 * documentation and/or other materials provided with the distribution. Neither the name of Sun
10 * Microsystems, Inc. or the names of contributors may be used to endorse or promote products
11 * derived from this software without specific prior written permission. This software is provided
12 * "AS IS," without a warranty of any kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
13 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
14 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
15 * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION OR
16 * DISTRIBUTION OF THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE
17 * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL
18 * OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE
19 * USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
20 * DAMAGES. You acknowledge that this software is not designed, licensed or intended for use in the
21 * design, construction, operation or maintenance of any nuclear facility.
22 */
23
24 import javax.swing.*;
25 import javax.swing.border.BevelBorder;
26 import javax.swing.border.LineBorder;
27 import javax.swing.plaf.ComponentUI;
28 import javax.swing.plaf.TreeUI;
29 import javax.swing.plaf.basic.BasicTreeUI;
30 import javax.swing.plaf.metal.MetalTreeUI;
31 import javax.swing.tree.*;
32 import javax.swing.table.*;
33
34 import java.awt.Color;
35 import java.awt.Dimension;
36 import java.awt.Component;
37 import java.awt.Graphics;
38
39 /***
40 * This example shows how to create a simple JTreeTable component, by using a JTree as a renderer
41 * (and editor) for the cells in a particular column in the JTable.
42 * @version %I% %G%
43 * @author Philip Milne
44 * @author Scott Violet
45 */
46
47 public class JTreeTable extends JTable {
48 protected TreeTableCellRenderer tree;
49
50 public JTreeTable(TreeTableModel treeTableModel) {
51 super();
52
53 // this.setGridColor( Color.BLACK );
54
55 this.setShowGrid(false);
56 // this.setShowHorizontalLines( true );
57 this.setIntercellSpacing(new Dimension(0, 0));
58
59 // Create the tree. It will be used as a renderer and editor.
60 this.tree = new TreeTableCellRenderer(treeTableModel);
61
62 // Install a tableModel representing the visible rows in the tree.
63 super.setModel(new TreeTableModelAdapter(treeTableModel, this.tree));
64
65 // Force the JTable and JTree to share their row selection models.
66 this.tree.setSelectionModel(new DefaultTreeSelectionModel() {
67 {
68 setSelectionModel(this.listSelectionModel);
69 }
70 });
71 // Make the tree and table row heights the same.
72 this.tree.setRowHeight(getRowHeight());
73
74 // Install the tree editor renderer and editor.
75 setDefaultRenderer(TreeTableModel.class, this.tree);
76 setDefaultEditor(TreeTableModel.class, new TreeTableCellEditor());
77 }
78
79 /*
80 * Workaround for BasicTableUI anomaly. Make sure the UI never tries to paint the editor. The UI
81 * currently uses different techniques to paint the renderers and editors and overriding
82 * setBounds() below is not the right thing to do for an editor. Returning -1 for the editing
83 * row in this case, ensures the editor is never painted.
84 */
85 public int getEditingRow() {
86 return (getColumnClass(editingColumn) == TreeTableModel.class) ? -1 : editingRow;
87 }
88
89 public void setTreeCellRenderer(TreeCellRenderer treeRenderer) {
90 this.tree.setCellRenderer(treeRenderer);
91 }
92
93
94 //
95 // The renderer used to display the tree nodes, a JTree.
96 //
97
98 public class TreeTableCellRenderer extends JTree implements TableCellRenderer {
99
100 protected int visibleRow;
101
102 private final boolean initialized;
103
104 public TreeTableCellRenderer(TreeModel model) {
105 super(model);
106 this.initialized = true;
107 this.setUI(this.getUI()); // Force UI setting
108 }
109
110 /*
111 * (non-Javadoc)
112 * @see javax.swing.JTree#setUI(javax.swing.plaf.TreeUI)
113 */
114 public void setUI(TreeUI ui) {
115 if (this.initialized && !(ui instanceof TableTreeUI)) {
116 ui = new TableTreeUI(ui, JTreeTable.this.getGridColor());
117 }
118
119 super.setUI(ui);
120 }
121
122 public void setBounds(int x, int y, int w, int h) {
123 super.setBounds(x, 0, w, JTreeTable.this.getHeight());
124 }
125
126 public void paint(Graphics g) {
127 g.translate(0, -visibleRow * getRowHeight());
128 super.paint(g);
129 }
130
131 public Component getTableCellRendererComponent(JTable table, Object value,
132 boolean isSelected, boolean hasFocus, int row, int column) {
133 if (isSelected)
134 setBackground(table.getSelectionBackground());
135 else
136 setBackground(table.getBackground());
137 setBorder(BorderFactory.createLineBorder(JTreeTable.this.getGridColor()));
138 ((TableTreeUI)getUI()).setLineColor(JTreeTable.this.getGridColor());
139 visibleRow = row;
140 return this;
141 }
142 }
143
144 //
145 // The editor used to interact with tree nodes, a JTree.
146 //
147
148 public class TreeTableCellEditor extends AbstractCellEditor implements TableCellEditor {
149 public Component getTableCellEditorComponent(JTable table, Object value,
150 boolean isSelected, int r, int c) {
151 return tree;
152 }
153 }
154
155 }