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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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;
  17. import com.google.gwt.core.client.GWT;
  18. import com.google.gwt.core.client.JsArrayString;
  19. import com.google.gwt.dom.client.Element;
  20. import com.google.gwt.dom.client.EventTarget;
  21. import com.google.gwt.dom.client.Touch;
  22. import com.google.gwt.event.dom.client.ContextMenuEvent;
  23. import com.google.gwt.event.dom.client.ContextMenuHandler;
  24. import com.google.gwt.event.dom.client.TouchEndEvent;
  25. import com.google.gwt.event.dom.client.TouchEndHandler;
  26. import com.google.gwt.event.dom.client.TouchMoveEvent;
  27. import com.google.gwt.event.dom.client.TouchMoveHandler;
  28. import com.google.gwt.event.dom.client.TouchStartEvent;
  29. import com.google.gwt.event.dom.client.TouchStartHandler;
  30. import com.google.gwt.event.shared.HandlerRegistration;
  31. import com.google.gwt.user.client.Timer;
  32. import com.google.gwt.user.client.ui.Focusable;
  33. import com.google.gwt.user.client.ui.HasEnabled;
  34. import com.google.gwt.user.client.ui.Widget;
  35. import com.vaadin.client.BrowserInfo;
  36. import com.vaadin.client.ComponentConnector;
  37. import com.vaadin.client.HasComponentsConnector;
  38. import com.vaadin.client.LayoutManager;
  39. import com.vaadin.client.MouseEventDetailsBuilder;
  40. import com.vaadin.client.Profiler;
  41. import com.vaadin.client.ServerConnector;
  42. import com.vaadin.client.StyleConstants;
  43. import com.vaadin.client.TooltipInfo;
  44. import com.vaadin.client.UIDL;
  45. import com.vaadin.client.Util;
  46. import com.vaadin.client.VConsole;
  47. import com.vaadin.client.WidgetUtil;
  48. import com.vaadin.client.annotations.OnStateChange;
  49. import com.vaadin.client.communication.StateChangeEvent;
  50. import com.vaadin.client.metadata.NoDataException;
  51. import com.vaadin.client.metadata.Type;
  52. import com.vaadin.client.metadata.TypeData;
  53. import com.vaadin.client.ui.ui.UIConnector;
  54. import com.vaadin.shared.AbstractComponentState;
  55. import com.vaadin.shared.ComponentConstants;
  56. import com.vaadin.shared.Connector;
  57. import com.vaadin.shared.ContextClickRpc;
  58. import com.vaadin.shared.EventId;
  59. import com.vaadin.shared.MouseEventDetails;
  60. import com.vaadin.shared.ui.ComponentStateUtil;
  61. import com.vaadin.shared.ui.TabIndexState;
  62. import com.vaadin.shared.ui.ui.UIState;
  63. public abstract class AbstractComponentConnector extends AbstractConnector
  64. implements ComponentConnector, HasErrorIndicator {
  65. private HandlerRegistration contextHandler = null;
  66. private Widget widget;
  67. private String lastKnownWidth = "";
  68. private String lastKnownHeight = "";
  69. private boolean tooltipListenersAttached = false;
  70. /**
  71. * The style names from getState().getStyles() which are currently applied
  72. * to the widget.
  73. */
  74. private JsArrayString styleNames = JsArrayString.createArray().cast();
  75. private Timer longTouchTimer;
  76. // TODO encapsulate into a nested class
  77. private HandlerRegistration touchStartHandler;
  78. private HandlerRegistration touchMoveHandler;
  79. private HandlerRegistration touchEndHandler;
  80. private int touchStartX;
  81. private int touchStartY;
  82. private boolean preventNextTouchEnd = false;
  83. protected int SIGNIFICANT_MOVE_THRESHOLD = 20; // pixels
  84. // long touch event delay
  85. // TODO replace with global constant for accessibility
  86. private static final int TOUCH_CONTEXT_MENU_TIMEOUT = 500;
  87. /**
  88. * Default constructor
  89. */
  90. public AbstractComponentConnector() {
  91. }
  92. @OnStateChange("registeredEventListeners")
  93. void handleContextClickListenerChange() {
  94. if (contextHandler == null && hasEventListener(EventId.CONTEXT_CLICK)) {
  95. contextHandler = getWidget()
  96. .addDomHandler(new ContextMenuHandler() {
  97. @Override
  98. public void onContextMenu(ContextMenuEvent event) {
  99. final MouseEventDetails mouseEventDetails = MouseEventDetailsBuilder
  100. .buildMouseEventDetails(
  101. event.getNativeEvent(),
  102. getWidget().getElement());
  103. event.preventDefault();
  104. event.stopPropagation();
  105. sendContextClickEvent(mouseEventDetails,
  106. event.getNativeEvent().getEventTarget());
  107. }
  108. }, ContextMenuEvent.getType());
  109. // if the widget has a contextclick listener, add touch support as
  110. // well.
  111. if (shouldHandleLongTap()) {
  112. registerTouchHandlers();
  113. }
  114. } else if (contextHandler != null
  115. && !hasEventListener(EventId.CONTEXT_CLICK)) {
  116. contextHandler.removeHandler();
  117. contextHandler = null;
  118. // remove the touch handlers as well
  119. unregisterTouchHandlers();
  120. }
  121. }
  122. /**
  123. * The new default behaviour is for long taps to fire a contextclick event
  124. * if there's a contextclick listener attached to the component.
  125. *
  126. * If you do not want this in your component, override this with a blank
  127. * method to get rid of said behaviour.
  128. *
  129. * @since 7.6
  130. */
  131. protected void unregisterTouchHandlers() {
  132. if (touchStartHandler != null) {
  133. touchStartHandler.removeHandler();
  134. touchStartHandler = null;
  135. }
  136. if (touchMoveHandler != null) {
  137. touchMoveHandler.removeHandler();
  138. touchMoveHandler = null;
  139. }
  140. if (touchEndHandler != null) {
  141. touchEndHandler.removeHandler();
  142. touchEndHandler = null;
  143. }
  144. }
  145. /**
  146. * The new default behaviour is for long taps to fire a contextclick event
  147. * if there's a contextclick listener attached to the component.
  148. *
  149. * If you do not want this in your component, override this with a blank
  150. * method to get rid of said behaviour.
  151. *
  152. * Some Vaadin Components already handle the long tap as a context menu.
  153. * This method is unnecessary for those.
  154. *
  155. * @since 7.6
  156. */
  157. protected void registerTouchHandlers() {
  158. touchStartHandler = getWidget().addDomHandler(new TouchStartHandler() {
  159. @Override
  160. public void onTouchStart(final TouchStartEvent event) {
  161. if (longTouchTimer != null && longTouchTimer.isRunning()) {
  162. return;
  163. }
  164. // Prevent selection for the element while pending long tap.
  165. WidgetUtil.setTextSelectionEnabled(getWidget().getElement(),
  166. false);
  167. if (BrowserInfo.get().isAndroid()) {
  168. // Android fires ContextMenu events automatically.
  169. return;
  170. }
  171. /*
  172. * we need to build mouseEventDetails eagerly - the event won't
  173. * be guaranteed to be around when the timer executes. At least
  174. * this was the case with iOS devices.
  175. */
  176. final MouseEventDetails mouseEventDetails = MouseEventDetailsBuilder
  177. .buildMouseEventDetails(event.getNativeEvent(),
  178. getWidget().getElement());
  179. final EventTarget eventTarget = event.getNativeEvent()
  180. .getEventTarget();
  181. longTouchTimer = new Timer() {
  182. @Override
  183. public void run() {
  184. cancelParentTouchTimers(); // we're handling this event,
  185. // our parent components
  186. // don't need to bother with
  187. // it anymore.
  188. // The default context click
  189. // implementation only provides the
  190. // mouse coordinates relative to root
  191. // element of widget.
  192. sendContextClickEvent(mouseEventDetails, eventTarget);
  193. preventNextTouchEnd = true;
  194. }
  195. };
  196. Touch touch = event.getChangedTouches().get(0);
  197. touchStartX = touch.getClientX();
  198. touchStartY = touch.getClientY();
  199. longTouchTimer.schedule(TOUCH_CONTEXT_MENU_TIMEOUT);
  200. }
  201. }, TouchStartEvent.getType());
  202. touchMoveHandler = getWidget().addDomHandler(new TouchMoveHandler() {
  203. @Override
  204. public void onTouchMove(TouchMoveEvent event) {
  205. if (isSignificantMove(event)) {
  206. // Moved finger before the context menu timer
  207. // expired, so let the browser handle the event.
  208. cancelTouchTimer();
  209. }
  210. }
  211. // mostly copy-pasted code from VScrollTable
  212. // TODO refactor main logic to a common class
  213. private boolean isSignificantMove(TouchMoveEvent event) {
  214. if (longTouchTimer == null) {
  215. // no touch start
  216. return false;
  217. }
  218. // Calculate the distance between touch start and the current
  219. // touch
  220. // position
  221. Touch touch = event.getChangedTouches().get(0);
  222. int deltaX = touch.getClientX() - touchStartX;
  223. int deltaY = touch.getClientY() - touchStartY;
  224. int delta = deltaX * deltaX + deltaY * deltaY;
  225. // Compare to the square of the significant move threshold to
  226. // remove the need for a square root
  227. if (delta > SIGNIFICANT_MOVE_THRESHOLD
  228. * SIGNIFICANT_MOVE_THRESHOLD) {
  229. return true;
  230. }
  231. return false;
  232. }
  233. }, TouchMoveEvent.getType());
  234. touchEndHandler = getWidget().addDomHandler(new TouchEndHandler() {
  235. @Override
  236. public void onTouchEnd(TouchEndEvent event) {
  237. // cancel the timer so the event doesn't fire
  238. cancelTouchTimer();
  239. if (preventNextTouchEnd) {
  240. event.preventDefault();
  241. preventNextTouchEnd = false;
  242. }
  243. }
  244. }, TouchEndEvent.getType());
  245. }
  246. protected boolean shouldHandleLongTap() {
  247. return BrowserInfo.get().isTouchDevice() && !BrowserInfo.get().isIOS();
  248. }
  249. /**
  250. * If a long touch event timer is running, cancel it.
  251. *
  252. * @since 7.6
  253. */
  254. private void cancelTouchTimer() {
  255. WidgetUtil.setTextSelectionEnabled(getWidget().getElement(), true);
  256. if (longTouchTimer != null) {
  257. // Re-enable text selection
  258. longTouchTimer.cancel();
  259. }
  260. }
  261. /**
  262. * Cancel the timer recursively for parent components that have timers
  263. * running
  264. *
  265. * @since 7.6
  266. */
  267. private void cancelParentTouchTimers() {
  268. ServerConnector parent = getParent();
  269. // we have to account for the parent being something other than an
  270. // abstractcomponent. getParent returns null for the root element.
  271. while (parent != null) {
  272. if (parent instanceof AbstractComponentConnector) {
  273. ((AbstractComponentConnector) parent).cancelTouchTimer();
  274. }
  275. parent = parent.getParent();
  276. }
  277. }
  278. /**
  279. * This method sends the context menu event to the server-side. Can be
  280. * overridden to provide extra information through an alternative RPC
  281. * interface.
  282. *
  283. * @since 7.6
  284. * @param event
  285. */
  286. protected void sendContextClickEvent(MouseEventDetails details,
  287. EventTarget eventTarget) {
  288. // The default context click implementation only provides the mouse
  289. // coordinates relative to root element of widget.
  290. getRpcProxy(ContextClickRpc.class).contextClick(details);
  291. WidgetUtil.clearTextSelection();
  292. }
  293. /**
  294. * Creates and returns the widget for this VPaintableWidget. This method
  295. * should only be called once when initializing the paintable.
  296. * <p>
  297. * You should typically not override this method since the framework by
  298. * default generates an implementation that uses {@link GWT#create(Class)}
  299. * to create a widget of the same type as returned by the most specific
  300. * override of {@link #getWidget()}. If you do override the method, you
  301. * can't call <code>super.createWidget()</code> since the metadata needed
  302. * for that implementation is not generated if there's an override of the
  303. * method.
  304. *
  305. * @return a new widget instance to use for this component connector
  306. */
  307. protected Widget createWidget() {
  308. Type type = TypeData.getType(getClass());
  309. try {
  310. Type widgetType = type.getMethod("getWidget").getReturnType();
  311. Object instance = widgetType.createInstance();
  312. return (Widget) instance;
  313. } catch (NoDataException e) {
  314. throw new IllegalStateException(
  315. "Default implementation of createWidget() does not work for "
  316. + getClass().getSimpleName()
  317. + ". This might be caused by explicitely using "
  318. + "super.createWidget() or some unspecified "
  319. + "problem with the widgetset compilation.",
  320. e);
  321. }
  322. }
  323. /**
  324. * Returns the widget associated with this paintable. The widget returned by
  325. * this method must not changed during the life time of the paintable.
  326. *
  327. * @return The widget associated with this paintable
  328. */
  329. @Override
  330. public Widget getWidget() {
  331. if (widget == null) {
  332. if (Profiler.isEnabled()) {
  333. Profiler.enter("AbstractComponentConnector.createWidget for "
  334. + getClass().getSimpleName());
  335. }
  336. widget = createWidget();
  337. if (Profiler.isEnabled()) {
  338. Profiler.leave("AbstractComponentConnector.createWidget for "
  339. + getClass().getSimpleName());
  340. }
  341. }
  342. return widget;
  343. }
  344. @Deprecated
  345. public static boolean isRealUpdate(UIDL uidl) {
  346. return !uidl.hasAttribute("cached");
  347. }
  348. @Override
  349. public AbstractComponentState getState() {
  350. return (AbstractComponentState) super.getState();
  351. }
  352. @Override
  353. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  354. Profiler.enter("AbstractComponentConnector.onStateChanged");
  355. Profiler.enter("AbstractComponentConnector.onStateChanged update id");
  356. if (stateChangeEvent.hasPropertyChanged("id")) {
  357. if (getState().id != null) {
  358. getWidget().getElement().setId(getState().id);
  359. } else if (!stateChangeEvent.isInitialStateChange()) {
  360. getWidget().getElement().removeAttribute("id");
  361. }
  362. }
  363. Profiler.leave("AbstractComponentConnector.onStateChanged update id");
  364. /*
  365. * Disabled state may affect (override) tabindex so the order must be
  366. * first setting tabindex, then enabled state (through super
  367. * implementation).
  368. */
  369. Profiler.enter(
  370. "AbstractComponentConnector.onStateChanged update tab index");
  371. if (getState() instanceof TabIndexState) {
  372. if (getWidget() instanceof Focusable) {
  373. ((Focusable) getWidget())
  374. .setTabIndex(((TabIndexState) getState()).tabIndex);
  375. } else {
  376. /*
  377. * TODO Enable this error when all widgets have been fixed to
  378. * properly support tabIndex, i.e. implement Focusable
  379. */
  380. // VConsole.error("Tab index received for "
  381. // + Util.getSimpleName(getWidget())
  382. // + " which does not implement Focusable");
  383. }
  384. } else if (getState() instanceof UIState
  385. && getWidget() instanceof Focusable) {
  386. // UI behaves like a component with TabIndexState
  387. ((Focusable) getWidget())
  388. .setTabIndex(((UIState) getState()).tabIndex);
  389. }
  390. Profiler.leave(
  391. "AbstractComponentConnector.onStateChanged update tab index");
  392. Profiler.enter(
  393. "AbstractComponentConnector.onStateChanged AbstractConnector.onStateChanged()");
  394. super.onStateChanged(stateChangeEvent);
  395. Profiler.leave(
  396. "AbstractComponentConnector.onStateChanged AbstractConnector.onStateChanged()");
  397. // Style names
  398. Profiler.enter(
  399. "AbstractComponentConnector.onStateChanged updateWidgetStyleNames");
  400. updateWidgetStyleNames();
  401. Profiler.leave(
  402. "AbstractComponentConnector.onStateChanged updateWidgetStyleNames");
  403. /*
  404. * updateComponentSize need to be after caption update so caption can be
  405. * taken into account
  406. */
  407. Profiler.enter(
  408. "AbstractComponentConnector.onStateChanged updateComponentSize");
  409. updateComponentSize();
  410. Profiler.leave(
  411. "AbstractComponentConnector.onStateChanged updateComponentSize");
  412. Profiler.enter(
  413. "AbstractComponentContainer.onStateChanged check tooltip");
  414. if (!tooltipListenersAttached && hasTooltip()) {
  415. /*
  416. * Add event handlers for tooltips if they are needed but have not
  417. * yet been added.
  418. */
  419. tooltipListenersAttached = true;
  420. getConnection().getVTooltip().connectHandlersToWidget(getWidget());
  421. }
  422. Profiler.leave(
  423. "AbstractComponentContainer.onStateChanged check tooltip");
  424. Profiler.leave("AbstractComponentConnector.onStateChanged");
  425. }
  426. @Override
  427. public void setWidgetEnabled(boolean widgetEnabled) {
  428. // add or remove v-disabled style name from the widget
  429. setWidgetStyleName(StyleConstants.DISABLED, !widgetEnabled);
  430. if (getWidget() instanceof HasEnabled) {
  431. // set widget specific enabled state
  432. ((HasEnabled) getWidget()).setEnabled(widgetEnabled);
  433. }
  434. // make sure the caption has or has not v-disabled style
  435. if (delegateCaptionHandling()) {
  436. ServerConnector parent = getParent();
  437. if (parent instanceof HasComponentsConnector) {
  438. ((HasComponentsConnector) parent).updateCaption(this);
  439. } else if (parent == null && !(this instanceof UIConnector)) {
  440. VConsole.error("Parent of connector "
  441. + Util.getConnectorString(this)
  442. + " is null. This is typically an indication of a broken component hierarchy");
  443. }
  444. }
  445. }
  446. /**
  447. * Updates the component size based on the shared state, invoking the
  448. * {@link LayoutManager layout manager} if necessary.
  449. */
  450. protected void updateComponentSize() {
  451. updateComponentSize(getState().width == null ? "" : getState().width,
  452. getState().height == null ? "" : getState().height);
  453. }
  454. /**
  455. * Updates the component size, invoking the {@link LayoutManager layout
  456. * manager} if necessary.
  457. *
  458. * @param newWidth
  459. * The new width as a CSS string. Cannot be null.
  460. * @param newHeight
  461. * The new height as a CSS string. Cannot be null.
  462. */
  463. protected void updateComponentSize(String newWidth, String newHeight) {
  464. Profiler.enter("AbstractComponentConnector.updateComponentSize");
  465. // Parent should be updated if either dimension changed between relative
  466. // and non-relative
  467. if (newWidth.endsWith("%") != lastKnownWidth.endsWith("%")) {
  468. Connector parent = getParent();
  469. if (parent instanceof ManagedLayout) {
  470. getLayoutManager()
  471. .setNeedsHorizontalLayout((ManagedLayout) parent);
  472. }
  473. }
  474. if (newHeight.endsWith("%") != lastKnownHeight.endsWith("%")) {
  475. Connector parent = getParent();
  476. if (parent instanceof ManagedLayout) {
  477. getLayoutManager()
  478. .setNeedsVerticalLayout((ManagedLayout) parent);
  479. }
  480. }
  481. lastKnownWidth = newWidth;
  482. lastKnownHeight = newHeight;
  483. // Set defined sizes
  484. Widget widget = getWidget();
  485. Profiler.enter(
  486. "AbstractComponentConnector.updateComponentSize update styleNames");
  487. widget.setStyleName("v-has-width", !isUndefinedWidth());
  488. widget.setStyleName("v-has-height", !isUndefinedHeight());
  489. Profiler.leave(
  490. "AbstractComponentConnector.updateComponentSize update styleNames");
  491. Profiler.enter(
  492. "AbstractComponentConnector.updateComponentSize update DOM");
  493. updateWidgetSize(newWidth, newHeight);
  494. Profiler.leave(
  495. "AbstractComponentConnector.updateComponentSize update DOM");
  496. Profiler.leave("AbstractComponentConnector.updateComponentSize");
  497. }
  498. /**
  499. * Updates the DOM size of this connector's {@link #getWidget() widget}.
  500. *
  501. * @since 7.1.15
  502. * @param newWidth
  503. * The new width as a CSS string. Cannot be null.
  504. * @param newHeight
  505. * The new height as a CSS string. Cannot be null.
  506. */
  507. protected void updateWidgetSize(String newWidth, String newHeight) {
  508. getWidget().setWidth(newWidth);
  509. getWidget().setHeight(newHeight);
  510. }
  511. @Override
  512. public boolean isRelativeHeight() {
  513. return ComponentStateUtil.isRelativeHeight(getState());
  514. }
  515. @Override
  516. public boolean isRelativeWidth() {
  517. return ComponentStateUtil.isRelativeWidth(getState());
  518. }
  519. @Override
  520. public boolean isUndefinedHeight() {
  521. return ComponentStateUtil.isUndefinedHeight(getState());
  522. }
  523. @Override
  524. public boolean isUndefinedWidth() {
  525. return ComponentStateUtil.isUndefinedWidth(getState());
  526. }
  527. /*
  528. * (non-Javadoc)
  529. *
  530. * @see com.vaadin.client.ComponentConnector#delegateCaptionHandling ()
  531. */
  532. @Override
  533. public boolean delegateCaptionHandling() {
  534. return true;
  535. }
  536. /**
  537. * Updates the user defined, read-only and error style names for the widget
  538. * based the shared state. User defined style names are prefixed with the
  539. * primary style name of the widget returned by {@link #getWidget()}
  540. * <p>
  541. * This method can be overridden to provide additional style names for the
  542. * component, for example see {@code AbstractFieldConnector}
  543. * </p>
  544. */
  545. protected void updateWidgetStyleNames() {
  546. Profiler.enter("AbstractComponentConnector.updateWidgetStyleNames");
  547. AbstractComponentState state = getState();
  548. String primaryStyleName = getWidget().getStylePrimaryName();
  549. // Set the core 'v' style name for the widget
  550. setWidgetStyleName(StyleConstants.UI_WIDGET, true);
  551. // add / remove error style name
  552. setWidgetStyleNameWithPrefix(primaryStyleName, StyleConstants.ERROR_EXT,
  553. null != state.errorMessage);
  554. // add additional user defined style names as class names, prefixed with
  555. // component default class name. remove nonexistent style names.
  556. // Remove all old stylenames
  557. for (int i = 0; i < styleNames.length(); i++) {
  558. String oldStyle = styleNames.get(i);
  559. setWidgetStyleName(oldStyle, false);
  560. setWidgetStyleNameWithPrefix(primaryStyleName + "-", oldStyle,
  561. false);
  562. }
  563. styleNames.setLength(0);
  564. if (ComponentStateUtil.hasStyles(state)) {
  565. // add new style names
  566. for (String newStyle : state.styles) {
  567. setWidgetStyleName(newStyle, true);
  568. setWidgetStyleNameWithPrefix(primaryStyleName + "-", newStyle,
  569. true);
  570. styleNames.push(newStyle);
  571. }
  572. }
  573. if (state.primaryStyleName != null
  574. && !state.primaryStyleName.equals(primaryStyleName)) {
  575. /*
  576. * We overwrite the widgets primary stylename if state defines a
  577. * primary stylename. This has to be done after updating other
  578. * styles to be sure the dependent styles are updated correctly.
  579. */
  580. getWidget().setStylePrimaryName(state.primaryStyleName);
  581. }
  582. // set required style name if components supports that
  583. if (this instanceof HasRequiredIndicator) {
  584. getWidget().setStyleName(StyleConstants.REQUIRED,
  585. ((HasRequiredIndicator) this).isRequiredIndicatorVisible());
  586. }
  587. Profiler.leave("AbstractComponentConnector.updateWidgetStyleNames");
  588. }
  589. /**
  590. * This is used to add / remove state related style names from the widget.
  591. * <p>
  592. * Override this method for example if the style name given here should be
  593. * updated in another widget in addition to the one returned by the
  594. * {@link #getWidget()}.
  595. * </p>
  596. *
  597. * @param styleName
  598. * the style name to be added or removed
  599. * @param add
  600. * <code>true</code> to add the given style, <code>false</code>
  601. * to remove it
  602. */
  603. protected void setWidgetStyleName(String styleName, boolean add) {
  604. getWidget().setStyleName(styleName, add);
  605. }
  606. /**
  607. * This is used to add / remove state related prefixed style names from the
  608. * widget.
  609. * <p>
  610. * Override this method if the prefixed style name given here should be
  611. * updated in another widget in addition to the one returned by the
  612. * <code>Connector</code>'s {@link #getWidget()}, or if the prefix should be
  613. * different. For example see
  614. * {@link com.vaadin.client.ui.datefield.TextualDateConnector#setWidgetStyleNameWithPrefix(String, String, boolean)}
  615. * </p>
  616. *
  617. * @param styleName
  618. * the style name to be added or removed
  619. * @param add
  620. * <code>true</code> to add the given style, <code>false</code>
  621. * to remove it
  622. * @deprecated This will be removed once styles are no longer added with
  623. * prefixes.
  624. */
  625. @Deprecated
  626. protected void setWidgetStyleNameWithPrefix(String prefix, String styleName,
  627. boolean add) {
  628. if (!styleName.startsWith("-")) {
  629. if (!prefix.endsWith("-")) {
  630. prefix += "-";
  631. }
  632. } else {
  633. if (prefix.endsWith("-")) {
  634. styleName.replaceFirst("-", "");
  635. }
  636. }
  637. getWidget().setStyleName(prefix + styleName, add);
  638. }
  639. @Override
  640. public LayoutManager getLayoutManager() {
  641. return LayoutManager.get(getConnection());
  642. }
  643. @Override
  644. public void updateEnabledState(boolean enabledState) {
  645. super.updateEnabledState(enabledState);
  646. setWidgetEnabled(isEnabled());
  647. }
  648. @Override
  649. public void onUnregister() {
  650. super.onUnregister();
  651. // Show an error if widget is still attached to DOM. It should never be
  652. // at this point.
  653. if (getWidget() != null && getWidget().isAttached()) {
  654. getWidget().removeFromParent();
  655. VConsole.error(
  656. "Widget is still attached to the DOM after the connector ("
  657. + Util.getConnectorString(this)
  658. + ") has been unregistered. Widget was removed.");
  659. }
  660. }
  661. @Override
  662. public TooltipInfo getTooltipInfo(Element element) {
  663. return new TooltipInfo(getState().description,
  664. getState().descriptionContentMode, getState().errorMessage);
  665. }
  666. @Override
  667. public boolean hasTooltip() {
  668. // Normally, there is a tooltip if description or errorMessage is set
  669. AbstractComponentState state = getState();
  670. if (state.description != null && !state.description.equals("")) {
  671. return true;
  672. } else if (state.errorMessage != null
  673. && !state.errorMessage.equals("")) {
  674. return true;
  675. } else {
  676. return false;
  677. }
  678. }
  679. /**
  680. * Gets the URI of the icon set for this component.
  681. *
  682. * @return the URI of the icon, or <code>null</code> if no icon has been
  683. * defined.
  684. */
  685. protected String getIconUri() {
  686. return getResourceUrl(ComponentConstants.ICON_RESOURCE);
  687. }
  688. /**
  689. * Gets the icon set for this component.
  690. *
  691. * @return the icon, or <code>null</code> if no icon has been defined.
  692. */
  693. protected Icon getIcon() {
  694. return getConnection().getIcon(getIconUri());
  695. }
  696. /*
  697. * (non-Javadoc)
  698. *
  699. * @see com.vaadin.client.ComponentConnector#flush()
  700. */
  701. @Override
  702. public void flush() {
  703. // No generic implementation. Override if needed
  704. }
  705. @Override
  706. public boolean isErrorIndicatorVisible() {
  707. return getState().errorMessage != null;
  708. }
  709. }