public abstract class AbstractView extends Object
parentView
and methods (e.g. prompt()
Modifier and Type | Field and Description |
---|---|
protected Scanner |
keyboard
the
Scanner to handle user command line input |
protected String |
nameInParentMenu
the name of this view when displayed in a parent
MenuView |
protected AbstractView |
parentView
the parent view of the current view
if the current view is the root view, this field will always be
null |
protected String |
runningTitle
the string always to be displayed in the first line
whenever this view is running
|
protected ViewConfig |
viewConfig
the configuration of default strings in the UI
|
Constructor and Description |
---|
AbstractView(String runningTitle,
String nameInParentMenu)
default constructor
|
AbstractView(String runningTitle,
String nameInParentMenu,
ViewConfig viewConfig)
create a view with your own choice of words
|
Modifier and Type | Method and Description |
---|---|
protected void |
actionCanceled()
print
this.viewConfig.actionCanceledMessage |
protected void |
actionFailed()
print
this.viewConfig.actionFailedMessage |
protected void |
actionSuccessful()
print
this.viewConfig.actionSuccessfulMessage |
protected boolean |
confirmDialog(String warningMessage)
Show confirmation dialog; if confirmed, return; else, print
this.viewConfig.actionCanceledMessage |
abstract void |
display()
display this view in the console
|
String |
getNameInParentMenu() |
AbstractView |
getParentView() |
String |
getRunningTitle() |
ViewConfig |
getViewConfig() |
protected void |
goBack()
Try to go back to the parent view.
|
protected void |
onBack()
Override this method to execute your own logic when the view is going back to the parent view
Remember to call "super.onBack()" after your own logic
|
protected void |
onQuit()
Override this method to execute your own logic when the user is quiting (i.e.
|
protected void |
pause()
print the default pause message, and the user may press enter to continue.
|
protected void |
print(Object o)
A wrapper of
System.out.print(Object o) |
protected void |
println()
A wrapper of
System.out.println() |
protected void |
println(Object o)
A wrapper of
System.out.println(Object o) |
protected <T> T |
prompt(String message,
Class<T> expectedClass)
Read an object from command line user input with default type validation.
|
protected <T> T |
prompt(String message,
Class<T> expectedClass,
Validator<T> validator)
Read an object from command line user input with custom validator
It will repeatedly ask the user for re-try if validation continues to fail.
|
void |
setNameInParentMenu(String nameInParentMenu) |
void |
setParentView(AbstractView parentView) |
void |
setRunningTitle(String runningTitle) |
void |
setViewConfig(ViewConfig viewConfig) |
protected AbstractView parentView
null
protected String runningTitle
protected String nameInParentMenu
MenuView
protected ViewConfig viewConfig
protected Scanner keyboard
Scanner
to handle user command line inputpublic AbstractView(String runningTitle, String nameInParentMenu)
runningTitle
- see runningTitle
nameInParentMenu
- see nameInParentView
public AbstractView(String runningTitle, String nameInParentMenu, ViewConfig viewConfig)
runningTitle
- see runningTitle
nameInParentMenu
- see nameInParentView
viewConfig
- public abstract void display()
protected void onBack()
protected void onQuit()
protected void goBack()
.display()
else if the parent view is an action, then just return without printing anythingprotected void pause()
protected boolean confirmDialog(String warningMessage)
this.viewConfig.actionCanceledMessage
warningMessage
- e.g. "Make a booking now?"protected void actionCanceled()
this.viewConfig.actionCanceledMessage
protected void actionSuccessful()
this.viewConfig.actionSuccessfulMessage
protected void actionFailed()
this.viewConfig.actionFailedMessage
protected void print(Object o)
System.out.print(Object o)
o
- the object to printprotected void println(Object o)
System.out.println(Object o)
o
- the object to printprotected void println()
System.out.println()
protected <T> T prompt(String message, Class<T> expectedClass)
T
- Expected classmessage
- The message to print asking for user's inputexpectedClass
- The class of the expected data scanned (e.g. Integer.class
).
Currently all expected classes as in the .nextInt()
,
.nextDouble()
etc. are supported. Aka when you create a
Scanner keyboard = new Scanner(System.in);
, all the next...()
methods you will invoke on the keyboard
object can be
replaced by this methodprotected <T> T prompt(String message, Class<T> expectedClass, Validator<T> validator)
T
- Expected classmessage
- The message to print asking for user's inputexpectedClass
- The class of the expected data scanned (e.g. Integer.class
).
Currently all expected classes as in the .nextInt()
,
.nextDouble()
etc. are supported. Aka when you create a
Scanner keyboard = new Scanner(System.in);
, all the next...()
methods you will invoke on the keyboard
object can be
replaced by this methodvalidator
- A custom validator to validate the user inputpublic AbstractView getParentView()
public void setParentView(AbstractView parentView)
public String getRunningTitle()
public void setRunningTitle(String runningTitle)
public String getNameInParentMenu()
public void setNameInParentMenu(String nameInParentMenu)
public ViewConfig getViewConfig()
public void setViewConfig(ViewConfig viewConfig)
Copyright © 2016. All rights reserved.