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.util.Hashtable;
23  
24  import org.xnap.XNap;
25  import org.xnap.gui.event.SwingListListener;
26  import org.xnap.gui.table.Column;
27  import org.xnap.gui.table.DefaultColumnTreeTableModel;
28  import org.xnap.gui.table.NumberCellRenderer;
29  import org.xnap.gui.table.StringCellRenderer;
30  import org.xnap.gui.table.TreeTableModel;
31  import org.xnap.gui.table.TreeTableNode;
32  import org.xnap.gui.util.SwingSynchronizedCache;
33  import org.xnap.plugin.opennap.OpenNapPlugin;
34  import org.xnap.plugin.opennap.net.OpenNapNetwork;
35  import org.xnap.plugin.opennap.net.OpenNapServer;
36  
37  public class OpenNapServerTableModel extends DefaultColumnTreeTableModel
38      implements SwingListListener {
39  
40      //--- Constant(s) ---
41  
42      //--- Data Field(s) ---
43  
44      private Hashtable nodeByNetworkName = new Hashtable();
45  
46      //--- Constructor(s) ---
47  
48      public OpenNapServerTableModel()
49      {
50  		Column columns[] = new Column[] {
51  			new Column("name", XNap.tr("Name"), TreeTableModel.class), 
52  			new Column("redirected", XNap.tr("Redirected"), String.class), 
53  			new Column("network", XNap.tr("Network"), String.class),
54  			new Column("localPort", XNap.tr("Local Port"), Integer.class,
55  					   new NumberCellRenderer(1)),
56  			new Column("version", XNap.tr("Version"), String.class,
57  					   new StringCellRenderer()),
58  			new Column("files", XNap.tr("Files"), Integer.class,
59  					   new NumberCellRenderer()),
60  			new Column("size", XNap.tr("Total (GB)"), Integer.class,
61  					   new NumberCellRenderer()),
62  			new Column("users", XNap.tr("Users"), Integer.class, 
63  					   new NumberCellRenderer()),
64  			new Column("status", XNap.tr("Status"), String.class,
65  					   new StringCellRenderer()),
66  		};
67  		addColumns(columns);
68  
69  		SwingSynchronizedCache cache = new SwingSynchronizedCache(this);
70  		OpenNapPlugin.getServerManager().addListListener(cache);
71  
72  		OpenNapServer[] servers = OpenNapPlugin.getServerManager().getServers();
73  		for (int i = 0; i < servers.length; i++) {
74  			add(servers[i]);
75  		}
76      }
77  
78      // --- Method(s) ---
79  
80      /***
81       * Invoked when servers have been added.
82       */
83      public void itemsAdded(Object[] items)
84      {
85  		for (int i = 0; i < items.length; i++) {
86  			add((OpenNapServer)items[i]);
87  		}
88      }
89  
90      /***
91       * Invoked when servers have been removed.
92       */
93      public void itemsRemoved(Object[] items) 
94      {
95  		for (int i = 0; i < items.length; i++) {
96  			remove((OpenNapServer)items[i]);
97  		}
98      }
99  
100     public void add(OpenNapServer server)
101     {
102 		move(server);
103     }
104 
105     /***
106      *
107      */
108     public OpenNapNetworkNode add(OpenNapNetwork network)
109     {
110 		OpenNapNetworkNode node = new OpenNapNetworkNode(this, network);
111 		nodeByNetworkName.put(network.getName(), node);
112 		add(node);
113 		return node;
114     }
115 
116     /***
117      * Invoked by {@link OpenNapNetworkNode} when the network of the server
118      * has changed.
119      */
120     public void move(OpenNapServer server)
121     {
122 		OpenNapNetworkNode node
123 			= (OpenNapNetworkNode)nodeByNetworkName.get(server.getNetwork().getName());
124 		if (node == null) {
125 			node = add(server.getNetwork());
126 		}
127 		node.add(server);
128     }
129 
130     /***
131      * Removes <code>server</code> from the table model.
132      */
133     public void remove(OpenNapServer server)
134     {
135 		OpenNapNetworkNode node
136 			= (OpenNapNetworkNode)nodeByNetworkName.get(server.getNetwork().getName());
137 		node.remove(server);
138     }
139 
140     public Object getValueAt(Object node, int column)
141     {
142 		if (node instanceof OpenNapNetworkNode) {
143 			OpenNapNetwork n = (OpenNapNetwork)((TreeTableNode)node).getData();
144 
145 			switch (column) {
146 			case 0:
147 				// return network name
148 				return n.toString();
149 			case 5:
150 				return new Integer(n.getFileCount());
151 			case 6:
152 				return new Integer(n.getFileSize());
153 			case 7:
154 				return new Integer(n.getUserCount());
155 			case 8:
156 				return n.getStatus();
157 			}
158 		}
159 		else if (node instanceof TreeTableNode) {
160 			OpenNapServer s = (OpenNapServer)((TreeTableNode)node).getData();
161 
162 			String host;
163 			switch (column) {
164 			case 0:
165 				host = s.getHost() + ":" + s.getPort();
166 				String redirectedHost = s.getRedirectedHost();
167 				return (s.isRedirector() && redirectedHost != null)
168 					? host + " -> " + s.getRedirectedHost() + ":" 
169 					+ s.getRedirectedPort()
170 					: host;
171 			case 1:
172 				host = s.getRedirectedHost();
173 				return (s.isRedirector() && host != null) 
174 					? host + ":" + s.getRedirectedPort() : null;
175 			case 2:
176 				return s.getNetwork().getName();
177 			case 3:
178 				return new Integer(s.getLocalPort());
179 			case 4:
180 				return s.getVersion().toString();
181 			case 5:
182 				return new Integer(s.getFileCount());
183 			case 6:
184 				return new Integer(s.getFileSize());
185 			case 7:
186 				return new Integer(s.getUserCount());
187 			case 8:
188 				return s.getStatus();
189 			}
190 		}
191 
192 		return null;
193     }
194 
195     public String[] getNetworkNames()
196     {
197 		return (String[])nodeByNetworkName.keySet().toArray(new String[0]);
198     }
199 
200 }