com.okmich.twitanalysis.gui.ApplicationFrame.java Source code

Java tutorial

Introduction

Here is the source code for com.okmich.twitanalysis.gui.ApplicationFrame.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.okmich.twitanalysis.gui;

import com.okmich.twitanalysis.ActionExecutor;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;

/**
 *
 * @author datadev
 */
public class ApplicationFrame extends javax.swing.JFrame implements ActionExecutor {

    /**
     * The time series data.
     */
    private TimeSeries postiveSeries, negativeSeries, neutralSeries;

    private static final String POSITIVE = "POSITIVE";
    private static final String NEGATIVE = "NEGATIVE";
    private static final String NEUTRAL = "NEUTRAL";

    private int tweetsCount = 0;
    private JLabel tweetCountLabel;
    private final ExecutorService executorService;

    public ApplicationFrame() {
        initComponents();
        this.executorService = Executors.newFixedThreadPool(1);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0,
                400, Short.MAX_VALUE));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 300,
                Short.MAX_VALUE));
        JPanel p = new JPanel(new BorderLayout()); //PREFERRED!
        setChartPanel(p);
        getRootPane().add(p);
        setPreferredSize(new Dimension(800, 600));
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void setChartPanel(JPanel jpanel) {
        this.negativeSeries = new TimeSeries("Negative", Millisecond.class);
        this.postiveSeries = new TimeSeries("Positive", Millisecond.class);
        this.neutralSeries = new TimeSeries("Neutral", Millisecond.class);
        final TimeSeriesCollection dataset = new TimeSeriesCollection(this.negativeSeries);
        dataset.addSeries(this.postiveSeries);
        dataset.addSeries(this.neutralSeries);
        final JFreeChart chart = createChart(dataset);
        chart.getXYPlot().getRenderer(0).setSeriesPaint(0, Color.RED);
        chart.getXYPlot().getRenderer(0).setSeriesStroke(0, new BasicStroke(2.0f));
        chart.getXYPlot().getRenderer(0).setSeriesPaint(1, Color.GREEN);
        chart.getXYPlot().getRenderer(0).setSeriesStroke(1, new BasicStroke(2.0f));
        chart.getXYPlot().getRenderer(0).setSeriesPaint(2, Color.GRAY);
        chart.getXYPlot().getRenderer(0).setSeriesStroke(2, new BasicStroke(2.0f));
        chart.getPlot().setBackgroundPaint(Color.BLACK);

        final ChartPanel chartPanel = new ChartPanel(chart);

        final JPanel tweetCountPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));

        tweetCountLabel = new JLabel();
        setTweetCountLabel();
        tweetCountPanel.add(tweetCountLabel);

        jpanel.setLayout(new BorderLayout(10, 10));
        jpanel.add(chartPanel, BorderLayout.CENTER);
        jpanel.add(tweetCountPanel, BorderLayout.SOUTH);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(jpanel);
    }

    private void setTweetCountLabel() {
        tweetCountLabel.setText("Total tweet count: " + this.tweetsCount);
    }

    /**
     * Creates a sample chart.
     *
     * @param dataset the dataset.
     *
     * @return A sample chart.
     */
    private JFreeChart createChart(final XYDataset dataset) {
        final JFreeChart result = ChartFactory.createTimeSeriesChart("Twitter Sentiments Analysis", "Time",
                "Sentiment score", dataset, true, true, false);
        final XYPlot plot = result.getXYPlot();
        ValueAxis axis = plot.getDomainAxis();
        axis.setAutoRange(true);
        axis.setFixedAutoRange(60000.0); // 60 seconds
        axis = plot.getRangeAxis();
        axis.setRange(0.0, 50.0);
        return result;
    }

    @Override
    public void executeAction(final Map<String, Integer> data) {
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                int val = 0;
                if (data.containsKey(POSITIVE)) {
                    val = data.get(POSITIVE);
                    postiveSeries.add(new Millisecond(), val);
                    tweetsCount += val;
                }
                if (data.containsKey(NEGATIVE)) {
                    val = data.get(NEGATIVE);
                    negativeSeries.add(new Millisecond(), val);
                    tweetsCount += val;
                }
                if (data.containsKey(NEUTRAL)) {
                    val = data.get(NEUTRAL);
                    neutralSeries.add(new Millisecond(), val);
                    tweetsCount += val;
                }
                setTweetCountLabel();
            }
        });
    }
}