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.

ColorPickerHistory.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.ui.components.colorpicker;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.Collections;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import java.util.concurrent.ArrayBlockingQueue;
  23. import com.vaadin.shared.ui.colorpicker.Color;
  24. import com.vaadin.ui.Component;
  25. import com.vaadin.ui.CustomField;
  26. /**
  27. * A component that represents color selection history within a color picker.
  28. *
  29. * @since 7.0.0
  30. */
  31. public class ColorPickerHistory extends CustomField<Color> {
  32. private static final String STYLENAME = "v-colorpicker-history";
  33. private static final int ROWS = 4;
  34. private static final int COLUMNS = 15;
  35. /** Temporary color history for when the component is detached. */
  36. private final ArrayBlockingQueue<Color> tempHistory = new ArrayBlockingQueue<>(
  37. ROWS * COLUMNS);
  38. @Override
  39. protected Component initContent() {
  40. setPrimaryStyleName(STYLENAME);
  41. ColorPickerGrid grid = new ColorPickerGrid(ROWS, COLUMNS);
  42. grid.setWidth("100%");
  43. grid.setPosition(0, 0);
  44. grid.addValueChangeListener(event -> fireEvent(
  45. new ValueChangeEvent<>(this, event.isUserOriginated())));
  46. return grid;
  47. }
  48. @Override
  49. protected ColorPickerGrid getContent() {
  50. return (ColorPickerGrid) super.getContent();
  51. }
  52. @Override
  53. public void attach() {
  54. super.attach();
  55. createColorHistoryIfNecessary();
  56. }
  57. private void createColorHistoryIfNecessary() {
  58. List<Color> tempColors = new ArrayList<>(tempHistory);
  59. if (getSession().getAttribute("colorPickerHistory") == null) {
  60. getSession().setAttribute("colorPickerHistory",
  61. new ArrayBlockingQueue<Color>(ROWS * COLUMNS));
  62. }
  63. for (Color color : tempColors) {
  64. setValue(color);
  65. }
  66. tempHistory.clear();
  67. }
  68. @SuppressWarnings("unchecked")
  69. private ArrayBlockingQueue<Color> getColorHistory() {
  70. if (isAttached()) {
  71. Object colorHistory = getSession()
  72. .getAttribute("colorPickerHistory");
  73. if (colorHistory instanceof ArrayBlockingQueue<?>) {
  74. return (ArrayBlockingQueue<Color>) colorHistory;
  75. }
  76. }
  77. return tempHistory;
  78. }
  79. @Override
  80. public void setHeight(String height) {
  81. super.setHeight(height);
  82. getContent().setHeight(height);
  83. }
  84. @Override
  85. public Color getValue() {
  86. return getColorHistory().peek();
  87. }
  88. @Override
  89. protected void doSetValue(Color color) {
  90. ArrayBlockingQueue<Color> colorHistory = getColorHistory();
  91. // Check that the color does not already exist
  92. boolean exists = false;
  93. Iterator<Color> iter = colorHistory.iterator();
  94. while (iter.hasNext()) {
  95. if (color.equals(iter.next())) {
  96. exists = true;
  97. break;
  98. }
  99. }
  100. // If the color does not exist then add it
  101. if (!exists) {
  102. if (!colorHistory.offer(color)) {
  103. colorHistory.poll();
  104. colorHistory.offer(color);
  105. }
  106. }
  107. List<Color> colorList = new ArrayList<>(colorHistory);
  108. // Invert order of colors
  109. Collections.reverse(colorList);
  110. // Move the selected color to the front of the list
  111. Collections.swap(colorList, colorList.indexOf(color), 0);
  112. // Create 2d color map
  113. Color[][] colors = new Color[ROWS][COLUMNS];
  114. iter = colorList.iterator();
  115. for (int row = 0; row < ROWS; row++) {
  116. for (int col = 0; col < COLUMNS; col++) {
  117. if (iter.hasNext()) {
  118. colors[row][col] = iter.next();
  119. } else {
  120. colors[row][col] = Color.WHITE;
  121. }
  122. }
  123. }
  124. getContent().setColorGrid(colors);
  125. getContent().markAsDirty();
  126. }
  127. /**
  128. * Gets the history.
  129. *
  130. * @return the history
  131. */
  132. public List<Color> getHistory() {
  133. ArrayBlockingQueue<Color> colorHistory = getColorHistory();
  134. Color[] array = colorHistory.toArray(new Color[colorHistory.size()]);
  135. return Collections.unmodifiableList(Arrays.asList(array));
  136. }
  137. /**
  138. * Checks if the history contains given color.
  139. *
  140. * @param c
  141. * the color
  142. *
  143. * @return true, if successful
  144. */
  145. public boolean hasColor(Color c) {
  146. return getColorHistory().contains(c);
  147. }
  148. }