Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

AbstractComponentConnector.java 14KB

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