If you are being fairly Object Oriented about things, then you don’t want outside code reaching into your ‘black box’ of a class to play with its internals. You can get arrested for that by the OO police.
You certainly don’t want the code inside your black box to contact the outside world, because it would neve be reusable.
One of the best solutions in the event-driven web world is for your class to fire off events in exactly the same way as a button does. You stick it on the screen, and wait for it to tell you something has happened.
It could be something unseen, like updating data (from elsewhere in the application) and that data change needs to be passed on to interested parties. For this example, though, I’ll have the simplest of password protection - a hard-coded password which needs to be input.
The first thing we need, is an interface. The interface is just an agreement between the class that is the source of the event, and the class that is listening for the event. It’s no good the listener listening for an onLogIn event and an onLogOut event, and the source putting out an onLogOn event and an onLogOff event.
I’m going to make it even simpler and just have an onSignInChange event. The listener will be called a SignInListener. We will pass a boolean to indicate if the password is OK
public interface SignInListener extends java.util.EventListener
void onSignInChange(boolean passwordOK);
The SignInForm Class
The next thing to do, is to create a class to let the user log in. In this case, just an instruction label, a PasswordTextBox, and a button. These are held in a HorizontalPanel, which is the top-level widget in a Composite class.
There’s an ArrayList to hold listeners (you could instantiate it the first time a listener is added, but it isn’t vital, and I’ve left it out for simplicity’s sake)
You can add or remove listeners.
The form class itself deals with clicks on the button. When this happens, the form raises an event (the onSignInAttempt event) passing a boolean indicating if the password is valid.
class SignInForm extends Composite implements ClickListener
private ArrayList listeners = new ArrayList();
HorizontalPanel panel = new HorizontalPanel();
panel.add(new Label("Type 'password' and click the button"));
panel.add(pwd = new PasswordTextBox());
public void addSignInListener(SignInListener listener)
public void removeSignInListener(SignInListener listener)
public void onSignInAttempt(boolean passwordOK)
for(Iterator it = listeners.iterator(); it.hasNext();)
SignInListener listener = (SignInListener) it.next();
public void onClick(Widget sender)
That’s almost it. We now have a class that can raise a customn event. All we have to do is use it.
Using The Class
You use it in the same way you would use anything which has an event available - you add a listener.
To keep things simple, we’ll have the main project class just put a SignInForm on the screen and issue an alert every time the button is clicked
public class Main implements EntryPoint, UserStatusListener
public void onModuleLoad()
SignInForm form = new SignInForm();
public void onStatusChanged(boolean passwordOK)
Window.alert("Passwod correct: " + passwordOK);
So, you can see that the Main class itself deals with the event and puts up an alert.