Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

VPanel.java 7.0KB

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