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

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