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.

VColorPickerArea.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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;
  17. import com.google.gwt.event.dom.client.ClickEvent;
  18. import com.google.gwt.event.dom.client.ClickHandler;
  19. import com.google.gwt.event.dom.client.HasClickHandlers;
  20. import com.google.gwt.event.shared.HandlerRegistration;
  21. import com.google.gwt.user.client.DOM;
  22. import com.google.gwt.user.client.Event;
  23. import com.google.gwt.user.client.ui.HTML;
  24. import com.google.gwt.user.client.ui.HasHTML;
  25. import com.google.gwt.user.client.ui.Label;
  26. import com.google.gwt.user.client.ui.Widget;
  27. /**
  28. * Client side implementation for ColorPickerArea.
  29. *
  30. * @since 7.0.0
  31. */
  32. public class VColorPickerArea extends Widget
  33. implements ClickHandler, HasHTML, HasClickHandlers {
  34. public static final String CLASSNAME = "v-colorpicker";
  35. private String color = null;
  36. private boolean isOpen;
  37. private HTML caption;
  38. private HTML area;
  39. /**
  40. * Initializes an area-style color picker widget.
  41. */
  42. public VColorPickerArea() {
  43. super();
  44. setElement(DOM.createDiv());
  45. setStyleName(CLASSNAME);
  46. caption = new HTML();
  47. caption.addStyleName("v-caption");
  48. caption.setWidth("");
  49. area = new HTML();
  50. area.setStylePrimaryName(getStylePrimaryName() + "-area");
  51. getElement().appendChild(caption.getElement());
  52. getElement().appendChild(area.getElement());
  53. addClickHandler(this);
  54. }
  55. /**
  56. * Adds a click handler to the widget and sinks the click event.
  57. *
  58. * @param handler
  59. * @return HandlerRegistration used to remove the handler
  60. */
  61. @Override
  62. public HandlerRegistration addClickHandler(ClickHandler handler) {
  63. return addDomHandler(handler, ClickEvent.getType());
  64. }
  65. @Override
  66. public void onClick(ClickEvent event) {
  67. setOpen(!isOpen);
  68. refreshColor();
  69. }
  70. @Override
  71. public void onBrowserEvent(Event event) {
  72. int type = DOM.eventGetType(event);
  73. switch (type) {
  74. case Event.ONCLICK:
  75. if (DOM.isOrHasChild(area.getElement(),
  76. DOM.eventGetTarget(event))) {
  77. super.onBrowserEvent(event);
  78. }
  79. break;
  80. default:
  81. super.onBrowserEvent(event);
  82. }
  83. }
  84. /**
  85. * Mark the popup opened/closed.
  86. *
  87. * @param open
  88. */
  89. public void setOpen(boolean open) {
  90. isOpen = open;
  91. }
  92. /**
  93. * Check the popup's marked state.
  94. *
  95. * @return true if the popup has been marked being open, false otherwise.
  96. */
  97. public boolean isOpen() {
  98. return isOpen;
  99. }
  100. /**
  101. * Sets the caption's content to the given text.
  102. *
  103. * @param text
  104. *
  105. * @see Label#setText(String)
  106. */
  107. @Override
  108. public void setText(String text) {
  109. caption.setText(text);
  110. }
  111. /**
  112. * Gets the caption's contents as text.
  113. *
  114. * @return the caption's text
  115. */
  116. @Override
  117. public String getText() {
  118. return caption.getText();
  119. }
  120. /**
  121. * Sets the caption's content to the given HTML.
  122. *
  123. * @param html
  124. */
  125. @Override
  126. public void setHTML(String html) {
  127. caption.setHTML(html);
  128. }
  129. /**
  130. * Gets the caption's contents as HTML.
  131. *
  132. * @return the caption's HTML
  133. */
  134. @Override
  135. public String getHTML() {
  136. return caption.getHTML();
  137. }
  138. /**
  139. * Sets the color for the area.
  140. *
  141. * @param color
  142. */
  143. public void setColor(String color) {
  144. this.color = color;
  145. }
  146. /**
  147. * Gets the color.
  148. *
  149. * @since 8.4
  150. * @return the color
  151. */
  152. public String getColor() {
  153. return color;
  154. }
  155. /**
  156. * Update the color area with the currently set color.
  157. */
  158. public void refreshColor() {
  159. if (color != null) {
  160. // Set the color
  161. area.getElement().getStyle().setProperty("background", color);
  162. }
  163. }
  164. @Override
  165. public void setStylePrimaryName(String style) {
  166. super.setStylePrimaryName(style);
  167. area.setStylePrimaryName(getStylePrimaryName() + "-area");
  168. }
  169. /**
  170. * Sets the color area's height. This height does not include caption or
  171. * decorations such as border, margin, and padding.
  172. */
  173. @Override
  174. public void setHeight(String height) {
  175. area.setHeight(height);
  176. }
  177. /**
  178. * Sets the color area's width. This width does not include caption or
  179. * decorations such as border, margin, and padding.
  180. */
  181. @Override
  182. public void setWidth(String width) {
  183. area.setWidth(width);
  184. }
  185. }