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.

AbstractComponentConnector.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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.core.client.JsArrayString;
  18. import com.google.gwt.dom.client.Element;
  19. import com.google.gwt.user.client.ui.Focusable;
  20. import com.google.gwt.user.client.ui.HasEnabled;
  21. import com.google.gwt.user.client.ui.Widget;
  22. import com.vaadin.client.ApplicationConnection;
  23. import com.vaadin.client.ComponentConnector;
  24. import com.vaadin.client.HasComponentsConnector;
  25. import com.vaadin.client.LayoutManager;
  26. import com.vaadin.client.Profiler;
  27. import com.vaadin.client.ServerConnector;
  28. import com.vaadin.client.StyleConstants;
  29. import com.vaadin.client.TooltipInfo;
  30. import com.vaadin.client.UIDL;
  31. import com.vaadin.client.Util;
  32. import com.vaadin.client.VConsole;
  33. import com.vaadin.client.communication.StateChangeEvent;
  34. import com.vaadin.client.metadata.NoDataException;
  35. import com.vaadin.client.metadata.Type;
  36. import com.vaadin.client.metadata.TypeData;
  37. import com.vaadin.client.ui.datefield.PopupDateFieldConnector;
  38. import com.vaadin.client.ui.ui.UIConnector;
  39. import com.vaadin.shared.AbstractComponentState;
  40. import com.vaadin.shared.ComponentConstants;
  41. import com.vaadin.shared.Connector;
  42. import com.vaadin.shared.ui.ComponentStateUtil;
  43. import com.vaadin.shared.ui.TabIndexState;
  44. public abstract class AbstractComponentConnector extends AbstractConnector
  45. implements ComponentConnector {
  46. private Widget widget;
  47. private String lastKnownWidth = "";
  48. private String lastKnownHeight = "";
  49. private boolean initialStateEvent = true;
  50. private boolean tooltipListenersAttached = false;
  51. /**
  52. * The style names from getState().getStyles() which are currently applied
  53. * to the widget.
  54. */
  55. private JsArrayString styleNames = JsArrayString.createArray().cast();
  56. /**
  57. * Default constructor
  58. */
  59. public AbstractComponentConnector() {
  60. }
  61. /**
  62. * Creates and returns the widget for this VPaintableWidget. This method
  63. * should only be called once when initializing the paintable.
  64. *
  65. * @return
  66. */
  67. protected Widget createWidget() {
  68. Type type = TypeData.getType(getClass());
  69. try {
  70. Type widgetType = type.getMethod("getWidget").getReturnType();
  71. Object instance = widgetType.createInstance();
  72. return (Widget) instance;
  73. } catch (NoDataException e) {
  74. throw new IllegalStateException(
  75. "There is no information about the widget for "
  76. + Util.getSimpleName(this)
  77. + ". Did you remember to compile the right widgetset?",
  78. e);
  79. }
  80. }
  81. /**
  82. * Returns the widget associated with this paintable. The widget returned by
  83. * this method must not changed during the life time of the paintable.
  84. *
  85. * @return The widget associated with this paintable
  86. */
  87. @Override
  88. public Widget getWidget() {
  89. if (widget == null) {
  90. Profiler.enter("AbstractComponentConnector.createWidget for "
  91. + Util.getSimpleName(this));
  92. widget = createWidget();
  93. Profiler.leave("AbstractComponentConnector.createWidget for "
  94. + Util.getSimpleName(this));
  95. }
  96. return widget;
  97. }
  98. @Deprecated
  99. public static boolean isRealUpdate(UIDL uidl) {
  100. return !uidl.hasAttribute("cached");
  101. }
  102. @Override
  103. public AbstractComponentState getState() {
  104. return (AbstractComponentState) super.getState();
  105. }
  106. @Override
  107. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  108. Profiler.enter("AbstractComponentConnector.onStateChanged");
  109. Profiler.enter("AbstractComponentConnector.onStateChanged update id");
  110. if (stateChangeEvent.hasPropertyChanged("id")) {
  111. if (getState().id != null) {
  112. getWidget().getElement().setId(getState().id);
  113. } else if (!initialStateEvent) {
  114. getWidget().getElement().removeAttribute("id");
  115. }
  116. }
  117. Profiler.leave("AbstractComponentConnector.onStateChanged update id");
  118. /*
  119. * Disabled state may affect (override) tabindex so the order must be
  120. * first setting tabindex, then enabled state (through super
  121. * implementation).
  122. */
  123. Profiler.enter("AbstractComponentConnector.onStateChanged update tab index");
  124. if (getState() instanceof TabIndexState) {
  125. if (getWidget() instanceof Focusable) {
  126. ((Focusable) getWidget())
  127. .setTabIndex(((TabIndexState) getState()).tabIndex);
  128. } else {
  129. /*
  130. * TODO Enable this error when all widgets have been fixed to
  131. * properly support tabIndex, i.e. implement Focusable
  132. */
  133. // VConsole.error("Tab index received for "
  134. // + Util.getSimpleName(getWidget())
  135. // + " which does not implement Focusable");
  136. }
  137. }
  138. Profiler.leave("AbstractComponentConnector.onStateChanged update tab index");
  139. super.onStateChanged(stateChangeEvent);
  140. // Style names
  141. updateWidgetStyleNames();
  142. /*
  143. * updateComponentSize need to be after caption update so caption can be
  144. * taken into account
  145. */
  146. updateComponentSize();
  147. Profiler.enter("AbstractComponentContainer.onStateChanged check tooltip");
  148. if (!tooltipListenersAttached && hasTooltip()) {
  149. /*
  150. * Add event handlers for tooltips if they are needed but have not
  151. * yet been added.
  152. */
  153. tooltipListenersAttached = true;
  154. getConnection().getVTooltip().connectHandlersToWidget(getWidget());
  155. }
  156. Profiler.leave("AbstractComponentContainer.onStateChanged check tooltip");
  157. initialStateEvent = false;
  158. Profiler.leave("AbstractComponentConnector.onStateChanged");
  159. }
  160. @Override
  161. public void setWidgetEnabled(boolean widgetEnabled) {
  162. // add or remove v-disabled style name from the widget
  163. setWidgetStyleName(ApplicationConnection.DISABLED_CLASSNAME,
  164. !widgetEnabled);
  165. if (getWidget() instanceof HasEnabled) {
  166. // set widget specific enabled state
  167. ((HasEnabled) getWidget()).setEnabled(widgetEnabled);
  168. // make sure the caption has or has not v-disabled style
  169. if (delegateCaptionHandling()) {
  170. ServerConnector parent = getParent();
  171. if (parent instanceof HasComponentsConnector) {
  172. ((HasComponentsConnector) parent).updateCaption(this);
  173. } else if (parent == null && !(this instanceof UIConnector)) {
  174. VConsole.error("Parent of connector "
  175. + Util.getConnectorString(this)
  176. + " is null. This is typically an indication of a broken component hierarchy");
  177. }
  178. }
  179. }
  180. }
  181. protected void updateComponentSize() {
  182. updateComponentSize(getState().width == null ? "" : getState().width,
  183. getState().height == null ? "" : getState().height);
  184. }
  185. protected void updateComponentSize(String newWidth, String newHeight) {
  186. Profiler.enter("AbstractComponentConnector.updateComponentSize");
  187. // Parent should be updated if either dimension changed between relative
  188. // and non-relative
  189. if (newWidth.endsWith("%") != lastKnownWidth.endsWith("%")) {
  190. Connector parent = getParent();
  191. if (parent instanceof ManagedLayout) {
  192. getLayoutManager().setNeedsHorizontalLayout(
  193. (ManagedLayout) parent);
  194. }
  195. }
  196. if (newHeight.endsWith("%") != lastKnownHeight.endsWith("%")) {
  197. Connector parent = getParent();
  198. if (parent instanceof ManagedLayout) {
  199. getLayoutManager().setNeedsVerticalLayout(
  200. (ManagedLayout) parent);
  201. }
  202. }
  203. lastKnownWidth = newWidth;
  204. lastKnownHeight = newHeight;
  205. // Set defined sizes
  206. Widget widget = getWidget();
  207. Profiler.enter("AbstractComponentConnector.updateComponentSize update styleNames");
  208. widget.setStyleName("v-has-width", !isUndefinedWidth());
  209. widget.setStyleName("v-has-height", !isUndefinedHeight());
  210. Profiler.leave("AbstractComponentConnector.updateComponentSize update styleNames");
  211. Profiler.enter("AbstractComponentConnector.updateComponentSize update DOM");
  212. widget.setHeight(newHeight);
  213. widget.setWidth(newWidth);
  214. Profiler.leave("AbstractComponentConnector.updateComponentSize update DOM");
  215. Profiler.leave("AbstractComponentConnector.updateComponentSize");
  216. }
  217. @Override
  218. public boolean isRelativeHeight() {
  219. return ComponentStateUtil.isRelativeHeight(getState());
  220. }
  221. @Override
  222. public boolean isRelativeWidth() {
  223. return ComponentStateUtil.isRelativeWidth(getState());
  224. }
  225. @Override
  226. public boolean isUndefinedHeight() {
  227. return ComponentStateUtil.isUndefinedHeight(getState());
  228. }
  229. @Override
  230. public boolean isUndefinedWidth() {
  231. return ComponentStateUtil.isUndefinedWidth(getState());
  232. }
  233. /*
  234. * (non-Javadoc)
  235. *
  236. * @see com.vaadin.client.ComponentConnector#delegateCaptionHandling ()
  237. */
  238. @Override
  239. public boolean delegateCaptionHandling() {
  240. return true;
  241. }
  242. /**
  243. * Updates the user defined, read-only and error style names for the widget
  244. * based the shared state. User defined style names are prefixed with the
  245. * primary style name of the widget returned by {@link #getWidget()}
  246. * <p>
  247. * This method can be overridden to provide additional style names for the
  248. * component, for example see
  249. * {@link AbstractFieldConnector#updateWidgetStyleNames()}
  250. * </p>
  251. */
  252. protected void updateWidgetStyleNames() {
  253. Profiler.enter("AbstractComponentConnector.updateWidgetStyleNames");
  254. AbstractComponentState state = getState();
  255. String primaryStyleName = getWidget().getStylePrimaryName();
  256. if (state.primaryStyleName != null
  257. && !state.primaryStyleName.equals(primaryStyleName)) {
  258. /*
  259. * We overwrite the widgets primary stylename if state defines a
  260. * primary stylename.
  261. */
  262. getWidget().setStylePrimaryName(state.primaryStyleName);
  263. }
  264. // Set the core 'v' style name for the widget
  265. setWidgetStyleName(StyleConstants.UI_WIDGET, true);
  266. // should be in AbstractFieldConnector ?
  267. // add / remove read-only style name
  268. setWidgetStyleName("v-readonly", isReadOnly());
  269. // add / remove error style name
  270. setWidgetStyleNameWithPrefix(primaryStyleName,
  271. ApplicationConnection.ERROR_CLASSNAME_EXT,
  272. null != state.errorMessage);
  273. // add additional user defined style names as class names, prefixed with
  274. // component default class name. remove nonexistent style names.
  275. // Remove all old stylenames
  276. for (int i = 0; i < styleNames.length(); i++) {
  277. String oldStyle = styleNames.get(i);
  278. setWidgetStyleName(oldStyle, false);
  279. setWidgetStyleNameWithPrefix(primaryStyleName + "-", oldStyle,
  280. false);
  281. }
  282. styleNames.setLength(0);
  283. if (ComponentStateUtil.hasStyles(state)) {
  284. // add new style names
  285. for (String newStyle : state.styles) {
  286. setWidgetStyleName(newStyle, true);
  287. setWidgetStyleNameWithPrefix(primaryStyleName + "-", newStyle,
  288. true);
  289. styleNames.push(newStyle);
  290. }
  291. }
  292. Profiler.leave("AbstractComponentConnector.updateWidgetStyleNames");
  293. }
  294. /**
  295. * This is used to add / remove state related style names from the widget.
  296. * <p>
  297. * Override this method for example if the style name given here should be
  298. * updated in another widget in addition to the one returned by the
  299. * {@link #getWidget()}.
  300. * </p>
  301. *
  302. * @param styleName
  303. * the style name to be added or removed
  304. * @param add
  305. * <code>true</code> to add the given style, <code>false</code>
  306. * to remove it
  307. */
  308. protected void setWidgetStyleName(String styleName, boolean add) {
  309. getWidget().setStyleName(styleName, add);
  310. }
  311. /**
  312. * This is used to add / remove state related prefixed style names from the
  313. * widget.
  314. * <p>
  315. * Override this method if the prefixed style name given here should be
  316. * updated in another widget in addition to the one returned by the
  317. * <code>Connector</code>'s {@link #getWidget()}, or if the prefix should be
  318. * different. For example see
  319. * {@link PopupDateFieldConnector#setWidgetStyleNameWithPrefix(String, String, boolean)}
  320. * </p>
  321. *
  322. * @param styleName
  323. * the style name to be added or removed
  324. * @param add
  325. * <code>true</code> to add the given style, <code>false</code>
  326. * to remove it
  327. * @deprecated This will be removed once styles are no longer added with
  328. * prefixes.
  329. */
  330. @Deprecated
  331. protected void setWidgetStyleNameWithPrefix(String prefix,
  332. String styleName, boolean add) {
  333. if (!styleName.startsWith("-")) {
  334. if (!prefix.endsWith("-")) {
  335. prefix += "-";
  336. }
  337. } else {
  338. if (prefix.endsWith("-")) {
  339. styleName.replaceFirst("-", "");
  340. }
  341. }
  342. getWidget().setStyleName(prefix + styleName, add);
  343. }
  344. /*
  345. * (non-Javadoc)
  346. *
  347. * @see com.vaadin.client.ComponentConnector#isReadOnly()
  348. */
  349. @Override
  350. @Deprecated
  351. public boolean isReadOnly() {
  352. return getState().readOnly;
  353. }
  354. @Override
  355. public LayoutManager getLayoutManager() {
  356. return LayoutManager.get(getConnection());
  357. }
  358. @Override
  359. public void updateEnabledState(boolean enabledState) {
  360. super.updateEnabledState(enabledState);
  361. setWidgetEnabled(isEnabled());
  362. }
  363. @Override
  364. public void onUnregister() {
  365. super.onUnregister();
  366. // Show an error if widget is still attached to DOM. It should never be
  367. // at this point.
  368. if (getWidget() != null && getWidget().isAttached()) {
  369. getWidget().removeFromParent();
  370. VConsole.error("Widget is still attached to the DOM after the connector ("
  371. + Util.getConnectorString(this)
  372. + ") has been unregistered. Widget was removed.");
  373. }
  374. }
  375. @Override
  376. public TooltipInfo getTooltipInfo(Element element) {
  377. return new TooltipInfo(getState().description, getState().errorMessage);
  378. }
  379. @Override
  380. public boolean hasTooltip() {
  381. // Normally, there is a tooltip if description or errorMessage is set
  382. AbstractComponentState state = getState();
  383. if (state.description != null && !state.description.equals("")) {
  384. return true;
  385. } else if (state.errorMessage != null && !state.errorMessage.equals("")) {
  386. return true;
  387. } else {
  388. return false;
  389. }
  390. }
  391. /**
  392. * Gets the icon set for this component.
  393. *
  394. * @return the URL of the icon, or <code>null</code> if no icon has been
  395. * defined.
  396. */
  397. protected String getIcon() {
  398. return getResourceUrl(ComponentConstants.ICON_RESOURCE);
  399. }
  400. /*
  401. * (non-Javadoc)
  402. *
  403. * @see com.vaadin.client.ComponentConnector#flush()
  404. */
  405. @Override
  406. public void flush() {
  407. // No generic implementation. Override if needed
  408. }
  409. }