Friday, September 24, 2010

CrazyPad

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.text.*;
import java.awt.datatransfer.*;
import java.util.*;
import java.beans.*;
public class CrazyPad extends JFrame implements ActionListener,KeyListener
{
boolean flag=false;
String filename="";
File fname;
static JFrame f;
JMenuBar menubar;
JMenu m1,m2,m3,m4,m5,m6;
JMenuItem m1item1,m1item2,m1item3,m1item4;
JMenuItem m2item1,m2item2,m2item3,m2item4,m2item5;
JMenuItem m3item1,m3item2;

JMenuItem m5item1;
JCheckBoxMenuItem tool;

Color c=new Color(0,0,0);
JTextArea textarea;
JScrollPane sp;
JFileChooser chooser;
JToolBar toolbar;
JButton New,open,save,cut,copy,paste;
Icon n,o,s,ct,cp,p;
CrazyPad()
{
chooser=new JFileChooser();
f=new JFrame("CrazyPad1.0");
menubar=new JMenuBar();

m1=new JMenu("File");
m2=new JMenu("Edit");
m3=new JMenu("Format");

m5=new JMenu("About");
m6=new JMenu("View");

m1.setMnemonic('f');
m2.setMnemonic('e');
m3.setMnemonic('o');

m5.setMnemonic('u');
m6.setMnemonic('v');

//file menu
m1item1=new JMenuItem("New");
m1item2=new JMenuItem("Open");
m1item3=new JMenuItem("Save");
m1item4=new JMenuItem("Exit");

//edit menu
m2item1=new JMenuItem("Cut");
m2item2=new JMenuItem("Copy");
m2item3=new JMenuItem("Paste");
m2item4=new JMenuItem("SelectAll");
m2item5=new JMenuItem("Delete");

//format menu
m3item1=new JMenuItem("Color");
m3item2=new JMenuItem("Font");

//about menu
m5item1=new JMenuItem("About");

tool=new JCheckBoxMenuItem("Toolbar");

//setting the shortcuts
m1item1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,KeyEvent.CTRL_MASK));
m1item2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,KeyEvent.CTRL_MASK));
m1item3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,KeyEvent.CTRL_MASK));
m1item4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,KeyEvent.ALT_MASK));

m2item1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,KeyEvent.CTRL_MASK));
m2item2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,KeyEvent.CTRL_MASK));
m2item3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,KeyEvent.CTRL_MASK));
m2item4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,KeyEvent.CTRL_MASK));


m3item1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L,KeyEvent.CTRL_MASK));
m3item2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,KeyEvent.CTRL_MASK));


m5item1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T,KeyEvent.CTRL_MASK));

//adding the menus on menu bar
menubar.add(m1);
menubar.add(m2);
menubar.add(m3);
menubar.add(m6);
menubar.add(m5);

//adding menuitems on menu
m1.add(m1item1);
m1.add(m1item2);
m1.add(m1item3);
m1.addSeparator();
m1.add(m1item4);

m2.add(m2item1);
m2.add(m2item2);
m2.add(m2item3);
m2.addSeparator();
m2.add(m2item4);
m2.add(m2item5);

m3.add(m3item1);
m3.add(m3item2);

m5.add(m5item1);

m6.add(tool);

//listener to the menu items
m1item1.addActionListener(this);
m1item2.addActionListener(this);
m1item3.addActionListener(this);
m1item4.addActionListener(this);

m2item1.addActionListener(new CutCopyPasteListener());
m2item2.addActionListener(new CutCopyPasteListener());
m2item3.addActionListener(new CutCopyPasteListener());
m2item4.addActionListener(this);
m2item5.addActionListener(this);


m3item1.addActionListener(this);
m3item2.addActionListener(this);

m5item1.addActionListener(this);

tool.addActionListener(new viewListener());

//toolbar settings
toolbar=new JToolBar("Toolbar",SwingConstants.VERTICAL);
toolbar.setFloatable(false);
n=new ImageIcon("new.gif");
o=new ImageIcon("open.gif");
s=new ImageIcon("save.gif");
ct=new ImageIcon("cut.gif");
cp=new ImageIcon("copy.gif");
p=new ImageIcon("paste.gif");

New=new JButton(n);
open=new JButton(o);
save=new JButton(s);
cut=new JButton(ct);
copy=new JButton(cp);
paste=new JButton(p);

toolbar.add(New);
toolbar.add(open);
toolbar.add(save);
toolbar.add(cut);
toolbar.add(copy);
toolbar.add(paste);

New.setToolTipText("New");
open.setToolTipText("Open");
save.setToolTipText("Save");
cut.setToolTipText("Cut");
copy.setToolTipText("Copy");
paste.setToolTipText("Paste");

