FreewayPanel.java

Index Score
org.xnap.plugin.freeway
XNap 3

View: Reasons, Metrics, Source Code

These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.

MetricDescription
/* * XNap - A P2P framework and client. * * See the file AUTHORS for copyright information. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package org.xnap.plugin.freeway; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.util.ArrayList; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.SwingUtilities; import javax.swing.border.EmptyBorder; import org.gnu.freeway.DaemonSocket; import org.gnu.freeway.protocol.afs.swing.SController; import org.gnu.freeway.util.CSMessage; import org.gnu.freeway.util.CSReturnValue; import org.gnu.freeway.util.CSStatistics; import org.gnu.freeway.util.CSStatisticsRequest; import org.gnu.freeway.util.ConfigurationService; import org.gnu.freeway.util.CronService; import org.gnu.freeway.util.Statistic; import org.xnap.XNap; import org.xnap.gui.ActionProvider; import org.xnap.gui.component.XNapButton; import org.xnap.gui.table.AbstractColumnTableModel; import org.xnap.gui.table.Column; import org.xnap.gui.util.GUIHelper; import org.xnap.gui.util.GridBagHelper; import org.xnap.gui.util.IconHelper; import org.xnap.util.Formatter; import org.xnap.util.Preferences; public class FreewayPanel extends JPanel implements ActionProvider { //--- Constant(s) --- //--- Data field(s) --- private JLabel jlStatus; private SController controller; private FreewayStatsTableModel statsTableModel; //--- Constructor(s) --- public FreewayPanel(SController controller) { this.controller = controller; initialize(); } //--- Method(s) --- public void initialize() { setBorder(new EmptyBorder(5, 5, 5, 5)); setLayout(new GridBagLayout()); JPanel jpStatus = new JPanel(new BorderLayout()); GridBagHelper.add(this, jpStatus); jpStatus.setBorder(GUIHelper.createDefaultBorder(XNap.tr("Status"))); jlStatus = new JLabel(XNap.tr("Unknown")); jpStatus.add(jlStatus, BorderLayout.CENTER); JPanel statsPanel = new JPanel(new BorderLayout()); statsPanel.setBorder (GUIHelper.createDefaultBorder(XNap.tr("Statistics"))); GridBagHelper.addPanel(this, statsPanel); statsTableModel = new FreewayStatsTableModel(); JTable statsTable = statsTableModel.createTable(Preferences.getInstance(), "freewayStats"); statsPanel.add(new JScrollPane(statsTable), BorderLayout.CENTER); GridBagHelper.addVerticalSpacer(this); JPanel jpButtons = new JPanel(new FlowLayout(FlowLayout.LEFT)); GridBagHelper.add(this, jpButtons); jpButtons.add(new XNapButton(new UpdateAction())); } public Action[] getActions() { return new Action[] { new UpdateAction() }; } public void updateStatus() { jlStatus.setText(XNap.tr("Updating statistics") + "..."); statsTableModel.clear(); Thread t = new Thread(new StatsRunner(), "FreewayStatsRunner"); t.start(); } /** * @param text the status message */ private void setStatusLater(final String text) { Runnable runner = new Runnable() { public void run() { jlStatus.setText(text); } }; SwingUtilities.invokeLater(runner); } private class UpdateAction extends AbstractAction { public UpdateAction() { putValue(Action.NAME, XNap.tr("Update")); putValue(Action.SHORT_DESCRIPTION, XNap.tr("Updates the status.")); putValue(IconHelper.XNAP_ICON, "undo.png"); } public void actionPerformed(ActionEvent event) { updateStatus(); } } public class FreewayStatsTableModel extends AbstractColumnTableModel { //--- Data field(s) --- private ArrayList data = new ArrayList(); //--- Constructor(s) --- public FreewayStatsTableModel() { Column columns[] = new Column[] { new Column("key", XNap.tr("Key"), String.class), new Column("value", XNap.tr("Value"), String.class), }; addColumns(columns); } /** * */ public void clear() { data.clear(); fireTableDataChanged(); } //--- Method(s) --- public void add(final String key, final String value) { Runnable runner = new Runnable() { public void run() { data.add(new Item(key, value)); fireTableRowsInserted(data.size() - 1, data.size() - 1); } }; SwingUtilities.invokeLater(runner); } public int getRowCount() { return data.size(); } public Object get(int i, int j) { Item item = (Item)data.get(i); switch (j) { case 0: return item.key; case 1: return item.value; default: return null; } } //--- Inner Class(es) --- private class Item { String key; String value; public Item(String key, String value) { this.key = key; this.value = value; } } } private class StatsRunner implements Runnable { public void run() { DaemonSocket socket = DaemonSocket.create ((ConfigurationService)controller.service(ConfigurationService.class), controller.getPreferences()); if (socket == null) { setStatusLater(XNap.tr("Could not establish connection to daemon.")); return; } socket.getDecoder().add(CSMessage.IS_STATISTICS,CSStatistics.class); socket.getDecoder().add(CSMessage.IS_RETURN,CSReturnValue.class); if (!socket.write(new CSStatisticsRequest())) { setStatusLater("Error sending request for statistics to gnunetd."); return; } int count = 0; int totalCounters = 1; while (count < totalCounters) { CSStatistics msg = (CSStatistics)socket.read(CSStatistics.class); if (msg == null) { setStatusLater("Error receiving reply for statistics from gnunetd."); return; } if (count == 0) { long t = CronService.toSeconds(CronService.time()) - msg.getStartTime(); setStatusLater(XNap.tr("Daemon Uptime: {0}", Formatter.formatLength(t))); totalCounters = msg.getTotalCounters(); } if (msg.getTotalCounters() != totalCounters) { setStatusLater("Corrupted data ?"); return; } Statistic[] stats = msg.getStatistics(); for (int i = 0; i < stats.length; i++) { statsTableModel.add(stats[i].getName(), "" + stats[i].get()); } count += stats.length; } socket.destroy(); } } }

The table below shows all metrics for FreewayPanel.java.

MetricValueDescription