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.

VColorPickerGrid.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.client.ui.colorpicker;
  17. import com.google.gwt.dom.client.Element;
  18. import com.google.gwt.event.dom.client.ClickEvent;
  19. import com.google.gwt.event.dom.client.ClickHandler;
  20. import com.google.gwt.event.dom.client.HasClickHandlers;
  21. import com.google.gwt.event.shared.HandlerRegistration;
  22. import com.google.gwt.user.client.ui.AbsolutePanel;
  23. import com.google.gwt.user.client.ui.Grid;
  24. import com.google.gwt.user.client.ui.HTMLTable.Cell;
  25. /**
  26. * Client side implementation for ColorPickerGrid.
  27. *
  28. * @since 7.0.0
  29. *
  30. */
  31. public class VColorPickerGrid extends AbsolutePanel
  32. implements ClickHandler, HasClickHandlers {
  33. private int rows = 1;
  34. private int columns = 1;
  35. private Grid grid;
  36. private boolean gridLoaded = false;
  37. private int selectedX;
  38. private int selectedY;
  39. /**
  40. * Instantiates the client side component for a color picker grid.
  41. */
  42. public VColorPickerGrid() {
  43. super();
  44. this.add(createGrid(), 0, 0);
  45. }
  46. /**
  47. * Creates a grid according to the current row and column count information.
  48. *
  49. * @return grid
  50. */
  51. private Grid createGrid() {
  52. grid = new Grid(rows, columns);
  53. grid.setWidth("100%");
  54. grid.setHeight("100%");
  55. grid.addClickHandler(this);
  56. return grid;
  57. }
  58. /**
  59. * Updates the row and column count and creates a new grid based on them.
  60. * The new grid replaces the old grid if one existed.
  61. * <p>
  62. * For internal use only. May be renamed or removed in a future release.
  63. *
  64. * @param rowCount
  65. * @param columnCount
  66. */
  67. public void updateGrid(int rowCount, int columnCount) {
  68. rows = rowCount;
  69. columns = columnCount;
  70. this.remove(grid);
  71. this.add(createGrid(), 0, 0);
  72. }
  73. /**
  74. * Updates the changed colors within the grid based on the given x- and
  75. * y-coordinates. Nothing happens if any of the parameters is null or the
  76. * parameter lengths don't match.
  77. * <p>
  78. * For internal use only. May be renamed or removed in a future release.
  79. *
  80. * @param changedColor
  81. * @param changedX
  82. * @param changedY
  83. */
  84. public void updateColor(String[] changedColor, String[] changedX,
  85. String[] changedY) {
  86. if (changedColor != null && changedX != null && changedY != null) {
  87. if (changedColor.length == changedX.length
  88. && changedX.length == changedY.length) {
  89. for (int c = 0; c < changedColor.length; c++) {
  90. Element element = grid.getCellFormatter().getElement(
  91. Integer.parseInt(changedX[c]),
  92. Integer.parseInt(changedY[c]));
  93. element.getStyle().setProperty("background",
  94. changedColor[c]);
  95. }
  96. }
  97. gridLoaded = true;
  98. }
  99. }
  100. /**
  101. * Returns currently selected x-coordinate of the grid.
  102. */
  103. public int getSelectedX() {
  104. return selectedX;
  105. }
  106. /**
  107. * Returns currently selected y-coordinate of the grid.
  108. */
  109. public int getSelectedY() {
  110. return selectedY;
  111. }
  112. /**
  113. * Returns true if the colors have been successfully updated at least once,
  114. * false otherwise.
  115. * <p>
  116. * For internal use only. May be renamed or removed in a future release.
  117. */
  118. public boolean isGridLoaded() {
  119. return gridLoaded;
  120. }
  121. @Override
  122. public void onClick(ClickEvent event) {
  123. Cell cell = grid.getCellForEvent(event);
  124. if (cell == null) {
  125. return;
  126. }
  127. selectedY = cell.getRowIndex();
  128. selectedX = cell.getCellIndex();
  129. }
  130. @Override
  131. public HandlerRegistration addClickHandler(ClickHandler handler) {
  132. return addDomHandler(handler, ClickEvent.getType());
  133. }
  134. }