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.

ColorPickerGrid.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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.io.Serializable;
  18. import java.lang.reflect.Method;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. import com.vaadin.ui.AbstractComponent;
  22. import com.vaadin.v7.shared.ui.colorpicker.Color;
  23. import com.vaadin.v7.shared.ui.colorpicker.ColorPickerGridServerRpc;
  24. import com.vaadin.v7.shared.ui.colorpicker.ColorPickerGridState;
  25. /**
  26. * A component that represents a color selection grid within a color picker.
  27. *
  28. * @since 7.0.0
  29. */
  30. @Deprecated
  31. public class ColorPickerGrid extends AbstractComponent
  32. implements ColorSelector {
  33. private static class Point implements Serializable {
  34. private int x;
  35. private int y;
  36. public Point(int x, int y) {
  37. this.x = x;
  38. this.y = y;
  39. }
  40. public int getX() {
  41. return x;
  42. }
  43. public int getY() {
  44. return y;
  45. }
  46. }
  47. private static final String STYLENAME = "v-colorpicker-grid";
  48. private static final Method COLOR_CHANGE_METHOD;
  49. static {
  50. try {
  51. COLOR_CHANGE_METHOD = ColorChangeListener.class.getDeclaredMethod(
  52. "colorChanged", new Class[] { ColorChangeEvent.class });
  53. } catch (final NoSuchMethodException e) {
  54. // This should never happen
  55. throw new RuntimeException(
  56. "Internal error finding methods in ColorPicker");
  57. }
  58. }
  59. private ColorPickerGridServerRpc rpc = new ColorPickerGridServerRpc() {
  60. @Override
  61. public void select(int x, int y) {
  62. ColorPickerGrid.this.x = x;
  63. ColorPickerGrid.this.y = y;
  64. fireColorChanged(colorGrid[y][x]);
  65. }
  66. @Override
  67. public void refresh() {
  68. for (int row = 0; row < rows; row++) {
  69. for (int col = 0; col < columns; col++) {
  70. changedColors.put(new Point(row, col), colorGrid[row][col]);
  71. }
  72. }
  73. sendChangedColors();
  74. markAsDirty();
  75. }
  76. };
  77. /** The x-coordinate. */
  78. private int x = 0;
  79. /** The y-coordinate. */
  80. private int y = 0;
  81. /** The rows. */
  82. private int rows;
  83. /** The columns. */
  84. private int columns;
  85. /** The color grid. */
  86. private Color[][] colorGrid = new Color[1][1];
  87. /** The changed colors. */
  88. private final Map<Point, Color> changedColors = new HashMap<Point, Color>();
  89. /**
  90. * Instantiates a new color picker grid.
  91. */
  92. public ColorPickerGrid() {
  93. registerRpc(rpc);
  94. setPrimaryStyleName(STYLENAME);
  95. setColorGrid(new Color[1][1]);
  96. setColor(Color.WHITE);
  97. }
  98. /**
  99. * Instantiates a new color picker grid.
  100. *
  101. * @param rows
  102. * the rows
  103. * @param columns
  104. * the columns
  105. */
  106. public ColorPickerGrid(int rows, int columns) {
  107. registerRpc(rpc);
  108. setPrimaryStyleName(STYLENAME);
  109. setColorGrid(new Color[rows][columns]);
  110. setColor(Color.WHITE);
  111. }
  112. /**
  113. * Instantiates a new color picker grid.
  114. *
  115. * @param colors
  116. * the colors
  117. */
  118. public ColorPickerGrid(Color[][] colors) {
  119. registerRpc(rpc);
  120. setPrimaryStyleName(STYLENAME);
  121. setColorGrid(colors);
  122. }
  123. private void setColumnCount(int columns) {
  124. this.columns = columns;
  125. getState().columnCount = columns;
  126. }
  127. private void setRowCount(int rows) {
  128. this.rows = rows;
  129. getState().rowCount = rows;
  130. }
  131. private void sendChangedColors() {
  132. if (!changedColors.isEmpty()) {
  133. String[] colors = new String[changedColors.size()];
  134. String[] xCoords = new String[changedColors.size()];
  135. String[] yCoords = new String[changedColors.size()];
  136. int counter = 0;
  137. for (Point p : changedColors.keySet()) {
  138. Color c = changedColors.get(p);
  139. if (c == null) {
  140. continue;
  141. }
  142. String color = c.getCSS();
  143. colors[counter] = color;
  144. xCoords[counter] = String.valueOf(p.getX());
  145. yCoords[counter] = String.valueOf(p.getY());
  146. counter++;
  147. }
  148. getState().changedColor = colors;
  149. getState().changedX = xCoords;
  150. getState().changedY = yCoords;
  151. changedColors.clear();
  152. }
  153. }
  154. /**
  155. * Sets the color grid.
  156. *
  157. * @param colors
  158. * the new color grid
  159. */
  160. public void setColorGrid(Color[][] colors) {
  161. setRowCount(colors.length);
  162. setColumnCount(colors[0].length);
  163. colorGrid = colors;
  164. for (int row = 0; row < rows; row++) {
  165. for (int col = 0; col < columns; col++) {
  166. changedColors.put(new Point(row, col), colorGrid[row][col]);
  167. }
  168. }
  169. sendChangedColors();
  170. markAsDirty();
  171. }
  172. /**
  173. * Adds a color change listener.
  174. *
  175. * @param listener
  176. * The color change listener
  177. */
  178. @Override
  179. public void addColorChangeListener(ColorChangeListener listener) {
  180. addListener(ColorChangeEvent.class, listener, COLOR_CHANGE_METHOD);
  181. }
  182. @Override
  183. public Color getColor() {
  184. return colorGrid[x][y];
  185. }
  186. /**
  187. * Removes a color change listener.
  188. *
  189. * @param listener
  190. * The listener
  191. */
  192. @Override
  193. public void removeColorChangeListener(ColorChangeListener listener) {
  194. removeListener(ColorChangeEvent.class, listener);
  195. }
  196. @Override
  197. public void setColor(Color color) {
  198. colorGrid[x][y] = color;
  199. changedColors.put(new Point(x, y), color);
  200. sendChangedColors();
  201. markAsDirty();
  202. }
  203. /**
  204. * Sets the position.
  205. *
  206. * @param x
  207. * the x
  208. * @param y
  209. * the y
  210. */
  211. public void setPosition(int x, int y) {
  212. if (x >= 0 && x < columns && y >= 0 && y < rows) {
  213. this.x = x;
  214. this.y = y;
  215. }
  216. }
  217. /**
  218. * Gets the position.
  219. *
  220. * @return the position
  221. */
  222. public int[] getPosition() {
  223. return new int[] { x, y };
  224. }
  225. /**
  226. * Notifies the listeners that a color change has occurred.
  227. *
  228. * @param color
  229. * The color which it changed to
  230. */
  231. public void fireColorChanged(Color color) {
  232. fireEvent(new ColorChangeEvent(this, color));
  233. }
  234. @Override
  235. protected ColorPickerGridState getState() {
  236. return (ColorPickerGridState) super.getState();
  237. }
  238. }