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.

ValoColorPickerTestUI.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. package com.vaadin.tests.components.colorpicker;
  2. import java.awt.Graphics;
  3. import java.awt.image.BufferedImage;
  4. import java.io.ByteArrayInputStream;
  5. import java.io.ByteArrayOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.text.SimpleDateFormat;
  9. import java.util.Date;
  10. import javax.imageio.ImageIO;
  11. import com.vaadin.annotations.Widgetset;
  12. import com.vaadin.data.HasValue.ValueChangeEvent;
  13. import com.vaadin.server.StreamResource;
  14. import com.vaadin.server.VaadinRequest;
  15. import com.vaadin.shared.ui.ContentMode;
  16. import com.vaadin.shared.ui.colorpicker.Color;
  17. import com.vaadin.tests.components.AbstractTestUI;
  18. import com.vaadin.ui.AbstractColorPicker;
  19. import com.vaadin.ui.Alignment;
  20. import com.vaadin.ui.CheckBox;
  21. import com.vaadin.ui.ColorPicker;
  22. import com.vaadin.ui.ColorPickerArea;
  23. import com.vaadin.ui.Embedded;
  24. import com.vaadin.ui.GridLayout;
  25. import com.vaadin.ui.HorizontalLayout;
  26. import com.vaadin.ui.Label;
  27. import com.vaadin.ui.Panel;
  28. import com.vaadin.ui.VerticalLayout;
  29. @Widgetset("com.vaadin.DefaultWidgetSet")
  30. public class ValoColorPickerTestUI extends AbstractTestUI {
  31. @Override
  32. public String getTestDescription() {
  33. return "Vaadin 8 Valo theme ColorPicker";
  34. }
  35. @Override
  36. protected Integer getTicketNumber() {
  37. return 10802;
  38. }
  39. /** The foreground color. */
  40. private Color foregroundColor = Color.BLACK; // The currently selected
  41. /** The background color. */
  42. private Color backgroundColor = Color.WHITE; // The currently selected
  43. // The display box where the image is rendered
  44. /** The display. */
  45. private Embedded display;
  46. private AbstractColorPicker colorpicker1;
  47. private AbstractColorPicker colorpicker2;
  48. private AbstractColorPicker colorpicker3;
  49. private AbstractColorPicker colorpicker4;
  50. private AbstractColorPicker colorpicker5;
  51. private AbstractColorPicker colorpicker6;
  52. private boolean rgbVisible = true;
  53. private boolean hsvVisible = true;
  54. private boolean swaVisible = true;
  55. private boolean historyVisible = true;
  56. private boolean txtfieldVisible = true;
  57. private final CheckBox rgbBox = new CheckBox("RGB tab visible");
  58. private final CheckBox hsvBox = new CheckBox("HSV tab visible");
  59. private final CheckBox swaBox = new CheckBox("Swatches tab visible");
  60. private final CheckBox hisBox = new CheckBox("History visible");
  61. private final CheckBox txtBox = new CheckBox("CSS field visible");
  62. /**
  63. * This class is used to represent the preview of the color selection.
  64. */
  65. public class MyImageSource implements StreamResource.StreamSource {
  66. /** The imagebuffer. */
  67. private ByteArrayOutputStream imagebuffer;
  68. /** The bg color. */
  69. private final java.awt.Color bgColor;
  70. /** The fg color. */
  71. private final java.awt.Color fgColor;
  72. /**
  73. * Instantiates a new my image source.
  74. *
  75. * @param fg
  76. * the foreground
  77. * @param bg
  78. * the background
  79. */
  80. public MyImageSource(java.awt.Color fg, java.awt.Color bg) {
  81. fgColor = fg;
  82. bgColor = bg;
  83. }
  84. /*
  85. * Must implement this method that returns the resource as a stream.
  86. */
  87. @Override
  88. public InputStream getStream() {
  89. /* Create an image and draw something on it. */
  90. BufferedImage image = new BufferedImage(270, 270,
  91. BufferedImage.TYPE_INT_RGB);
  92. Graphics drawable = image.getGraphics();
  93. drawable.setColor(bgColor);
  94. drawable.fillRect(0, 0, 270, 270);
  95. drawable.setColor(fgColor);
  96. drawable.fillOval(25, 25, 220, 220);
  97. drawable.setColor(java.awt.Color.blue);
  98. drawable.drawRect(0, 0, 269, 269);
  99. drawable.setColor(java.awt.Color.black);
  100. drawable.drawString("r=" + String.valueOf(fgColor.getRed()) + ",g="
  101. + String.valueOf(fgColor.getGreen()) + ",b="
  102. + String.valueOf(fgColor.getBlue()), 50, 100);
  103. drawable.drawString("r=" + String.valueOf(bgColor.getRed()) + ",g="
  104. + String.valueOf(bgColor.getGreen()) + ",b="
  105. + String.valueOf(bgColor.getBlue()), 5, 15);
  106. try {
  107. /* Write the image to a buffer. */
  108. imagebuffer = new ByteArrayOutputStream();
  109. ImageIO.write(image, "png", imagebuffer);
  110. /* Return a stream from the buffer. */
  111. return new ByteArrayInputStream(imagebuffer.toByteArray());
  112. } catch (IOException e) {
  113. return null;
  114. }
  115. }
  116. }
  117. private void setPopupVisibilities() {
  118. rgbBox.setEnabled(!(rgbVisible && !hsvVisible && !swaVisible));
  119. hsvBox.setEnabled(!(!rgbVisible && hsvVisible && !swaVisible));
  120. swaBox.setEnabled(!(!rgbVisible && !hsvVisible && swaVisible));
  121. colorpicker1.setRGBVisibility(rgbVisible);
  122. colorpicker2.setRGBVisibility(rgbVisible);
  123. colorpicker3.setRGBVisibility(rgbVisible);
  124. colorpicker4.setRGBVisibility(rgbVisible);
  125. colorpicker5.setRGBVisibility(rgbVisible);
  126. colorpicker6.setRGBVisibility(rgbVisible);
  127. colorpicker1.setHSVVisibility(hsvVisible);
  128. colorpicker2.setHSVVisibility(hsvVisible);
  129. colorpicker3.setHSVVisibility(hsvVisible);
  130. colorpicker4.setHSVVisibility(hsvVisible);
  131. colorpicker5.setHSVVisibility(hsvVisible);
  132. colorpicker6.setHSVVisibility(hsvVisible);
  133. colorpicker1.setSwatchesVisibility(swaVisible);
  134. colorpicker2.setSwatchesVisibility(swaVisible);
  135. colorpicker3.setSwatchesVisibility(swaVisible);
  136. colorpicker4.setSwatchesVisibility(swaVisible);
  137. colorpicker5.setSwatchesVisibility(swaVisible);
  138. colorpicker6.setSwatchesVisibility(swaVisible);
  139. colorpicker1.setHistoryVisibility(historyVisible);
  140. colorpicker2.setHistoryVisibility(historyVisible);
  141. colorpicker3.setHistoryVisibility(historyVisible);
  142. colorpicker4.setHistoryVisibility(historyVisible);
  143. colorpicker5.setHistoryVisibility(historyVisible);
  144. colorpicker6.setHistoryVisibility(historyVisible);
  145. colorpicker1.setTextfieldVisibility(txtfieldVisible);
  146. colorpicker2.setTextfieldVisibility(txtfieldVisible);
  147. colorpicker3.setTextfieldVisibility(txtfieldVisible);
  148. colorpicker4.setTextfieldVisibility(txtfieldVisible);
  149. colorpicker5.setTextfieldVisibility(txtfieldVisible);
  150. colorpicker6.setTextfieldVisibility(txtfieldVisible);
  151. }
  152. @Override
  153. protected void setup(VaadinRequest request) {
  154. getLayout().setWidth("1100px");
  155. getLayout().setHeight(null);
  156. getLayout().addStyleName("colorpicker-mainwindow-content");
  157. // Create an instance of the preview and add it to the window
  158. display = new Embedded("Color preview");
  159. display.setWidth("270px");
  160. display.setHeight("270px");
  161. // Add the foreground and background colorpickers to a layout
  162. HorizontalLayout mainLayout = new HorizontalLayout();
  163. mainLayout.addStyleName("colorpicker-mainlayout");
  164. mainLayout.setWidth("100%");
  165. mainLayout.setHeight(null);
  166. mainLayout.setMargin(true);
  167. mainLayout.setSpacing(true);
  168. getLayout().addComponent(mainLayout);
  169. VerticalLayout layoutLeft = new VerticalLayout();
  170. layoutLeft.setWidth("550px");
  171. layoutLeft.setHeight(null);
  172. layoutLeft.setSpacing(true);
  173. GridLayout optLayout = new GridLayout(2, 3);
  174. optLayout.setWidth("100%");
  175. optLayout.setHeight(null);
  176. optLayout.setMargin(true);
  177. optLayout.setSpacing(true);
  178. rgbBox.setValue(rgbVisible);
  179. rgbBox.addValueChangeListener(event -> {
  180. rgbVisible = event.getValue();
  181. setPopupVisibilities();
  182. });
  183. rgbBox.setId("rgbBox");
  184. optLayout.addComponent(rgbBox);
  185. hsvBox.setValue(hsvVisible);
  186. hsvBox.addValueChangeListener(event -> {
  187. hsvVisible = event.getValue();
  188. setPopupVisibilities();
  189. });
  190. hsvBox.setId("hsvBox");
  191. optLayout.addComponent(hsvBox);
  192. swaBox.setValue(swaVisible);
  193. swaBox.addValueChangeListener(event -> {
  194. swaVisible = event.getValue();
  195. setPopupVisibilities();
  196. });
  197. swaBox.setId("swaBox");
  198. optLayout.addComponent(swaBox);
  199. hisBox.setValue(historyVisible);
  200. hisBox.addValueChangeListener(event -> {
  201. historyVisible = event.getValue();
  202. setPopupVisibilities();
  203. });
  204. hisBox.setId("hisBox");
  205. optLayout.addComponent(hisBox);
  206. txtBox.setValue(txtfieldVisible);
  207. txtBox.addValueChangeListener(event -> {
  208. txtfieldVisible = event.getValue();
  209. setPopupVisibilities();
  210. });
  211. txtBox.setId("txtBox");
  212. optLayout.addComponent(txtBox);
  213. Panel optPanel = new Panel("Customize the color picker popup window",
  214. optLayout);
  215. layoutLeft.addComponent(optPanel);
  216. HorizontalLayout layout1 = createHorizontalLayout();
  217. colorpicker1 = new ColorPicker("Foreground", foregroundColor);
  218. colorpicker1.setCaptionAsHtml(true);
  219. colorpicker1.addValueChangeListener(this::colorChanged);
  220. colorpicker1.setId("colorpicker1");
  221. layout1.addComponent(colorpicker1);
  222. layout1.setComponentAlignment(colorpicker1, Alignment.MIDDLE_CENTER);
  223. colorpicker2 = new ColorPicker("Background", backgroundColor);
  224. colorpicker2.addValueChangeListener(this::colorChanged);
  225. colorpicker2.setId("colorpicker2");
  226. layout1.addComponent(colorpicker2);
  227. layout1.setComponentAlignment(colorpicker2, Alignment.MIDDLE_CENTER);
  228. Panel panel1 = new Panel(
  229. "Button-like colorpicker with current color and CSS code",
  230. layout1);
  231. layoutLeft.addComponent(panel1);
  232. HorizontalLayout layout2 = createHorizontalLayout();
  233. colorpicker3 = new ColorPicker("Foreground", foregroundColor);
  234. colorpicker3.addValueChangeListener(this::colorChanged);
  235. colorpicker3.setWidth("130px");
  236. colorpicker3.setCaption("Foreground");
  237. colorpicker3.setId("colorpicker3");
  238. layout2.addComponent(colorpicker3);
  239. layout2.setComponentAlignment(colorpicker3, Alignment.MIDDLE_CENTER);
  240. colorpicker4 = new ColorPicker("Background", backgroundColor);
  241. colorpicker4.addValueChangeListener(this::colorChanged);
  242. colorpicker4.setWidth("130px");
  243. colorpicker4.setCaption("Background");
  244. colorpicker4.setId("colorpicker4");
  245. layout2.addComponent(colorpicker4);
  246. layout2.setComponentAlignment(colorpicker4, Alignment.MIDDLE_CENTER);
  247. Panel panel2 = new Panel(
  248. "Button-like colorpicker with current color and custom caption",
  249. layout2);
  250. layoutLeft.addComponent(panel2);
  251. HorizontalLayout layout3 = createHorizontalLayout();
  252. colorpicker5 = new ColorPickerArea("Foreground", foregroundColor);
  253. colorpicker5.setCaption("Foreground");
  254. colorpicker5.addValueChangeListener(this::colorChanged);
  255. colorpicker5.setId("colorpicker5");
  256. layout3.addComponent(colorpicker5);
  257. layout3.setComponentAlignment(colorpicker5, Alignment.MIDDLE_CENTER);
  258. colorpicker6 = new ColorPickerArea("Background", backgroundColor);
  259. colorpicker6.setCaption("Background");
  260. colorpicker6.setDefaultCaptionEnabled(false);
  261. colorpicker6.addValueChangeListener(this::colorChanged);
  262. colorpicker6.setId("colorpicker6");
  263. layout3.addComponent(colorpicker6);
  264. layout3.setComponentAlignment(colorpicker6, Alignment.MIDDLE_CENTER);
  265. Panel panel3 = new Panel("Color area colorpicker with caption",
  266. layout3);
  267. panel3.setWidth("100%");
  268. panel3.setHeight(null);
  269. layoutLeft.addComponent(panel3);
  270. Label divider1 = new Label("<hr>", ContentMode.HTML);
  271. layoutLeft.addComponent(divider1);
  272. Label divider2 = new Label("<hr>", ContentMode.HTML);
  273. layoutLeft.addComponent(divider2);
  274. HorizontalLayout layout4 = createHorizontalLayout();
  275. addShadeButton(new Color(Integer.parseInt("000000", 16)), layout4);
  276. addShadeButton(new Color(Integer.parseInt("333333", 16)), layout4);
  277. addShadeButton(new Color(Integer.parseInt("666666", 16)), layout4);
  278. addShadeButton(new Color(Integer.parseInt("999999", 16)), layout4);
  279. addShadeButton(new Color(Integer.parseInt("cccccc", 16)), layout4);
  280. addShadeButton(new Color(Integer.parseInt("ffffff", 16)), layout4);
  281. Panel panel4 = new Panel(
  282. "Button-like colorpickers with disabled caption (no effect on fg/bg colors)",
  283. layout4);
  284. layoutLeft.addComponent(panel4);
  285. HorizontalLayout layout5 = createHorizontalLayout();
  286. addShadeArea(new Color(Integer.parseInt("000000", 16)), layout5);
  287. addShadeArea(new Color(Integer.parseInt("111111", 16)), layout5);
  288. addShadeArea(new Color(Integer.parseInt("222222", 16)), layout5);
  289. addShadeArea(new Color(Integer.parseInt("333333", 16)), layout5);
  290. addShadeArea(new Color(Integer.parseInt("444444", 16)), layout5);
  291. addShadeArea(new Color(Integer.parseInt("555555", 16)), layout5);
  292. addShadeArea(new Color(Integer.parseInt("666666", 16)), layout5);
  293. addShadeArea(new Color(Integer.parseInt("777777", 16)), layout5);
  294. addShadeArea(new Color(Integer.parseInt("888888", 16)), layout5);
  295. addShadeArea(new Color(Integer.parseInt("999999", 16)), layout5);
  296. addShadeArea(new Color(Integer.parseInt("aaaaaa", 16)), layout5);
  297. addShadeArea(new Color(Integer.parseInt("bbbbbb", 16)), layout5);
  298. addShadeArea(new Color(Integer.parseInt("cccccc", 16)), layout5);
  299. addShadeArea(new Color(Integer.parseInt("dddddd", 16)), layout5);
  300. addShadeArea(new Color(Integer.parseInt("eeeeee", 16)), layout5);
  301. addShadeArea(new Color(Integer.parseInt("ffffff", 16)), layout5);
  302. Panel panel5 = new Panel(
  303. "Area colorpickers with no given caption (no effect on fg/bg colors)",
  304. layout5);
  305. layoutLeft.addComponent(panel5);
  306. mainLayout.addComponent(layoutLeft);
  307. mainLayout.addComponent(display);
  308. updateDisplay(foregroundColor, backgroundColor);
  309. }
  310. private HorizontalLayout createHorizontalLayout() {
  311. HorizontalLayout layout = new HorizontalLayout();
  312. layout.setWidth("100%");
  313. layout.setHeight(null);
  314. layout.setMargin(true);
  315. return layout;
  316. }
  317. private int shadeButtonCounter = 1;
  318. private void addShadeButton(Color color, HorizontalLayout layout) {
  319. AbstractColorPicker colorPicker = new ColorPicker(color.toString(),
  320. color);
  321. colorPicker.setDefaultCaptionEnabled(false);
  322. colorPicker.setWidth("41px");
  323. colorPicker.setId("shadebutton_" + shadeButtonCounter);
  324. layout.addComponent(colorPicker);
  325. layout.setComponentAlignment(colorPicker, Alignment.MIDDLE_CENTER);
  326. ++shadeButtonCounter;
  327. }
  328. private int shadeAreaCounter = 1;
  329. private void addShadeArea(Color color, HorizontalLayout layout) {
  330. AbstractColorPicker colorPicker = new ColorPickerArea(color.toString(),
  331. color);
  332. colorPicker.setWidth("20px");
  333. colorPicker.setHeight("20px");
  334. colorPicker.setId("shadearea_" + shadeAreaCounter);
  335. layout.addComponent(colorPicker);
  336. layout.setComponentAlignment(colorPicker, Alignment.MIDDLE_CENTER);
  337. ++shadeAreaCounter;
  338. }
  339. // This is called whenever a colorpicker popup is closed
  340. /**
  341. * Update display.
  342. *
  343. * @param fg
  344. * the fg
  345. * @param bg
  346. * the bg
  347. */
  348. public void updateDisplay(Color fg, Color bg) {
  349. java.awt.Color awtFg = new java.awt.Color(fg.getRed(), fg.getGreen(),
  350. fg.getBlue());
  351. java.awt.Color awtBg = new java.awt.Color(bg.getRed(), bg.getGreen(),
  352. bg.getBlue());
  353. StreamResource.StreamSource imagesource = new MyImageSource(awtFg,
  354. awtBg);
  355. Date now = new Date();
  356. SimpleDateFormat format = new SimpleDateFormat("hhmmss");
  357. StreamResource imageresource = new StreamResource(imagesource,
  358. "myimage" + format.format(now) + ".png");
  359. imageresource.setCacheTime(0);
  360. display.setSource(imageresource);
  361. }
  362. private void colorChanged(ValueChangeEvent<Color> event) {
  363. if (event.getSource() == colorpicker1
  364. || event.getSource() == colorpicker3
  365. || event.getSource() == colorpicker5) {
  366. foregroundColor = event.getValue();
  367. if (event.getSource() != colorpicker1) {
  368. colorpicker1.setValue(event.getValue());
  369. }
  370. if (event.getSource() != colorpicker3) {
  371. colorpicker3.setValue(event.getValue());
  372. }
  373. if (event.getSource() != colorpicker5) {
  374. colorpicker5.setValue(event.getValue());
  375. }
  376. } else if (event.getSource() == colorpicker2
  377. || event.getSource() == colorpicker4
  378. || event.getSource() == colorpicker6) {
  379. backgroundColor = event.getValue();
  380. if (event.getSource() != colorpicker2) {
  381. colorpicker2.setValue(event.getValue());
  382. }
  383. if (event.getSource() != colorpicker4) {
  384. colorpicker4.setValue(event.getValue());
  385. }
  386. if (event.getSource() != colorpicker6) {
  387. colorpicker6.setValue(event.getValue());
  388. }
  389. } else {
  390. return;
  391. }
  392. updateDisplay(foregroundColor, backgroundColor);
  393. }
  394. }