Saturday, November 19, 2011

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.lang.*;
import java.io.*;

class frame52 extends JFrame
{

    private Container konten = new Container();

    private JMenuBar bar = new JMenuBar();
    private JMenu mnFile = new JMenu("File");
    private JMenuItem mniFileBuka = new JMenuItem("Buka File");
    private JMenuItem mniFileSimpan = new JMenuItem("Simpan File");
    private JMenuItem mniFileKeluar = new JMenuItem("Keluar");
    private JMenu mnEdit = new JMenu("Edit");
    private JMenuItem mniCopy = new JMenuItem("Copy");
    private JMenuItem mniCut = new JMenuItem("Cut");
    private JMenuItem mniPaste = new JMenuItem("Paste");
    private JMenu mnTentang = new JMenu("Tentang");

    private JLabel lblKategori = new JLabel("Kategori");
    String pilKategori[]={"Nasional","Daerah","Internasional"};
    private JComboBox cmbKategori = new JComboBox(pilKategori);

    private JLabel lblDaerah = new JLabel("Daerah");
    String pilDaerah[]={"Indonesia Bagian Barat", "Indonesia Bagian Timur"};
    private JComboBox cmbDaerah = new JComboBox(pilDaerah);

    private JLabel lblJudul = new JLabel("Judul");
    private JTextField txtJudul = new JTextField();

    private JLabel lblIsi = new JLabel("Isi");
    private JTextArea txtArea;
      private JScrollPane jScrollPane1 = new JScrollPane();

    private JPanel panel1 = new JPanel();
    private JPanel panel2 = new JPanel();
    private Box box1 = Box.createVerticalBox();

    private JPopupMenu popmenu = new JPopupMenu();
    private JMenuItem mniPopCopy = new JMenuItem("Copy");
    private JMenuItem mniPopCut = new JMenuItem("Cut");
    private JMenuItem mniPopPaste = new JMenuItem("Paste");

    FileDialog fd2=new FileDialog(this,"Save As..",FileDialog.SAVE);
    FileDialog fd1=new FileDialog(this,"Open..",FileDialog.LOAD);

    String txtmsg, cuttext;
    Clipboard clipb;

    frame52()
    {
        super("Universitas Budi Luhur");
        setSize(500,500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        ItemHandler itemHandler = new ItemHandler();
        mniFileBuka.addActionListener(itemHandler);
        mniFileSimpan.addActionListener(itemHandler);
        mniFileKeluar.addActionListener(itemHandler);
        mniCopy.addActionListener(itemHandler);
        mniCut.addActionListener(itemHandler);
        mniPaste.addActionListener(itemHandler);

        mnFile.add(mniFileBuka);
        mnFile.add(mniFileSimpan);
        mnFile.add(mniFileKeluar);

        mnEdit.add(mniCopy);
        mnEdit.add(mniCut);
        mnEdit.add(mniPaste);

        bar.add(mnFile);
        bar.add(mnEdit);
        bar.add(mnTentang);

        setJMenuBar(bar);

        popmenu.add(mniPopCopy);
        popmenu.add(mniPopCut);
        popmenu.add(mniPopPaste);

        panel1.setLayout(new GridLayout(2,2));
        panel1.add(lblKategori);
        panel1.add(cmbKategori);
        panel1.add(lblDaerah);
        panel1.add(cmbDaerah);

        box1.add(panel1);

        panel2.setLayout(new GridLayout(3,1));
        panel2.add(lblJudul);
        panel2.add(txtJudul);
        panel2.add(lblIsi);

        box1.add(panel2);

        txtArea = new JTextArea(20,15);
        txtArea.setLineWrap(true);

        txtArea.addMouseListener
        ( new MouseListener()
          {
           public void mousePressed(MouseEvent e) { }
           public void mouseExited(MouseEvent e) { }
           public void mouseEntered(MouseEvent e) { }
           public void mouseClicked(MouseEvent e) { }
           public void mouseReleased(MouseEvent e)
           {
              //jika Click kanan maka tampilkan popup
              if (e.getButton() == 3)
                  popmenu.show(e.getComponent(), e.getX(), e.getY());

            }
          }
        );
        jScrollPane1.getViewport().add(txtArea,null);
        box1.add(jScrollPane1);


        konten = getContentPane();
        konten.setLayout(new BorderLayout());
        konten.add(box1, BorderLayout.NORTH);

        setVisible(true); //sama dengan show()
    }

    /*private class ItemHandler implements ActionListener
    {public void actionPerformed(ActionEvent e)
     {
         Object objek = e.getSource();
         if (objek == mniFileBuka)
             JOptionPane.showMessageDialog
               (frame52.this,"Buka File","Buka",JOptionPane.PLAIN_MESSAGE);
         else if (objek == mniFileSimpan)
             JOptionPane.showMessageDialog
              (frame52.this,"Simpan","Simpan File",JOptionPane.PLAIN_MESSAGE);
         else if (objek == mniCopy)
             JOptionPane.showMessageDialog(frame52.this,"Copy "
                   + txtArea.getSelectedText() + " Di Text Area ", "Copy",
                   JOptionPane.PLAIN_MESSAGE);
         else if (objek == mniCut)
             JOptionPane.showMessageDialog
              (frame52.this,"Cut Teks di Teks Area","Cut",
                   JOptionPane.PLAIN_MESSAGE);
         else if (objek == mniPaste)
             JOptionPane.showMessageDialog
              (frame52.this,"Paste Teks di Text Area","Paste",
                   JOptionPane.PLAIN_MESSAGE);
         else if (objek == mniFileKeluar)
             System.exit(0);

     }
    }*/
   
    private class ItemHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
             clipb=Toolkit.getDefaultToolkit().getSystemClipboard();
            Object objek = e.getSource();
            
            if (objek == mniFileBuka) {
              try {
                fd1.setVisible(true);
                String filename=fd1.getFile();
                String dirname=fd1.getDirectory();

                File openfile=new File(dirname,filename);

                  FileInputStream fis=new FileInputStream(openfile);
                int bytelength=fis.available();

                txtmsg = "";
                for (int bytecount=0;bytecount<bytelength;bytecount++) {
                    char fch=(char)fis.read();
                    txtmsg=txtmsg+fch;
                }

                String atxt[] = txtmsg.split("~");
                System.out.println(atxt.length);
                if ( atxt.length == 1) {
                  txtArea.setText(atxt[0]);
                }
                else {
                  cmbKategori.setSelectedIndex(Integer.parseInt(atxt[0]));
                  cmbDaerah.setSelectedIndex(Integer.parseInt(atxt[1]));
                  txtJudul.setText(atxt[2]);
                  txtArea.setText(atxt[3]);
                }
              } catch(Exception ioe) {
                  JOptionPane.showMessageDialog
                   (frame52.this,ioe.toString(),"Error",JOptionPane.PLAIN_MESSAGE);
               }
            }
            
             else if (objek == mniFileSimpan)
              try {
                String sSimpan;

                sSimpan = cmbKategori.getSelectedIndex() + "~" +
                          cmbDaerah.getSelectedIndex() + "~" +
                          txtJudul.getText() + "~" + txtArea.getText();

                fd2.setVisible(true);
                FileOutputStream fos1=new FileOutputStream(fd2.getDirectory()+
                  fd2.getFile());
                fos1.write(sSimpan.getBytes());
                fos1.close();
              } catch(Exception ioe) {
                  JOptionPane.showMessageDialog
                   (frame52.this,ioe.toString(),"Error",JOptionPane.PLAIN_MESSAGE);
              }

             else if (objek == mniCopy || objek == mniPopCopy) {
              try {
                  cuttext=txtArea.getSelectedText();
                  if (cuttext.equals("")) cuttext = txtArea.getText();

                  StringSelection sel=new StringSelection(cuttext);
                  clipb.setContents(sel,null);

               } catch(Exception ioe) {
                  JOptionPane.showMessageDialog(frame52.this,ioe.toString(),
                  "Error Copy",JOptionPane.PLAIN_MESSAGE);
              }
             }
            
             else if (objek == mniCut || objek == mniPopCut) {
               try {
                 cuttext=txtArea.getSelectedText();
                StringSelection sel=new StringSelection(cuttext);
                clipb.setContents(sel,null);
                int cutindex=txtArea.getText().indexOf(cuttext);
                txtArea.replaceRange("",cutindex,cutindex+cuttext.length());
              } catch(Exception ioe) {
                  JOptionPane.showMessageDialog
                   (frame52.this,ioe.toString(),
                  "Error",JOptionPane.PLAIN_MESSAGE);
              }
             }
            
             else if (objek == mniPaste || objek == mniPopPaste) {
               try {
                Transferable cont=clipb.getContents(this);
                cuttext=(String)(cont.getTransferData
                  (DataFlavor.stringFlavor));

                int getcaretpos=txtArea.getCaretPosition();
                 txtArea.insert(cuttext,getcaretpos);
              } catch(Exception ioe) {
                  JOptionPane.showMessageDialog
                     (frame52.this,ioe.toString(),
                    "Error",JOptionPane.PLAIN_MESSAGE);
                 }
             }
            else if (objek == mniFileKeluar)
             System.exit(0);
        }
    }
}

public class praktek52
{
    public static void main(String args[])
    {
        new frame52();
    }

}

0 komentar: