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.jtella;
21  
22  import java.util.ArrayList;
23  
24  import org.xnap.XNap;
25  import org.xnap.gui.table.AbstractColumnTableModel;
26  import org.xnap.gui.table.Column;
27  import org.xnap.gui.table.StringCellRenderer;
28  import org.xnap.gui.table.TimeCellRenderer;
29  import org.xnap.gui.util.SwingSynchronizedTask;
30  import org.xnap.util.Scheduler;
31  import org.xnap.util.XNapTask;
32  
33  import com.kenmccrary.jtella.Connection;
34  import com.kenmccrary.jtella.GNUTellaConnection;
35  
36  /***
37   * Table model for {@link Connection} objects.
38   */
39  public class JTellaConnectionTableModel extends AbstractColumnTableModel
40  {
41  
42      //--- Constant(s) ---
43      
44      public static int UPDATE_INTERVAL = 1 * 1000;
45  
46      //--- Data field(s) ---
47  
48      private ArrayList data = new ArrayList();
49      private XNapTask task;
50  
51      //--- Constructor(s) ---
52  
53      public JTellaConnectionTableModel() 
54      {
55  	Column columns[] = new Column[] {
56  	    new Column("host", XNap.tr("Host"), String.class,
57  		       new StringCellRenderer()),
58  	    new Column("status", XNap.tr("Status"), String.class, 
59  		       new StringCellRenderer()),
60  	    new Column("msgCount", XNap.tr("In / Out / Dropped"), 
61  		       String.class, 
62  		       new StringCellRenderer()),
63  	    new Column("type", XNap.tr("Type"), String.class),
64  	    new Column("uptime", XNap.tr("Uptime"), Integer.class, 
65  		       new TimeCellRenderer()),
66  	};
67  	addColumns(columns);
68      }
69  
70      //--- Method(s) ---
71  
72  
73      public int getRowCount() 
74      {
75          return data.size();
76      }
77  
78      public String getStatusString(int status)
79      {
80  	switch (status) {
81  	case Connection.STATUS_CONNECTING:
82  	    return XNap.tr("Connecting");
83  	case Connection.STATUS_FAILED:
84  	    return XNap.tr("Failed");
85  	case Connection.STATUS_OK:
86  	    return XNap.tr("Connected");
87  	case Connection.STATUS_STOPPED:
88  	    return XNap.tr("Stopped");
89  	}
90  
91  	return null;
92      }
93  
94      public String getTypeString(int type)
95      {
96  	switch (type) {
97  	case Connection.CONNECTION_INCOMING:
98  	    return XNap.tr("Incoming");
99  	case Connection.CONNECTION_OUTGOING:
100 	    return XNap.tr("Outgoing");
101 	}
102 
103 	return null;
104     }
105 
106     public Object get(int i, int j) 
107     {
108 	Connection c = (Connection)data.get(i);
109 
110         switch (j) {
111 	case 0:
112 	    return c.getConnectedServant();
113 	case 1:
114 	    return getStatusString(c.getStatus());
115 	case 2:
116 	    StringBuffer sb = new StringBuffer();
117 	    sb.append(c.getMessageInput());
118 	    sb.append(" / ");
119 	    sb.append(c.getMessageOutput());
120 	    sb.append(" / ");
121 	    sb.append(c.getMessageDropCount());
122 	    return sb.toString();
123 	case 3:
124 	    return getTypeString(c.getType());
125 	case 4:
126 	    return new Integer(c.getUpTime());
127 	default:
128 	    return null;
129         }
130     }
131 
132     public void setConnected(boolean connected)
133     {
134 	if (task != null) {
135 	    task.cancel();
136 	    task = null;
137 	}
138 
139 	if (connected) {
140 	    task = new Updater();
141 	    Scheduler.run(0, UPDATE_INTERVAL, task);
142 	}
143     }
144 
145     //--- Inner Class(es) ---
146 
147     private class Updater extends SwingSynchronizedTask
148     {
149 	
150 	public void runSynchronized()
151 	{
152 	    JTellaPlugin p = JTellaPlugin.getInstance();
153 	    if (p != null) {
154 		GNUTellaConnection c 
155 		    = JTellaPlugin.getInstance().getConnection();
156 		if (c != null) {
157 		    data = new ArrayList(c.getConnectionList());
158 		    fireTableDataChanged();
159 		}
160 	    }
161 	}
162 
163     }
164 
165 }