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.table;
21  
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.Comparator;
25  
26  /***
27   * This class is a default implementation of a {@link TreeTableNode} with
28   * children. All data structures holding the childern are instantiated
29   * lazyly though to allow for less memory consumption when not needed.
30   */
31  public class LazyTreeTableNode implements TreeTableNode
32  {
33  
34      //--- Constant(s) ---
35  
36      //--- Data field(s) ---
37  
38      private DefaultColumnTreeTableModel model;
39      private Object data;
40  
41      /***
42       * List of {@link TreeTableNode} objects
43       */
44      private ArrayList children;
45  
46      //--- Constructor(s) ---
47  
48      public LazyTreeTableNode(DefaultColumnTreeTableModel model, 
49  							 Object data) 
50      {
51  		this.model = model;
52  		this.data = data;
53      }
54  
55      //--- Method(s) ---
56  
57      public void add(TreeTableNode node)
58      {
59  		if (children == null) {
60  			children = new ArrayList();
61  		}
62  
63  		children.add(node);
64  		model.childInserted(this, children.size() - 1, node);
65  
66  		// this might affect our data
67  		model.nodeChanged(this);
68      }
69      
70      /***
71       * Returns the child node at <code>index</code>.
72       */
73      public Object getChildAt(int index)
74      {
75  		return children.get(index);
76      }
77  
78      /***
79       * Returns the number of children of the node.
80       */
81      public int getChildCount()
82      {
83  		return (children != null) ? children.size() : 0;
84      }
85  
86      public Object getData()
87      {
88  		return data;
89      }
90  
91      public void removeChildAt(int index)
92      {
93  		children.remove(index);
94  		model.childRemoved(this, index, children.get(index));
95      }
96  
97      public void setData(Object data)
98      {
99  		this.data = data;
100     }
101 
102 	public void sort(Comparator c)
103 	{
104 		if (children != null) {
105 			Collections.sort(children, c);
106 		}
107 	}
108 
109     /***
110      * Returns null.
111      */
112     public String toString()
113     {
114 		return null;
115     }
116 
117 }