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.

BootstrapModifyUI.java 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.vaadincontext;
  17. import org.jsoup.nodes.Element;
  18. import org.jsoup.parser.Tag;
  19. import com.vaadin.server.BootstrapFragmentResponse;
  20. import com.vaadin.server.BootstrapListener;
  21. import com.vaadin.server.BootstrapPageResponse;
  22. import com.vaadin.server.BootstrapResponse;
  23. import com.vaadin.server.VaadinRequest;
  24. import com.vaadin.tests.components.AbstractReindeerTestUI;
  25. import com.vaadin.ui.Button;
  26. import com.vaadin.ui.UI;
  27. public class BootstrapModifyUI extends AbstractReindeerTestUI {
  28. private static final String INSTALLED_ATRIBUTE_NAME = BootstrapModifyUI.class
  29. .getName() + ".installed";
  30. @Override
  31. protected void setup(VaadinRequest request) {
  32. Button c = new Button("Add bootstrap listener",
  33. event->{
  34. getSession().addBootstrapListener(
  35. createBootstrapListener());
  36. event.getButton().setEnabled(false);
  37. getSession().setAttribute(INSTALLED_ATRIBUTE_NAME,
  38. Boolean.TRUE);
  39. });
  40. addComponent(c);
  41. c.setEnabled(
  42. getSession().getAttribute(INSTALLED_ATRIBUTE_NAME) == null);
  43. }
  44. private static BootstrapListener createBootstrapListener() {
  45. return new BootstrapListener() {
  46. @Override
  47. public void modifyBootstrapFragment(
  48. BootstrapFragmentResponse response) {
  49. if (shouldModify(response)) {
  50. Element heading = new Element(Tag.valueOf("div"), "")
  51. .text("Added by modifyBootstrapFragment");
  52. response.getFragmentNodes().add(0, heading);
  53. }
  54. }
  55. private boolean shouldModify(BootstrapResponse response) {
  56. Class<? extends UI> uiClass = response.getUiClass();
  57. boolean shouldModify = uiClass == BootstrapModifyUI.class;
  58. return shouldModify;
  59. }
  60. @Override
  61. public void modifyBootstrapPage(BootstrapPageResponse response) {
  62. if (shouldModify(response)) {
  63. response.getDocument().body().child(0)
  64. .before("<div>Added by modifyBootstrapPage</div>");
  65. }
  66. }
  67. };
  68. }
  69. @Override
  70. protected String getTestDescription() {
  71. return "There should be two additional divs in the HTML of the bootstrap page for this UI";
  72. }
  73. @Override
  74. protected Integer getTicketNumber() {
  75. return Integer.valueOf(9274);
  76. }
  77. }