Advertisement
spacewarp

TweetPanel

Nov 25th, 2011
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.18 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. /*
  7.  * TweetPanel.java
  8.  *
  9.  * Created on 2011.11.05., 0:00:26
  10.  */
  11. package net.rothens.twitt3ns.gui;
  12.  
  13. import java.awt.Color;
  14. import java.awt.Desktop;
  15. import java.awt.event.ActionEvent;
  16. import java.awt.event.ActionListener;
  17. import java.io.IOException;
  18. import java.net.URI;
  19. import java.net.URISyntaxException;
  20. import java.util.Iterator;
  21. import java.util.LinkedList;
  22. import java.util.List;
  23. import javax.swing.JLabel;
  24. import javax.swing.border.LineBorder;
  25. import javax.swing.text.html.HTMLEditorKit;
  26. import net.rothens.twitt3ns.Twitt3ns;
  27. import twitter4j.Status;
  28. import twitter4j.URLEntity;
  29.  
  30. /**
  31.  *
  32.  * @author Rothens
  33.  */
  34. public class TweetPanel extends javax.swing.JPanel {
  35.  
  36.     private Twitt3ns tw = Twitt3ns.tw;
  37.     private Status s;
  38.     public static TweetPanel instance;
  39.  
  40.     /** Creates new form TweetPanel */
  41.     public TweetPanel(Status s) {
  42.         this.s = s;
  43.         instance = this;
  44.  
  45.  
  46.         initComponents();
  47.        
  48.  
  49.         picture.setIcon(tw.mw.createImageIcon("/res/default_normal.png", "Profilkép"));
  50.         picture.setIcon(tw.mw.createImageFromURL(s.getUser().getProfileImageURL(), "profilkép", 3));
  51.         displayName.setText(s.getUser().getScreenName());
  52.  
  53.  
  54.         lblTime.setText(getTimeAgo(s.getCreatedAt().getTime()));
  55.         tweet.setEditorKit(new HTMLEditorKit());
  56.         String text = s.getText();
  57.        
  58.         ((TwitterPane)tweet).updateText(s);
  59.  
  60.  
  61.         tweets.setText(s.getUser().getStatusesCount() + "");
  62.         btnFav.setSelected(s.isFavorited());
  63.         //RT button
  64.         btnRT.setText(null);
  65.         btnRT.setIcon(tw.mw.createImageIcon("/res/rt.png", "RT"));
  66.         btnRT.setRolloverIcon(tw.mw.createImageIcon("/res/rt_hover.png", "RT"));
  67.         btnRT.setPressedIcon(tw.mw.createImageIcon("/res/rt_selected.png", "RT"));
  68.         btnRT.setActionCommand("RT");
  69.         btnRT.addActionListener(new tweetClick());
  70.         //Reply button
  71.         btnReply.setText(null);
  72.         btnReply.setIcon(tw.mw.createImageIcon("/res/reply_normal.png", "reply"));
  73.         btnReply.setRolloverIcon(tw.mw.createImageIcon("/res/reply_hover.png", "reply"));
  74.         btnReply.setPressedIcon(tw.mw.createImageIcon("/res/reply_selected.png", "reply"));
  75.         btnReply.setActionCommand("Reply");
  76.         btnReply.addActionListener(new tweetClick());
  77.         //Favorite button
  78.         btnFav.setText(null);
  79.         btnFav.setIcon(tw.mw.createImageIcon("/res/fav_off.png", "reply"));
  80.         btnFav.setRolloverIcon(tw.mw.createImageIcon("/res/fav_off_rollover.png", "favorite"));
  81.         btnFav.setSelectedIcon(tw.mw.createImageIcon("/res/fav_on.png", "favorite"));
  82.         btnFav.setRolloverSelectedIcon(tw.mw.createImageIcon("/res/fav_on_rollover.png", "favorite"));
  83.  
  84.     }
  85.  
  86.     public String getTimeAgo(long from) {
  87.         long curr = System.currentTimeMillis();
  88.  
  89.         long diff = curr / 1000 - from / 1000;
  90.  
  91.         if (diff < 30) {
  92.             return "most";
  93.         } else if (diff < 60) {
  94.             return "fél perce";
  95.         } else if (diff < 1800) {
  96.             return (int) Math.floor(diff / 60) + " perce";
  97.         } else if (diff < 3600) {
  98.             return "fél órája";
  99.         } else if (diff < 86400) {
  100.             return (int) Math.floor(diff / 3600) + " órája";
  101.         } else if (diff < 2592000) {
  102.             return (int) Math.floor(diff / 86400) + " napja";
  103.         } else {
  104.             return "jó rég";
  105.         }
  106.  
  107.     }
  108.  
  109.     public class tweetClick implements ActionListener {
  110.  
  111.         @Override
  112.         public void actionPerformed(ActionEvent e) {
  113.             if (e.getActionCommand() == null) {
  114.                 return;
  115.             }
  116.             if (e.getActionCommand().equals("RT")) {
  117.                 tw.t.retweetStatus(s.getId());
  118.             } else if (e.getActionCommand().equals("Reply")) {
  119.                 ReplyFrame rf = new ReplyFrame(s);
  120.                 rf.setVisible(true);
  121.  
  122.             }
  123.         }
  124.     }
  125.  
  126.     /** This method is called from within the constructor to
  127.      * initialize the form.
  128.      * WARNING: Do NOT modify this code. The content of this method is
  129.      * always regenerated by the Form Editor.
  130.      */
  131.     @SuppressWarnings("unchecked")
  132.         // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  133.         private void initComponents() {
  134.  
  135.                 picture = new javax.swing.JLabel();
  136.                 tweets = new javax.swing.JLabel();
  137.                 displayName = new javax.swing.JLabel();
  138.                 btnRT = new javax.swing.JButton();
  139.                 btnReply = new javax.swing.JButton();
  140.                 jScrollPane1 = new javax.swing.JScrollPane();
  141.                 tweet = new TwitterPane();
  142.                 lblTime = new javax.swing.JLabel();
  143.                 btnFav = new javax.swing.JToggleButton();
  144.  
  145.                 setBackground(new java.awt.Color(255, 255, 255));
  146.                 setBorder(javax.swing.BorderFactory.createEtchedBorder());
  147.                 setAlignmentX(0.0F);
  148.                 setAlignmentY(0.0F);
  149.                 setMaximumSize(new java.awt.Dimension(500, 120));
  150.                 setMinimumSize(new java.awt.Dimension(500, 120));
  151.  
  152.                 picture.setMaximumSize(new java.awt.Dimension(48, 48));
  153.                 picture.setMinimumSize(new java.awt.Dimension(48, 48));
  154.                 picture.setPreferredSize(new java.awt.Dimension(48, 48));
  155.  
  156.                 tweets.setText("1122");
  157.  
  158.                 displayName.setText("@name");
  159.                 displayName.setToolTipText("Profil megtekintése");
  160.                 displayName.addMouseListener(new java.awt.event.MouseAdapter() {
  161.                         public void mouseClicked(java.awt.event.MouseEvent evt) {
  162.                                 displayNameMouseClicked(evt);
  163.                         }
  164.                         public void mouseEntered(java.awt.event.MouseEvent evt) {
  165.                                 displayNameMouseEntered(evt);
  166.                         }
  167.                         public void mouseExited(java.awt.event.MouseEvent evt) {
  168.                                 displayNameMouseExited(evt);
  169.                         }
  170.                 });
  171.  
  172.                 btnRT.setText("RT");
  173.                 btnRT.setToolTipText("ReTweet");
  174.                 btnRT.setBorder(null);
  175.                 btnRT.setContentAreaFilled(false);
  176.  
  177.                 btnReply.setText("Reply");
  178.                 btnReply.setToolTipText("válasz");
  179.                 btnReply.setBorder(null);
  180.                 btnReply.setBorderPainted(false);
  181.                 btnReply.setContentAreaFilled(false);
  182.  
  183.                 jScrollPane1.setBorder(null);
  184.                 jScrollPane1.setHorizontalScrollBar(null);
  185.  
  186.                 tweet.setBorder(null);
  187.                 tweet.setEditable(false);
  188.                 tweet.setText("qwertzuiop asdfghjkl qwert zui qwe asdfghjk lqwert zfdscx gerdasdea fd gflrp afde rptlgn adfd sdf vcx ds as df er ddfdqw qwertz asd 112 116");
  189.                 tweet.setAlignmentX(0.0F);
  190.                 tweet.setAlignmentY(0.0F);
  191.                 jScrollPane1.setViewportView(tweet);
  192.  
  193.                 lblTime.setForeground(new java.awt.Color(204, 204, 204));
  194.                 lblTime.setText("5 perce");
  195.  
  196.                 btnFav.setText("Favorite");
  197.                 btnFav.setToolTipText("tetszik");
  198.                 btnFav.setActionCommand("btnFav");
  199.                 btnFav.setBorder(null);
  200.                 btnFav.setContentAreaFilled(false);
  201.  
  202.                 javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
  203.                 this.setLayout(layout);
  204.                 layout.setHorizontalGroup(
  205.                         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  206.                         .addGroup(layout.createSequentialGroup()
  207.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
  208.                                         .addComponent(tweets, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  209.                                         .addComponent(picture, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  210.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  211.                                         .addGroup(layout.createSequentialGroup()
  212.                                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  213.                                                         .addGroup(layout.createSequentialGroup()
  214.                                                                 .addGap(28, 28, 28)
  215.                                                                 .addComponent(btnRT)
  216.                                                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  217.                                                                 .addComponent(btnReply)
  218.                                                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  219.                                                                 .addComponent(btnFav))
  220.                                                         .addGroup(layout.createSequentialGroup()
  221.                                                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  222.                                                                 .addComponent(jScrollPane1)))
  223.                                                 .addContainerGap())
  224.                                         .addGroup(layout.createSequentialGroup()
  225.                                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  226.                                                 .addComponent(displayName)
  227.                                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 260, Short.MAX_VALUE)
  228.                                                 .addComponent(lblTime)
  229.                                                 .addGap(110, 110, 110))))
  230.                 );
  231.                 layout.setVerticalGroup(
  232.                         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  233.                         .addGroup(layout.createSequentialGroup()
  234.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  235.                                         .addComponent(picture, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  236.                                         .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
  237.                                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  238.                                                         .addComponent(displayName)
  239.                                                         .addComponent(lblTime))
  240.                                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
  241.                                                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)))
  242.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  243.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  244.                                         .addComponent(tweets)
  245.                                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  246.                                                 .addComponent(btnRT)
  247.                                                 .addComponent(btnReply)
  248.                                                 .addComponent(btnFav)))
  249.                                 .addContainerGap())
  250.                 );
  251.         }// </editor-fold>                        
  252.  
  253.     private void displayNameMouseClicked(java.awt.event.MouseEvent evt) {                                        
  254.         URI uri = null;
  255.         try {
  256.             uri = new URI("http://twitter.com/" + ((JLabel) evt.getComponent()).getText());
  257.         } catch (URISyntaxException e) {
  258.         }
  259.         if (uri != null) {
  260.             try {
  261.                 Desktop.getDesktop().browse(uri);
  262.             } catch (IOException ex) {
  263.             }
  264.         }
  265.     }                                        
  266.  
  267.     private void displayNameMouseEntered(java.awt.event.MouseEvent evt) {                                        
  268.         displayName.setBorder(new LineBorder(Color.BLACK, 1, true));
  269.     }                                        
  270.  
  271.     private void displayNameMouseExited(java.awt.event.MouseEvent evt) {                                        
  272.         displayName.setBorder(null);
  273.     }                                      
  274.  
  275.     public void updateTime() {
  276.         lblTime.setText(getTimeAgo(s.getCreatedAt().getTime()));
  277.     }
  278.         // Variables declaration - do not modify                    
  279.         private javax.swing.JToggleButton btnFav;
  280.         private javax.swing.JButton btnRT;
  281.         private javax.swing.JButton btnReply;
  282.         private javax.swing.JLabel displayName;
  283.         private javax.swing.JScrollPane jScrollPane1;
  284.         private javax.swing.JLabel lblTime;
  285.         private javax.swing.JLabel picture;
  286.         private javax.swing.JEditorPane tweet;
  287.         private javax.swing.JLabel tweets;
  288.         // End of variables declaration                  
  289. }
  290.  
  291.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement