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

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