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.

CSSInjectTest.java 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package com.vaadin.tests.themes;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.InputStream;
  4. import java.util.UUID;
  5. import com.vaadin.server.Page;
  6. import com.vaadin.server.Page.Styles;
  7. import com.vaadin.shared.ui.ContentMode;
  8. import com.vaadin.server.StreamResource;
  9. import com.vaadin.tests.components.TestBase;
  10. import com.vaadin.ui.Button;
  11. import com.vaadin.ui.Button.ClickEvent;
  12. import com.vaadin.ui.Label;
  13. import com.vaadin.v7.ui.TextArea;
  14. public class CSSInjectTest extends TestBase {
  15. @Override
  16. protected void setup() {
  17. final Styles stylesheet = Page.getCurrent().getStyles();
  18. // Inject some resources initially
  19. final StreamResource initialResource = new StreamResource(
  20. new StreamResource.StreamSource() {
  21. @Override
  22. public InputStream getStream() {
  23. return new ByteArrayInputStream(
  24. ".hello, .world { color:silver; }".getBytes());
  25. }
  26. }, "mystyles-" + System.currentTimeMillis() + ".css");
  27. stylesheet.add(initialResource);
  28. Label hello = new Label(
  29. "<span class='hello'>Hello</span> <span class='world'>world</span>",
  30. ContentMode.HTML);
  31. addComponent(hello);
  32. final TextArea cssToInject = new TextArea();
  33. cssToInject.setImmediate(true);
  34. addComponent(cssToInject);
  35. Button inject = new Button("Inject!", new Button.ClickListener() {
  36. @Override
  37. public void buttonClick(ClickEvent event) {
  38. stylesheet.add(cssToInject.getValue());
  39. cssToInject.setValue("");
  40. }
  41. });
  42. addComponent(inject);
  43. Button injectRandom = new Button("Inject as resource!",
  44. new Button.ClickListener() {
  45. @Override
  46. public void buttonClick(ClickEvent event) {
  47. final String css = cssToInject.getValue();
  48. stylesheet.add(new StreamResource(
  49. new StreamResource.StreamSource() {
  50. @Override
  51. public InputStream getStream() {
  52. return new ByteArrayInputStream(
  53. css.getBytes());
  54. }
  55. }, UUID.randomUUID().toString() + ".css"));
  56. cssToInject.setValue("");
  57. }
  58. });
  59. addComponent(injectRandom);
  60. addComponent(
  61. new Button("Inject initial again!", new Button.ClickListener() {
  62. @Override
  63. public void buttonClick(ClickEvent event) {
  64. stylesheet.add(initialResource);
  65. }
  66. }));
  67. }
  68. @Override
  69. protected String getDescription() {
  70. return "Demonstrates how CSS injections can be used to theme the \"Hello world\" label below";
  71. }
  72. @Override
  73. protected Integer getTicketNumber() {
  74. return 5500;
  75. }
  76. }