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.

VPanel.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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.Util;
  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 {
  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. * behaviour 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 Element getContainerElement() {
  100. return 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 setErrorIndicatorVisible(boolean showError) {
  108. if (showError) {
  109. if (errorIndicatorElement == null) {
  110. errorIndicatorElement = DOM.createSpan();
  111. DOM.setElementProperty(errorIndicatorElement, "className",
  112. "v-errorindicator");
  113. DOM.sinkEvents(errorIndicatorElement, Event.MOUSEEVENTS);
  114. sinkEvents(Event.MOUSEEVENTS);
  115. }
  116. DOM.insertBefore(captionNode, errorIndicatorElement, captionText);
  117. } else if (errorIndicatorElement != null) {
  118. DOM.removeChild(captionNode, errorIndicatorElement);
  119. errorIndicatorElement = null;
  120. }
  121. }
  122. /** For internal use only. May be removed or replaced in the future. */
  123. public void setIconUri(String iconUri, ApplicationConnection client) {
  124. if (iconUri == null) {
  125. if (icon != null) {
  126. DOM.removeChild(captionNode, icon.getElement());
  127. icon = null;
  128. }
  129. } else {
  130. if (icon == null) {
  131. icon = new Icon(client);
  132. DOM.insertChild(captionNode, icon.getElement(), 0);
  133. }
  134. icon.setUri(iconUri);
  135. }
  136. }
  137. @Override
  138. public void onBrowserEvent(Event event) {
  139. super.onBrowserEvent(event);
  140. final Element target = DOM.eventGetTarget(event);
  141. final int type = DOM.eventGetType(event);
  142. if (type == Event.ONKEYDOWN && shortcutHandler != null) {
  143. shortcutHandler.handleKeyboardEvent(event);
  144. return;
  145. }
  146. if (type == Event.ONSCROLL) {
  147. int newscrollTop = DOM.getElementPropertyInt(contentNode,
  148. "scrollTop");
  149. int newscrollLeft = DOM.getElementPropertyInt(contentNode,
  150. "scrollLeft");
  151. if (client != null
  152. && (newscrollLeft != scrollLeft || newscrollTop != scrollTop)) {
  153. scrollLeft = newscrollLeft;
  154. scrollTop = newscrollTop;
  155. client.updateVariable(id, "scrollTop", scrollTop, false);
  156. client.updateVariable(id, "scrollLeft", scrollLeft, false);
  157. }
  158. }
  159. }
  160. @Override
  161. public ShortcutActionHandler getShortcutActionHandler() {
  162. return shortcutHandler;
  163. }
  164. /**
  165. * Ensures the panel is scrollable eg. after style name changes.
  166. * <p>
  167. * For internal use only. May be removed or replaced in the future.
  168. */
  169. public void makeScrollable() {
  170. if (touchScrollHandler == null) {
  171. touchScrollHandler = TouchScrollDelegate.enableTouchScrolling(this);
  172. }
  173. touchScrollHandler.addElement(contentNode);
  174. Util.removeUnneededScrollbars(contentNode);
  175. }
  176. }