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.LinkedList;
23  import javax.swing.SwingUtilities;
24  
25  import org.xnap.event.*;
26  import org.xnap.transfer.Transfer;
27  
28  /***
29   * 
30   */
31  public class FilteredTransferContainerTreeTableNode 
32  	extends TransferContainerTreeTableNode
33  {
34  
35      //--- Constant(s) ---
36  
37      //--- Data field(s) ---
38  	
39  	private LinkedList children = new LinkedList();
40  
41      //--- Constructor(s) ---
42  
43      /***
44       * Constructs a <code>TransferContainerTreeTableNode</code>.
45       *
46       * @param model the table model
47       * @param transfer the container
48       */
49      public FilteredTransferContainerTreeTableNode(TransferTableModel model, 
50  												  Transfer transfer) 
51      {
52  		super(model, transfer);
53      }
54  
55      //--- Method(s) ---
56      
57      /***
58       * Creates a {@link LeafTreeTableNode}, if <code>data</code> is
59       * not already contained in children.
60       *
61       * @return true, if data was added; false, otherwise
62       */
63      public boolean add(Transfer transfer)
64      {
65  		if (!children.contains(transfer)) {
66  			children.add(transfer);
67  			transfer.addStateListener(this);
68  			if (transfer.isRunning()) {
69  				super.add(new LeafTreeTableNode(transfer));
70  			}
71  			return true;
72  		}
73  		return false;
74      }
75  
76  	public void remove(Transfer transfer)
77  	{
78  		if (children.contains(transfer)) {
79  			transfer.removeStateListener(this);
80  			int index = getIndexOfChildByData(transfer);
81  			if (index != -1) {
82  				removeChildAt(index);
83  			}
84  		}
85  	}
86  
87      public void stateChanged(final StateEvent event)
88  	{
89  		Runnable runner = new Runnable() 
90  			{
91  				public void run()
92  				{
93  					if (event.getSource() == getData()) {
94  						changed();
95  					}
96  					else {
97  						Transfer transfer = (Transfer)event.getSource();
98  						int index = getIndexOfChildByData(transfer);
99  						if (transfer.isRunning()) {
100 							if (index != -1) {
101 								changedChildAt(index);
102 							}
103 							else {
104 								FilteredTransferContainerTreeTableNode
105 									.super.add
106 									(new LeafTreeTableNode(transfer));
107 							}
108 						}
109 						else if (index != -1) {
110 							removeChildAt(index);
111 						}
112 					}
113 				}
114 			};
115 		SwingUtilities.invokeLater(runner);
116 	}
117 
118 }