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.

CustomFieldConnector.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright 2000-2016 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.v7.client.ui.customfield;
  17. import java.util.Collections;
  18. import java.util.List;
  19. import java.util.logging.Logger;
  20. import com.google.gwt.event.shared.HandlerRegistration;
  21. import com.google.gwt.user.client.ui.Widget;
  22. import com.vaadin.client.ComponentConnector;
  23. import com.vaadin.client.ConnectorHierarchyChangeEvent;
  24. import com.vaadin.client.ConnectorHierarchyChangeEvent.ConnectorHierarchyChangeHandler;
  25. import com.vaadin.client.Focusable;
  26. import com.vaadin.client.HasComponentsConnector;
  27. import com.vaadin.client.communication.StateChangeEvent;
  28. import com.vaadin.shared.ui.Connect;
  29. import com.vaadin.v7.client.ui.AbstractFieldConnector;
  30. import com.vaadin.v7.client.ui.VCustomField;
  31. import com.vaadin.v7.ui.CustomField;
  32. @Connect(value = CustomField.class)
  33. public class CustomFieldConnector extends AbstractFieldConnector
  34. implements HasComponentsConnector, ConnectorHierarchyChangeHandler {
  35. List<ComponentConnector> childComponents;
  36. /**
  37. * Default constructor
  38. */
  39. public CustomFieldConnector() {
  40. addConnectorHierarchyChangeHandler(this);
  41. }
  42. @Override
  43. public VCustomField getWidget() {
  44. return (VCustomField) super.getWidget();
  45. }
  46. @Override
  47. public void updateCaption(ComponentConnector connector) {
  48. // NOP, custom field does not render the caption of its content
  49. }
  50. @Override
  51. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  52. super.onStateChanged(stateChangeEvent);
  53. if (getState().focusDelegate != null) {
  54. Widget widget = ((ComponentConnector) getState().focusDelegate)
  55. .getWidget();
  56. if (widget instanceof Focusable) {
  57. getWidget().setFocusDelegate((Focusable) widget);
  58. } else if (widget instanceof com.google.gwt.user.client.ui.Focusable) {
  59. getWidget().setFocusDelegate(
  60. (com.google.gwt.user.client.ui.Focusable) widget);
  61. } else {
  62. getLogger().warning(
  63. "The given focus delegate does not implement Focusable: "
  64. + widget.getClass().getName());
  65. }
  66. } else {
  67. getWidget().setFocusDelegate((Focusable) null);
  68. }
  69. }
  70. private static Logger getLogger() {
  71. return Logger.getLogger(CustomFieldConnector.class.getName());
  72. }
  73. @Override
  74. public void onConnectorHierarchyChange(
  75. ConnectorHierarchyChangeEvent event) {
  76. // We always have 1 child, unless the child is hidden
  77. getWidget().setWidget(getContentWidget());
  78. }
  79. /*
  80. * (non-Javadoc)
  81. *
  82. * @see com.vaadin.client.HasComponentsConnector#getChildren()
  83. */
  84. @Override
  85. public List<ComponentConnector> getChildComponents() {
  86. if (childComponents == null) {
  87. return Collections.emptyList();
  88. }
  89. return childComponents;
  90. }
  91. /*
  92. * (non-Javadoc)
  93. *
  94. * @see com.vaadin.client.HasComponentsConnector#setChildren
  95. * (java.util.Collection)
  96. */
  97. @Override
  98. public void setChildComponents(List<ComponentConnector> childComponents) {
  99. this.childComponents = childComponents;
  100. }
  101. @Override
  102. public HandlerRegistration addConnectorHierarchyChangeHandler(
  103. ConnectorHierarchyChangeHandler handler) {
  104. return ensureHandlerManager()
  105. .addHandler(ConnectorHierarchyChangeEvent.TYPE, handler);
  106. }
  107. /**
  108. * Returns the content (only/first child) of the container.
  109. *
  110. * @return child connector or null if none (e.g. invisible or not set on
  111. * server)
  112. */
  113. protected ComponentConnector getContent() {
  114. List<ComponentConnector> children = getChildComponents();
  115. if (children.isEmpty()) {
  116. return null;
  117. } else {
  118. return children.get(0);
  119. }
  120. }
  121. /**
  122. * Returns the widget (if any) of the content of the container.
  123. *
  124. * @return widget of the only/first connector of the container, null if no
  125. * content or if there is no widget for the connector
  126. */
  127. protected Widget getContentWidget() {
  128. ComponentConnector content = getContent();
  129. if (null != content) {
  130. return content.getWidget();
  131. } else {
  132. return null;
  133. }
  134. }
  135. }