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.

59 lines
1.5 KiB

  1. package Application;
  2. import java.util.ArrayList;
  3. public class MenuManager {
  4. private ArrayList<Menu> menuList;
  5. private Menu currentMenu;
  6. public MenuManager() {
  7. menuList = new ArrayList<>();
  8. currentMenu = null;
  9. }
  10. public void addMenu(Menu menu) {
  11. menuList.add(menu);
  12. }
  13. public int getSize() {
  14. if(inRootMenu())
  15. return menuList.size();
  16. return currentMenu.getSubMenuList().size();
  17. }
  18. public void select(int i) {
  19. if (currentMenu == null)
  20. this.currentMenu = menuList.get(i);
  21. else
  22. this.currentMenu = currentMenu.getMenu(i);
  23. }
  24. public void back() throws Exception {
  25. if (!this.inRootMenu())
  26. this.currentMenu = this.currentMenu.getPreviousMenu();
  27. else
  28. throw new Exception("Menu is in the root menu, a previous menu doesn't exist");
  29. }
  30. public Menu getCurrentMenu() {
  31. return this.currentMenu;
  32. }
  33. public boolean inRootMenu() {
  34. return this.currentMenu == null;
  35. }
  36. public String getFormattedMenuList() {
  37. StringBuilder result = new StringBuilder();
  38. ArrayList<Menu> baseMenuList = this.menuList;
  39. if (!inRootMenu())
  40. baseMenuList = currentMenu.getSubMenuList();
  41. for (int i = 0; i < baseMenuList.size(); i++)
  42. result.append(i + 1).append(": ").append(baseMenuList.get(i).getName()).append("\n");
  43. return result.toString();
  44. }
  45. }