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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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;
  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. }
  69. @Override
  70. public void onBrowserEvent(Event event) {
  71. int type = DOM.eventGetType(event);
  72. switch (type) {
  73. case Event.ONCLICK:
  74. if (DOM.isOrHasChild(area.getElement(),
  75. DOM.eventGetTarget(event))) {
  76. super.onBrowserEvent(event);
  77. }
  78. break;
  79. default:
  80. super.onBrowserEvent(event);
  81. }
  82. }
  83. /**
  84. * Mark the popup opened/closed.
  85. *
  86. * @param open
  87. */
  88. public void setOpen(boolean open) {
  89. isOpen = open;
  90. }
  91. /**
  92. * Check the popup's marked state.
  93. *
  94. * @return true if the popup has been marked being open, false otherwise.
  95. */
  96. public boolean isOpen() {
  97. return isOpen;
  98. }
  99. /**
  100. * Sets the caption's content to the given text.
  101. *
  102. * @param text
  103. *
  104. * @see Label#setText(String)
  105. */
  106. @Override
  107. public void setText(String text) {
  108. caption.setText(text);
  109. }
  110. /**
  111. * Gets the caption's contents as text.
  112. *
  113. * @return the caption's text
  114. */
  115. @Override
  116. public String getText() {
  117. return caption.getText();
  118. }
  119. /**
  120. * Sets the caption's content to the given HTML.
  121. *
  122. * @param html
  123. */
  124. @Override
  125. public void setHTML(String html) {
  126. caption.setHTML(html);
  127. }
  128. /**
  129. * Gets the caption's contents as HTML.
  130. *
  131. * @return the caption's HTML
  132. */
  133. @Override
  134. public String getHTML() {
  135. return caption.getHTML();
  136. }
  137. /**
  138. * Sets the color for the area.
  139. *
  140. * @param color
  141. */
  142. public void setColor(String color) {
  143. this.color = color;
  144. }
  145. /**
  146. * Update the color area with the currently set color.
  147. */
  148. public void refreshColor() {
  149. if (color != null) {
  150. // Set the color
  151. area.getElement().getStyle().setProperty("background", color);
  152. }
  153. }
  154. @Override
  155. public void setStylePrimaryName(String style) {
  156. super.setStylePrimaryName(style);
  157. area.setStylePrimaryName(getStylePrimaryName() + "-area");
  158. }
  159. /**
  160. * Sets the color area's height. This height does not include caption or
  161. * decorations such as border, margin, and padding.
  162. */
  163. @Override
  164. public void setHeight(String height) {
  165. area.setHeight(height);
  166. }
  167. /**
  168. * Sets the color area's width. This width does not include caption or
  169. * decorations such as border, margin, and padding.
  170. */
  171. @Override
  172. public void setWidth(String width) {
  173. area.setWidth(width);
  174. }
  175. }