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.8KB

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