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.

CSSInjectWithColorpicker.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package com.vaadin.tests.minitutorials.v71beta;
  2. import java.util.Arrays;
  3. import com.vaadin.annotations.Widgetset;
  4. import com.vaadin.server.Page;
  5. import com.vaadin.server.Page.Styles;
  6. import com.vaadin.server.VaadinRequest;
  7. import com.vaadin.shared.ui.ContentMode;
  8. import com.vaadin.shared.ui.MarginInfo;
  9. import com.vaadin.shared.ui.colorpicker.Color;
  10. import com.vaadin.ui.Alignment;
  11. import com.vaadin.ui.ColorPicker;
  12. import com.vaadin.ui.ComboBox;
  13. import com.vaadin.ui.Component;
  14. import com.vaadin.ui.HorizontalLayout;
  15. import com.vaadin.ui.Label;
  16. import com.vaadin.ui.Panel;
  17. import com.vaadin.ui.TextArea;
  18. import com.vaadin.ui.UI;
  19. import com.vaadin.ui.VerticalLayout;
  20. @Widgetset("com.vaadin.DefaultWidgetSet")
  21. public class CSSInjectWithColorpicker extends UI {
  22. @Override
  23. protected void init(VaadinRequest request) {
  24. // Create a text editor
  25. Component editor = createEditor(
  26. "Lorem ipsum dolor sit amet, lacus pharetra sed, sit a "
  27. + "tortor. Id aliquam lorem pede, orci ut enim metus, diam nulla mi "
  28. + "suspendisse tempor tortor. Eleifend lorem proin, morbi vel diam ut. "
  29. + "Tempor est tellus vitae, pretium condimentum facilisis sit. Sagittis "
  30. + "quam, ac urna eros est cras id cras, eleifend eu mattis nec."
  31. + "Lorem ipsum dolor sit amet, lacus pharetra sed, sit a "
  32. + "tortor. Id aliquam lorem pede, orci ut enim metus, diam nulla mi "
  33. + "suspendisse tempor tortor. Eleifend lorem proin, morbi vel diam ut. "
  34. + "Tempor est tellus vitae, pretium condimentum facilisis sit. Sagittis "
  35. + "quam, ac urna eros est cras id cras, eleifend eu mattis nec."
  36. + "Lorem ipsum dolor sit amet, lacus pharetra sed, sit a "
  37. + "tortor. Id aliquam lorem pede, orci ut enim metus, diam nulla mi "
  38. + "suspendisse tempor tortor. Eleifend lorem proin, morbi vel diam ut. "
  39. + "Tempor est tellus vitae, pretium condimentum facilisis sit. Sagittis "
  40. + "quam, ac urna eros est cras id cras, eleifend eu mattis nec."
  41. + "Lorem ipsum dolor sit amet, lacus pharetra sed, sit a "
  42. + "tortor. Id aliquam lorem pede, orci ut enim metus, diam nulla mi "
  43. + "suspendisse tempor tortor. Eleifend lorem proin, morbi vel diam ut. "
  44. + "Tempor est tellus vitae, pretium condimentum facilisis sit. Sagittis "
  45. + "quam, ac urna eros est cras id cras, eleifend eu mattis nec.");
  46. VerticalLayout content = new VerticalLayout(editor);
  47. content.setMargin(true);
  48. setContent(content);
  49. }
  50. /**
  51. * Creates a text editor for visually editing text
  52. *
  53. * @param text
  54. * The text editor
  55. * @return
  56. */
  57. private Component createEditor(String text) {
  58. Panel editor = new Panel("Text Editor");
  59. editor.setWidth("580px");
  60. VerticalLayout panelContent = new VerticalLayout();
  61. panelContent.setSpacing(true);
  62. panelContent.setMargin(new MarginInfo(true, false, false, false));
  63. editor.setContent(panelContent);
  64. // Create the toolbar
  65. HorizontalLayout toolbar = new HorizontalLayout();
  66. toolbar.setSpacing(true);
  67. toolbar.setMargin(new MarginInfo(false, false, false, true));
  68. // Create the font family selector
  69. toolbar.addComponent(createFontSelect());
  70. // Create the font size selector
  71. toolbar.addComponent(createFontSizeSelect());
  72. // Create the text color selector
  73. toolbar.addComponent(createTextColorSelect());
  74. // Create the background color selector
  75. toolbar.addComponent(createBackgroundColorSelect());
  76. panelContent.addComponent(toolbar);
  77. panelContent.setComponentAlignment(toolbar, Alignment.MIDDLE_LEFT);
  78. // Spacer between toolbar and text
  79. panelContent.addComponent(new Label("<hr/>", ContentMode.HTML));
  80. // The text to edit
  81. TextArea textLabel = new TextArea(null, text);
  82. textLabel.setWidth("100%");
  83. textLabel.setHeight("200px");
  84. // IMPORTANT: We are here setting the style name of the label, we are
  85. // going to use this in our injected styles to target the label
  86. textLabel.setStyleName("text-label");
  87. panelContent.addComponent(textLabel);
  88. return editor;
  89. }
  90. /**
  91. * Creates a background color select dialog
  92. */
  93. private Component createBackgroundColorSelect() {
  94. ColorPicker bgColor = new ColorPicker("Background", Color.WHITE);
  95. bgColor.setWidth("110px");
  96. bgColor.setCaption("Background");
  97. bgColor.addValueChangeListener(event -> {
  98. // Get the new background color
  99. Color color = event.getValue();
  100. // Get the stylesheet of the page
  101. Styles styles = Page.getCurrent().getStyles();
  102. // inject the new background color
  103. styles.add(".v-app .v-textarea.text-label { background-color:"
  104. + color.getCSS() + "; }");
  105. });
  106. return bgColor;
  107. }
  108. /**
  109. * Create a text color selction dialog
  110. */
  111. private Component createTextColorSelect() {
  112. // Colorpicker for changing text color
  113. ColorPicker textColor = new ColorPicker("Color", Color.BLACK);
  114. textColor.setWidth("110px");
  115. textColor.setCaption("Color");
  116. textColor.addValueChangeListener(event -> {
  117. // Get the new text color
  118. Color color = event.getValue();
  119. // Get the stylesheet of the page
  120. Styles styles = Page.getCurrent().getStyles();
  121. // inject the new color as a style
  122. styles.add(".v-app .v-textarea.text-label { color:" + color.getCSS()
  123. + "; }");
  124. });
  125. return textColor;
  126. }
  127. /**
  128. * Creates a font family selection dialog
  129. */
  130. private Component createFontSelect() {
  131. final ComboBox<String> select = new ComboBox<>(null,
  132. Arrays.asList("Arial", "Helvetica", "Verdana", "Courier",
  133. "Times", "sans-serif"));
  134. select.setValue("Arial");
  135. select.setWidth("200px");
  136. select.setPlaceholder("Font");
  137. select.setDescription("Font");
  138. select.setEmptySelectionAllowed(false);
  139. select.addValueChangeListener(event -> {
  140. // Get the new font family
  141. String fontFamily = select.getValue().toString();
  142. // Get the stylesheet of the page
  143. Styles styles = Page.getCurrent().getStyles();
  144. // inject the new font size as a style. We need .v-app to
  145. // override Vaadin's default styles here
  146. styles.add(".v-app .v-textarea.text-label { font-family:"
  147. + fontFamily + "; }");
  148. });
  149. return select;
  150. }
  151. /**
  152. * Creates a font size selection control
  153. */
  154. private Component createFontSizeSelect() {
  155. final ComboBox<Integer> select = new ComboBox<>(null,
  156. Arrays.asList(8, 9, 10, 12, 14, 16, 20, 25, 30, 40, 50));
  157. select.setWidth("100px");
  158. select.setValue(12);
  159. select.setPlaceholder("Font size");
  160. select.setDescription("Font size");
  161. select.setEmptySelectionAllowed(false);
  162. select.addValueChangeListener(event -> {
  163. // Get the new font size
  164. Integer fontSize = select.getValue();
  165. // Get the stylesheet of the page
  166. Styles styles = Page.getCurrent().getStyles();
  167. // inject the new font size as a style. We need .v-app to
  168. // override Vaadin's default styles here
  169. styles.add(".v-app .v-textarea.text-label { font-size:"
  170. + String.valueOf(fontSize) + "px; }");
  171. });
  172. return select;
  173. }
  174. }