You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Controller.java 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package kontakte.controller ;
  7. import java.awt.Component;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import kontakte.model.Model;
  11. import kontakte.view.View;
  12. /**
  13. * Diese Klasse leitet Events von der View an den CommandInvoker weiter.
  14. *
  15. * @see View
  16. * @see CommandInvoker
  17. */
  18. public class Controller implements ActionListener
  19. {
  20. private View view;
  21. private Model model;
  22. private CommandInvoker invoker;
  23. public Controller(View view, Model model)
  24. {
  25. this.view = view;
  26. this.model = model;
  27. invoker = new CommandInvoker();
  28. }
  29. public void registerEvents()
  30. {
  31. view.getBtnOpen().addActionListener(this);
  32. view.getMenuOpen().addActionListener(this);
  33. view.getBtnSave().addActionListener(this);
  34. view.getMenuSave().addActionListener(this);
  35. view.getBtnNewEntry().addActionListener(this);
  36. view.getBtnOk().addActionListener(this);
  37. view.getBtnCancel().addActionListener(this);
  38. view.getMenuUndo().addActionListener(this);
  39. }
  40. public void registerCommands()
  41. {
  42. CommandOpen cmdOpen = new CommandOpen(view, model);
  43. invoker.addCommand(view.getBtnOpen(), cmdOpen);
  44. invoker.addCommand(view.getMenuOpen(), cmdOpen);
  45. CommandSave cmdSave = new CommandSave(view, model);
  46. invoker.addCommand(view.getBtnSave(), cmdSave);
  47. invoker.addCommand(view.getMenuSave(), cmdSave);
  48. invoker.addCommand(view.getBtnNewEntry(), new CommandNewKontaktVisible(view, model));
  49. invoker.addCommand(view.getBtnOk(),new CommandNewKontaktOk(view,model));
  50. invoker.addCommand(view.getBtnCancel(),new CommandNewKontaktCancel(view,model));
  51. }
  52. @Override
  53. public void actionPerformed(ActionEvent e)
  54. {
  55. Component key = (Component)e.getSource();
  56. if(key != view.getMenuUndo())
  57. {
  58. invoker.executeCommand(key);
  59. }
  60. else
  61. {
  62. invoker.undoCommand();
  63. }
  64. }
  65. }