View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.gui.tree;
21  
22  import javax.swing.JTree;
23  import javax.swing.event.TreeModelEvent;
24  import javax.swing.event.TreeModelListener;
25  
26  /***
27   * Do not use this class, it will break your JTree. This class causes
28   * funny ghost rows with JTreeTable objects.  
29   */
30  public class AutoTreeExpander implements TreeModelListener 
31  {
32  
33      //--- Data field(s) ---
34  
35      private JTree jt;
36      private boolean expandRootOnly;
37  
38      //--- Constructor(s) ---
39  
40      /***
41       *
42       */
43      public AutoTreeExpander(JTree jt, boolean expandRootOnly)
44      {
45  		this.jt = jt;
46  		this.expandRootOnly = expandRootOnly;
47  
48  		jt.getModel().addTreeModelListener(this);
49      }
50  
51      public AutoTreeExpander(JTree jt)
52      {
53  		this(jt, false);
54      }
55  
56      //--- Method(s) ---
57  
58      public static AutoTreeExpander install(JTree jt, boolean expandRootOnly)
59      {
60  		return new AutoTreeExpander(jt, expandRootOnly);
61      }
62  
63      public static AutoTreeExpander install(JTree jt)
64      {
65  		return new AutoTreeExpander(jt);
66      }
67  
68      public void treeNodesChanged(TreeModelEvent e) 
69      {
70      }
71  
72      public void treeNodesInserted(TreeModelEvent e) 
73      {
74  		if (expandRootOnly) {
75  			jt.expandPath(e.getTreePath());
76  			jt.getModel().removeTreeModelListener(this);
77  		}
78  		else {
79  			jt.expandPath(e.getTreePath().getParentPath());
80  		}
81      }
82  
83      public void treeNodesRemoved(TreeModelEvent e) 
84      {
85      }
86  
87      public void treeStructureChanged(TreeModelEvent e) 
88      {
89      }
90  
91  }