import javax.swing.*; import javax.swing.text.*; import java.awt.event.*; import java.awt.*; import java.lang.String.*; import javax.swing.text.JTextComponent.*; import javax.swing.text.Document.*; import java.awt.Dialog.*; import java.applet.*; import java.awt.datatransfer.*; import java.awt.datatransfer.Clipboard; import java.io.*; import java.io.File; import java.util.Date; import java.util.Calendar; import java.util.GregorianCalendar; import javax.net.ssl.*; import java.security.*; class Find extends JFrame{ //implements ActionListener{ Container con; JPanel p; JLabel l; JTextField t; JButton b1,b2; static String strtemp; Find(){ con = getContentPane(); p = new JPanel(); l = new JLabel("Find Text "); t = new JTextField(20); b1 = new JButton("Ok"); b2 = new JButton("Cancel"); p.add(l); p.add(t); p.add(b1); p.add(b2); con.add(p); /*b1.addActionListener(this); b2.addActionListener(this); } public void actionPerformed(ActionEvent e){ if (e.getSource() == b1){ strtemp = t.getText(); //dispose(); } if (e.getSource() == b2){ //System.out.println(strtemp); dispose(); } }*/ } /* class Replace extends JFrame implements ActionListener{ Container con; JPanel p; JLabel l1,l2; JTextField t1,t2; JButton b1,b2; Replace(){ con = getContentPane(); p = new JPanel(); l1 = new JLabel("Replace Text "); l2 = new JLabel("By Text"); t1 = new JTextField(25); t2 = new JTextField(25); b1 = new JButton("Ok"); b2 = new JButton("Cancel"); p.add(l1); p.add(t1); p.add(b1); p.add(l2); p.add(t2); p.add(b2); con.add(p); b1.addActionListener(this); b2.addActionListener(this); } public void actionPerformed(ActionEvent e){ if (e.getSource() == b1){ dispose(); } if (e.getSource() == b2){ dispose(); } } } */ } class About extends JFrame implements ActionListener{ About(){ Container con = getContentPane(); JPanel p = new JPanel(); JLabel l1 = new JLabel("Created By: "); JLabel l2 = new JLabel("P Jayasunderam"); JLabel l3 = new JLabel("Ankur Deshmukh"); JButton b = new JButton("Ok"); p.add(l1); p.add(l2); p.add(l3); p.add(b); con.add(p); b.addActionListener(this); } public void actionPerformed(ActionEvent e){ dispose(); } } class Send extends JFrame implements ActionListener{ JButton b1 = new JButton("Send File"); JButton b2 = new JButton("Cancel"); Send(){ Container con = getContentPane(); JPanel p = new JPanel(); JLabel l1 = new JLabel("Username"); JTextField t1 = new JTextField(16); JLabel l3 = new JLabel("Password"); JPasswordField p1 = new JPasswordField(16); JLabel l2 = new JLabel("Please enter the remote host name."); JTextField t2 = new JTextField(40); p.add(l1); p.add(t1); p.add(l3); p.add(p1); p.add(l2); p.add(t2); p.add(b1); p.add(b2); con.add(p); b1.addActionListener(this); b2.addActionListener(this); } public void actionPerformed(ActionEvent e){ if (e.getSource() == b1){ System.out.println("Sending file...."); dispose(); } else{ dispose(); } } } class mframe extends JFrame{ //String s=" "; JTextArea ta; int x = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS; int y = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS; Container con = getContentPane(); mframe(String title){ super(title); ta=new JTextArea(5,50); JScrollPane sp = new JScrollPane(ta,y,x); con.add(sp,BorderLayout.CENTER); JMenuBar mbar=new JMenuBar(); // setMenuBar(mbar); con.add(mbar,BorderLayout.NORTH); // add menu File JMenu file=new JMenu("File"); JMenuItem item1,item2,item3,itemSaveas,itemSave; file.add(item1=new JMenuItem("New")); file.add(item2=new JMenuItem("Open")); file.add(itemSave=new JMenuItem("Save")); file.add(itemSaveas=new JMenuItem("SaveAs...")); file.add(item3=new JMenuItem("Close")); mbar.add(file); // add menu Edit JMenu edit=new JMenu("Edit"); JMenuItem item4,item5,item6,item7,item8,item9,item10,item11,item12,item13,item14,item15; edit.add(item4=new JMenuItem("Undo")); edit.add(item5=new JMenuItem("Cut")); edit.add(item6=new JMenuItem("Copy")); edit.add(item7=new JMenuItem("Paste")); edit.add(item8=new JMenuItem("Delete")); edit.add(item9=new JMenuItem("Find")); edit.add(item10=new JMenuItem("Find Next")); edit.add(item11=new JMenuItem("Goto")); edit.add(item12=new JMenuItem("Replace")); edit.add(item13=new JMenuItem("Replace All")); edit.add(item14=new JMenuItem("SelectAll")); edit.add(item15=new JMenuItem("Time/Date")); mbar.add(edit); // add menu Format JMenu format=new JMenu("Format"); JMenuItem item16; JCheckBoxMenuItem item17; format.add(item16=new JMenuItem("Font")); format.add(item17=new JCheckBoxMenuItem("WordWrap")); mbar.add(format); // add menu Help JMenu help=new JMenu("Help"); JMenuItem about; help.add(about=new JMenuItem("About")); mbar.add(help); JMenu send_receive = new JMenu("Send/Receive Files"); JMenuItem send; JMenuItem receive; send_receive.add(send = new JMenuItem("Send a file")); send_receive.add(receive = new JMenuItem("Receive a file")); mbar.add(send_receive); mymenuhandler h=new mymenuhandler(this); item1.addActionListener(h); item2.addActionListener(h); itemSave.addActionListener(h); itemSaveas.addActionListener(h); item3.addActionListener(h); // item4.addActionListener(h); item5.addActionListener(h); item6.addActionListener(h); item7.addActionListener(h); item8.addActionListener(h); item9.addActionListener(h); /* item10.addActionListener(h); item11.addActionListener(h); item12.addActionListener(h); item13.addActionListener(h);*/ item14.addActionListener(h); item15.addActionListener(h); /* item16.addActionListener(h); item17.addActionListener(h); */ about.addActionListener(h); send.addActionListener(h); receive.addActionListener(h); mywindowadapter mw=new mywindowadapter(this); addWindowListener(mw); } } class mywindowadapter extends WindowAdapter{ mframe f; public mywindowadapter (mframe f){ this.f=f; } public void windowClosing(WindowEvent we){ String title="Untitled - Notepad"; File F1= new File(""); File F = F1; String str = f.ta.getText(); int l = str.length(); if (l != 0){ Object options[]={"Yes","No","Cancel"}; //int n= JOptionPane.showOptionDialog(f, "The text in the file has changed. Do you want to save the changes?", //"Notepad",,JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE,null,options,options[2]); int n= JOptionPane.showOptionDialog(f, "The text in the file haschanged. Do you want to save the changes?","Notepad",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE,null,options,options[2]); int value = n; if (value == JOptionPane.YES_OPTION) { System.out.println("Yes is pressed"); //save as code try { JFileChooser ch1=new JFileChooser(); ch1.setDialogType(JFileChooser.SAVE_DIALOG); ch1.showDialog(f,"SAVE"); File f2=ch1.getSelectedFile(); F = f2; String Name=ch1.getName(f2); title = Name; f.setTitle(Name); FileOutputStream fout=new FileOutputStream(f2); String data=f.ta.getText(); byte b1[]=new byte[data.length()]; b1=data.getBytes(); fout.write(b1); fout.close(); System.out.println("Save is done"); }catch(Exception eel){} System.exit(0); } else if(value == JOptionPane.NO_OPTION) { System.exit(0); } else{ System.exit(0); } } System.exit(0); } } class mymenuhandler implements ActionListener{ mframe f; String title="Untitled - SecureNotepad"; File F1= new File(""); File F = F1; File temp = F1; File temp1 = F1; Find f2 = new Find(); String stemp = f2.strtemp; public mymenuhandler (mframe f){ this.f=f; } public void actionPerformed(ActionEvent e){ String arg=(String)e.getActionCommand(); // New action if(arg.equals("New")) { System.out.println(f2.strtemp); String str,sample=""; str = f.ta.getText(); int l = str.length(); if (l == 0){ System.out.println(str); System.out.println("Text was empty"); f.ta.setText(""); f.setTitle("Untitled - Notepad"); } else{ System.out.println("Text is not empty"); Object options[]={"Yes","No","Cancel"}; int n= JOptionPane.showOptionDialog(f, "The text in the file haschanged. Do you want to save the changes?","Notepad",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE,null,options,options[2]); int value = n; if (value == JOptionPane.YES_OPTION) { System.out.println("Yes is pressed"); // Save code try { JFileChooser ch=new JFileChooser(); ch.setDialogType(JFileChooser.SAVE_DIALOG); ch.showDialog(f,"SAVE"); File f1=ch.getSelectedFile(); String Name=ch.getName(f1); title = Name; f.setTitle(Name); FileOutputStream fout=new FileOutputStream(f1); String data=f.ta.getText(); byte b[]=new byte[data.length()]; b=data.getBytes(); fout.write(b); fout.close(); System.out.println("Save is done"); }catch(Exception en){} f.ta.setText(""); f.setTitle("Untitled - Notepad"); F = F1; } else if(value == JOptionPane.NO_OPTION){ System.out.println("No is pressed"); f.ta.setText(""); f.setTitle("Untitled - Notepad"); } else { System.out.println("Cancel is pressed"); } } } // Open action if(arg.equals("Open")) { String str = f.ta.getText(); int l = str.length(); if (l == 0){ // code for open try{ JFileChooser ch=new JFileChooser(); ch.setDialogType(JFileChooser.OPEN_DIALOG); ch.showDialog(f,"Open"); File f1=ch.getSelectedFile(); F = f1; f.setTitle(f1.getName()); FileInputStream fin=new FileInputStream(f1); int size=(int)f1.length(); byte b[]=new byte[size]; fin.read(b,0,size); f.ta.setText(new String(b)); fin.close(); System.out.println("File opened"); temp = f1; } catch(Exception el){} } else{ Object options[]={"Yes","No","Cancel"}; int n= JOptionPane.showOptionDialog(f, "The text in the file has changed. Do you want to save the changes?" ,"Notepad",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE,null,options,options[2]); int value = n; if (value == JOptionPane.YES_OPTION) { System.out.println("Yes is pressed"); //save as code try { JFileChooser ch1=new JFileChooser(); ch1.setDialogType(JFileChooser.SAVE_DIALOG); ch1.showDialog(f,"SAVE"); File f2=ch1.getSelectedFile(); F = f2; String Name=ch1.getName(f2); title = Name; f.setTitle(Name); FileOutputStream fout=new FileOutputStream(f2); String data=f.ta.getText(); byte b1[]=new byte[data.length()]; b1=data.getBytes(); fout.write(b1); fout.close(); System.out.println("Save is done"); } catch(Exception el){} // code for open try{ JFileChooser ch2=new JFileChooser(); ch2.setDialogType(JFileChooser.OPEN_DIALOG); ch2.showDialog(f,"Open"); File f3=ch2.getSelectedFile(); F = f3; f.setTitle(f3.getName()); FileInputStream fin=new FileInputStream(f3); int size=(int)f3.length(); byte b2[]=new byte[size]; fin.read(b2,0,size); f.ta.setText(new String(b2)); fin.close(); System.out.println("File opened"); temp = f3; } catch(Exception el){} }//yes option else if (value == JOptionPane.NO_OPTION) { System.out.println("No is pressed"); //code for open try{ JFileChooser ch3=new JFileChooser(); ch3.setDialogType(JFileChooser.OPEN_DIALOG); ch3.showDialog(f,"Open"); File f4=ch3.getSelectedFile(); F = f4; f.setTitle(f4.getName()); FileInputStream fin=new FileInputStream(f4); int size=(int)f4.length(); byte b3[]=new byte[size]; fin.read(b3,0,size); f.ta.setText(new String(b3)); fin.close(); System.out.println("File opened"); temp = f4; } catch(Exception el){} }//no option else { System.out.println("Cancel is pressed"); } } } // Save action if(arg.equals("Save")) { String str = f.ta.getText(); int l = str.length(); if ((l == 0) || (F == F1)){ //code for save as try { JFileChooser ch=new JFileChooser(); ch.setDialogType(JFileChooser.SAVE_DIALOG); ch.showDialog(f,"SAVE"); File f1=ch.getSelectedFile(); F = f1; String Name=ch.getName(f1); title = Name; f.setTitle(Name); FileOutputStream fout=new FileOutputStream(f1); String data=f.ta.getText(); byte b[]=new byte[data.length()]; b=data.getBytes(); fout.write(b); fout.close(); System.out.println("Save is done"); } catch(Exception el){} }//l=0 else{ try { JFileChooser ch1=new JFileChooser(); File f2 = F; String Name=ch1.getName(f2); title = Name; f.setTitle(Name); FileOutputStream fout=new FileOutputStream(f2); String data=f.ta.getText(); byte b1[]=new byte[data.length()]; b1=data.getBytes(); fout.write(b1); fout.close(); System.out.println("Save is done"); } catch(Exception el){} } } // SaveAs action if(arg.equals("SaveAs...")) { try { JFileChooser ch=new JFileChooser(); ch.setDialogType(JFileChooser.SAVE_DIALOG); ch.showDialog(f,"SAVE"); File f1=ch.getSelectedFile(); F = f1; String Name=ch.getName(f1); title = Name; f.setTitle(Name); FileOutputStream fout=new FileOutputStream(f1); String data=f.ta.getText(); byte b[]=new byte[data.length()]; b=data.getBytes(); fout.write(b); fout.close(); System.out.println("Save is done"); } catch(Exception el){} } // Cut action if(arg.equals("Cut")) { try{ f.ta.cut(); System.out.println("Cut is done"); } catch(Exception el) { System.out.println(el); } } // Copy action if(arg.equals("Copy")) { try{ f.ta.copy(); System.out.println("Copy is done"); } catch(Exception el) { System.out.println(el); } } // Paste action if(arg.equals("Paste")) { try{ f.ta.paste(); System.out.println("Paste is done"); } catch(Exception el) { System.out.println(el); } } // Delete action if(arg.equals("Delete")) { String t = f.ta.getSelectedText(); f.ta.cut(); } // Close action if(arg.equals("Close")) { // System.out.println(f2.strtemp); String str = f.ta.getText(); int l = str.length(); if (l != 0){ Object options[]={"Yes","No","Cancel"}; int n= JOptionPane.showOptionDialog(f, "The text in the file has changed. Do you want to save the changes?" ,"Notepad",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE,null,options,options[2]); int value = n; if (value == JOptionPane.YES_OPTION) { System.out.println("Yes is pressed"); //save as code try { JFileChooser ch1=new JFileChooser(); ch1.setDialogType(JFileChooser.SAVE_DIALOG); ch1.showDialog(f,"SAVE"); File f2=ch1.getSelectedFile(); F = f2; String Name=ch1.getName(f2); title = Name; f.setTitle(Name); FileOutputStream fout=new FileOutputStream(f2); String data=f.ta.getText(); byte b1[]=new byte[data.length()]; b1=data.getBytes(); fout.write(b1); fout.close(); System.out.println("Save is done"); }catch(Exception eel){} F = F1; temp = F1; temp1 = F1; System.exit(0); } else if(value == JOptionPane.NO_OPTION) { F = F1; temp = F1; temp1 = F1; System.exit(0); } else{} } else{ F = F1; temp = F1; temp1 = F1; System.exit(0); } } // About action if(arg.equals("About")) { JFrame f1; f1 = new About(); f1.setTitle("About Notepad"); f1.setSize(250,200); f1.show(); System.out.println(f2.strtemp); } /* // Replace action if(arg.equals("Replace")) { JFrame f1; f1 = new Replace(); f1.setTitle("Notepad"); f1.setSize(250,200); f1.show(); String str = f1.t2.getText(); f.ta.replaceSelection(str); } */ // Find action if(arg.equals("Find")) { try{ JFrame f1; f1 = new Find(); // Find f2 = new Find(); f1.setTitle("Find"); f1.setSize(300,110); f1.show(); System.out.println(stemp); } catch(Exception e1){ System.out.println(e1); } // System.out.println(f2.strtemp); /* String search = f.ta.getSelectedText(); System.out.println(search); // String str = f.ta.getText(); int n = f2.strtemp.length(); System.out.println(n); try{ System.out.println(search.indexOf(f2.strtemp)); }catch(Exception em){} int t = search.indexOf(f2.strtemp) + (n-1); f.ta.select(search.indexOf(f2.strtemp),t); System.out.println(t); */ } // SelectAll action if(arg.equals("SelectAll")) { f.ta.selectAll(); } // Time/Date action if(arg.equals("Time/Date")) { Date date = new Date(); GregorianCalendar calendar = new GregorianCalendar(); calendar.setTime(date); String strDate,strTime; strTime = calendar.get(Calendar.HOUR)+":"+calendar.get(Calendar.MINUTE)+":"+calendar.get(Calendar.SECOND); strDate = calendar.get(Calendar.MONTH)+"/"+calendar.get(Calendar.DATE)+"/"+calendar.get(Calendar.YEAR); /*int n = calendar.get(Calendar.AM_PM); if (n == getValue(calendar.get(Calendar.PM))){ System.out.println(strTime+" PM "+strDate); } else{*/ System.out.println(strTime+" "+strDate); //} f.ta.append(strTime+" "+strDate); } // Sending files over a secure socket layer if (arg.equals("Send a file")) { try{ JFrame f1; f1 = new Send(); f1.setTitle("Send file"); f1.setLocation(100, 150); f1.setSize(300,110); f1.show(); System.out.println("GUI up\n"); } catch(Exception e1){ System.out.println(e1); } } } } public class Noteeditor1{ void displayFrame() { JFrame f; f=new mframe("Untitled -SecureNotepad"); f.setSize(500,500); f.setVisible(true); } public static void main (String args[]){ Noteeditor1 notepad1 = new Noteeditor1(); notepad1.displayFrame(); } }