View Javadoc
1   package wjhk.jupload2.gui.filepanel.treeview;
2   
3   import java.awt.Component;
4   import java.awt.Graphics;
5   
6   import javax.swing.JTable;
7   import javax.swing.JTree;
8   import javax.swing.table.TableCellRenderer;
9   import javax.swing.tree.TreeModel;
10  
11  /**
12   * This code is taken from the tutorial written by Jörn Hameister, <A
13   * HREF="http://www.hameister.org/JavaSwingTreeTable.html">available here</A>.<BR/>
14   * <BR/>
15   * <UL>
16   * <LI>Call to tree.setVisibleRoot(false) pour masquer la racine.</LI>
17   * <UL>
18   * <BR/>
19   * The last thing the JTree and JTable which must now be created. The Tree component MyTreeTableCellRenderer inherits
20   * from JTree and implements the TableCellRenderer interface. This class ensures that the row heights of Tree and Table
21   * are the same and are set correctly in the selection of the background colors. It also ensures that the elements of
22   * the tree are indented correctly depending on the level.
23   * 
24   * @author Jörn Hameister
25   */
26  public class MyTreeTableCellRenderer extends JTree implements TableCellRenderer {
27  
28      private static final long serialVersionUID = 1L;
29  
30      /** Die letzte Zeile, die gerendert wurde. */
31      protected int visibleRow;
32  
33      private MyTreeTable treeTable;
34  
35      public MyTreeTableCellRenderer(MyTreeTable treeTable, TreeModel model) {
36          super(model);
37          this.treeTable = treeTable;
38  
39          // Setzen der Zeilenhoehe fuer die JTable
40          // Muss explizit aufgerufen werden, weil treeTable noch
41          // null ist, wenn super(model) setRowHeight aufruft!
42          setRowHeight(getRowHeight());
43          
44          setRootVisible(false);
45          setScrollsOnExpand(true);
46          setShowsRootHandles(true);
47      }
48  
49      /**
50       * Tree und Table muessen die gleiche Hoehe haben.
51       */
52      public void setRowHeight(int rowHeight) {
53          if (rowHeight > 0) {
54              super.setRowHeight(rowHeight);
55              if (treeTable != null && treeTable.getRowHeight() != rowHeight) {
56                  treeTable.setRowHeight(getRowHeight());
57              }
58          }
59      }
60  
61      /**
62       * Tree muss die gleiche Hoehe haben wie Table.
63       */
64      public void setBounds(int x, int y, int w, int h) {
65          super.setBounds(x, 0, w, treeTable.getHeight());
66      }
67  
68      /**
69       * Sorgt fuer die Einrueckung der Ordner.
70       */
71      public void paint(Graphics g) {
72          g.translate(0, -visibleRow * getRowHeight());
73  
74          super.paint(g);
75      }
76  
77      /**
78       * Liefert den Renderer mit der passenden Hintergrundfarbe zurueck.
79       */
80      public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
81              int row, int column) {
82          if (isSelected)
83              setBackground(table.getSelectionBackground());
84          else
85              setBackground(table.getBackground());
86  
87          visibleRow = row;
88          return this;
89      }
90  }