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.

ColorPickerTestUI.java 18KB

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