aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/UpgradingSample.java
diff options
context:
space:
mode:
Diffstat (limited to 'uitest/src/com/vaadin/tests/UpgradingSample.java')
-rw-r--r--uitest/src/com/vaadin/tests/UpgradingSample.java197
1 files changed, 197 insertions, 0 deletions
diff --git a/uitest/src/com/vaadin/tests/UpgradingSample.java b/uitest/src/com/vaadin/tests/UpgradingSample.java
new file mode 100644
index 0000000000..48e2222d7e
--- /dev/null
+++ b/uitest/src/com/vaadin/tests/UpgradingSample.java
@@ -0,0 +1,197 @@
+/*
+ * Copyright 2011 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.vaadin.tests;
+
+//
+// Millstone imports were replaced
+//
+// import org.millstone.base.Application;
+// import org.millstone.base.ui.*;
+// import org.millstone.base.data.*;
+//
+import com.vaadin.Application;
+import com.vaadin.data.Property;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Button.ClickEvent;
+import com.vaadin.ui.CustomComponent;
+import com.vaadin.ui.GridLayout;
+import com.vaadin.ui.Label;
+import com.vaadin.ui.Panel;
+import com.vaadin.ui.UI.LegacyWindow;
+import com.vaadin.ui.TextField;
+import com.vaadin.ui.Tree;
+import com.vaadin.ui.VerticalLayout;
+
+/**
+ * <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 Vaadin Ltd.
+ */
+public class UpgradingSample extends Application.LegacyApplication implements
+ Property.ValueChangeListener {
+
+ /* Menu for navigating inside the application. */
+ private final Tree menu = new Tree();
+
+ /* Contents of the website */
+ private final 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 final GridLayout layout = new GridLayout(2, 1);
+
+ /* Initialize the application */
+ @Override
+ public void init() {
+
+ // Create the main window of the application
+ final LegacyWindow main = new LegacyWindow("Login example", layout);
+ setMainWindow(main);
+
+ // Add menu and loginbox to the application
+ final VerticalLayout l = new VerticalLayout();
+ layout.addComponent(l, 0, 0);
+ l.addComponent(menu);
+ l.addComponent(new LoginBox());
+
+ // Setup menu
+ menu.setStyleName("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
+ @Override
+ 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
+ @Override
+ public void valueChange(Property.ValueChangeEvent event) {
+ layout.removeComponent(1, 0);
+ final String title = (String) menu.getValue();
+ for (int i = 0; i < pages.length; i++) {
+ if (pages[i][0].equals(title)) {
+ final Panel p = new Panel(pages[i][0]);
+ p.addComponent(new Label(pages[i][1]));
+ p.setStyleName("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 final TextField loginName = new TextField("Name");
+
+ private final Button loginButton = new Button("Enter",
+ new Button.ClickListener() {
+ @Override
+ public void buttonClick(ClickEvent event) {
+ login();
+ }
+ });
+
+ private final Panel loginPanel = new Panel("Login");
+
+ private final Panel statusPanel = new Panel();
+
+ private final Button logoutButton = new Button("Logout",
+ new Button.ClickListener() {
+ @Override
+ public void buttonClick(ClickEvent event) {
+ close();
+ }
+ });
+
+ private final Label statusLabel = new Label();
+
+ // Initialize login component
+ public LoginBox() {
+
+ // Initialize the component
+ loginPanel.addComponent(loginName);
+ loginPanel.addComponent(loginButton);
+ loginPanel.setStyleName("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() {
+ final String name = loginName.getValue();
+ if (name != null && name.length() > 0) {
+ setUser(name);
+ }
+ loginName.setValue("");
+ }
+
+ // Update login status on application user change events
+ @Override
+ 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);
+ }
+ }
+ }
+}