Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* LAWYERDOG IS IN DA HAUS */
- import java.io.*;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.SwingUtilities;
- public class IRCReplay extends JPanel implements ActionListener {
- static private final String newline = "\n";
- static private final int messageSpeed = 1000; /* The number of milliseconds
- * that it will take to display 1 second of the log file. 1000 is normal
- * speed, 500 would display messages twice as fast as normal. */
- JButton selectButton, replayButton;
- JTextArea log;
- JFileChooser fileDialog;
- JScrollPane logScrollPane;
- JPanel buttonPanel;
- FileInputStream fstream;
- BufferedReader dstream;
- boolean fileSelected = false;
- public IRCReplay() {
- super(new BorderLayout());
- fileDialog = new JFileChooser();
- //Log where all text output by the program will go
- log = new JTextArea(18,37);
- Font font = new Font("Times New Roman", Font.PLAIN, 18);
- log.setFont(font);
- System.out.println(log.getPreferredSize());
- log.setMargin(new Insets(0,6,0,6));
- log.setEditable(false);
- log.setLineWrap(true);
- log.setWrapStyleWord(true);
- logScrollPane = new JScrollPane(log);
- selectButton = new JButton("Select File");
- selectButton.addActionListener(this);
- replayButton = new JButton("Replay File");
- replayButton.addActionListener(this);
- buttonPanel = new JPanel();
- buttonPanel.add(selectButton);
- buttonPanel.add(replayButton);
- add(buttonPanel, BorderLayout.PAGE_START);
- add(logScrollPane, BorderLayout.CENTER);
- }
- @Override public void actionPerformed(ActionEvent event) {
- //When "Select File" is chosen, a file dialog opens, and the selected
- //file is what will be used when "Replay File" is selected.
- if (event.getSource() == selectButton) {
- int returnVal = fileDialog.showOpenDialog(IRCReplay.this);
- if (returnVal == JFileChooser.APPROVE_OPTION) {
- File file = fileDialog.getSelectedFile();
- try
- {
- fstream = new FileInputStream(file);
- dstream = new BufferedReader(new InputStreamReader(fstream));
- log.setText(""); //Clears log
- log.append("Selected file: " + file.getName() + newline);
- fileSelected = true;
- }
- catch (IOException e)
- {
- System.err.println ("Error: "+e.getMessage());
- System.exit(-1);
- }
- } else {
- //If the user hits cancel in the file open dialog
- log.append("No file was selected." + newline);
- }
- //"Replay File" is chosen
- } else if (event.getSource() == replayButton) {
- if (fileSelected == true){
- String currentLine;
- Runnable runnable = new appendingLines();
- Thread thread = new Thread(runnable);
- thread.start();
- } else {
- log.append("Please select a file before attempting to replay."
- + newline);
- }
- }
- }
- private static void runGUI() {
- //Method for creating and formatting GUI
- JFrame frame = new JFrame("IRC Replay");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.add(new IRCReplay());
- frame.pack();
- frame.setVisible(true);
- }
- public static void main(String[] args) {
- SwingUtilities.invokeLater(new Runnable() {
- @Override public void run() {
- runGUI();
- }
- });
- }
- class appendingLines implements Runnable {
- @Override public void run() {
- String currentLine, fixedLine;
- int newTimeStamp = 500, oldTimeStamp = -1;
- try {
- while (dstream.ready() == true) {
- currentLine = dstream.readLine();
- fixedLine = validate(currentLine);
- if (!fixedLine.equals("invalid")){
- newTimeStamp = delayTime(fixedLine);
- if(newTimeStamp < oldTimeStamp){
- oldTimeStamp = oldTimeStamp - 43200;
- }
- try {
- if(oldTimeStamp == -1){
- Thread.sleep(500);
- }else{
- Thread.sleep(messageSpeed*(newTimeStamp - oldTimeStamp));
- //System.out.println(newTimeStamp - oldTimeStamp);
- }
- } catch (InterruptedException ex) {
- Logger.getLogger(IRCReplay.class.getName()).log(Level.SEVERE, null, ex);
- }
- oldTimeStamp = newTimeStamp;
- log.append(fixedLine + newline);
- log.setCaretPosition(log.getDocument().getLength());
- }
- }
- dstream.close();
- fileSelected = false;
- } catch (IOException e) {
- System.err.println("Error: " + e.getMessage());
- System.exit(-1);
- }
- }
- }
- public static String validate(String line) {
- int index = line.indexOf("] entered the room.")
- + line.indexOf("left the room (")
- + line.indexOf("has changed the topic");
- if (index <= -1) {
- return line;
- } else {
- return "invalid";
- }
- }
- public static int delayTime(String line) {
- int timeStamp, pofix;
- String hour, minute, second;
- pofix = line.indexOf(":");
- hour = line.substring(1, pofix);
- minute = line.substring(pofix+1, pofix+3);
- second = line.substring(pofix+4, pofix+6);
- timeStamp = 3600*(Integer.parseInt(hour))
- + 60*(Integer.parseInt(minute))
- + (Integer.parseInt(second));
- return timeStamp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement