import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JMenuBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.BorderFactory;
import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class EditorTextoJFC extends EditorTexto {
  private JFrame _main ;
  private JTextArea _editor;
  private JMenuBar _menuBar;

  public EditorTextoJFC() {
     _main = new JFrame();
     _editor = new JTextArea();
  }
  private void initFunctors() {
//inicializar los componentes que aplican las funciones
   super.setFunctorEditorTexto(EditorTextoIF.LOAD,
      new LoadEditorTextoJFC(_main,_editor) );
   super.setFunctorEditorTexto(EditorTextoIF.SAVE,
      new SaveEditorTextoJFC(_main,_editor) );
   super.setFunctorEditorTexto(EditorTextoIF.CUT,
      new CutEditorTextoJFC(_editor) );
   super.setFunctorEditorTexto(EditorTextoIF.COPY, 
     new CopyEditorTextoJFC(_editor));
   super.setFunctorEditorTexto(EditorTextoIF.PASTE, 
     new PasteEditorTextoJFC(_editor) );
   super.setFunctorEditorTexto(EditorTextoIF.SELECTALL, 
     new SelectAllEditorTextoJFC(_editor) );

  }
  private void addFileMenu() {
//agregar menu e items
    JMenu fileMenu = new JMenu("File");
    JMenuItem fileMenuLoad = new JMenuItem("Load ...");
    JMenuItem fileMenuSave = new JMenuItem("Save ...");
    JMenuItem fileMenuExit = new JMenuItem("Exit ...");
    fileMenu.add(fileMenuLoad);
    fileMenu.add(fileMenuSave);
    fileMenu.addSeparator();
    fileMenu.add(fileMenuExit);
    _menuBar.add(fileMenu);
//configurar acciones, usando los functores
    fileMenuLoad.addActionListener( new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            FunctorEditorTextoIF functor = 
		EditorTextoJFC.super.
		getFunctorEditorTexto(EditorTextoIF.LOAD);
            functor.operar();
       }
     }
    );
    fileMenuSave.addActionListener( new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            FunctorEditorTextoIF functor = 
		EditorTextoJFC.super.
		getFunctorEditorTexto(EditorTextoIF.SAVE);
            functor.operar();
       }
     }
    );
    fileMenuExit.addActionListener( new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
       }
     }
    );
  }
  private void addEditMenu() {
//agregar menu e items
    JMenu editMenu = new JMenu("Edit");
    JMenuItem editMenuCopy = new JMenuItem("Copy");
    JMenuItem editMenuPaste = new JMenuItem("Paste");
    JMenuItem editMenuCut = new JMenuItem("Cut");
    JMenuItem editMenuSelectAll = new JMenuItem("Select All");
    editMenu.add(editMenuCopy);
    editMenu.add(editMenuPaste);
    editMenu.add(editMenuCut);
    editMenu.add(editMenuSelectAll);
    _menuBar.add(editMenu);
//configurar acciones, usando los functores
    editMenuCopy.addActionListener( new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            FunctorEditorTextoIF functor = 
		EditorTextoJFC.super.
		getFunctorEditorTexto(EditorTextoIF.COPY);
            functor.operar();
       }
     }
    );
    editMenuPaste.addActionListener( new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            FunctorEditorTextoIF functor = 
		EditorTextoJFC.super.
		getFunctorEditorTexto(EditorTextoIF.PASTE);
            functor.operar();
       }
     }
    );
    editMenuCut.addActionListener( new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            FunctorEditorTextoIF functor = 
		EditorTextoJFC.super.
		getFunctorEditorTexto(EditorTextoIF.CUT);
            functor.operar();
       }
     }
    );
    editMenuSelectAll.addActionListener( new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            FunctorEditorTextoIF functor = 
		EditorTextoJFC.super.
		getFunctorEditorTexto(EditorTextoIF.SELECTALL);
            functor.operar();
       }
     }
    );
  }
  public void initGUI() {
//configurar ventantana principal
   _main.setTitle("Editor de Texto Muy Simple");
   _main.addWindowListener( new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
           System.exit(0);
         }
      }
    );
//configurar paneles
   JPanel topPanel = new JPanel();
   topPanel.setLayout(new BorderLayout());
   _main.getContentPane().add(topPanel,BorderLayout.CENTER);
   JPanel editorPanel = new JPanel();
   editorPanel.setLayout(new BorderLayout());
   editorPanel.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
   JScrollPane scroller = new JScrollPane();
//configurar area de texto
   _editor.setColumns(40);
//agregar componentes
   topPanel.add(editorPanel,BorderLayout.CENTER);
   editorPanel.add(scroller,BorderLayout.CENTER);
   scroller.getViewport().add(_editor);
//configurar menu
   _menuBar = new JMenuBar();
   _main.setJMenuBar(_menuBar);
   addFileMenu();
   addEditMenu();
//desplegar ventana principal
   _main.pack();
   _main.setSize(400,300);
   _main.setVisible(true);
  };

  public void init() {
//inicializar los componentes que aplican las funciones
   initFunctors();
//inicializar los elementos graficos
    initGUI();
  }

  public static void main(String [] args) {
     (new EditorTextoJFC()).init();
  }
}
