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.

CssLayoutConnector.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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.client.ui.csslayout;
  17. import com.google.gwt.dom.client.Style;
  18. import com.google.gwt.user.client.ui.Widget;
  19. import com.vaadin.client.BrowserInfo;
  20. import com.vaadin.client.ComponentConnector;
  21. import com.vaadin.client.ConnectorHierarchyChangeEvent;
  22. import com.vaadin.client.FastStringMap;
  23. import com.vaadin.client.Profiler;
  24. import com.vaadin.client.Util;
  25. import com.vaadin.client.VCaption;
  26. import com.vaadin.client.communication.StateChangeEvent;
  27. import com.vaadin.client.ui.AbstractLayoutConnector;
  28. import com.vaadin.client.ui.LayoutClickEventHandler;
  29. import com.vaadin.client.ui.VCssLayout;
  30. import com.vaadin.shared.ui.Connect;
  31. import com.vaadin.shared.ui.LayoutClickRpc;
  32. import com.vaadin.shared.ui.csslayout.CssLayoutServerRpc;
  33. import com.vaadin.shared.ui.csslayout.CssLayoutState;
  34. import com.vaadin.shared.util.SharedUtil;
  35. import com.vaadin.ui.CssLayout;
  36. /**
  37. * Connects the server side widget {@link CssLayout} with the client side
  38. * counterpart {@link VCssLayout}
  39. */
  40. @Connect(CssLayout.class)
  41. public class CssLayoutConnector extends AbstractLayoutConnector {
  42. private LayoutClickEventHandler clickEventHandler = new LayoutClickEventHandler(
  43. this) {
  44. @Override
  45. protected ComponentConnector getChildComponent(
  46. com.google.gwt.user.client.Element element) {
  47. return Util.getConnectorForElement(getConnection(), getWidget(),
  48. element);
  49. }
  50. @Override
  51. protected LayoutClickRpc getLayoutClickRPC() {
  52. return getRpcProxy(CssLayoutServerRpc.class);
  53. }
  54. };
  55. private final FastStringMap<VCaption> childIdToCaption = FastStringMap
  56. .create();
  57. /*
  58. * (non-Javadoc)
  59. *
  60. * @see com.vaadin.client.ui.AbstractLayoutConnector#getState()
  61. */
  62. @Override
  63. public CssLayoutState getState() {
  64. return (CssLayoutState) super.getState();
  65. }
  66. /*
  67. * (non-Javadoc)
  68. *
  69. * @see
  70. * com.vaadin.client.ui.AbstractComponentConnector#onStateChanged(com.vaadin
  71. * .client.communication.StateChangeEvent)
  72. */
  73. @Override
  74. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  75. super.onStateChanged(stateChangeEvent);
  76. clickEventHandler.handleEventHandlerRegistration();
  77. for (ComponentConnector child : getChildComponents()) {
  78. if (!getState().childCss.containsKey(child)) {
  79. continue;
  80. }
  81. String css = getState().childCss.get(child);
  82. Style style = child.getWidget().getElement().getStyle();
  83. // should we remove styles also? How can we know what we have added
  84. // as it is added directly to the child component?
  85. String[] cssRules = css.split(";");
  86. for (String cssRule : cssRules) {
  87. String[] parts = cssRule.split(":", 2);
  88. if (parts.length == 2) {
  89. style.setProperty(makeCamelCase(parts[0].trim()),
  90. parts[1].trim());
  91. }
  92. }
  93. }
  94. }
  95. /*
  96. * (non-Javadoc)
  97. *
  98. * @see com.vaadin.client.ui.AbstractComponentContainerConnector#
  99. * onConnectorHierarchyChange
  100. * (com.vaadin.client.ConnectorHierarchyChangeEvent)
  101. */
  102. @Override
  103. public void onConnectorHierarchyChange(
  104. ConnectorHierarchyChangeEvent event) {
  105. Profiler.enter("CssLayoutConnector.onConnectorHierarchyChange");
  106. // Detach old child widgets and possibly their caption
  107. Profiler.enter(
  108. "CssLayoutConnector.onConnectorHierarchyChange remove old children");
  109. for (ComponentConnector child : event.getOldChildren()) {
  110. if (child.getParent() == this) {
  111. // Skip current children
  112. continue;
  113. }
  114. getWidget().remove(child.getWidget());
  115. VCaption vCaption = childIdToCaption.get(child.getConnectorId());
  116. if (vCaption != null) {
  117. childIdToCaption.remove(child.getConnectorId());
  118. getWidget().remove(vCaption);
  119. }
  120. }
  121. Profiler.leave(
  122. "CssLayoutConnector.onConnectorHierarchyChange remove old children");
  123. Profiler.enter(
  124. "CssLayoutConnector.onConnectorHierarchyChange add children");
  125. int index = 0;
  126. for (ComponentConnector child : getChildComponents()) {
  127. VCaption childCaption = childIdToCaption
  128. .get(child.getConnectorId());
  129. if (childCaption != null) {
  130. getWidget().addOrMove(childCaption, index++);
  131. }
  132. getWidget().addOrMove(child.getWidget(), index++);
  133. }
  134. Profiler.leave(
  135. "CssLayoutConnector.onConnectorHierarchyChange add children");
  136. Profiler.leave("CssLayoutConnector.onConnectorHierarchyChange");
  137. }
  138. /**
  139. * Converts a css property string to CamelCase
  140. *
  141. * @param cssProperty
  142. * The property string
  143. * @return A string converted to camelcase
  144. */
  145. private static final String makeCamelCase(String cssProperty) {
  146. cssProperty = SharedUtil.dashSeparatedToCamelCase(cssProperty);
  147. if ("float".equals(cssProperty)) {
  148. if (BrowserInfo.get().isIE()) {
  149. return "styleFloat";
  150. } else {
  151. return "cssFloat";
  152. }
  153. }
  154. return cssProperty;
  155. }
  156. /*
  157. * (non-Javadoc)
  158. *
  159. * @see com.vaadin.client.ui.AbstractComponentConnector#getWidget()
  160. */
  161. @Override
  162. public VCssLayout getWidget() {
  163. return (VCssLayout) super.getWidget();
  164. }
  165. /*
  166. * (non-Javadoc)
  167. *
  168. * @see com.vaadin.client.HasComponentsConnector#updateCaption(com.vaadin
  169. * .client.ComponentConnector)
  170. */
  171. @Override
  172. public void updateCaption(ComponentConnector child) {
  173. Widget childWidget = child.getWidget();
  174. int widgetPosition = getWidget().getWidgetIndex(childWidget);
  175. String childId = child.getConnectorId();
  176. VCaption caption = childIdToCaption.get(childId);
  177. if (VCaption.isNeeded(child)) {
  178. if (caption == null) {
  179. caption = new VCaption(child, getConnection());
  180. childIdToCaption.put(childId, caption);
  181. }
  182. if (!caption.isAttached()) {
  183. // Insert caption at widget index == before widget
  184. getWidget().insert(caption, widgetPosition);
  185. }
  186. caption.updateCaption();
  187. } else if (caption != null) {
  188. childIdToCaption.remove(childId);
  189. getWidget().remove(caption);
  190. }
  191. }
  192. }