001package state.ui.main; 002 003import state.ui.UI; 004import state.ui.UIMenu; 005import state.ui.UIMenuBuilder; 006 007class Control { 008 final State EXITED; 009 final State EXIT; 010 private State state; 011 Control(UI ui) { 012 EXITED = new ExitedState(); 013 EXIT = new ExitState(this,ui); 014 state = EXIT; 015 } 016 017 void run() { 018 while (state != EXITED) { 019 state = state.run(); 020 } 021 } 022} 023 024interface State { 025 public State run(); 026} 027 028final class ExitedState implements State { 029 public State run() { 030 return this; 031 } 032} 033 034final class ExitState implements State { 035 Control control; 036 UI ui; 037 UIMenu m; 038 ExitState(Control control, UI ui) { 039 this.control = control; 040 this.ui = ui; 041 042 UIMenuBuilder mb; 043 mb = new UIMenuBuilder(); 044 045 //mb.add("Default", new UIMenuAction() { public Object run() {return this;} }); 046 mb.add("Default", () -> this); 047 mb.add("Yes", () -> control.EXITED); 048 mb.add("No", () -> control.EXIT); 049 this.m = mb.toUIMenu("Are you sure you want to exit?"); 050 } 051 public State run() { 052 return (State) ui.processMenu(m); 053 } 054}