Tuesday, October 14, 2008

Search engine in java

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;



class window extends JFrame implements ActionListener
{
String drives[] = new String[6];

static JCheckBox cb[];
static JComboBox jcb;
JPanel p[]=new JPanel[8];
java.awt.List t[] = new java.awt.List[7];
TextField tf;
//creathrd obj[];
JButton b1,b2;
static JRadioButton rb1,rb2,rb3,rb4,rb5;
Label ltot=new Label();

window(String title)
{

super(title);
validDri vd = new validDri();
try
{
drives=vd.valdrive();
}
catch(Exception e)
{}



setLayout(new GridLayout(2,4));
for(int i=0;i<8;i++)
{
p[i]=new JPanel();

}


p[0].setLayout(new GridLayout(8,1));
p[4].setLayout(new GridLayout(9,1));

for(int i=1;i<8;i++)
{
if(i!=4)
p[i].setLayout(new GridLayout(1,1)); ////////////////// DOUBT
}


for(int i=0;i<6;i++)
{

t[i]=new java.awt.List();//////////////////// DOUBT

}
for(int i=0;i {
t[i].add("DRIVE "+drives[i]);
t[i].addActionListener(this);
}

JLabel L1=new JLabel("Enter the KeyWord to be Searched");
JLabel a = new JLabel("");
JLabel c = new JLabel("");
JLabel d = new JLabel("");
JLabel b = new JLabel("");

rb3 = new JRadioButton("Contents Search");

rb4 = new JRadioButton("FileName Search",true);
rb5 = new JRadioButton("Both");
rb3.addActionListener(this);
rb4.addActionListener(this);
rb5.addActionListener(this);
ButtonGroup bg1 = new ButtonGroup();

bg1.add(rb3);
bg1.add(rb4);
bg1.add(rb5);

tf = new TextField(50);

rb1 = new JRadioButton("DEFAULT");

jcb = new JComboBox();

jcb.addItem("ALL");
for(int i=0;i {
jcb.addItem(drives[i]);
}

//BUTTONS FOR STARTING AND STOPPING THE SEARCH
b1 = new JButton("SEARCH");
b2 = new JButton("STOP");
//b1.setBounds(40,440,40,450);
b1.addActionListener(this);
b2.addActionListener(this);
rb2 = new JRadioButton("OTHERS");


cb = new JCheckBox[drives.length];

for(int i=0;i {
cb[i]=new JCheckBox(drives[i]);
cb[i].setEnabled(false);
}
rb1.addActionListener(this);
rb2.addActionListener(this);
ButtonGroup bg = new ButtonGroup();

bg.add(rb1);
bg.add(rb2);

p[0].add(L1);
p[0].add(a);
p[0].add(tf);
p[0].add(rb3);
p[0].add(rb4);
p[0].add(rb5);
p[0].add(ltot);
p[0].add(jcb);



p[1].add(t[0]);
p[2].add(t[1]);
p[3].add(t[2]);

p[4].add(rb1);
p[4].add(jcb);
p[4].add(rb2);
for(int i=0;i p[4].add(cb[i]);
p[4].add(b1);
//p[4].add(b2);

p[5].add(t[3]);
p[6].add(t[4]);
p[7].add(t[5]);

GridBagConstraints g = new GridBagConstraints();

g.gridx=0;
g.gridy=0;

add(p[0]);

g.gridx++;
add(p[1]);

g.gridx++;
add(p[2]);

g.gridx++;
add(p[3]);

g.gridy++;
g.gridx=0;
add(p[4]);

g.gridx++;
add(p[5]);

g.gridx++;
add(p[6]);

g.gridx++;
add(p[7]);


addWindowListener(new windowadapter());
}

public void actionPerformed(ActionEvent ae)
{
String tem=ae.getActionCommand();

int ind=0;
for(int j=0;j if(tem.contains(drives[j]))
{
ind=j;
break;
}
String s1=this.t[ind].getSelectedItem();

if(ae.getActionCommand().equals("DEFAULT"))
{
for(int i=0;i this.cb[i].setEnabled(false);
this.jcb.setEnabled(true);
}
else if(ae.getActionCommand().equals("OTHERS"))
{
this.jcb.setEnabled(false);
for(int i=0;i this.cb[i].setEnabled(true);
}
else if(ae.getActionCommand().equals(s1))
{

}
else if(ae.getActionCommand().equals("SEARCH"))
{
/*String fname;
String drives1[] = new String[drives.length];

ltot.setText("");
for(int i=0;i drives1[i]=drives[i];

fname=this.tf.getText();
fname=convert(fname);
System.out.println("Converted String : "+fname);
System.out.println();
System.out.println();

for(int i=0;i<6;i++)
t[i].clear();

drives1=limit(drives1);

for(int i=0;i if(drives1[i].equals("null")==false)
t[i].add("DRIVE "+drives1[i]);

obj=new creathrd[drives1.length];

for(int i=0;i if(drives1[i].equals("null")==false)
obj[i]=new creathrd(drives1[i],fname,this,drives1);

infacethrd ob=new infacethrd(fname,this);

for(int i=0;i {
try{
obj[i].t.join();
}
catch(Exception e){}
}
try{

ob.t.join();
}catch(Exception e){}

System.out.println();
int tot=0;

for(int i=0;i if(drives1[i].equals("null")==false)
tot+=obj[i].fc;

System.out.println("Total Number of Files Found is :"+tot);
ltot.setText("Total Number of Files Found is :"+tot);
//p[0].add(ltot);
System.out.println("Finished");*/

}

}

class windowadapter extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(1);
}
}
class validDri
{

String[] valdrive() throws IOException
{
String drives[]={"A:\\","B:\\","C:\\","D:\\","E:\\","F:\\"};
String drlist[]=new String[6];
String temp[];
int idx=0,inc=0;
for(int i=0;i {
File f = new File(drives[i]);
if(f!=null)
{
temp=f.list();
if(temp!=null)
{
drlist[idx]=drives[i];
idx++;
}
}
}
for(int j=0;j {
if(drlist[j]!=null)
{
inc++;
}
}
String vallist[]=new String[inc];
for(int k=0;k {
if(drlist[k]!=null)
vallist[k]=drlist[k];
}
return vallist;
}
}
}
class search
{



public static void main(String args[]) throws IOException
{
boolean flag;
int tot;
String fname;


//validDri vd1 = new validDri();
String valdri[] = new String[26];

//valdri=vd1.valdrive();
//creathrd obj[]=new creathrd[valdri.length];

window w = new window("Search Engine");
//new infacethrd("Interface",w);

w.setSize(750,450);
w.setBounds(0,0,800,572);
w.setVisible(true);



}

}

No comments: