Windows GUI File Parser using Groovy

I find myself doing a lot of file parsing on my Windows XP machine lately. I decide to write a quick utility that would allow me to drag and drop files and search for the key words that I have identified. The utility doesn’t have the logic for searching using regex’s yet, but it should be really easy to add this functionality.

I hacked some Groovy Code with some Java Code and came up with the following script. Hope it is useful.

DISCLAIMER:

As the title suggests, I have only been able to get this to work on my Windows XP machine, OS X didn’t like the javax.swing.TransferHandler and it appears some other operating systems have a hard time with this also.

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.awt.BorderLayout;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.TransferHandler.*;

class FileDropHandler extends TransferHandler {

    private static final long serialVersionUID = 1L;

    def wordsToFind =[]
    JTextArea output
    private JLabel errorMsg;
    private String fileText = "";
    private boolean test = false;
    private boolean same = true;

    public boolean canImport(TransferSupport supp) {
        /* for the demo, we'll only support drops (not clipboard paste) */
        if (!supp.isDrop()) {
        return false;
        }

        /* return false if the drop doesn't contain a list of files */
        if (!supp.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            return false;
        }

        boolean copySupported = (COPY & supp.getSourceDropActions()) == COPY;

        if (copySupported) {
            supp.setDropAction(COPY);
            return true;
        }

        return false;
    }

    public boolean importData(TransferSupport supp) {
        if (!canImport(supp)) {
        return false;
    }

    /* get the Transferable */
    Transferable t = supp.getTransferable();

    try {

        Object data = t.getTransferData(DataFlavor.javaFileListFlavor);

        List fileList = (List) data;

        for (int j = 0; j < fileList.size(); j++) {
            File file = (File) fileList.get(j); //file.getAbsolutePath()
            def tmpfh = new File("FileParser.txt")
            println wordsToFind.inspect()
            new File(file.getAbsolutePath()).eachLine{line->
                wordsToFind.each{
                    if(line =~ "${it}" ){
                        println "${line}"
                        tmpfh.append(line)
                        tmpfh.append("\n\n")
                        this.output.setText(this.output.getText()+line+"\n")
                    }
                }
        }//end for

        tmpfh.close()

    }
    } catch (UnsupportedFlavorException e) {
        return false;
    } catch (IOException e) {
        return false;
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return true;
    }

    public void setOutput(JTextArea jta) {
        this.output = jta;
    }

    public void setOutput(JLabel jta) {
        errorMsg = jta;
    }

    public String getText() {
        return fileText;
    }

    public void clearAll() {
        fileText = "";
        test = false;
        same = true;

    }
}

class AL implements ActionListener{
public JTextField jtf
public FileDropHandler dh
public AL(JTextField jtf,FileDropHandler dh){
this.jtf = jtf
this.dh =dh
}
public void actionPerformed(ActionEvent actionEvent){
    println "${this.jtf.getText()}"
    dh.wordsToFind= this.jtf.getText().split(' ')
    }
}

JTextArea dTextArea = new JTextArea("Drop on me");
FileDropHandler dh = new FileDropHandler()
dh.setOutput(dTextArea)
JTextField jta = new JTextField("Enter words seperated by spaces")
dh.wordsToFind= jta.getText().split(' ')
JButton jb =new JButton("Update Word List")
jb.addActionListener( new AL(jta ,dh ))

dh.setOutput(dTextArea);
dTextArea.setDragEnabled(true);
dTextArea.setTransferHandler(dh);

JPanel p =new JPanel(new BorderLayout());
JFrame f = new JFrame()
p.add(jta, BorderLayout.NORTH)
p.add(dTextArea, BorderLayout.CENTER)
p.add(jb, BorderLayout.SOUTH)

f.getContentPane().add(p)
f.setSize(400,400)
f.setVisible(true)