Monday, April 28, 2008

File Searching with JAVA

FileSearch.java


 

package com.joshi;


 

import java.awt.Container;

import java.awt.Dimension;

import java.awt.Insets;

import java.awt.Point;

import java.awt.Rectangle;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.io.File;

import javax.swing.ImageIcon;

import javax.swing.JApplet;

import javax.swing.JButton;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JScrollPane;

import javax.swing.JTextField;

import javax.swing.ListSelectionModel;


 

/** *

* @author SJoshi

*/

public class FileSearch extends JApplet implements ActionListener {

SearchThread sc;

@Override

public void init() {

FileSearch fs = new FileSearch();

}

public FileSearch() {

initComponents();

lblProcess.setVisible(false);

this.setSize(450, 350);

this.setVisible(true);

// this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

initListener();

btnStop.setEnabled(false);


 

}

private void initComponents() {

// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents

// Generated using JFormDesigner Evaluation license - Shirin Joshi

txtSearch = new JTextField();

btnSearch = new JButton();

lblEnterSearch = new JLabel();

spFileList = new JScrollPane();

lstFileList = new CustomJList();

lblSearch = new JLabel();

lblProcess = new JLabel();

btnStop = new JButton();

lblCurrentScan = new JLabel();

lblScan = new JLabel();


 

//======== this ========

// setTitle("Search");

Container contentPane = getContentPane();

contentPane.setLayout(null);

contentPane.add(txtSearch);

txtSearch.setBounds(115, 30, 190, txtSearch.getPreferredSize().height);


 

//---- btnSearch ----

btnSearch.setText("Search");

contentPane.add(btnSearch);

btnSearch.setBounds(310, 30, btnSearch.getPreferredSize().width, 20);


 

//---- lblEnterSearch ----

lblEnterSearch.setText("Enter File Name:");

contentPane.add(lblEnterSearch);

lblEnterSearch.setBounds(15, 30, lblEnterSearch.getPreferredSize().width, 20);


 

//======== spFileList ========

{


 

//---- lstFileList ----

lstFileList.setVisibleRowCount(10);

lstFileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

spFileList.setViewportView(lstFileList);

}

contentPane.add(spFileList);

spFileList.setBounds(15, 100, 350, spFileList.getPreferredSize().height);


 

//---- lblSearch ----

lblSearch.setText("Search Result :");

contentPane.add(lblSearch);

lblSearch.setBounds(new Rectangle(new Point(15, 70), lblSearch.getPreferredSize()));


 

//---- lblProcess ----

lblProcess.setIcon(new ImageIcon(getClass().getResource("resource/ajax-loader.gif")));

contentPane.add(lblProcess);

lblProcess.setBounds(320, 65, 32, 32);


 

//---- btnStop ----

btnStop.setText("Stop");

contentPane.add(btnStop);

btnStop.setBounds(new Rectangle(new Point(240, 70), btnStop.getPreferredSize()));


 

//---- lblScan ----

lblScan.setText("Scan: ");

contentPane.add(lblScan);

lblScan.setBounds(10, 275, 80, 20);


 

contentPane.add(lblCurrentScan);

lblCurrentScan.setBounds(60, 275, 305, 20);


 

{ // compute preferred size

Dimension preferredSize = new Dimension();

for (int i = 0; i < contentPane.getComponentCount(); i++) {

Rectangle bounds = contentPane.getComponent(i).getBounds();

preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);

preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);

}

Insets insets = contentPane.getInsets();

preferredSize.width += insets.right;

preferredSize.height += insets.bottom;

contentPane.setMinimumSize(preferredSize);

contentPane.setPreferredSize(preferredSize);

}

// pack();

// setLocationRelativeTo(getOwner());

// JFormDesigner - End of component initialization //GEN-END:initComponents

}


 

// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables

// Generated using JFormDesigner Evaluation license - Shirin Joshi

