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

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