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 13KB

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