Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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