Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

VColorPickerGradient.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright 2000-2018 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.Style.Unit;
  18. import com.google.gwt.event.dom.client.MouseDownEvent;
  19. import com.google.gwt.event.dom.client.MouseDownHandler;
  20. import com.google.gwt.event.dom.client.MouseMoveEvent;
  21. import com.google.gwt.event.dom.client.MouseMoveHandler;
  22. import com.google.gwt.event.dom.client.MouseUpEvent;
  23. import com.google.gwt.event.dom.client.MouseUpHandler;
  24. import com.google.gwt.user.client.ui.AbsolutePanel;
  25. import com.google.gwt.user.client.ui.FocusPanel;
  26. import com.google.gwt.user.client.ui.HTML;
  27. import com.vaadin.client.ui.SubPartAware;
  28. /**
  29. * Client side implementation for ColorPickerGradient.
  30. *
  31. * @since 7.0.0
  32. *
  33. */
  34. public class VColorPickerGradient extends FocusPanel implements
  35. MouseDownHandler, MouseUpHandler, MouseMoveHandler, SubPartAware {
  36. /** Set the CSS class name to allow styling. */
  37. public static final String CLASSNAME = "v-colorpicker-gradient";
  38. public static final String CLASSNAME_BACKGROUND = CLASSNAME + "-background";
  39. public static final String CLASSNAME_FOREGROUND = CLASSNAME + "-foreground";
  40. public static final String CLASSNAME_LOWERBOX = CLASSNAME + "-lowerbox";
  41. public static final String CLASSNAME_HIGHERBOX = CLASSNAME + "-higherbox";
  42. public static final String CLASSNAME_CONTAINER = CLASSNAME + "-container";
  43. public static final String CLASSNAME_CLICKLAYER = CLASSNAME + "-clicklayer";
  44. private static final String CLICKLAYER_ID = "clicklayer";
  45. private final HTML background;
  46. private final HTML foreground;
  47. private final HTML lowercross;
  48. private final HTML highercross;
  49. private final HTML clicklayer;
  50. private final AbsolutePanel container;
  51. private boolean mouseIsDown = false;
  52. private int cursorX;
  53. private int cursorY;
  54. private int width = 220;
  55. private int height = 220;
  56. /**
  57. * Instantiates the client side component for a color picker gradient.
  58. */
  59. public VColorPickerGradient() {
  60. super();
  61. setStyleName(CLASSNAME);
  62. background = new HTML();
  63. background.setStyleName(CLASSNAME_BACKGROUND);
  64. background.setPixelSize(width, height);
  65. foreground = new HTML();
  66. foreground.setStyleName(CLASSNAME_FOREGROUND);
  67. foreground.setPixelSize(width, height);
  68. clicklayer = new HTML();
  69. clicklayer.setStyleName(CLASSNAME_CLICKLAYER);
  70. clicklayer.setPixelSize(width, height);
  71. clicklayer.addMouseDownHandler(this);
  72. clicklayer.addMouseUpHandler(this);
  73. clicklayer.addMouseMoveHandler(this);
  74. lowercross = new HTML();
  75. lowercross.setPixelSize(width / 2, height / 2);
  76. lowercross.setStyleName(CLASSNAME_LOWERBOX);
  77. highercross = new HTML();
  78. highercross.setPixelSize(width / 2, height / 2);
  79. highercross.setStyleName(CLASSNAME_HIGHERBOX);
  80. container = new AbsolutePanel();
  81. container.setStyleName(CLASSNAME_CONTAINER);
  82. container.setPixelSize(width, height);
  83. container.add(background, 0, 0);
  84. container.add(foreground, 0, 0);
  85. container.add(lowercross, 0, height / 2);
  86. container.add(highercross, width / 2, 0);
  87. container.add(clicklayer, 0, 0);
  88. add(container);
  89. }
  90. /**
  91. * Returns the latest x-coordinate for pressed-down mouse cursor.
  92. */
  93. public int getCursorX() {
  94. return cursorX;
  95. }
  96. /**
  97. * Returns the latest y-coordinate for pressed-down mouse cursor.
  98. */
  99. public int getCursorY() {
  100. return cursorY;
  101. }
  102. /**
  103. * Sets the given css color as the background.
  104. *
  105. * @param bgColor
  106. */
  107. public void setBGColor(String bgColor) {
  108. if (bgColor == null) {
  109. background.getElement().getStyle().clearBackgroundColor();
  110. } else {
  111. background.getElement().getStyle().setBackgroundColor(bgColor);
  112. }
  113. }
  114. @Override
  115. public void onMouseDown(MouseDownEvent event) {
  116. event.preventDefault();
  117. mouseIsDown = true;
  118. setCursor(event.getX(), event.getY());
  119. }
  120. @Override
  121. public void onMouseUp(MouseUpEvent event) {
  122. event.preventDefault();
  123. mouseIsDown = false;
  124. setCursor(event.getX(), event.getY());
  125. cursorX = event.getX();
  126. cursorY = event.getY();
  127. }
  128. @Override
  129. public void onMouseMove(MouseMoveEvent event) {
  130. event.preventDefault();
  131. if (mouseIsDown) {
  132. setCursor(event.getX(), event.getY());
  133. }
  134. }
  135. /**
  136. * Sets the latest coordinates for pressed-down mouse cursor and updates the
  137. * cross elements.
  138. *
  139. * @param x
  140. * @param y
  141. */
  142. public void setCursor(int x, int y) {
  143. cursorX = x;
  144. cursorY = y;
  145. if (x >= 0) {
  146. lowercross.getElement().getStyle().setWidth(x, Unit.PX);
  147. }
  148. if (y >= 0) {
  149. lowercross.getElement().getStyle().setTop(y, Unit.PX);
  150. }
  151. if (y >= 0) {
  152. lowercross.getElement().getStyle().setHeight(height - y, Unit.PX);
  153. } else {
  154. lowercross.getElement().getStyle().setHeight(Math.abs(y), Unit.PX);
  155. }
  156. if (x >= 0) {
  157. highercross.getElement().getStyle().setWidth(width - x, Unit.PX);
  158. }
  159. if (x >= 0) {
  160. highercross.getElement().getStyle().setLeft(x, Unit.PX);
  161. }
  162. if (y >= 0) {
  163. highercross.getElement().getStyle().setHeight(y, Unit.PX);
  164. } else {
  165. highercross.getElement().getStyle().setHeight(height + y, Unit.PX);
  166. }
  167. }
  168. @Override
  169. public com.google.gwt.user.client.Element getSubPartElement(
  170. String subPart) {
  171. if (subPart.equals(CLICKLAYER_ID)) {
  172. return clicklayer.getElement();
  173. }
  174. return null;
  175. }
  176. @Override
  177. public String getSubPartName(
  178. com.google.gwt.user.client.Element subElement) {
  179. if (clicklayer.getElement().isOrHasChild(subElement)) {
  180. return CLICKLAYER_ID;
  181. }
  182. return null;
  183. }
  184. }