public JTextField txtSearch;

private JButton btnSearch;

private JLabel lblEnterSearch;

private JScrollPane spFileList;

public CustomJList lstFileList;

private JLabel lblSearch;

public JLabel lblProcess;

private JButton btnStop;

public JLabel lblCurrentScan;

private JLabel lblScan;

public boolean isStopSearch = false;


 

private void initListener() {

btnSearch.setActionCommand("Search");

btnSearch.addActionListener(this);

btnStop.setActionCommand("Stop");

btnStop.addActionListener(this);


 

lstFileList.addMouseListener(new MouseAdapter() {


 

@Override

public void mouseClicked(MouseEvent e) {

if(e.getButton()==MouseEvent.BUTTON1 && e.getClickCount()==2) {

showFileInformation();

}

}

});

}

private void showFileInformation() {

String file = lstFileList.getSelectedValue().toString();


 

File clickFile = new File(file);

StringBuffer sbFileInfo = new StringBuffer();

sbFileInfo.append("\nFile Name : ").append(clickFile.getName());

sbFileInfo.append("\nRead Only : ").append(!clickFile.canRead());

JOptionPane.showMessageDialog(this,sbFileInfo.toString());

}


 

public void actionPerformed(ActionEvent e) {

if (e.getActionCommand().equalsIgnoreCase("Search")) {

if (txtSearch.getText().trim().length() != 0) {

btnSearch.setEnabled(false);

lblProcess.setVisible(true);

isStopSearch = false;

lstFileList.getListModel().removeAllElements();

lblCurrentScan.setText("");

sc = new SearchThread(this);

sc.start();

btnStop.setEnabled(true);

} else {

JOptionPane.showMessageDialog(this, "Please Provide Search File Name", "File Name Error..", JOptionPane.ERROR_MESSAGE);

}

} else if (e.getActionCommand().equalsIgnoreCase("Stop")) {

sc.stop();

lblProcess.setVisible(false);

lblCurrentScan.setText("");

isStopSearch = true;

btnSearch.setEnabled(true);

btnStop.setEnabled(true);

}

}

}


 

SearchThread.java


 

package com.joshi

import java.io.File;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import javax.swing.JOptionPane;


 

/**

*

* @author SJoshi

*/

public class SearchThread extends Thread {


 

File[] roots = File.listRoots();

FileSearch fs;


 

public SearchThread(FileSearch p_fs) {

fs = p_fs;

}//SearchThread

@Override

public void run() {

List list = new ArrayList();

for (int i = 0; i < roots.length; i++) {

try {

if (fs.isStopSearch) {

break;

}//if

try {

scan(roots[i], list);

fs.lblProcess.setVisible(false);

fs.lblCurrentScan.setText("");

} //try

catch (IOException ex) {

JOptionPane.showMessageDialog(fs, ex, "Searching Error..", JOptionPane.ERROR_MESSAGE);

}//catch

sleep(100);

} //try

catch (InterruptedException ex) {

JOptionPane.showMessageDialog(fs, ex, "Searching Error..", JOptionPane.ERROR_MESSAGE);

}//catch

}//for

}//run

public void scan(File path,

List list) throws IOException {

// Get filtered files in the current path

File[] files = path.listFiles();

//path.listFiles(filter);

// Process each filtered entry

for (int i = 0; i < files.length; i++) {

// recurse if the entry is a directory

try {

fs.lblCurrentScan.setText(files[i].getPath());

if (files[i].isDirectory()) {

scan(files[i], list);//, filter);

} else {

// add the filtered file to the list

if (files[i].getName().equalsIgnoreCase(fs.txtSearch.getText())) {

fs.lstFileList.addElement(files[i].getCanonicalPath());

list.add(files[i]);

}

}

} catch (NullPointerException ne) {

System.out.println("Null Pointer.." + files[i]);

continue;

}

} // for

} // scan

}

No comments:

Contributors