August 4, 2014

How to Visualise Sent Links

Finally, a GUI for my shared links data. It uses only standard JDK methods, and the source is below, enjoy:

import java.awt.BorderLayout;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Date;
import java.util.Scanner;
import java.util.zip.GZIPInputStream;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

/**
 * Visualises my shared links
 * @author Hasan Diwan 
 */
public class LinksVisualiser extends JFrame {
    JTable table;
    DefaultTableModel model;
    JButton closeButton, webButton;
 /**
  * Takes data from a CSV file and places it into a table for display.
  * @param source - a reference to the file where the CSV data is located.
  */
 static String title = "Shared Links";
 public LinksVisualiser(String source) {
  super(title);
  table = new JTable();
  JScrollPane scroll = new JScrollPane(table);
  String[] colNames = { "Timestamp", "Recipient", "Link"};
  model = new DefaultTableModel(colNames, 0);
  InputStream is;
  try {
   if(source.indexOf("http")==0) {
    URL facultyURL = new URL(source);
    is = new GZIPInputStream(facultyURL.openStream());
   }
   else { //local file?
    File f = new File(source);
    is = new GZIPInputStream(new FileInputStream(f));
   }
   insertData(is);
   //table.getColumnModel().getColumn(0).setCellRenderer(new CustomCellRenderer());
  }
  catch(IOException ioe) {
   JOptionPane.showMessageDialog(this, ioe, "Error reading data", JOptionPane.ERROR_MESSAGE);
  }

  JPanel buttonPanel = new JPanel();
  closeButton = new JButton("Close");
  webButton = new JButton("Weblog");
  buttonPanel.add(closeButton);
  buttonPanel.add(new JLabel("   You can download this file from our site: "));
  buttonPanel.add(webButton);

  getContentPane().add(scroll, BorderLayout.CENTER);
  getContentPane().add(buttonPanel, BorderLayout.SOUTH);
  pack();
 }

 /**
  * Places the data from the specified stream into this table for display.  The data from the file must be in CSV format
  * @param is - an input stream which could be from a file or a network connection or URL.
  */
 void insertData(InputStream is) {
  Scanner scan = new Scanner(is);
  scan.nextLine();
  String[] array;
  while (scan.hasNextLine()) {
   String line = scan.nextLine();
   if(line.indexOf(",")>-1)
    array = line.split(",");
   else
    array = line.split("\t");
   Object[] data = new Object[array.length];
   for (int i = 0; i < array.length; i++)
    data[i] = array[i];
   Date time = new Date(new Long((String)data[0])*1000);
   data[0] = time;
   model.addRow(data);
  }
  table.setModel(model);
 } 

 public static void main(String args[]) {
  LinksVisualiser l = new LinksVisualiser("http://hasan.d8u.us/sent_links.csv.gz");
  l.setVisible(true);
  l.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
}

No comments:

Post a Comment