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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 = colorHistory.contains(color);
  103. // If the color does not exist then add it
  104. if (!exists) {
  105. if (!colorHistory.offer(color)) {
  106. colorHistory.poll();
  107. colorHistory.offer(color);
  108. }
  109. }
  110. List<Color> colorList = new ArrayList<Color>(colorHistory);
  111. // Invert order of colors
  112. Collections.reverse(colorList);
  113. // Move the selected color to the front of the list
  114. Collections.swap(colorList, colorList.indexOf(color), 0);
  115. // Create 2d color map
  116. Color[][] colors = new Color[ROWS][COLUMNS];
  117. Iterator<Color> iter = colorList.iterator();
  118. for (int row = 0; row < ROWS; row++) {
  119. for (int col = 0; col < COLUMNS; col++) {
  120. if (iter.hasNext()) {
  121. colors[row][col] = iter.next();
  122. } else {
  123. colors[row][col] = Color.WHITE;
  124. }
  125. }
  126. }
  127. grid.setColorGrid(colors);
  128. grid.markAsDirty();
  129. }
  130. @Override
  131. public Color getColor() {
  132. return getColorHistory().peek();
  133. }
  134. /**
  135. * Gets the history.
  136. *
  137. * @return the history
  138. */
  139. public List<Color> getHistory() {
  140. ArrayBlockingQueue<Color> colorHistory = getColorHistory();
  141. Color[] array = colorHistory.toArray(new Color[colorHistory.size()]);
  142. return Collections.unmodifiableList(Arrays.asList(array));
  143. }
  144. /**
  145. * Checks if the history contains given color.
  146. *
  147. * @param c
  148. * the color
  149. *
  150. * @return true, if successful
  151. */
  152. public boolean hasColor(Color c) {
  153. return getColorHistory().contains(c);
  154. }
  155. /**
  156. * Adds a color change listener.
  157. *
  158. * @param listener
  159. * The listener
  160. */
  161. @Override
  162. public void addColorChangeListener(ColorChangeListener listener) {
  163. addListener(ColorChangeEvent.class, listener, COLOR_CHANGE_METHOD);
  164. }
  165. /**
  166. * Removes a color change listener.
  167. *
  168. * @param listener
  169. * The listener
  170. */
  171. @Override
  172. public void removeColorChangeListener(ColorChangeListener listener) {
  173. removeListener(ColorChangeEvent.class, listener);
  174. }
  175. @Override
  176. public void colorChanged(ColorChangeEvent event) {
  177. fireEvent(new ColorChangeEvent(this, event.getColor()));
  178. }
  179. }