Java tutorial
/* * 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; import java.awt.Color; import java.awt.event.KeyEvent; import java.text.DecimalFormat; import java.util.ArrayList; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextPane; import javax.swing.UIDefaults; import javax.swing.text.AttributeSet; import javax.swing.text.SimpleAttributeSet; import javax.swing.text.StyleConstants; import javax.swing.text.StyleContext; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.labels.PieSectionLabelGenerator; import org.jfree.chart.labels.StandardPieSectionLabelGenerator; import org.jfree.chart.plot.PiePlot; import org.jfree.data.general.DefaultPieDataset; import org.jfree.data.general.PieDataset; /** * * @author Shan */ public class NewJFrame extends javax.swing.JFrame { ArrayList<Tweet> tweetlist = new ArrayList<Tweet>(); TweetExtractor extractor; static ArrayList<TweetAnalyser> positivetweets = new ArrayList<TweetAnalyser>(); static ArrayList<TweetAnalyser> negativetweets = new ArrayList<TweetAnalyser>(); static ArrayList<TweetAnalyser> neutraltweets = new ArrayList<TweetAnalyser>(); static int alltweetNumber; /** * Creates new form NewJFrame */ public NewJFrame() { initComponents(); textPane.setText("Hello"); Color bgColor = Color.BLACK; UIDefaults defaults = new UIDefaults(); defaults.put("TextPane[Enabled].backgroundPainter", bgColor); textPane.putClientProperty("Nimbus.Overrides", defaults); textPane.putClientProperty("Nimbus.Overrides.InheritDefaults", true); textPane.setBackground(bgColor); } /** * 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() { jLabel4 = new javax.swing.JLabel(); jPanel1 = new javax.swing.JPanel(); jLabel8 = new javax.swing.JLabel(); textField = new javax.swing.JTextField(); searchButton = new javax.swing.JButton(); viewPositiveButton = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); positiveTweetCount = new javax.swing.JLabel(); neutralTweetCount = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); negativeTweetCount = new javax.swing.JLabel(); viewNegativeButton = new javax.swing.JButton(); viewNeutralButton = new javax.swing.JButton(); jScrollPane2 = new javax.swing.JScrollPane(); textPane = new javax.swing.JTextPane(); viewResult = new javax.swing.JButton(); jLabel4.setText("jLabel4"); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Tweets"); jPanel1.setBackground(new java.awt.Color(255, 255, 255)); jLabel8.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N jLabel8.setText("Handle:"); textField.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N textField.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { textFieldActionPerformed(evt); } }); textField.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { textFieldKeyTyped(evt); } }); searchButton.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N searchButton.setText("Search"); searchButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { searchButtonActionPerformed(evt); } }); viewPositiveButton.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N viewPositiveButton.setText("View"); viewPositiveButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { viewPositiveButtonActionPerformed(evt); } }); jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N jLabel1.setText("Positive Tweets :"); positiveTweetCount.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N positiveTweetCount.setText("0"); neutralTweetCount.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N neutralTweetCount.setText("0"); jLabel2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N jLabel2.setText("Netural Tweets :"); jLabel3.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N jLabel3.setText("Negative Tweets :"); negativeTweetCount.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N negativeTweetCount.setText("0"); viewNegativeButton.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N viewNegativeButton.setText("View"); viewNegativeButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { viewNegativeButtonActionPerformed(evt); } }); viewNeutralButton.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N viewNeutralButton.setText("View"); viewNeutralButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { viewNeutralButtonActionPerformed(evt); } }); textPane.setBackground(new java.awt.Color(0, 0, 0)); jScrollPane2.setViewportView(textPane); viewResult.setText("View Chart"); viewResult.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { viewResultActionPerformed(evt); } }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jScrollPane2) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1).addComponent(jLabel2)) .addGap(26, 26, 26)) .addGroup( javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout .createSequentialGroup().addComponent(jLabel3) .addGap(18, 18, 18))) .addGroup(jPanel1Layout .createParallelGroup( javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(neutralTweetCount, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE) .addComponent(positiveTweetCount, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent( negativeTweetCount, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(18, 18, 18) .addGroup(jPanel1Layout .createParallelGroup( javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(viewNeutralButton, javax.swing.GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE) .addComponent(viewPositiveButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(viewNegativeButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup().addComponent(jLabel8) .addGap(18, 18, 18).addComponent(textField).addGap(18, 18, 18) .addComponent(searchButton, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap()) .addGroup(jPanel1Layout.createSequentialGroup().addGap(227, 227, 227).addComponent(viewResult) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addGap(18, 18, 18).addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(textField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(searchButton).addComponent(jLabel8)).addGap(11, 11, 11) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1).addComponent(positiveTweetCount) .addComponent(viewPositiveButton)) .addGap(14, 14, 14) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(viewNeutralButton).addComponent(jLabel2) .addComponent(neutralTweetCount)) .addGap(18, 18, 18) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(viewNegativeButton) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3).addComponent(negativeTweetCount))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(viewResult) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap())); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent( jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); setSize(new java.awt.Dimension(571, 505)); setLocationRelativeTo(null); }// </editor-fold>//GEN-END:initComponents private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed new Thread(new Runnable() { @Override public void run() { searchTweets(); } }).start(); }//GEN-LAST:event_searchButtonActionPerformed private void viewPositiveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_viewPositiveButtonActionPerformed displayPositive(); }//GEN-LAST:event_viewPositiveButtonActionPerformed private void viewNeutralButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_viewNeutralButtonActionPerformed displayNeutral(); }//GEN-LAST:event_viewNeutralButtonActionPerformed private void viewNegativeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_viewNegativeButtonActionPerformed displayNegative(); }//GEN-LAST:event_viewNegativeButtonActionPerformed private void textFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textFieldKeyTyped // TODO add your handling code here: if (evt.getKeyChar() == KeyEvent.VK_ENTER) { searchTweets(); } }//GEN-LAST:event_textFieldKeyTyped private void textFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textFieldActionPerformed // TODO add your handling code here: }//GEN-LAST:event_textFieldActionPerformed private void viewResultActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_viewResultActionPerformed JFrame f = new JFrame(); f.add(createPiePanel()); f.pack(); f.setVisible(true); f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); }//GEN-LAST:event_viewResultActionPerformed public static JPanel createPiePanel() { JFreeChart pieChart = createPieChart(createPieDataset()); return new ChartPanel(pieChart); } // BUILD THE PIE CHART private static JFreeChart createPieChart(PieDataset dataset) { JFreeChart chart = ChartFactory.createPieChart("Chart Title", // chart title dataset, // data true, // include legend true, false); PiePlot plot = (PiePlot) chart.getPlot(); plot.setSectionOutlinesVisible(false); plot.setNoDataMessage("No data available"); PieSectionLabelGenerator gen = new StandardPieSectionLabelGenerator("{0}: {2}", new DecimalFormat("0"), new DecimalFormat("0%")); plot.setLabelGenerator(gen); return chart; } // CREATE THE PIE CHART DATA private static PieDataset createPieDataset() { DefaultPieDataset dataset = new DefaultPieDataset(); dataset.setValue("positive tweets", new Double((double) positivetweets.size())); dataset.setValue("Negative tweets", new Double((double) negativetweets.size())); dataset.setValue("Neutral tweets", new Double((double) neutraltweets.size())); dataset.setValue("Unknown", new Double((double) (alltweetNumber - (positivetweets.size() + negativetweets.size() + neutraltweets.size())))); return dataset; } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NewJFrame().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel8; private javax.swing.JPanel jPanel1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JLabel negativeTweetCount; private javax.swing.JLabel neutralTweetCount; private javax.swing.JLabel positiveTweetCount; private javax.swing.JButton searchButton; private javax.swing.JTextField textField; private javax.swing.JTextPane textPane; private javax.swing.JButton viewNegativeButton; private javax.swing.JButton viewNeutralButton; private javax.swing.JButton viewPositiveButton; private javax.swing.JButton viewResult; // End of variables declaration//GEN-END:variables private void displayPositive() { textPane.setText(""); if (positivetweets != null) { for (TweetAnalyser analyser : positivetweets) { appendToPane(textPane, analyser.toString() + "\n", Color.green); } } } private void displayNeutral() { textPane.setText(""); if (neutraltweets != null) { for (TweetAnalyser analyser : neutraltweets) { appendToPane(textPane, analyser.toString() + "\n", Color.yellow); } } } private void displayNegative() { textPane.setText(""); if (negativetweets != null) { for (TweetAnalyser analyser : negativetweets) { appendToPane(textPane, analyser.toString() + "\n", Color.red); } } } private void appendToPane(JTextPane tp, String msg, Color c) { StyleContext sc = StyleContext.getDefaultStyleContext(); AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c); // aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console"); // aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED); int len = tp.getDocument().getLength(); tp.setCaretPosition(len); tp.setCharacterAttributes(aset, false); tp.replaceSelection(msg); } private void searchTweets() { textPane.setText(""); if (textField.getText().equals("")) { JOptionPane.showMessageDialog(this, "Please enter a user handle."); return; } try { tweetlist.clear(); positivetweets.clear(); negativetweets.clear(); neutraltweets.clear(); alltweetNumber = 0; extractor = new TweetExtractor(); tweetlist = extractor.retrieveTweets(textField.getText());//Nuwan_Prabhath TweetAnalyser ta; String tweet_status; if (tweetlist.isEmpty()) { textPane.setText("No Tweets for given handle"); System.out.println("Empty"); // System.exit(0); } else { System.out.println("tweetlist.size(): " + tweetlist.size()); alltweetNumber = tweetlist.size(); for (int j = 0; j < tweetlist.size(); j++) { ta = new TweetAnalyser(); ta.setTweet(tweetlist.get(j).getText()); tweet_status = SentimentalResponse.readJsonFromUrl(tweetlist.get(j).getText()); ta.setTweet_status(tweet_status); switch (tweet_status) { case "positive": System.out.println("Tweet: " + ta.toString()); positivetweets.add(ta); appendToPane(textPane, ta.toString() + "\n", Color.green); break; case "negative": System.out.println("J" + j); negativetweets.add(ta); appendToPane(textPane, ta.toString() + "\n", Color.red); break; case "neutral": System.out.println("J" + j); neutraltweets.add(ta); appendToPane(textPane, ta.toString() + "\n", Color.yellow); break; } } positiveTweetCount.setText(positivetweets.size() + ""); neutralTweetCount.setText(neutraltweets.size() + ""); negativeTweetCount.setText(negativetweets.size() + ""); } } catch (Exception e) { System.out.println(e.getMessage()); } } }