SOURCECODE |
How to... use a LogOnForm
Description:
A LogOnForm is a special FormSheet with a ComboBox and an optional PasswordField.
It can be used where ever you want a User to be identified.
The ComboBox will show the Users stored in the UserManager and in the PasswordField you can enter the password.
If the identification has been successful, the LogOnForm will return the User that was identified, otherwise null.
ToDo's:
- Instantiate a new LogOnForm with
- Its title
- The prompt of the ComboBox
- and of the PasswordField
- boolean wether password is needed or not
- the UserManager storing the User to be identified
- a comparator for the appearance of the Users (null -> Users will be ordered by their names)
- a filter in order to display only certain Users (null -> all Users will be displayed)
- Display the FormSheet in a suitable FormSheetContainer
The method getResult() will return the User if the identification was successful, else null.
Uses:
UserManager User LogOnForm
//here we add the LogOnForm to a menu item
MenuSheetItem msLogOnItem = new MenuSheetItem( "LogOn", new sale.Action() {
public void doAction (SaleProcess p, SalesPoint sp) {
1
//define a new LogOnForm
LogOnForm lof = new LogOnForm( "Please identify yourself", //title
"Username: ", //ComboBox prompt
"Password: ", //PasswordField prompt
true, //password needed
usermanager, //userManager, initialized above (UserManager.getGlobalUM())
null, //no Comparator for order
null) { //no Filter
//to inform the user that no new logon will be performed
public void cancel() {
JOptionPane.showMessageDialog( null,
"Logon canceled",
"No new User is being logged on",
JOptionPane.WARNING_MESSAGE);
super.cancel();
}
};
2
//display it
JDisplayDialog jdd = new JDisplayDialog();
jdd.show();
try{
jdd.setFormSheet(lof);
}
catch (InterruptedException ie) {
System.out.println("InterruptedException in VideoMachine.java line 345");
}
//method to resolve the identified User after pressing "ok".
//Will be null, if unsuccessful (wrong password...) or canceled
//the entered password will be automatically garbled with the
//global password garbler before being checked. For more information
//on password garbling, please refere to "How to garble a User's password".
User u = lof.getResult();
//logon was successful
if (u != null) {
//log on the user to the shop
usermanager.logOn(Shop.getTheShop(), u);
//logon failed, but not due to cancellation
} else if(!lof.isCancelled())
JOptionPane.showMessageDialog( null,
"Wrong password",
"No user is being logged on",
JOptionPane.ERROR_MESSAGE);
((MultiWindow)getShopFrame()).setMenuSheet(createShopMenuSheet());
}
});