Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.25 KB | None | 0 0
  1. package me.bubbles.twitchbot.gui.bottabs;
  2.  
  3. import java.applet.Applet;
  4. import java.applet.AudioClip;
  5. import java.awt.Color;
  6. import java.awt.Font;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.util.ArrayList;
  10.  
  11. import javax.swing.AbstractAction;
  12. import javax.swing.Action;
  13. import javax.swing.JButton;
  14. import javax.swing.JLabel;
  15. import javax.swing.JScrollPane;
  16. import javax.swing.JTextArea;
  17. import javax.swing.JTextPane;
  18. import javax.swing.KeyStroke;
  19. import javax.swing.SpringLayout;
  20. import javax.swing.text.BadLocationException;
  21. import javax.swing.text.DefaultCaret;
  22. import javax.swing.text.Style;
  23. import javax.swing.text.StyleConstants;
  24. import javax.swing.text.StyledDocument;
  25.  
  26. import me.bubbles.twitchbot.TwitchBot;
  27. import me.bubbles.twitchbot.command.PermissionLevel;
  28. import me.bubbles.twitchbot.gui.BotTab;
  29. import me.bubbles.twitchbot.gui.BotWindow;
  30. import me.bubbles.twitchbot.gui.bottabs.ConsoleTab.Level;
  31. import me.bubbles.twitchbot.utils.BotUser;
  32. import me.bubbles.twitchbot.utils.UserUtils;
  33.  
  34. public class ChatTab extends BotTab {
  35.     private static final long serialVersionUID = 1L;
  36.     private JScrollPane scrollPane;
  37.     private JTextArea txtrMessage;
  38.     private JScrollPane scrollPane_1;
  39.     private static JTextArea txtrViewers;
  40.  
  41.     private static JTextPane txtChat;
  42.     private static StyledDocument chat;
  43.     private static Style style;
  44.  
  45.     private static JLabel lblViewers;
  46.  
  47.     private static AudioClip clip;
  48.  
  49.     /**
  50.      * Create the panel.
  51.      */
  52.     public ChatTab() {
  53.         clip = Applet.newAudioClip(BotWindow.class.getResource("/me/bubbles/twitchbot/res/notify.wav"));
  54.  
  55.         setBackground(Color.DARK_GRAY);
  56.         SpringLayout springLayout = new SpringLayout();
  57.         setLayout(springLayout);
  58.  
  59.         JButton btnSend = new JButton("Send");
  60.         btnSend.addActionListener(new ActionListener() {
  61.             public void actionPerformed(ActionEvent arg0) {
  62.                 String message = txtrMessage.getText().replace(" i ", " I ");
  63.                 output(BotWindow.getTwitchBot().getBotName(), message);
  64.                 BotWindow.getTwitchBot().messageChannel(message);
  65.                 txtrMessage.setText("");
  66.                 txtrMessage.requestFocus();
  67.             }
  68.         });
  69.         btnSend.setBackground(Color.LIGHT_GRAY);
  70.         springLayout.putConstraint(SpringLayout.SOUTH, btnSend, -10, SpringLayout.SOUTH, this);
  71.         springLayout.putConstraint(SpringLayout.EAST, btnSend, -10, SpringLayout.EAST, this);
  72.         add(btnSend);
  73.  
  74.         scrollPane = new JScrollPane();
  75.         springLayout.putConstraint(SpringLayout.NORTH, scrollPane, -30, SpringLayout.SOUTH, this);
  76.         springLayout.putConstraint(SpringLayout.WEST, scrollPane, 10, SpringLayout.WEST, this);
  77.         springLayout.putConstraint(SpringLayout.SOUTH, scrollPane, -10, SpringLayout.SOUTH, this);
  78.         springLayout.putConstraint(SpringLayout.EAST, scrollPane, -10, SpringLayout.WEST, btnSend);
  79.         add(scrollPane);
  80.  
  81.         txtrMessage = new JTextArea();
  82.         txtrMessage.setForeground(Color.DARK_GRAY);
  83.         txtrMessage.setBackground(Color.LIGHT_GRAY);
  84.         txtrMessage.setWrapStyleWord(true);
  85.         txtrMessage.setLineWrap(true);
  86.         scrollPane.setViewportView(txtrMessage);
  87.  
  88.         scrollPane_1 = new JScrollPane();
  89.         springLayout.putConstraint(SpringLayout.WEST, scrollPane_1, 10, SpringLayout.WEST, this);
  90.         springLayout.putConstraint(SpringLayout.SOUTH, scrollPane_1, -10, SpringLayout.NORTH, scrollPane);
  91.         springLayout.putConstraint(SpringLayout.EAST, scrollPane_1, -218, SpringLayout.EAST, this);
  92.         add(scrollPane_1);
  93.  
  94.         txtChat = new JTextPane();
  95.         txtChat.setBackground(Color.LIGHT_GRAY);
  96.         chat = txtChat.getStyledDocument();
  97.         txtChat.setFont(new Font("Serif", Font.BOLD, 12));
  98.         txtChat.setEditable(false);
  99.         txtChat.setCaretPosition(txtChat.getDocument().getLength());
  100.         scrollPane_1.setViewportView(txtChat);
  101.  
  102.         JLabel lblChat = new JLabel("Chat");
  103.         springLayout.putConstraint(SpringLayout.NORTH, scrollPane_1, 6, SpringLayout.SOUTH, lblChat);
  104.         springLayout.putConstraint(SpringLayout.NORTH, lblChat, 0, SpringLayout.NORTH, this);
  105.         springLayout.putConstraint(SpringLayout.WEST, lblChat, 0, SpringLayout.WEST, scrollPane);
  106.         lblChat.setLabelFor(this);
  107.         lblChat.setForeground(new Color(0, 0, 0));
  108.         lblChat.setBackground(Color.LIGHT_GRAY);
  109.         add(lblChat);
  110.  
  111.         txtrViewers = new JTextArea();
  112.         txtrViewers.setEditable(false);
  113.         txtrViewers.setBackground(Color.LIGHT_GRAY);
  114.  
  115.         lblViewers = new JLabel("Viewers  Followers: " + TwitchBot.followers.size());
  116.         springLayout.putConstraint(SpringLayout.NORTH, lblViewers, 0, SpringLayout.NORTH, lblChat);
  117.         springLayout.putConstraint(SpringLayout.EAST, lblViewers, -90, SpringLayout.EAST, this);
  118.         add(lblViewers);
  119.  
  120.         JScrollPane scrollPane_2 = new JScrollPane();
  121.         springLayout.putConstraint(SpringLayout.NORTH, scrollPane_2, 20, SpringLayout.NORTH, this);
  122.         springLayout.putConstraint(SpringLayout.WEST, scrollPane_2, 6, SpringLayout.EAST, scrollPane_1);
  123.         springLayout.putConstraint(SpringLayout.SOUTH, scrollPane_2, -6, SpringLayout.NORTH, btnSend);
  124.         springLayout.putConstraint(SpringLayout.EAST, scrollPane_2, -10, SpringLayout.EAST, this);
  125.         scrollPane_2.add(txtrViewers);
  126.         scrollPane_2.setViewportView(txtrViewers);
  127.         add(scrollPane_2);
  128.  
  129.         DefaultCaret caret = (DefaultCaret) txtChat.getCaret();
  130.         caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
  131.         caret.setLocation(0, chat.getLength());
  132.         DefaultCaret caret_1 = (DefaultCaret) txtrMessage.getCaret();
  133.         caret_1.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
  134.  
  135.         txtrMessage.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "sendMessage");
  136.  
  137.         Action sendMessage = new AbstractAction() {
  138.             private static final long serialVersionUID = 1L;
  139.  
  140.             public void actionPerformed(ActionEvent arg0) {
  141.                 String message = txtrMessage.getText().replace(" i ", " I ");
  142.                 output(BotWindow.getTwitchBot().getBotName(), message);
  143.                 BotWindow.getTwitchBot().messageChannel(message);
  144.                 BotWindow.getTwitchBot().refreshViewers();
  145.                 txtrMessage.setText("");
  146.                 txtrMessage.requestFocus();
  147.             }
  148.         };
  149.  
  150.         txtrMessage.getActionMap().put("sendMessage", sendMessage);
  151.     }
  152.  
  153.     static int i = 0;
  154.  
  155.     public static void output(String name, String message) {
  156.         message.replace("Kappa", "PLACE HOLDER");
  157.         if (BotWindow.getTwitchBot().inStream()) {
  158.             if (message.toLowerCase().contains(BotWindow.getTwitchBot().getBotName().toLowerCase())
  159.                     || message.toLowerCase().contains(BotWindow.getTwitchBot().getChannel(false).toLowerCase())) {
  160.                 clip.play();
  161.             }
  162.  
  163.             if (i == 0) {
  164.                 style = txtChat.addStyle("I'm a Style", null);
  165.                 StyleConstants.setForeground(style, Color.GRAY);
  166.                 StyleConstants.isBold(style);
  167.  
  168.                 try {
  169.                     chat.insertString(chat.getLength(), "[" + UserUtils.capitalizeName(name) + "]: " + message + "\n",
  170.                             style);
  171.                 } catch (BadLocationException e) {
  172.                 }
  173.                 i = 1;
  174.             } else {
  175.                 style = txtChat.addStyle("I'm a Style", null);
  176.                 StyleConstants.setForeground(style, Color.DARK_GRAY);
  177.                 StyleConstants.isBold(style);
  178.  
  179.                 try {
  180.                     chat.insertString(chat.getLength(), "[" + UserUtils.capitalizeName(name) + "]: " + message + "\n",
  181.                             style);
  182.                 } catch (BadLocationException e) {
  183.                 }
  184.                 i = 0;
  185.             }
  186.         } else
  187.             ConsoleTab.output(Level.Alert, "You must be in a channel before you can chat!");
  188.     }
  189.     public static void viewers(ArrayList<BotUser> viewers) {
  190.         txtrViewers.setText("");
  191.         lblViewers.setText("Viewers: " + viewers.size() + "   Followers: " + TwitchBot.followers.size());
  192.  
  193.         for (BotUser user : TwitchBot.viewers) {
  194.             if (user.getPermissionLevel() == PermissionLevel.MOD) {
  195.                 if (user.getUsername().equalsIgnoreCase(BotWindow.getTwitchBot().getChannel(false))) {
  196.                     txtrViewers.append(UserUtils.capitalizeName(user.getUsername()) + " - Streamer\n");
  197.                 } else {
  198.                     if (TwitchBot.followers.contains(user.getUsername())) {
  199.                         txtrViewers.append(UserUtils.capitalizeName(user.getUsername()) + "- Following - Mod\n");
  200.                     } else {
  201.                         txtrViewers.append(UserUtils.capitalizeName(user.getUsername()) + "- Mod\n");
  202.                     }
  203.                 }
  204.             } else {
  205.                 if (TwitchBot.followers.contains(user.getUsername().toLowerCase())) {
  206.                     txtrViewers.append(UserUtils.capitalizeName(user.getUsername()) + " - Following\n");
  207.                 } else {
  208.                     txtrViewers.append(UserUtils.capitalizeName(user.getUsername()) + "\n");
  209.                 }
  210.             }
  211.         }
  212.     }
  213.  
  214.     public void load() {
  215.         this.setVisible(true);
  216.         txtrMessage.requestFocus();
  217.     }
  218.  
  219.     public void unload() {
  220.         this.setVisible(false);
  221.     }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement