org.dodjsoft.tail.TailTopComponent.java Source code

Java tutorial

Introduction

Here is the source code for org.dodjsoft.tail.TailTopComponent.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.List;
import java.awt.event.MouseEvent;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayDeque;
import java.util.ArrayList;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import org.apache.commons.codec.binary.Base64;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.util.Exceptions;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//org.dodjsoft.tail//Tail//EN", autostore = false)
@TopComponent.Description(preferredID = "TailTopComponent",
        //iconBase="SET/PATH/TO/ICON/HERE", 
        persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "output", openAtStartup = false)
@ActionID(category = "Window", id = "org.dodjsoft.tail.TailTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(displayName = "#CTL_TailAction", preferredID = "TailTopComponent")
@Messages({ "CTL_TailAction=Tail", "CTL_TailTopComponent=Tail Window",
        "HINT_TailTopComponent=This is a Tail window" })
public final class TailTopComponent extends TopComponent {

    private final TailPopupMenu contextMenu;
    private final ArrayList<String> files = new ArrayList<>();

    public TailTopComponent() {
        initComponents();
        setName(Bundle.CTL_TailTopComponent());
        setToolTipText(Bundle.HINT_TailTopComponent());
        contextMenu = new TailPopupMenu(this);
    }

    /**
     * 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tailFileChooser = new javax.swing.JFileChooser();
        tailContainerTabPane = new javax.swing.JTabbedPane();

        tailFileChooser.setDialogTitle(org.openide.util.NbBundle.getMessage(TailTopComponent.class,
                "TailTopComponent.tailFileChooser.dialogTitle")); // NOI18N

        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));

        tailContainerTabPane.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tailContainerTabPaneMouseClicked(evt);
            }
        });
        add(tailContainerTabPane);
    }// </editor-fold>//GEN-END:initComponents

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

    private void tailContainerTabPaneMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tailContainerTabPaneMouseClicked
        showContextMenu(evt, null);
    }//GEN-LAST:event_tailContainerTabPaneMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTabbedPane tailContainerTabPane;
    private javax.swing.JFileChooser tailFileChooser;
    // End of variables declaration//GEN-END:variables

    @Override
    public void componentOpened() {

        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p) {
        ObjectOutputStream oos = null;
        try {
            // better to version settings since initial version as advocated at
            // http://wiki.apidesign.org/wiki/PropertyFiles
            p.setProperty("version", "1.0");
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(files);
            Base64 encoder = new Base64();
            String filesEnc = encoder.encodeAsString(baos.toByteArray());
            p.setProperty("files", filesEnc);
            // TODO store your settings
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        } finally {
            try {
                if (oos != null) {
                    oos.close();
                }
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
    }

    void readProperties(java.util.Properties p) {
        ObjectInputStream ois = null;
        try {
            String version = p.getProperty("version");
            // TODO read your settings according to their version
            String filesEnc = p.getProperty("files");
            if (filesEnc != null) {
                Base64 decoder = new Base64();
                ByteArrayInputStream bais = new ByteArrayInputStream(decoder.decode(filesEnc));
                ois = new ObjectInputStream(bais);
                setFiles((ArrayList<String>) ois.readObject());
            }
        } catch (IOException | ClassNotFoundException ex) {
            Exceptions.printStackTrace(ex);
        } finally {
            try {
                if (ois != null) {
                    ois.close();
                }
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
        }

    }

    void addPanel(File file) {
        TailFilePanel filePanel = new TailFilePanel(this);
        filePanel.setFile(file);
        tailContainerTabPane.add(filePanel);
        files.add(file.getAbsolutePath());
    }

    void showContextMenu(MouseEvent evt, TailFilePanel pane) {
        if (evt.getButton() == MouseEvent.BUTTON3) {
            contextMenu.setCurrentPane(pane);
            contextMenu.show(this, evt.getX(), evt.getY());
        }
    }

    void showNewTail() {
        int returnVal = tailFileChooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            addPanel(tailFileChooser.getSelectedFile());
        }
    }

    void removeTail(TailFilePanel panel) {
        if (panel == null) {
            return;
        }
        String path = panel.getFile().getAbsolutePath();
        files.remove(path);
        panel.close();
        tailContainerTabPane.remove(panel);
    }

    private void setFiles(ArrayList<String> arrayList) {
        for (String file : arrayList) {
            System.out.println(file);
        }
    }
}