aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/demo/UpgradingSample.java
blob: 922c129932d7b84a9648c126a40eca608c91ab4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.itmill.toolkit.demo;

//
// Millstone imports were replaced
//
// import org.millstone.base.Application;
// import org.millstone.base.ui.*;
// import org.millstone.base.data.*;
//
import com.itmill.toolkit.Application;
import com.itmill.toolkit.data.Property;
import com.itmill.toolkit.ui.Button;
import com.itmill.toolkit.ui.CustomComponent;
import com.itmill.toolkit.ui.GridLayout;
import com.itmill.toolkit.ui.Label;
import com.itmill.toolkit.ui.OrderedLayout;
import com.itmill.toolkit.ui.Panel;
import com.itmill.toolkit.ui.TextField;
import com.itmill.toolkit.ui.Tree;
import com.itmill.toolkit.ui.Window;

/**
 * <p>
 * Example application demonstrating simple user login. This example is from
 * MillStone 3.1.1 examples section. Upgrading from 3.1.1 to 4.0.0 was done by
 * updating imports, also setTheme("corporate") call was added to application
 * init method.
 * </p>
 * 
 * @since 3.1.1
 * @author IT Mill Ltd.
 */
public class UpgradingSample extends Application implements
        Property.ValueChangeListener {

    /* Menu for navigating inside the application. */
    private Tree menu = new Tree();

    /* Contents of the website */
    private String[][] pages = { { "Welcome", "Welcome to our website..." },
            { "Products", "Public product information." },
            { "Contact", "Public contact information." },
            { "CRM", "CRM Database requiring login." },
            { "Intranet", "Internal information database." } };

    /* Application layout */
    private GridLayout layout = new GridLayout(2, 1);

    /* Initialize the application */
    public void init() {

        // Create the main window of the application
        Window main = new Window("Login example", layout);
        setMainWindow(main);

        // Add menu and loginbox to the application
        OrderedLayout l = new OrderedLayout();
        layout.addComponent(l, 0, 0);
        l.addComponent(menu);
        l.addComponent(new LoginBox());

        // Setup menu
        menu.setStyle("menu");
        menu.addListener(this);
        menu.setImmediate(true);
        addToMenu(new String[] { "Welcome", "Products", "Contact" });
    }

    // Overriding usetUser method is a simple way of updating application
    // privileges when the user is changed
    public void setUser(Object user) {
        super.setUser(user);
        if (user != null) {
            addToMenu(new String[] { "CRM", "Intranet" });
        }
    }

    public void addToMenu(String[] items) {
        for (int i = 0; i < items.length; i++) {
            menu.addItem(items[i]);
            menu.setChildrenAllowed(items[i], false);
        }
        if (menu.getValue() == null) {
            menu.setValue(items[0]);
        }
    }

    // Handle menu selection and update visible page
    public void valueChange(Property.ValueChangeEvent event) {
        layout.removeComponent(1, 0);
        String title = (String) menu.getValue();
        for (int i = 0; i < pages.length; i++) {
            if (pages[i][0].equals(title)) {
                Panel p = new Panel(pages[i][0]);
                p.addComponent(new Label(pages[i][1]));
                p.setStyle("strong");
                layout.addComponent(p, 1, 0);
            }
        }
    }

    // Simple loginbox component for the application
    public class LoginBox extends CustomComponent implements
            Application.UserChangeListener {

        // The components this loginbox is composed of
        private TextField loginName = new TextField("Name");

        private Button loginButton = new Button("Enter", this, "login");

        private Panel loginPanel = new Panel("Login");

        private Panel statusPanel = new Panel();

        private Button logoutButton = new Button("Logout",
                UpgradingSample.this, "close");

        private Label statusLabel = new Label();

        // Initialize login component
        public LoginBox() {

            // Initialize the component
            loginPanel.addComponent(loginName);
            loginPanel.addComponent(loginButton);
            loginPanel.setStyle("strong");
            loginName.setColumns(8);
            statusPanel.addComponent(statusLabel);
            statusPanel.addComponent(logoutButton);

            // Set the status of the loginbox and show correct
            // components
            updateStatus();

            // Listen application user change events
            UpgradingSample.this.addListener(this);
        }

        // Login into application
        public void login() {
            String name = (String) loginName.getValue();
            if (name != null && name.length() > 0) {
                setUser(name);
            }
            loginName.setValue("");
        }

        // Update login status on application user change events
        public void applicationUserChanged(Application.UserChangeEvent event) {
            updateStatus();
        }

        // Update login status of the component by exposing correct
        // components
        private void updateStatus() {
            statusLabel.setValue("User: " + getUser());
            if (getUser() != null) {
                setCompositionRoot(statusPanel);
            } else {
                setCompositionRoot(loginPanel);
            }
        }
    }
}