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.stats;
21  
22  import java.awt.Color;
23  import java.awt.GridBagLayout;
24  import java.awt.event.ActionEvent;
25  import java.util.Date;
26  
27  import javax.swing.AbstractAction;
28  import javax.swing.Action;
29  import javax.swing.JLabel;
30  import javax.swing.JPanel;
31  import javax.swing.JTabbedPane;
32  
33  import org.jfree.chart.ChartFactory;
34  import org.jfree.chart.ChartPanel;
35  import org.jfree.chart.JFreeChart;
36  import org.jfree.data.time.Millisecond;
37  import org.jfree.data.time.TimeSeries;
38  import org.jfree.data.time.TimeSeriesCollection;
39  import org.xnap.XNap;
40  import org.xnap.gui.ActionProvider;
41  import org.xnap.gui.StatusBar;
42  import org.xnap.gui.StatusPanel;
43  import org.xnap.gui.component.XNapButton;
44  import org.xnap.gui.util.GUIHelper;
45  import org.xnap.gui.util.GridBagHelper;
46  import org.xnap.gui.util.IconHelper;
47  import org.xnap.gui.util.SwingSynchronizedTask;
48  import org.xnap.transfer.DownloadManager;
49  import org.xnap.transfer.UploadManager;
50  import org.xnap.util.Formatter;
51  import org.xnap.util.Scheduler;
52  
53  
54  public class StatsPanel extends JPanel implements ActionProvider {
55  
56      // --- Data Field(s) ---
57  
58  	private JLabel jlStats;
59  
60      private Action updateAction = new UpdateAction();
61      private TimeSeriesCollection data;
62      private TimeSeries uploadBandwidthSeries;
63      private TimeSeries downloadBandwidthSeries;
64  
65  	private UpdateTask updateTask;
66  	private StatusPanel uptimeStatusPanel;
67      
68      // --- Constructor(s) ---
69  
70      public StatsPanel() 
71      {
72          setLayout(new GridBagLayout());
73  
74  		// status
75  		JPanel jpStats = new JPanel(new GridBagLayout());
76  		jpStats.setBorder(GUIHelper.createDefaultBorder(XNap.tr("Statistics")));
77  		GridBagHelper.add(this, jpStats);
78  
79  		jlStats = new JLabel();
80  		GridBagHelper.add(jpStats, jlStats);
81  	
82  		downloadBandwidthSeries = new MyTimeSeries(XNap.tr("Download"), 
83  												   Millisecond.class);
84  		downloadBandwidthSeries.setMaximumItemCount(60 * 10); // 10 minutes	
85  
86  		uploadBandwidthSeries = new MyTimeSeries(XNap.tr("Upload"), 
87  												 Millisecond.class);
88  		uploadBandwidthSeries.setMaximumItemCount(60 * 10); // 10 minutes
89  		
90  		data = new TimeSeriesCollection();
91  		data.addSeries(downloadBandwidthSeries);
92  		data.addSeries(uploadBandwidthSeries);
93  
94  		JFreeChart chart = ChartFactory.createTimeSeriesChart
95          	(XNap.tr("XNap Statistics"), XNap.tr("Time"), XNap.tr("Bandwidth"), 
96          	 data,
97               true,
98               true,
99               false);
100         chart.setBackgroundPaint(Color.white);
101         
102         ChartPanel panel = new ChartPanel(chart);
103 //        panel.setMinimumDrawWidth(640);
104 //        panel.setMinimumDrawHeight(480);
105 //        panel.setMinimumSize(new Dimension(640, 480));
106 
107         JTabbedPane tabbedPane = new JTabbedPane();
108         tabbedPane.addTab(XNap.tr("Current"), null, panel);
109         GridBagHelper.addPanel(this, tabbedPane);
110        
111 		// button
112 		GridBagHelper.addComponent(this, new XNapButton(updateAction));
113 
114 		GridBagHelper.addVerticalSpacer(this);
115 	
116 		// status panel
117 		uptimeStatusPanel = new StatusPanel();
118 		uptimeStatusPanel.setIcon(IconHelper.getStatusBarIcon("history.png"));
119 		uptimeStatusPanel.setName(XNap.tr("Uptime"));
120 		uptimeStatusPanel.setToolTipText(XNap.tr("Uptime"));
121 		StatusBar.getInstance().appendComponent(uptimeStatusPanel);
122 		
123 		// initialize
124 		updateStatus();
125 
126 		// start graph update task
127 		updateTask = new UpdateTask();
128 		Scheduler.run(0, 1000, updateTask);
129     }
130 
131     // --- Method(s) ---
132 
133 
134     public Action[] getActions()
135     {
136 		return new Action[] { updateAction };
137     }
138 
139 	public void updateStatus()
140 	{
141 		StringBuffer sb = new StringBuffer();
142 		sb.append("<html>");
143 		sb.append(XNap.tr("Alle Systeme arbeiten innerhalb normaler Parameter.<br>"));
144 		sb.append(XNap.tr("Uptime: {0}", getUptime()));
145 		jlStats.setText(sb.toString());
146 	}
147 
148 	/***
149 	 * @return
150 	 */
151 	private String getUptime()
152 	{
153 		return Formatter.formatLength((System.currentTimeMillis() - XNap.START_TIME) / 1000);
154 	}
155 
156 	void stop()
157 	{
158 		updateTask.cancel();
159 		
160 		StatusBar.getInstance().removeComponent(uptimeStatusPanel);
161 	}
162 	
163     // --- Inner Class(es) ---
164 
165     /***
166      * Updates the statistics
167      */
168     private class UpdateAction extends AbstractAction 
169 	{
170         public UpdateAction() 
171 		{
172             putValue(Action.NAME, XNap.tr("Update"));
173             putValue(Action.SHORT_DESCRIPTION,
174 					 XNap.tr("Update statistics display"));
175 			putValue(IconHelper.XNAP_ICON, "reload.png");
176         }
177 
178         public void actionPerformed(ActionEvent event) 
179 		{
180         	updateStatus();
181 		}
182     } 
183 
184     private class UpdateTask extends SwingSynchronizedTask
185 	{
186     	private long updateCounter;
187 
188 		/***
189 		 *  @see org.xnap.gui.util.SwingSynchronizedTask#runSynchronized()
190 		 */
191 		public void runSynchronized()
192 		{
193 			Millisecond time = new Millisecond(new Date());
194 			downloadBandwidthSeries.addOrUpdate(time, new Double(DownloadManager.getInstance().getCurrentRate()));
195 			uploadBandwidthSeries.addOrUpdate(time, new Double(UploadManager.getInstance().getCurrentRate()));
196 			
197 			if (++updateCounter % 5 == 0) {
198 				uptimeStatusPanel.setText(getUptime());
199 			}
200 			if (updateCounter % 60 == 0) {
201 				updateStatus();
202 			}
203 		}
204     	
205     }
206 
207     private class MyTimeSeries extends TimeSeries
208 	{
209     	
210         public MyTimeSeries(String name, Class timePeriodClass) 
211 		{
212         	super(name, timePeriodClass);
213         }
214 
215     	public void fireSeriesChanged()
216 		{
217     		if (StatsPanel.this.hasFocus()) {
218     			super.fireSeriesChanged();
219     		}
220     	}
221     	
222     } 
223     
224 }