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.

CustomLayoutDemo.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package com.vaadin.tests;
  2. import com.vaadin.shared.ui.ContentMode;
  3. import com.vaadin.ui.Button;
  4. import com.vaadin.ui.Component.Event;
  5. import com.vaadin.ui.Component.Listener;
  6. import com.vaadin.ui.CustomLayout;
  7. import com.vaadin.ui.Label;
  8. import com.vaadin.ui.LegacyWindow;
  9. import com.vaadin.ui.Panel;
  10. import com.vaadin.ui.PasswordField;
  11. import com.vaadin.ui.TextField;
  12. import com.vaadin.ui.VerticalLayout;
  13. import com.vaadin.v7.ui.Field;
  14. import com.vaadin.v7.ui.Tree;
  15. /**
  16. * This example demonstrates custom layout. All components created here are
  17. * placed using custom.html file. Custom layouts may be created with any web
  18. * designer tool such as Dreamweaver. To place Vaadin components into html page,
  19. * use divs with location tag as an identifier for Vaadin components, see html
  20. * page (themes/example/layout/custom.html) and source code below. Body panel
  21. * contents are changed when menu items are clicked. Contents are HTML pages
  22. * located at themes/example/layout directory.
  23. *
  24. * @author Vaadin Ltd.
  25. * @since 4.0.0
  26. *
  27. */
  28. public class CustomLayoutDemo extends com.vaadin.server.LegacyApplication
  29. implements Listener {
  30. private CustomLayout mainLayout = null;
  31. private final Panel bodyPanel = new Panel();
  32. private final TextField username = new TextField("Username");
  33. private final PasswordField loginPwd = new PasswordField("Password");
  34. private final Button loginButton = new Button("Login",
  35. event -> loginClicked());
  36. private final Tree menu = new Tree();
  37. /**
  38. * Initialize Application. Demo components are added to main window.
  39. */
  40. @Override
  41. public void init() {
  42. final LegacyWindow mainWindow = new LegacyWindow("CustomLayout demo");
  43. setMainWindow(mainWindow);
  44. // set the application to use example -theme
  45. setTheme("tests-components");
  46. // Create custom layout, themes/example/layout/mainLayout.html
  47. mainLayout = new CustomLayout("mainLayout");
  48. // wrap custom layout inside a panel
  49. VerticalLayout customLayoutPanelLayout = new VerticalLayout();
  50. customLayoutPanelLayout.setMargin(true);
  51. final Panel customLayoutPanel = new Panel(
  52. "Panel containing custom layout (mainLayout.html)",
  53. customLayoutPanelLayout);
  54. customLayoutPanelLayout.addComponent(mainLayout);
  55. // Login components
  56. mainLayout.addComponent(username, "loginUser");
  57. mainLayout.addComponent(loginPwd, "loginPassword");
  58. mainLayout.addComponent(loginButton, "loginButton");
  59. // Menu component, when clicked bodyPanel is updated
  60. menu.addItem("Welcome");
  61. menu.addItem("Products");
  62. menu.addItem("Support");
  63. menu.addItem("News");
  64. menu.addItem("Developers");
  65. menu.addItem("Contact");
  66. // "this" handles all menu events, e.g. node clicked event
  67. menu.addListener(this);
  68. // Value changes are immediate
  69. menu.setImmediate(true);
  70. menu.setNullSelectionAllowed(false);
  71. mainLayout.addComponent(menu, "menu");
  72. // Body component
  73. mainLayout.addComponent(bodyPanel, "body");
  74. // Initial body are comes from Welcome.html
  75. setBody("Welcome");
  76. // Add heading label and custom layout panel to main window
  77. mainWindow.addComponent(
  78. new Label("<h3>Custom layout demo</h3>", ContentMode.HTML));
  79. mainWindow.addComponent(customLayoutPanel);
  80. }
  81. /**
  82. * Login button clicked. Hide login components and replace username
  83. * component with "Welcome user Username" message.
  84. *
  85. */
  86. public void loginClicked() {
  87. username.setVisible(false);
  88. loginPwd.setVisible(false);
  89. if (username.getValue().length() < 1) {
  90. username.setValue("Anonymous");
  91. }
  92. mainLayout.replaceComponent(loginButton,
  93. new Label("Welcome user <em>" + username.getValue() + "</em>",
  94. ContentMode.HTML));
  95. }
  96. /**
  97. * Set body panel caption, remove all existing components and add given
  98. * custom layout in it.
  99. *
  100. */
  101. public void setBody(String customLayout) {
  102. VerticalLayout bodyLayout = new VerticalLayout();
  103. bodyLayout.setMargin(true);
  104. bodyLayout.addComponent(new CustomLayout(customLayout));
  105. bodyPanel.setContent(bodyLayout);
  106. bodyPanel.setCaption(customLayout + ".html");
  107. }
  108. /**
  109. * Handle all menu events. Updates body panel contents if menu item is
  110. * clicked.
  111. */
  112. @Override
  113. public void componentEvent(Event event) {
  114. // Check if event occurred at fsTree component
  115. if (event.getSource() == menu) {
  116. // Check if event is about changing value
  117. if (event.getClass() == Field.ValueChangeEvent.class) {
  118. // Update body area with selected item
  119. setBody(menu.getValue().toString());
  120. }
  121. // here we could check for other type of events for tree
  122. // component
  123. }
  124. // here we could check for other component's events
  125. }
  126. }