You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UpgradingSample.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.demo;
  5. //
  6. // Millstone imports were replaced
  7. //
  8. // import org.millstone.base.Application;
  9. // import org.millstone.base.ui.*;
  10. // import org.millstone.base.data.*;
  11. //
  12. import com.itmill.toolkit.Application;
  13. import com.itmill.toolkit.data.Property;
  14. import com.itmill.toolkit.ui.Button;
  15. import com.itmill.toolkit.ui.CustomComponent;
  16. import com.itmill.toolkit.ui.GridLayout;
  17. import com.itmill.toolkit.ui.Label;
  18. import com.itmill.toolkit.ui.OrderedLayout;
  19. import com.itmill.toolkit.ui.Panel;
  20. import com.itmill.toolkit.ui.TextField;
  21. import com.itmill.toolkit.ui.Tree;
  22. import com.itmill.toolkit.ui.Window;
  23. /**
  24. * <p>
  25. * Example application demonstrating simple user login. This example is from
  26. * MillStone 3.1.1 examples section. Upgrading from 3.1.1 to 4.0.0 was done by
  27. * updating imports, also setTheme("corporate") call was added to application
  28. * init method.
  29. * </p>
  30. *
  31. * @since 3.1.1
  32. * @author IT Mill Ltd.
  33. */
  34. public class UpgradingSample extends Application implements
  35. Property.ValueChangeListener {
  36. /* Menu for navigating inside the application. */
  37. private final Tree menu = new Tree();
  38. /* Contents of the website */
  39. private final String[][] pages = {
  40. { "Welcome", "Welcome to our website..." },
  41. { "Products", "Public product information." },
  42. { "Contact", "Public contact information." },
  43. { "CRM", "CRM Database requiring login." },
  44. { "Intranet", "Internal information database." } };
  45. /* Application layout */
  46. private final GridLayout layout = new GridLayout(2, 1);
  47. /* Initialize the application */
  48. public void init() {
  49. // Create the main window of the application
  50. final Window main = new Window("Login example", layout);
  51. setMainWindow(main);
  52. // Add menu and loginbox to the application
  53. final OrderedLayout l = new OrderedLayout();
  54. layout.addComponent(l, 0, 0);
  55. l.addComponent(menu);
  56. l.addComponent(new LoginBox());
  57. // Setup menu
  58. menu.setStyle("menu");
  59. menu.addListener(this);
  60. menu.setImmediate(true);
  61. addToMenu(new String[] { "Welcome", "Products", "Contact" });
  62. }
  63. // Overriding usetUser method is a simple way of updating application
  64. // privileges when the user is changed
  65. public void setUser(Object user) {
  66. super.setUser(user);
  67. if (user != null) {
  68. addToMenu(new String[] { "CRM", "Intranet" });
  69. }
  70. }
  71. public void addToMenu(String[] items) {
  72. for (int i = 0; i < items.length; i++) {
  73. menu.addItem(items[i]);
  74. menu.setChildrenAllowed(items[i], false);
  75. }
  76. if (menu.getValue() == null) {
  77. menu.setValue(items[0]);
  78. }
  79. }
  80. // Handle menu selection and update visible page
  81. public void valueChange(Property.ValueChangeEvent event) {
  82. layout.removeComponent(1, 0);
  83. final String title = (String) menu.getValue();
  84. for (int i = 0; i < pages.length; i++) {
  85. if (pages[i][0].equals(title)) {
  86. final Panel p = new Panel(pages[i][0]);
  87. p.addComponent(new Label(pages[i][1]));
  88. p.setStyle("strong");
  89. layout.addComponent(p, 1, 0);
  90. }
  91. }
  92. }
  93. // Simple loginbox component for the application
  94. public class LoginBox extends CustomComponent implements
  95. Application.UserChangeListener {
  96. // The components this loginbox is composed of
  97. private final TextField loginName = new TextField("Name");
  98. private final Button loginButton = new Button("Enter", this, "login");
  99. private final Panel loginPanel = new Panel("Login");
  100. private final Panel statusPanel = new Panel();
  101. private final Button logoutButton = new Button("Logout",
  102. UpgradingSample.this, "close");
  103. private final Label statusLabel = new Label();
  104. // Initialize login component
  105. public LoginBox() {
  106. // Initialize the component
  107. loginPanel.addComponent(loginName);
  108. loginPanel.addComponent(loginButton);
  109. loginPanel.setStyle("strong");
  110. loginName.setColumns(8);
  111. statusPanel.addComponent(statusLabel);
  112. statusPanel.addComponent(logoutButton);
  113. // Set the status of the loginbox and show correct
  114. // components
  115. updateStatus();
  116. // Listen application user change events
  117. UpgradingSample.this.addListener(this);
  118. }
  119. // Login into application
  120. public void login() {
  121. final String name = (String) loginName.getValue();
  122. if (name != null && name.length() > 0) {
  123. setUser(name);
  124. }
  125. loginName.setValue("");
  126. }
  127. // Update login status on application user change events
  128. public void applicationUserChanged(Application.UserChangeEvent event) {
  129. updateStatus();
  130. }
  131. // Update login status of the component by exposing correct
  132. // components
  133. private void updateStatus() {
  134. statusLabel.setValue("User: " + getUser());
  135. if (getUser() != null) {
  136. setCompositionRoot(statusPanel);
  137. } else {
  138. setCompositionRoot(loginPanel);
  139. }
  140. }
  141. }
  142. }