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 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.tests;
  5. import com.vaadin.ui.Button;
  6. import com.vaadin.ui.CustomLayout;
  7. import com.vaadin.ui.Field;
  8. import com.vaadin.ui.Label;
  9. import com.vaadin.ui.Panel;
  10. import com.vaadin.ui.TextField;
  11. import com.vaadin.ui.Tree;
  12. import com.vaadin.ui.Window;
  13. import com.vaadin.ui.Component.Event;
  14. import com.vaadin.ui.Component.Listener;
  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 IT Mill Ltd.
  25. * @since 4.0.0
  26. *
  27. */
  28. public class CustomLayoutDemo extends com.vaadin.Application implements
  29. Listener {
  30. private CustomLayout mainLayout = null;
  31. private final Panel bodyPanel = new Panel();
  32. private final TextField username = new TextField("Username");
  33. private final TextField loginPwd = new TextField("Password");
  34. private final Button loginButton = new Button("Login", this, "loginClicked");
  35. private final Tree menu = new Tree();
  36. /**
  37. * Initialize Application. Demo components are added to main window.
  38. */
  39. @Override
  40. public void init() {
  41. final Window mainWindow = new Window("CustomLayout demo");
  42. setMainWindow(mainWindow);
  43. // set the application to use example -theme
  44. setTheme("example");
  45. // Create custom layout, themes/example/layout/mainLayout.html
  46. mainLayout = new CustomLayout("mainLayout");
  47. // wrap custom layout inside a panel
  48. final Panel customLayoutPanel = new Panel(
  49. "Panel containing custom layout (mainLayout.html)");
  50. customLayoutPanel.addComponent(mainLayout);
  51. // Login components
  52. loginPwd.setSecret(true);
  53. mainLayout.addComponent(username, "loginUser");
  54. mainLayout.addComponent(loginPwd, "loginPassword");
  55. mainLayout.addComponent(loginButton, "loginButton");
  56. // Menu component, when clicked bodyPanel is updated
  57. menu.addItem("Welcome");
  58. menu.addItem("Products");
  59. menu.addItem("Support");
  60. menu.addItem("News");
  61. menu.addItem("Developers");
  62. menu.addItem("Contact");
  63. // "this" handles all menu events, e.g. node clicked event
  64. menu.addListener(this);
  65. // Value changes are immediate
  66. menu.setImmediate(true);
  67. menu.setNullSelectionAllowed(false);
  68. mainLayout.addComponent(menu, "menu");
  69. // Body component
  70. mainLayout.addComponent(bodyPanel, "body");
  71. // Initial body are comes from Welcome.html
  72. setBody("Welcome");
  73. // Add heading label and custom layout panel to main window
  74. mainWindow.addComponent(new Label("<h3>Custom layout demo</h3>",
  75. Label.CONTENT_XHTML));
  76. mainWindow.addComponent(customLayoutPanel);
  77. }
  78. /**
  79. * Login button clicked. Hide login components and replace username
  80. * component with "Welcome user Username" message.
  81. *
  82. */
  83. public void loginClicked() {
  84. username.setVisible(false);
  85. loginPwd.setVisible(false);
  86. if (username.getValue().toString().length() < 1) {
  87. username.setValue("Anonymous");
  88. }
  89. mainLayout.replaceComponent(loginButton, new Label("Welcome user <em>"
  90. + username.getValue() + "</em>", Label.CONTENT_XHTML));
  91. }
  92. /**
  93. * Set body panel caption, remove all existing components and add given
  94. * custom layout in it.
  95. *
  96. */
  97. public void setBody(String customLayout) {
  98. bodyPanel.setCaption(customLayout + ".html");
  99. bodyPanel.removeAllComponents();
  100. bodyPanel.addComponent(new CustomLayout(customLayout));
  101. }
  102. /**
  103. * Handle all menu events. Updates body panel contents if menu item is
  104. * clicked.
  105. */
  106. public void componentEvent(Event event) {
  107. // Check if event occured at fsTree component
  108. if (event.getSource() == menu) {
  109. // Check if event is about changing value
  110. if (event.getClass() == Field.ValueChangeEvent.class) {
  111. // Update body area with selected item
  112. setBody(menu.getValue().toString());
  113. }
  114. // here we could check for other type of events for tree
  115. // component
  116. }
  117. // here we could check for other component's events
  118. }
  119. }