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 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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.v7.ui.components.colorpicker;
  17. import java.lang.reflect.Method;
  18. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.Collections;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. import java.util.concurrent.ArrayBlockingQueue;
  24. import com.vaadin.ui.CustomComponent;
  25. import com.vaadin.v7.shared.ui.colorpicker.Color;
  26. /**
  27. * A component that represents color selection history within a color picker.
  28. *
  29. * @since 7.0.0
  30. */
  31. @Deprecated
  32. public class ColorPickerHistory extends CustomComponent
  33. implements ColorSelector, ColorChangeListener {
  34. private static final String STYLENAME = "v-colorpicker-history";
  35. private static final Method COLOR_CHANGE_METHOD;
  36. static {
  37. try {
  38. COLOR_CHANGE_METHOD = ColorChangeListener.class.getDeclaredMethod(
  39. "colorChanged", new Class[] { ColorChangeEvent.class });
  40. } catch (final NoSuchMethodException e) {
  41. // This should never happen
  42. throw new RuntimeException(
  43. "Internal error finding methods in ColorPicker");
  44. }
  45. }
  46. /** The rows. */
  47. private static final int rows = 4;
  48. /** The columns. */
  49. private static final int columns = 15;
  50. /** Temporary color history for when the component is detached. */
  51. private ArrayBlockingQueue<Color> tempHistory = new ArrayBlockingQueue<Color>(
  52. rows * columns);
  53. /** The grid. */
  54. private final ColorPickerGrid grid;
  55. /**
  56. * Instantiates a new color picker history.
  57. */
  58. public ColorPickerHistory() {
  59. setPrimaryStyleName(STYLENAME);
  60. grid = new ColorPickerGrid(rows, columns);
  61. grid.setWidth("100%");
  62. grid.setPosition(0, 0);
  63. grid.addColorChangeListener(this);
  64. setCompositionRoot(grid);
  65. }
  66. @Override
  67. public void attach() {
  68. super.attach();
  69. createColorHistoryIfNecessary();
  70. }
  71. private void createColorHistoryIfNecessary() {
  72. List<Color> tempColors = new ArrayList<Color>(tempHistory);
  73. if (getSession().getAttribute("colorPickerHistory") == null) {
  74. getSession().setAttribute("colorPickerHistory",
  75. new ArrayBlockingQueue<Color>(rows * columns));
  76. }
  77. for (Color color : tempColors) {
  78. setColor(color);
  79. }
  80. tempHistory.clear();
  81. }
  82. @SuppressWarnings("unchecked")
  83. private ArrayBlockingQueue<Color> getColorHistory() {
  84. if (isAttached()) {
  85. Object colorHistory = getSession()
  86. .getAttribute("colorPickerHistory");
  87. if (colorHistory instanceof ArrayBlockingQueue<?>) {
  88. return (ArrayBlockingQueue<Color>) colorHistory;
  89. }
  90. }
  91. return tempHistory;
  92. }
  93. @Override
  94. public void setHeight(String height) {
  95. super.setHeight(height);
  96. grid.setHeight(height);
  97. }
  98. @Override
  99. public void setColor(Color color) {
  100. ArrayBlockingQueue<Color> colorHistory = getColorHistory();
  101. // Check that the color does not already exist
  102. boolean exists = false;
  103. Iterator<Color> iter = colorHistory.iterator();
  104. while (iter.hasNext()) {
  105. if (color.equals(iter.next())) {
  106. exists = true;
  107. break;
  108. }
  109. }
  110. // If the color does not exist then add it
  111. if (!exists) {
  112. if (!colorHistory.offer(color)) {
  113. colorHistory.poll();
  114. colorHistory.offer(color);
  115. }
  116. }
  117. List<Color> colorList = new ArrayList<Color>(colorHistory);
  118. // Invert order of colors
  119. Collections.reverse(colorList);
  120. // Move the selected color to the front of the list
  121. Collections.swap(colorList, colorList.indexOf(color), 0);
  122. // Create 2d color map
  123. Color[][] colors = new Color[rows][columns];
  124. iter = colorList.iterator();
  125. for (int row = 0; row < rows; row++) {
  126. for (int col = 0; col < columns; col++) {
  127. if (iter.hasNext()) {
  128. colors[row][col] = iter.next();
  129. } else {
  130. colors[row][col] = Color.WHITE;
  131. }
  132. }
  133. }
  134. grid.setColorGrid(colors);
  135. grid.markAsDirty();
  136. }
  137. @Override
  138. public Color getColor() {
  139. return getColorHistory().peek();
  140. }
  141. /**
  142. * Gets the history.
  143. *
  144. * @return the history
  145. */
  146. public List<Color> getHistory() {
  147. ArrayBlockingQueue<Color> colorHistory = getColorHistory();
  148. Color[] array = colorHistory.toArray(new Color[colorHistory.size()]);
  149. return Collections.unmodifiableList(Arrays.asList(array));
  150. }
  151. /**
  152. * Checks if the history contains given color.
  153. *
  154. * @param c
  155. * the color
  156. *
  157. * @return true, if successful
  158. */
  159. public boolean hasColor(Color c) {
  160. return getColorHistory().contains(c);
  161. }
  162. /**
  163. * Adds a color change listener
  164. *
  165. * @param listener
  166. * The listener
  167. */
  168. @Override
  169. public void addColorChangeListener(ColorChangeListener listener) {
  170. addListener(ColorChangeEvent.class, listener, COLOR_CHANGE_METHOD);
  171. }
  172. /**
  173. * Removes a color change listener
  174. *
  175. * @param listener
  176. * The listener
  177. */
  178. @Override
  179. public void removeColorChangeListener(ColorChangeListener listener) {
  180. removeListener(ColorChangeEvent.class, listener);
  181. }
  182. @Override
  183. public void colorChanged(ColorChangeEvent event) {
  184. fireEvent(new ColorChangeEvent(this, event.getColor()));
  185. }
  186. }