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

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