Advertisement
LawyerDog

IRC Replay fixed

Jan 25th, 2012
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.49 KB | None | 0 0
  1. /* LAWYERDOG IS IN DA HAUS */
  2.  
  3. import java.io.*;
  4. import java.util.logging.Level;
  5. import java.util.logging.Logger;
  6. import javax.swing.*;
  7. import java.awt.*;
  8. import java.awt.event.*;
  9. import javax.swing.SwingUtilities;
  10.  
  11. public class IRCReplay extends JPanel implements ActionListener {
  12.     static private final String newline = "\n";
  13.     static private final int messageSpeed = 1000; /* The number of milliseconds
  14.      * that it will take to display 1 second of the log file. 1000 is normal
  15.      * speed, 500 would display messages twice as fast as normal. */
  16.     JButton selectButton, replayButton;
  17.     JTextArea log;
  18.     JFileChooser fileDialog;
  19.     JScrollPane logScrollPane;
  20.     JPanel buttonPanel;
  21.     FileInputStream fstream;
  22.     BufferedReader dstream;
  23.     boolean fileSelected = false;
  24.  
  25.     public IRCReplay() {
  26.         super(new BorderLayout());
  27.         fileDialog = new JFileChooser();
  28.  
  29.         //Log where all text output by the program will go
  30.         log = new JTextArea(18,37);
  31.         Font font = new Font("Times New Roman", Font.PLAIN, 18);
  32.         log.setFont(font);
  33.         System.out.println(log.getPreferredSize());
  34.         log.setMargin(new Insets(0,6,0,6));
  35.         log.setEditable(false);
  36.         log.setLineWrap(true);
  37.         log.setWrapStyleWord(true);
  38.        
  39.        
  40.         logScrollPane = new JScrollPane(log);
  41.  
  42.         selectButton = new JButton("Select File");
  43.         selectButton.addActionListener(this);
  44.  
  45.         replayButton = new JButton("Replay File");
  46.         replayButton.addActionListener(this);
  47.  
  48.         buttonPanel = new JPanel();
  49.         buttonPanel.add(selectButton);
  50.         buttonPanel.add(replayButton);
  51.  
  52.         add(buttonPanel, BorderLayout.PAGE_START);
  53.         add(logScrollPane, BorderLayout.CENTER);
  54.     }
  55.  
  56.     @Override public void actionPerformed(ActionEvent event) {
  57.         //When "Select File" is chosen, a file dialog opens, and the selected
  58.         //file is what will be used when "Replay File" is selected.
  59.         if (event.getSource() == selectButton) {
  60.             int returnVal = fileDialog.showOpenDialog(IRCReplay.this);
  61.             if (returnVal == JFileChooser.APPROVE_OPTION) {
  62.                 File file = fileDialog.getSelectedFile();
  63.                 try
  64.                 {
  65.                     fstream = new FileInputStream(file);
  66.                     dstream = new BufferedReader(new InputStreamReader(fstream));
  67.                     log.setText(""); //Clears log
  68.                     log.append("Selected file: " + file.getName() + newline);
  69.                     fileSelected = true;
  70.                 }
  71.                 catch (IOException e)
  72.                 {
  73.                     System.err.println ("Error: "+e.getMessage());
  74.                     System.exit(-1);
  75.                 }
  76.             } else {
  77.                 //If the user hits cancel in the file open dialog
  78.                 log.append("No file was selected." + newline);
  79.             }
  80.  
  81.             //"Replay File" is chosen
  82.         } else if (event.getSource() == replayButton) {
  83.             if (fileSelected == true){
  84.                     String currentLine;
  85.                    
  86.                     Runnable runnable = new appendingLines();
  87.                     Thread thread = new Thread(runnable);
  88.                     thread.start();
  89.                    
  90.             } else {
  91.                 log.append("Please select a file before attempting to replay."
  92.                     + newline);
  93.             }
  94.         }
  95.     }
  96.  
  97.     private static void runGUI() {
  98.         //Method for creating and formatting GUI
  99.         JFrame frame = new JFrame("IRC Replay");
  100.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  101.         frame.add(new IRCReplay());
  102.         frame.pack();
  103.         frame.setVisible(true);
  104.        
  105.     }
  106.  
  107.     public static void main(String[] args) {
  108.         SwingUtilities.invokeLater(new Runnable() {
  109.                 @Override public void run() {
  110.                     runGUI();
  111.                 }
  112.             });
  113.     }
  114.  
  115.    
  116.     class appendingLines implements Runnable {
  117.         @Override public void run() {
  118.             String currentLine, fixedLine;
  119.             int newTimeStamp = 500, oldTimeStamp = -1;
  120.             try {
  121.                 while (dstream.ready() == true) {
  122.                     currentLine = dstream.readLine();
  123.                     fixedLine = validate(currentLine);
  124.                    
  125.                     if (!fixedLine.equals("invalid")){
  126.                         newTimeStamp = delayTime(fixedLine);
  127.                         if(newTimeStamp < oldTimeStamp){
  128.                             oldTimeStamp = oldTimeStamp - 43200;
  129.                         }
  130.                         try {
  131.                             if(oldTimeStamp == -1){
  132.                                 Thread.sleep(500);
  133.                             }else{
  134.                                 Thread.sleep(messageSpeed*(newTimeStamp - oldTimeStamp));
  135.                                 //System.out.println(newTimeStamp - oldTimeStamp);
  136.                             }
  137.                         } catch (InterruptedException ex) {
  138.                             Logger.getLogger(IRCReplay.class.getName()).log(Level.SEVERE, null, ex);
  139.                         }
  140.                         oldTimeStamp = newTimeStamp;
  141.                         log.append(fixedLine + newline);
  142.                         log.setCaretPosition(log.getDocument().getLength());
  143.                     }
  144.                 }
  145.                 dstream.close();
  146.                 fileSelected = false;
  147.  
  148.             } catch (IOException e) {
  149.                 System.err.println("Error: " + e.getMessage());
  150.                 System.exit(-1);
  151.             }
  152.         }
  153.     }
  154.  
  155.     public static String validate(String line) {
  156.         int index = line.indexOf("] entered the room.")
  157.                 + line.indexOf("left the room (")
  158.                 + line.indexOf("has changed the topic");
  159.         if (index <= -1) {
  160.             return line;
  161.         } else {
  162.             return "invalid";
  163.         }
  164.     }
  165.  
  166.     public static int delayTime(String line) {
  167.         int timeStamp, pofix;
  168.         String hour, minute, second;
  169.        
  170.         pofix = line.indexOf(":");
  171.        
  172.         hour = line.substring(1, pofix);
  173.         minute = line.substring(pofix+1, pofix+3);
  174.         second = line.substring(pofix+4, pofix+6);
  175.  
  176.         timeStamp = 3600*(Integer.parseInt(hour))
  177.                 + 60*(Integer.parseInt(minute))
  178.                 + (Integer.parseInt(second));
  179.        
  180.         return timeStamp;
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement