/uitest/src/com/vaadin/tests/minitutorials/v71beta/CSSInjectWithColorpicker.java

https://gitlab.com/jforge/vaadin · Java · 234 lines · 146 code · 44 blank · 44 comment · 0 complexity · d717dd3e280f90e5591d3611f48cf276 MD5 · raw file

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