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.

FormLayoutConnector.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.formlayout;
  17. import com.google.gwt.dom.client.Element;
  18. import com.google.gwt.user.client.ui.Widget;
  19. import com.vaadin.client.ComponentConnector;
  20. import com.vaadin.client.ConnectorHierarchyChangeEvent;
  21. import com.vaadin.client.TooltipInfo;
  22. import com.vaadin.client.Util;
  23. import com.vaadin.client.communication.StateChangeEvent;
  24. import com.vaadin.client.ui.AbstractFieldConnector;
  25. import com.vaadin.client.ui.AbstractLayoutConnector;
  26. import com.vaadin.client.ui.VFormLayout;
  27. import com.vaadin.client.ui.VFormLayout.Caption;
  28. import com.vaadin.client.ui.VFormLayout.ErrorFlag;
  29. import com.vaadin.client.ui.VFormLayout.VFormLayoutTable;
  30. import com.vaadin.shared.ui.Connect;
  31. import com.vaadin.shared.ui.MarginInfo;
  32. import com.vaadin.shared.ui.orderedlayout.AbstractOrderedLayoutState;
  33. import com.vaadin.ui.FormLayout;
  34. @Connect(FormLayout.class)
  35. public class FormLayoutConnector extends AbstractLayoutConnector {
  36. @Override
  37. public AbstractOrderedLayoutState getState() {
  38. return (AbstractOrderedLayoutState) super.getState();
  39. }
  40. @Override
  41. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  42. super.onStateChanged(stateChangeEvent);
  43. VFormLayoutTable formLayoutTable = getWidget().table;
  44. formLayoutTable.setMargins(new MarginInfo(getState().marginsBitmask));
  45. formLayoutTable.setSpacing(getState().spacing);
  46. }
  47. @Override
  48. public void onConnectorHierarchyChange(ConnectorHierarchyChangeEvent event) {
  49. VFormLayout formLayout = getWidget();
  50. VFormLayoutTable formLayoutTable = getWidget().table;
  51. int childId = 0;
  52. formLayoutTable.setRowCount(getChildComponents().size());
  53. for (ComponentConnector child : getChildComponents()) {
  54. Widget childWidget = child.getWidget();
  55. Caption caption = formLayoutTable.getCaption(childWidget);
  56. if (caption == null) {
  57. caption = formLayout.new Caption(child);
  58. caption.addClickHandler(formLayoutTable);
  59. }
  60. ErrorFlag error = formLayoutTable.getError(childWidget);
  61. if (error == null) {
  62. error = formLayout.new ErrorFlag(child);
  63. }
  64. formLayoutTable.setChild(childId, childWidget, caption, error);
  65. childId++;
  66. }
  67. for (ComponentConnector oldChild : event.getOldChildren()) {
  68. if (oldChild.getParent() == this) {
  69. continue;
  70. }
  71. formLayoutTable.cleanReferences(oldChild.getWidget());
  72. }
  73. }
  74. @Override
  75. public void updateCaption(ComponentConnector component) {
  76. getWidget().table.updateCaption(component.getWidget(),
  77. component.getState(), component.isEnabled());
  78. boolean hideErrors = false;
  79. // FIXME This incorrectly depends on AbstractFieldConnector
  80. if (component instanceof AbstractFieldConnector) {
  81. hideErrors = ((AbstractFieldConnector) component).getState().hideErrors;
  82. }
  83. getWidget().table.updateError(component.getWidget(),
  84. component.getState().errorMessage, hideErrors);
  85. }
  86. @Override
  87. public VFormLayout getWidget() {
  88. return (VFormLayout) super.getWidget();
  89. }
  90. @Override
  91. public TooltipInfo getTooltipInfo(Element element) {
  92. TooltipInfo info = null;
  93. if (element != getWidget().getElement()) {
  94. Object node = Util.findWidget(
  95. (com.google.gwt.user.client.Element) element,
  96. VFormLayout.Caption.class);
  97. if (node != null) {
  98. VFormLayout.Caption caption = (VFormLayout.Caption) node;
  99. info = caption.getOwner().getTooltipInfo(element);
  100. } else {
  101. node = Util.findWidget(
  102. (com.google.gwt.user.client.Element) element,
  103. VFormLayout.ErrorFlag.class);
  104. if (node != null) {
  105. VFormLayout.ErrorFlag flag = (VFormLayout.ErrorFlag) node;
  106. info = flag.getOwner().getTooltipInfo(element);
  107. }
  108. }
  109. }
  110. if (info == null) {
  111. info = super.getTooltipInfo(element);
  112. }
  113. return info;
  114. }
  115. @Override
  116. public boolean hasTooltip() {
  117. /*
  118. * Tooltips are fetched from child connectors -> there's no quick way of
  119. * checking whether there might a tooltip hiding somewhere
  120. */
  121. return true;
  122. }
  123. }