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.plugin.opennap.gui;
21  
22  import java.beans.PropertyChangeEvent;
23  import java.beans.PropertyChangeListener;
24  
25  import javax.swing.SwingUtilities;
26  
27  import org.xnap.event.StateEvent;
28  import org.xnap.event.StateListener;
29  import org.xnap.gui.table.DefaultTreeTableNode;
30  import org.xnap.gui.table.LeafTreeTableNode;
31  import org.xnap.gui.table.TreeTableNode;
32  import org.xnap.plugin.opennap.net.OpenNapNetwork;
33  import org.xnap.plugin.opennap.net.OpenNapServer;
34  
35  /***
36   * 
37   */
38  public class OpenNapNetworkNode extends DefaultTreeTableNode
39      implements PropertyChangeListener, StateListener
40  {
41  
42      //--- Constant(s) ---
43  
44      //--- Data field(s) ---
45  
46      //--- Constructor(s) ---
47  
48      /***
49       * Constructs a <code>TransferTreeTableNode</code>.
50       *
51       * @param model the table model
52       * @param name the name of the network
53       */
54      public OpenNapNetworkNode(OpenNapServerTableModel model, OpenNapNetwork network)
55      {
56  	super(model, network);
57  
58  	network.addPropertyChangeListener(this);
59      }
60  
61      //--- Method(s) ---
62      
63      public void add(OpenNapServer server)
64      {
65  //  	if (getIndexOfChildByData(server) == -1) {
66  	    add(new LeafTreeTableNode(server));
67  	    
68  	    server.addPropertyChangeListener(this);
69  	    server.addStateListener(this);
70  //  	}
71      }
72  
73      public void remove(OpenNapServer server)
74      {
75  	server.removePropertyChangeListener(this);
76  	server.removeStateListener(this);
77  
78  	int index = getIndexOfChildByData(server);
79  	removeChildAt(index);
80      }
81  
82      public void removeAll()
83      {
84  	while (getChildCount() > 0) {
85  	    remove((OpenNapServer)((TreeTableNode)getChildAt(0)).getData());
86  	}
87      }
88  
89      public void propertyChange(final PropertyChangeEvent event)
90      {
91  	Runnable runner = new Runnable() 
92  	    {
93  		public void run()
94  		{
95  		    if (event.getSource() == getData()) {
96  			changed();
97  		    }
98  		    else {
99  			String p = event.getPropertyName();
100 			
101 			int index = getIndexOfChildByData(event.getSource());
102 			if (index != -1) {
103 			    if (p.equals("network")) {
104 				OpenNapServer s = (OpenNapServer)event.getSource();
105 				remove(s);
106 				((OpenNapServerTableModel)getModel()).move(s);
107 			    }
108 			    else {
109 				changedChildAt(index);
110 			    }
111 			}
112 		    }
113 		}
114 	    };
115 	SwingUtilities.invokeLater(runner);
116     }
117 
118     public void stateChanged(final StateEvent event)
119     {
120 	Runnable runner = new Runnable() 
121 	    {
122 		public void run()
123 		{
124 		    int index = getIndexOfChildByData(event.getSource());
125 		    if (index != -1) {
126 			changedChildAt(index);
127 		    }
128 		}
129 	    };
130 	SwingUtilities.invokeLater(runner);
131     }
132 
133 }