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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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.dom.client.DivElement;
  18. import com.google.gwt.dom.client.Document;
  19. import com.google.gwt.dom.client.Element;
  20. import com.google.gwt.user.client.DOM;
  21. import com.google.gwt.user.client.Event;
  22. import com.google.gwt.user.client.ui.SimplePanel;
  23. import com.vaadin.client.ApplicationConnection;
  24. import com.vaadin.client.Focusable;
  25. import com.vaadin.client.StyleConstants;
  26. import com.vaadin.client.WidgetUtil.ErrorUtil;
  27. import com.vaadin.client.ui.ShortcutActionHandler.ShortcutActionHandlerOwner;
  28. import com.vaadin.client.ui.TouchScrollDelegate.TouchScrollHandler;
  29. import com.vaadin.shared.ui.ErrorLevel;
  30. public class VPanel extends SimplePanel
  31. implements ShortcutActionHandlerOwner, Focusable {
  32. public static final String CLASSNAME = "v-panel";
  33. /** For internal use only. May be removed or replaced in the future. */
  34. public ApplicationConnection client;
  35. /** For internal use only. May be removed or replaced in the future. */
  36. public String id;
  37. /** For internal use only. May be removed or replaced in the future. */
  38. public final Element captionNode = DOM.createDiv();
  39. private final Element captionText = DOM.createSpan();
  40. private Icon icon;
  41. /** For internal use only. May be removed or replaced in the future. */
  42. public final Element bottomDecoration = DOM.createDiv();
  43. /** For internal use only. May be removed or replaced in the future. */
  44. public final Element contentNode = DOM.createDiv();
  45. private Element errorIndicatorElement;
  46. /** For internal use only. May be removed or replaced in the future. */
  47. public ShortcutActionHandler shortcutHandler;
  48. /** For internal use only. May be removed or replaced in the future. */
  49. public int scrollTop;
  50. /** For internal use only. May be removed or replaced in the future. */
  51. public int scrollLeft;
  52. private TouchScrollHandler touchScrollHandler;
  53. public VPanel() {
  54. super();
  55. DivElement captionWrap = Document.get().createDivElement();
  56. captionWrap.appendChild(captionNode);
  57. captionNode.appendChild(captionText);
  58. captionWrap.setClassName(CLASSNAME + "-captionwrap");
  59. captionNode.setClassName(CLASSNAME + "-caption");
  60. contentNode.setClassName(CLASSNAME + "-content");
  61. bottomDecoration.setClassName(CLASSNAME + "-deco");
  62. getElement().appendChild(captionWrap);
  63. /*
  64. * Make contentNode focusable only by using the setFocus() method. This
  65. * behaviour can be changed by invoking setTabIndex() in the serverside
  66. * implementation
  67. */
  68. contentNode.setTabIndex(-1);
  69. getElement().appendChild(contentNode);
  70. getElement().appendChild(bottomDecoration);
  71. setStyleName(CLASSNAME);
  72. DOM.sinkEvents(getElement(), Event.ONKEYDOWN);
  73. DOM.sinkEvents(contentNode, Event.ONSCROLL | Event.TOUCHEVENTS);
  74. contentNode.getStyle().setProperty("position", "relative");
  75. getElement().getStyle().setProperty("overflow", "hidden");
  76. makeScrollable();
  77. }
  78. /**
  79. * Sets the keyboard focus on the Panel
  80. *
  81. * @param focus
  82. * Should the panel have focus or not.
  83. */
  84. public void setFocus(boolean focus) {
  85. if (focus) {
  86. getContainerElement().focus();
  87. } else {
  88. getContainerElement().blur();
  89. }
  90. }
  91. /*
  92. * (non-Javadoc)
  93. *
  94. * @see com.vaadin.client.Focusable#focus()
  95. */
  96. @Override
  97. public void focus() {
  98. setFocus(true);
  99. }
  100. @Override
  101. protected com.google.gwt.user.client.Element getContainerElement() {
  102. return DOM.asOld(contentNode);
  103. }
  104. /** For internal use only. May be removed or replaced in the future. */
  105. public void setCaption(String text, boolean captionAsHtml) {
  106. if (captionAsHtml) {
  107. captionText.setInnerHTML(text);
  108. } else {
  109. captionText.setInnerText(text);
  110. }
  111. }
  112. /** For internal use only. May be removed or replaced in the future. */
  113. public void setErrorIndicatorVisible(boolean showError,
  114. ErrorLevel errorLevel) {
  115. if (showError) {
  116. if (errorIndicatorElement == null) {
  117. errorIndicatorElement = DOM.createSpan();
  118. DOM.setElementProperty(errorIndicatorElement, "className",
  119. StyleConstants.STYLE_NAME_ERROR_INDICATOR);
  120. DOM.sinkEvents(errorIndicatorElement, Event.MOUSEEVENTS);
  121. sinkEvents(Event.MOUSEEVENTS);
  122. }
  123. ErrorUtil.setErrorLevelStyle(errorIndicatorElement,
  124. StyleConstants.STYLE_NAME_ERROR_INDICATOR, errorLevel);
  125. DOM.insertBefore(captionNode, errorIndicatorElement, captionText);
  126. } else if (errorIndicatorElement != null) {
  127. DOM.removeChild(captionNode, errorIndicatorElement);
  128. errorIndicatorElement = null;
  129. }
  130. }
  131. /** For internal use only. May be removed or replaced in the future. */
  132. public void setIconUri(String iconUri, ApplicationConnection client) {
  133. if (icon != null) {
  134. captionNode.removeChild(icon.getElement());
  135. }
  136. icon = client.getIcon(iconUri);
  137. if (icon != null) {
  138. DOM.insertChild(captionNode, icon.getElement(), 0);
  139. }
  140. }
  141. @Override
  142. public void onBrowserEvent(Event event) {
  143. super.onBrowserEvent(event);
  144. final int type = DOM.eventGetType(event);
  145. if (type == Event.ONKEYDOWN && shortcutHandler != null) {
  146. shortcutHandler.handleKeyboardEvent(event);
  147. return;
  148. }
  149. if (type == Event.ONSCROLL) {
  150. int newscrollTop = DOM.getElementPropertyInt(contentNode,
  151. "scrollTop");
  152. int newscrollLeft = DOM.getElementPropertyInt(contentNode,
  153. "scrollLeft");
  154. if (client != null && (newscrollLeft != scrollLeft
  155. || newscrollTop != scrollTop)) {
  156. scrollLeft = newscrollLeft;
  157. scrollTop = newscrollTop;
  158. client.updateVariable(id, "scrollTop", scrollTop, false);
  159. client.updateVariable(id, "scrollLeft", scrollLeft, false);
  160. }
  161. }
  162. }
  163. @Override
  164. public ShortcutActionHandler getShortcutActionHandler() {
  165. return shortcutHandler;
  166. }
  167. /**
  168. * Ensures the panel is scrollable eg. after style name changes.
  169. * <p>
  170. * For internal use only. May be removed or replaced in the future.
  171. */
  172. public void makeScrollable() {
  173. if (touchScrollHandler == null) {
  174. touchScrollHandler = TouchScrollDelegate.enableTouchScrolling(this);
  175. }
  176. touchScrollHandler.addElement(contentNode);
  177. }
  178. }