org.dodjsoft.tail.TailFilePanel.java Source code

Java tutorial

Introduction

Here is the source code for org.dodjsoft.tail.TailFilePanel.java

Source

/*
 * 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 org.dodjsoft.tail;

import java.awt.event.MouseEvent;
import java.io.File;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;
import org.apache.commons.io.input.Tailer;
import org.apache.commons.io.input.TailerListener;
import org.openide.util.Exceptions;

/**
 *
 * @author Richard Dodd
 */
public class TailFilePanel extends javax.swing.JPanel implements TailerListener {

    private final TailTopComponent root;
    private File file;
    private Tailer tailer;

    /**
     * Creates new form TailFilePanel
     */
    public TailFilePanel(TailTopComponent root) {
        this.root = root;
        initComponents();
    }

    public TailFilePanel() {
        this.root = null;
    }

    /**
     * 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() {

        tailScrollPanel = new javax.swing.JScrollPane();
        tailTextPanel = new javax.swing.JTextPane();

        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });
        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.LINE_AXIS));

        tailTextPanel.setEditable(false);
        tailTextPanel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tailTextPanelMouseClicked(evt);
            }
        });
        tailScrollPanel.setViewportView(tailTextPanel);

        add(tailScrollPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void tailTextPanelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tailTextPanelMouseClicked
        showContextMenu(evt);
    }//GEN-LAST:event_tailTextPanelMouseClicked

    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
        showContextMenu(evt);
    }//GEN-LAST:event_formMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane tailScrollPanel;
    private javax.swing.JTextPane tailTextPanel;
    // End of variables declaration//GEN-END:variables

    public JTextPane getTailTextPanel() {
        return tailTextPanel;
    }

    private void showContextMenu(MouseEvent evt) {
        root.showContextMenu(evt, this);
    }

    void setFile(File file) {
        this.file = file;

        // stop currently running tail
        if (this.tailer != null) {
            this.tailer.stop();
        }
        // set title
        this.setName(file.getAbsolutePath());
        // set content
        Tailer.create(file, this);
    }

    @Override
    public void init(Tailer tailer) {
        //System.err.println("init called");
    }

    @Override
    public void fileNotFound() {
        //System.err.println("File not found");
    }

    @Override
    public void fileRotated() {
        System.err.println("File rotated");
    }

    @Override
    public void handle(final String string) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                StyledDocument document = (StyledDocument) tailTextPanel.getDocument();
                try {
                    document.insertString(document.getLength(), string + "\n", null);
                } catch (BadLocationException ex) {
                    Exceptions.printStackTrace(ex);
                }
                tailTextPanel.setCaretPosition(document.getLength());
            }
        });
    }

    @Override
    public void handle(Exception excptn) {
        System.err.println("handle(Exception) called");
    }

    File getFile() {
        return file;
    }

    void close() {
        if (tailer != null) {
            tailer.stop();
        }
    }
}