//toolbar listener
New.addActionListener(this);
open.addActionListener(this);
save.addActionListener(this);
cut.addActionListener(new CutCopyPasteListener());
copy.addActionListener(new CutCopyPasteListener());
paste.addActionListener(new CutCopyPasteListener());

f.setJMenuBar(menubar);
f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
f.addWindowListener(new appCloseHandler());
Toolkit k=Toolkit.getDefaultToolkit();
Dimension d=k.getScreenSize();
int sw=d.width;
int sh=d.height;
f.setSize(400,400);
f.setLocation(sw/4,sh/4);
textarea=new JTextArea();
sp=new JScrollPane(textarea);

f.add(sp,BorderLayout.CENTER);
textarea.addKeyListener(this);
textarea.setFont(new Font("Comic Sans MS",Font.PLAIN,24));
f.setVisible(true);
f.setSize(401,401);
}

public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e)
{
flag=true;
}

public void newFile()
{
if(flag==true)
{
int result;
result=JOptionPane.showConfirmDialog(this,"Do You Want To Save The Changes?","New File",JOptionPane.YES_NO_CANCEL_OPTION);
if(result==JOptionPane.YES_OPTION)
{
saveFile();
}
else if(result==JOptionPane.CANCEL_OPTION)
{
return;
}
}
filename="";
flag=false;
textarea.setText("");
f.setTitle("Untitled");
}


public void saveFile()
{

if(filename.equals(""))
{
chooser= new JFileChooser();
int result;
result=chooser.showSaveDialog(this);
if(result==chooser.APPROVE_OPTION)
{
filename=chooser.getSelectedFile().getAbsolutePath();
fname=new File(filename);
if(fname.exists())
{
if(JOptionPane.showConfirmDialog(null,"File already exists.Do you want to Raplace?", "Hi..", JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
{
madeFile();
}
else
{
filename="";
saveFile();
}
}
else
madeFile();
}

else
{
return;
}

}
else
madeFile();
}

public void madeFile()
{

try{
String data1=textarea.getText();
String[] a=data1.split("\n");
BufferedWriter bw=new BufferedWriter(new FileWriter(filename));
for(String data:a){
bw.write(data);
bw.newLine();}
bw.close();
flag=false;
fname=new File(filename);
f.setTitle(fname.getName());
}
catch(Exception e2){ System.out.println("can not write");}
}


public void openFile()
{
int result;
String data="";
if(flag==true)
{
if(JOptionPane.showConfirmDialog(null,"Do you want to save the changes?", "Open File", JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
saveFile();
}
else
textarea.setText("");
result=chooser.showOpenDialog(this);
if(result==chooser.APPROVE_OPTION)
{
textarea.setText("");
filename=chooser.getSelectedFile().getAbsolutePath();
try{
BufferedReader in=new BufferedReader(new FileReader(filename));
while((data=in.readLine())!=null)
{
textarea.append(data);
textarea.append("\n");
}
}catch(Exception m){}

fname=new File(filename);
f.setTitle(fname.getName());
}
}


public void exitFile()
{
if(flag==true)
{
int result;
result=JOptionPane.showConfirmDialog(this,"Do You Want To Save The Changes?","File Exit",JOptionPane.YES_NO_CANCEL_OPTION);
if(result==JOptionPane.YES_OPTION)
{
saveFile();
}
else if(result==JOptionPane.CANCEL_OPTION)
{
return;
}
}
System.exit(0);
}


public void actionPerformed(ActionEvent e)
{
if(e.getSource()==m1item1 || e.getSource()==New)
{
newFile();
}
if(e.getSource()==m1item2 || e.getSource()==open)
{
openFile();
}
if(e.getSource()==m1item3 || e.getSource()==save)
{
saveFile();
}
if(e.getSource()==m1item4)
{
exitFile();
}
if(e.getSource()==m3item1)
{
c=JColorChooser.showDialog(null,"choose a color",c);
textarea.setForeground(c);
if(c==null)
c=Color.BLACK;
}
if(e.getSource()==m3item2)
new Myfont();
if(e.getSource()==m5item1)
{
String msg=" ----------------CrazyPad1.0--------------\nThis Pad Is Created purely in core java By Milan\n"+
" your suggestions are invited..."+"E-Mail me at:\n milankmr@gmail.com";
JOptionPane.showMessageDialog(null,msg);
}
if(e.getSource()==m2item4)
textarea.selectAll();
if(e.getSource()==m2item5)
textarea.replaceSelection("");
}
class Myfont
{
String[] font=GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
JFrame f;
JPanel p;
JComboBox cb,cb1,cb2;
JLabel lb;
JLabel fontlb,stylelb,sizelb;
String fname="Comic Sans MS";
int style=Font.PLAIN;
int size=20;
JButton ok,cancel;
Myfont()
{
String lbtext="AaBbXxYyZz";
f=new JFrame("FontDialog");
p=new JPanel();
cb=new JComboBox();
cb1=new JComboBox();
cb2=new JComboBox();
fontlb=new JLabel("Font");
stylelb=new JLabel("Style");
sizelb=new JLabel("Size");
ok=new JButton("Ok");
cancel=new JButton("Cancel");
f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
f.add(p);
f.setVisible(true);
f.setSize(400,300);
lb=new JLabel(lbtext,JLabel.CENTER);
lb.setBorder(BorderFactory.createTitledBorder("Sample"));
lb.setFont(new Font(fname,style,size));
cb.setEditable(false);
cb1.setEditable(false);
cb2.setEditable(false);
p.setLayout(null);
cb.setBounds(10,50,170,40);
cb1.setBounds(200,50,80,40);
cb2.setBounds(310,50,60,40);
lb.setBounds(30,100,300,80);
fontlb.setBounds(80,20,80,40);
stylelb.setBounds(220,20,80,40);
sizelb.setBounds(320,20,80,40);
ok.setBounds(90,200,80,40);
cancel.setBounds(220,200,80,40);
p.add(cb);
p.add(lb);
p.add(cb1);
p.add(cb2);
p.add(fontlb);
p.add(stylelb);
p.add(sizelb);
p.add(ok);
p.add(cancel);
for(int i=0;i{
cb.addItem(font[i]);
}
for(int i=2;i<=72;i=i+2)
{
cb2.addItem(""+i);
}
Toolkit k=Toolkit.getDefaultToolkit();
Dimension ss=k.getScreenSize();
int sw=ss.width;
int sh=ss.height;
f.setLocation(sw/4,sh/4);
JFrame.setDefaultLookAndFeelDecorated(true);

cb1.addItem("Bold");
cb1.addItem("Italic");
cb1.addItem("Plain");
cb1.addItem("BoldItalic");
cb.addActionListener(new FontListener());
cb1.addActionListener(new StyleListener());
cb2.addActionListener(new SizeListener());
ok.addActionListener(new ButtonListener());
cancel.addActionListener(new ButtonListener());
}
class FontListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
fname=(String)cb.getSelectedItem();
lb.setFont(new Font(fname,style,size));
}
}
class StyleListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String temp=(String)cb1.getSelectedItem();
if(temp.equals("Bold"))
style=Font.BOLD;
if(temp.equals("Italic"))
style=Font.ITALIC;
if(temp.equals("Plain"))
style=Font.PLAIN;
if(temp.equals("BoldItalic"))
style=Font.BOLD+Font.ITALIC;
lb.setFont(new Font(fname,style,size));
}
}
class SizeListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
size=Integer.parseInt((String)cb2.getSelectedItem());
lb.setFont(new Font(fname,style,size));
}
}
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==ok)
{
textarea.setFont(new Font(fname,style,size));
f.setVisible(false);
}
if(e.getSource()==cancel)
{
f.setVisible(false);
}
}
}
}

class viewListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Dimension d;
int sw;
int sh;
if(e.getSource()==tool)
{
if(tool.isSelected())
{
f.add(toolbar,BorderLayout.WEST);
d=f.getSize();
sw=d.width;
sh=d.height;
f.setLocation(f.getLocation());
f.setSize(sw+1,sh+1);
}
else
{
f.remove(toolbar);
f.setLocation(f.getLocation());
d=f.getSize();
sw=d.width;
sh=d.height;
f.setSize(sw+1,sh+1);
}
}
}
}

class CutCopyPasteListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==m2item1 || e.getSource()==cut)
cutdata();
if(e.getSource()==m2item2 || e.getSource()==copy)
copydata();
if(e.getSource()==m2item3 || e.getSource()==paste)
pastedata();
}
public void cutdata()
{
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
String text =textarea.getSelectedText();
StringSelection selection = new StringSelection(text);
clipboard.setContents(selection, null);
text="";
textarea.replaceSelection(text);
}
public void copydata()
{
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
String text = textarea.getSelectedText();
if (text == null) text="";
StringSelection selection = new StringSelection(text);
clipboard.setContents(selection, null);
}
public void pastedata()
{
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
DataFlavor flavor = DataFlavor.stringFlavor;
if (clipboard.isDataFlavorAvailable(flavor))
{
try
{
String text = (String) clipboard.getData(flavor);
textarea.replaceSelection(text);
}
catch (IOException e4){System.out.println("error2");}
catch (UnsupportedFlavorException e3){System.out.println("error1");}
}
}
}

class appCloseHandler extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
exitFile();
}
}

public static void main(String... s) throws Exception
{
new CrazyPad();
Date d = new Date();
JPanel datePanel = new JPanel();
JLabel label = new JLabel(d.toLocaleString());
datePanel.add(label);
f.add(datePanel,"South");
while(true)
{
d=new Date();
label.setText(""+d.toLocaleString());
}
}
}

Download

0 comments:

Post a Comment