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 javax.swing.SwingUtilities;
23  
24  import org.xnap.event.ListEvent;
25  import org.xnap.event.ListListener;
26  import org.xnap.event.StateEvent;
27  import org.xnap.event.StateListener;
28  import org.xnap.transfer.Transfer;
29  import org.xnap.transfer.TransferContainer;
30  
31  /***
32   *
33   */
34  public class TransferContainerTreeTableNode extends DefaultTreeTableNode
35      implements StateListener, ListListener
36  {
37  
38      //--- Constant(s) ---
39  
40      //--- Data field(s) ---
41  
42      //--- Constructor(s) ---
43  
44      /***
45       * Constructs a <code>TransferContainerTreeTableNode</code>.
46       *
47       * @param model the table model
48       * @param transfer must implement {@link TransferContainer}
49       */
50      public TransferContainerTreeTableNode(TransferTableModel model, 
51  										  Transfer transfer) 
52      {
53  		super(model, transfer);
54  
55  		transfer.addStateListener(this);
56  		((TransferContainer)transfer).addListListener(this);
57      }
58  
59      //--- Method(s) ---
60      
61      /***
62       * Creates a {@link LeafTreeTableNode}, if <code>data</code> is
63       * not already contained in children.
64       *
65       * @return true, if data was added; false, otherwise
66       */
67      public boolean add(Transfer transfer)
68      {
69  		// check if node was already inserted
70  		int index = getIndexOfChildByData(transfer);
71  		if (index == -1) {
72  			transfer.addStateListener(this);
73  			super.add(new LeafTreeTableNode(transfer));
74  			return true;
75  		}
76  		return false;
77      }
78  
79      public void addExistingChildren()
80      {
81  		Transfer[] children = ((TransferContainer)getData()).getChildren();
82  		for (int i = 0; i < children.length; i++) {
83  			add(children[i]);
84  		}
85      }
86  
87  	public void remove(Transfer transfer)
88  	{
89  		int index = getIndexOfChildByData(transfer);
90  		if (index != -1) {
91  			transfer.removeStateListener(this);
92  			removeChildAt(index);
93  		}
94  	}
95  
96      public void itemAdded(final ListEvent event)
97      {
98  		Runnable runner = new Runnable() 
99  			{
100 				public void run()
101 				{
102 					add((Transfer)event.getItem());
103 				}
104 			};
105 		SwingUtilities.invokeLater(runner);
106     }
107 
108     public void itemRemoved(final ListEvent event)
109     {
110 		Runnable runner = new Runnable() 
111 			{
112 				public void run()
113 				{
114 					remove((Transfer)event.getItem());
115 				}
116 			};
117 		SwingUtilities.invokeLater(runner);	
118     }
119 
120     public void stateChanged(final StateEvent event)
121     {
122 		Runnable runner = new Runnable() 
123 			{
124 				public void run()
125 				{
126 					if (event.getSource() == getData()) {
127 						changed();
128 					}
129 					else {
130 						int index = getIndexOfChildByData(event.getSource());
131 						if (index != -1) {
132 							changedChildAt(index);
133 						}
134 					}
135 				}
136 			};
137 		SwingUtilities.invokeLater(runner);
138     }
139 
140 }