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