Anda di halaman 1dari 9

import java.awt.

*;
import java.awt.event.*;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Editor extends WindowAdapter implements ActionListener,TextListener
{
FileInputStream fis;
FileOutputStream fos;
Frame f,f1,f2,f3,f4,f5,f6,f7,f8,f9;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18;
Label l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11;
FileDialog fd,fd1;
MenuBar mb;
Menu m1,m2,m3;
MenuItem mi1,mi2,mi3,mi4,mi5,mi6,mi7,mi8;
String framename="Untitled",filename="",filecontents2="",filecontents1="",filed
ir="",ptr="";
File fe;
int ch=0,index=0;;
TextField tf = new TextField(20);
TextArea t;
public Editor()
{
t = new TextArea("");
t.addTextListener(this);
f = new Frame(framename);
f1 = new Frame();
f2 = new Frame();
f4 = new Frame();
f5 = new Frame();
f6 = new Frame();
f7 = new Frame();
f8 = new Frame();
f9 = new Frame();
f8.setLayout(new FlowLayout());
f7.setLayout(new FlowLayout());
f6.setLayout(new FlowLayout());
f5.setLayout(new FlowLayout());
f4.setLayout(new FlowLayout());
f.addWindowListener(this);
f1.addWindowListener(this);
f2.addWindowListener(this);
f4.addWindowListener(this);
f5.addWindowListener(this);
f6.addWindowListener(this);
f7.addWindowListener(this);
f8.addWindowListener(this);
f9.addWindowListener(this);
f2.setLayout(new FlowLayout());
mb =new MenuBar();
m1 =new Menu("File");
m2 =new Menu("Tools");
m3 =new Menu("Help");
mi1= new MenuItem("New");
mi1.addActionListener(this);
mi2= new MenuItem("Open");
mi2.addActionListener(this);
mi3= new MenuItem("Save");
mi3.addActionListener(this);
mi4= new MenuItem("Find");
mi4.addActionListener(this);
mi5= new MenuItem("Find & Replace");
mi5.addActionListener(this);
mi6= new MenuItem("Exit");
mi6.addActionListener(this);
mi7= new MenuItem("About");
mi7.addActionListener(this);
mi8= new MenuItem("Save As");
mi8.addActionListener(this);
b1 = new Button("OK");
b1.addActionListener(this);
b2 =new Button("Yes");
b2.addActionListener(this);
b3 =new Button("No");
b3.addActionListener(this);
b4 =new Button("Save ");
b4.addActionListener(this);
b5 =new Button("Don't Save");
b5.addActionListener(this);
b6 =new Button("Cancel");
b6.addActionListener(this);
b7 = new Button("Save ");
b7.addActionListener(this);
b8 = new Button("Don't Save ");
b8.addActionListener(this);
b9 = new Button("Save ") ;
b9.addActionListener(this);
b10 = new Button("Don't Save ");
b10.addActionListener(this);
b11 = new Button("Save ");
b11.addActionListener(this);
b12 = new Button("Don't Save ");
b12.addActionListener(this);
b13 = new Button("Cancel ");
b13.addActionListener(this);
b14 = new Button("Cancel ");
b14.addActionListener(this);
b15 = new Button("Cancel ");
b15.addActionListener(this);
b16 = new Button("Find Next");
b16.addActionListener(this);
b17 = new Button("Cancel");
b17.addActionListener(this);
b18 = new Button("OK ");
b18.addActionListener(this);
m1.add(mi1);
m1.add(mi2);
m1.add(mi3);
m1.add(mi8);
m1.addSeparator();
m1.add(mi6);
m2.add(mi4);
m2.add(mi5);
m3.add(mi7);
mb.add(m1);
mb.add(m2);
mb.add(m3);
l2 = new Label("File Not Found");
l6 = new Label("check the file name and try again");
f2.add(l2);
f2.add(l6);
f2.add(b1);
l5 = new Label("Do you Want to save the document?");
l9 = new Label("Do you Want to save the document?");
l7 = new Label("Do you Want to save the document?");
l8 = new Label("Do you Want to save the document?");
l10 = new Label("FInd What");
l11 = new Label("Match not find");
f9.add(l11);
f9.add(b18);
f8.add(l10);
f8.add(tf);
f8.add(b16);
f8.add(b17);
f4.add(l5);
f4.add(b4);
f4.add(b5);
f4.add(b6);
f5.add(l6);
f5.add(b7);
f5.add(b8);
f5.add(b15);
f6.add(l7);
f6.add(b9);
f6.add(b10);
f6.add(b14);
f7.add(l8);
f7.add(b11);
f7.add(b12);
f7.add(b13);
f.setMenuBar(mb);
f.add(t);
mi4.setEnabled(false);
f.setSize(600,400);
f.setVisible(true);
}
public void textValueChanged(TextEvent te)
{
if(t.getText().equals(""))
{
mi4.setEnabled(false);
}
else
{
mi4.setEnabled(true);
}
}
public void actionPerformed(ActionEvent e)
{
ptr="";
String q="";
ptr =e.getActionCommand();
if(ptr=="About")
{
f1.setSize(200,200);
l1= new Label("Created By Abhishek Bhagia \n Version 1.0");
f1.add(l1);
f1.setVisible(true);
}
else
if(ptr=="New")
{
q=t.getText();
if(filename=="")
{
if(!q.equals(""))
{
f6.setSize(250,120);
f6.setVisible(true);
}
}
else if(!q.equals(filecontents1))
{
f6.setSize(250,120);
f6.setVisible(true);
}
else
{
t.setText("");
filecontents1="";
filename="";
filedir="";
f.setTitle("Untitled");
}
}
else
if(ptr=="Open")
{
if(filename.equals("")&!t.getText().equals(""))
{
f5.setSize(250,120);
f5.setVisible(true);
}
else if(!filename.equals("")&!t.getText().equals(filecontents1))
{
f5.setSize(250,120);
f5.setVisible(true);
}
else
{
open();
}

}
else
if(ptr=="Save"|ptr=="Save As"|ptr=="Save "|ptr=="Save "|ptr=="S
ave ")
{
f7.setVisible(false);
f6.setVisible(false);
f5.setVisible(false);
f4.setVisible(false);
fe = new File(filedir);
if((ptr=="Save As"|!fe.exists())&(ptr!="Save "&ptr!="Sa
ve "&ptr!="Save "))
{
fd = new FileDialog(f,"Save",FileDialog.SAVE);
fd.setVisible(true);
filename = fd.getFile();
try
{
if(filename.equals(""))
{
}
else
{
filedir = fd.getDirectory();
filedir = filedir+filename+".txt";
save(filename);
}
}
catch(Exception s)
{
}
}
else if(ptr=="Save ")
{
if(!fe.exists())
{
fd = new FileDialog(f,"Save",FileDialog.SAVE);
fd.setVisible(true);
filename = fd.getFile();
filedir = fd.getDirectory();
filedir = filedir+filename+".txt";
}
save(filename);
filecontents1="";
filename="";
filedir="";
open();
}
else if(ptr=="Save ")
{
if(!fe.exists())
{
fd = new FileDialog(f,"Save",FileDialog.SAV
E);
fd.setVisible(true);
filename = fd.getFile();
filedir = fd.getDirectory();
filedir = filedir+filename+".txt";
}
save(filename);
filecontents1="";
filename="";
filedir="";
t.setText("");
f.setTitle("Untitled");
}
else if(ptr=="Save ")
{
if(!fe.exists())
{
fd = new FileDialog(f,"Save",FileDialo
g.SAVE);
fd.setVisible(true);
filename = fd.getFile();
filedir = fd.getDirectory();
filedir = filedir+filename+".txt";
}
save(filename);
System.exit(1);
}
else
{
save(filename);
}
}
else
if(ptr=="OK"|ptr=="OK ")
{
f9.setVisible(false);
f2.setVisible(false);
}
else
if(ptr=="Exit")
{
exit();
}
else
if(ptr=="Don't Save"|ptr=="Cancel"|ptr=="Don't Save "|ptr=="Don
't Save "|ptr=="Don't Save "|ptr=="Cancel "|ptr=="Cancel "|ptr=="Cancel ")
{
System.out.print("bye");
f8.setVisible(false);
f7.setVisible(false);
f6.setVisible(false);
f5.setVisible(false);
f4.setVisible(false);
if(ptr=="Don't Save ")
{
System.exit(1);
}
if(ptr=="Don't Save")
{
if(filename=="")
t.setText("");
else
t.setText(filecontents1);
}
if(ptr=="Don't Save ")
{
open();
}
if(ptr=="Don't Save ")
{
filecontents1="";
filename="";
filedir="";
t.setText("");
}
}
else
if(ptr=="Find")
{
f8.setSize(350,250);
f8.setVisible(true);
}
else
if(ptr=="Find Next")
{
findnext();
}

}
public void findnext()
{
String h="";
int a,b;
h=tf.getText();
try
{
Pattern p = Pattern.compile(h);
Matcher mr = p.matcher(t.getText());
while(mr.find(index))
{
a=mr.start();
b=mr.end();
t.select(a,b);
break;
}
index=mr.end();
f.setSize(600,400);
f.setVisible(true);
System.out.print("length = "+ t.getText().length());
if(index==t.getText().length())
{
index=0;
}
}
catch(Exception g)
{
}
}
public void exit()
{
if(filecontents1.equals(t.getText()))
{
t.setText("");
System.exit(1);
}
else
{
f7.setSize(250,120);
f7.setVisible(true);
}
}
public void open()
{
try
{
fd = new FileDialog(f,"Open",FileDialog.LOAD);
fd.setVisible(true);
filename = fd.getFile();
if(filename.equals(""))
{
System.out.print("hello");
}
else
{
filedir =fd.getDirectory();
filedir=filedir+filename;
fis =new FileInputStream(filedir);
while((ch=fis.read())!=-1)
{
filecontents1 +=((char)ch+"");
}
f.setTitle(filename);
t.setText(filecontents1);
}
}
catch(IOException m)
{
System.out.print("bye");
}
}
public void save(String filename)
{
try
{
ch=0;
fos =new FileOutputStream(filedir);
filecontents2 = t.getText();
while(ch<filecontents2.length())
{
fos.write(filecontents2.charAt(ch));
ch++;
}
filecontents1=filecontents2;
f.setTitle(filename);
}
catch(IOException m)
{
}
}
public void windowClosing(WindowEvent r)
{
Window w = r.getWindow();
if(filecontents1.equals(t.getText()))
{
t.setText("");
System.exit(1);
}
else
{
f7.setSize(250,120);
f7.setVisible(true);
}
w.setVisible(false);
w.dispose();
}
public static void main(String args[])
{
Editor z =new Editor();
}
}