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

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