01: /**
02: * Copyright 2006 Webmedia Group Ltd.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: **/package org.araneaframework.example.main.web;
16:
17: import org.araneaframework.core.ProxyEventListener;
18: import org.araneaframework.example.main.TemplateBaseWidget;
19: import org.araneaframework.uilib.form.FormWidget;
20: import org.araneaframework.uilib.form.control.TextControl;
21: import org.araneaframework.uilib.form.data.StringData;
22:
23: /**
24: * This is login widget. After receiving "successful" login event, it
25: * replaces itself on the call stack with real application.
26: *
27: * @author Rein Raudjärv <reinra@ut.ee>
28: */
29: public class LoginWidget extends TemplateBaseWidget {
30: private static final long serialVersionUID = 1L;
31: /* Widget we will create and attach to this widget. */
32: private FormWidget form;
33:
34: protected void init() throws Exception {
35: /* Sets the view selector that will be used for rendering this widget.
36: * The path to real JSP file is determined by:
37: * StandardJspFilterService field jspPath (configured in aranea-conf.xml) +
38: * viewselector +
39: * ".jsp" */
40: setViewSelector("login");
41:
42: /* Register a global proxying eventlistener - it receives all widget events and upon
43: * receiving event named "someEvent" proxies it to "handleEventSomeEvent" method
44: *
45: * This listener is added by default in super class and is only shown here for
46: * illustrative purposes. It can also be overridden on need.
47: */
48: setGlobalEventListener(new ProxyEventListener(this ));
49:
50: /* Create a new FormWidget with two self-described input fields. */
51: form = new FormWidget();
52: // Add the input fields. Arguments taken by addElement() :
53: // String elementName, String labelId, Control control, Data data, boolean mandatory
54: form.addElement("username", "#User", new TextControl(),
55: new StringData(), false);
56: form.addElement("password", "#Password", new TextControl(),
57: new StringData(), false);
58:
59: // attach created form to our widget.
60: addWidget("loginForm", form);
61: }
62:
63: // implementation of the login handler
64: private void handleEventLogin() throws Exception {
65: /* convertAndValidate() fails if data found from form does not
66: * satisfy the restrictions laid on it. If that is the case,
67: * we ignore received event. There is only one restriction -
68: * username field must not be empty. */
69: if (form.convertAndValidate()) {
70: // find out the username supplied
71: String username = (String) form
72: .getValueByFullName("username");
73: String password = (String) form
74: .getValueByFullName("password");
75: /* Add the message about wrong credentials to message context.
76: * Messages will be shown to user upon exiting this event. */
77: getMessageCtx().showErrorMessage(
78: "User '" + username + "'"
79: + " not allowed to log in with password '"
80: + password + "'");
81: // do nothing (do not let anyone in :))
82: }
83: }
84:
85: /* Successful login event - does not check supplied credentials,
86: * promptly replaces login widget with root widget - allowing
87: * user to start work with real examples. Demonstrates
88: * simple flow navigation. */
89: private void handleEventBypass() throws Exception {
90: getFlowCtx().replace(new RootWidget(), null);
91: }
92: }
|