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  import java.util.Iterator;
26  
27  /***
28   * This class is a default implementation of a {@link TreeTableNode} with
29   * children.
30   */
31  public class DefaultTreeTableNode 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 = new ArrayList();
45  
46      //--- Constructor(s) ---
47  
48      public DefaultTreeTableNode(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  		children.add(node);
60  		model.childInserted(this, children.size() - 1, node);
61      }
62  
63      public void changed()
64      {
65  		model.nodeChanged(this);
66      }
67  
68      public void changedChildAt(int index)
69      {
70  		model.childChanged(this, index, children.get(index));
71      }
72  
73      /***
74       * Returns an Iterator over all child nodes.
75       */
76      public Iterator children()
77      {
78  		return children.iterator();
79      }
80  
81      /***
82       * Returns the child that stores <code>data</code>.
83       *
84       * @return -1, if data could not be found
85       */
86      public int getIndexOfChildByData(Object data)
87      {
88  		for (int i = 0; i < children.size(); i++) {
89  			TreeTableNode node = (TreeTableNode)children.get(i);
90  			if (node.getData() == data) {
91  				return i;
92  			}
93  		}
94  		return -1;
95      }
96      
97      /***
98       * Returns the child node at <code>index</code>.
99       */
100     public Object getChildAt(int index)
101     {
102 		return children.get(index);
103     }
104 
105     /***
106      * Returns the number of children of the node.
107      */
108     public int getChildCount()
109     {
110 		return children.size();
111     }
112 
113     public Object getData()
114     {
115 		return data;
116     }
117 
118     public DefaultColumnTreeTableModel getModel()
119     {
120 		return model;
121     }
122 
123     public void removeChildAt(int index)
124     {
125 		Object o = children.get(index);
126 		children.remove(index);
127 		model.childRemoved(this, index, o);
128     }
129 
130 	public void sort(Comparator c)
131 	{
132 		Collections.sort(children, c);
133 	}
134 
135     /***
136      * Returns the <code>data.toString()</code>.
137      */
138     public String toString()
139     {
140 		return null;
141     }
142 
143 }