001: package org.millstone.examples;
002:
003: import org.millstone.base.Application;
004: import org.millstone.base.ui.*;
005: import org.millstone.base.data.*;
006:
007: /** <p>Example application demonstrating simple user login.</p>
008: *
009: * @author IT Mill Ltd.
010: * @see org.millstone.base.service.Application
011: * @see org.millstone.base.ui.Window
012: * @see org.millstone.base.ui.TextField
013: * @see org.millstone.base.ui.Button
014: */
015: public class Login extends Application implements
016: Property.ValueChangeListener {
017:
018: /* Menu for navigating inside the application. */
019: private Tree menu = new Tree();
020:
021: /* Contents of the website */
022: private String[][] pages = {
023: { "Welcome", "Welcome to out website..." },
024: { "Products", "Public product information." },
025: { "Contact", "Public contact information." },
026: { "CRM", "CRM Database requiring login." },
027: { "Intranet", "Internal information database." } };
028:
029: /* Application layout */
030: private GridLayout layout = new GridLayout(2, 1);
031:
032: /* Initialize the application */
033: public void init() {
034:
035: // Create the main window of the application
036: Window main = new Window("Login example", layout);
037: setMainWindow(main);
038:
039: // Add menu and loginbox to the application
040: OrderedLayout l = new OrderedLayout();
041: layout.addComponent(l, 0, 0);
042: l.addComponent(menu);
043: l.addComponent(new LoginBox());
044:
045: // Setup menu
046: menu.setStyle("menu");
047: menu.addListener(this );
048: menu.setImmediate(true);
049: addToMenu(new String[] { "Welcome", "Products", "Contact" });
050: }
051:
052: // Overriding usetUser method is a simple way of updating application
053: // privileges when the user is changed
054: public void setUser(Object user) {
055: super .setUser(user);
056: if (user != null)
057: addToMenu(new String[] { "CRM", "Intranet" });
058: }
059:
060: public void addToMenu(String[] items) {
061: for (int i = 0; i < items.length; i++) {
062: menu.addItem(items[i]);
063: menu.setChildrenAllowed(items[i], false);
064: }
065: if (menu.getValue() == null)
066: menu.setValue(items[0]);
067: }
068:
069: // Handle menu selection and update visible page
070: public void valueChange(Property.ValueChangeEvent event) {
071: layout.removeComponent(1, 0);
072: String title = (String) menu.getValue();
073: for (int i = 0; i < pages.length; i++)
074: if (pages[i][0].equals(title)) {
075: Panel p = new Panel(pages[i][0]);
076: p.addComponent(new Label(pages[i][1]));
077: p.setStyle("strong");
078: layout.addComponent(p, 1, 0);
079: }
080: }
081:
082: // Simple loginbox component for the application
083: public class LoginBox extends CustomComponent implements
084: Application.UserChangeListener {
085:
086: // The components this loginbox is composed of
087: private TextField loginName = new TextField("Name");
088: private Button loginButton = new Button("Enter", this , "login");
089: private Panel loginPanel = new Panel("Login");
090: private Panel statusPanel = new Panel();
091: private Button logoutButton = new Button("Logout", Login.this ,
092: "close");
093: private Label statusLabel = new Label();
094:
095: // Initialize login component
096: public LoginBox() {
097:
098: // Initialize the component
099: loginPanel.addComponent(loginName);
100: loginPanel.addComponent(loginButton);
101: loginPanel.setStyle("strong");
102: loginButton.dependsOn(loginName);
103: loginName.setColumns(8);
104: statusPanel.addComponent(statusLabel);
105: statusPanel.addComponent(logoutButton);
106:
107: // Set the status of the loginbox and show correct components
108: updateStatus();
109:
110: // Listen application user change events
111: Login.this .addListener(this );
112: }
113:
114: // Login into application
115: public void login() {
116: String name = (String) loginName.getValue();
117: if (name != null && name.length() > 0)
118: setUser(name);
119: loginName.setValue("");
120: }
121:
122: // Update login status on application user change events
123: public void applicationUserChanged(
124: Application.UserChangeEvent event) {
125: updateStatus();
126: }
127:
128: // Update login status of the component by exposing correct components
129: private void updateStatus() {
130: statusLabel.setValue("User: " + getUser());
131: if (getUser() != null)
132: setCompositionRoot(statusPanel);
133: else
134: setCompositionRoot(loginPanel);
135: }
136: }
137: }
138:
139: /* This Millstone sample code is public domain. *
140: * For more information see www.millstone.org. */
|