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

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