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.

AbstractFocusable.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.ui;
  17. import com.vaadin.event.FieldEvents.BlurEvent;
  18. import com.vaadin.event.FieldEvents.BlurListener;
  19. import com.vaadin.event.FieldEvents.BlurNotifier;
  20. import com.vaadin.event.FieldEvents.FocusAndBlurServerRpcDecorator;
  21. import com.vaadin.event.FieldEvents.FocusEvent;
  22. import com.vaadin.event.FieldEvents.FocusListener;
  23. import com.vaadin.event.FieldEvents.FocusNotifier;
  24. import com.vaadin.shared.Registration;
  25. import com.vaadin.shared.ui.TabIndexState;
  26. import com.vaadin.ui.Component.Focusable;
  27. /**
  28. * An abstract base class for focusable components. Includes API for setting the
  29. * tab index, programmatic focusing, and adding focus and blur listeners.
  30. *
  31. * @since 7.6
  32. * @author Vaadin Ltd
  33. */
  34. public abstract class AbstractFocusable extends AbstractComponent
  35. implements Focusable, FocusNotifier, BlurNotifier {
  36. protected AbstractFocusable() {
  37. registerRpc(new FocusAndBlurServerRpcDecorator(this, this::fireEvent));
  38. }
  39. @Override
  40. public Registration addBlurListener(BlurListener listener) {
  41. addListener(BlurEvent.EVENT_ID, BlurEvent.class, listener,
  42. BlurListener.blurMethod);
  43. return () -> removeListener(BlurEvent.EVENT_ID, BlurEvent.class,
  44. listener);
  45. }
  46. @Override
  47. @Deprecated
  48. public void removeBlurListener(BlurListener listener) {
  49. removeListener(BlurEvent.EVENT_ID, BlurEvent.class, listener);
  50. }
  51. @Override
  52. public Registration addFocusListener(FocusListener listener) {
  53. addListener(FocusEvent.EVENT_ID, FocusEvent.class, listener,
  54. FocusListener.focusMethod);
  55. return () -> removeListener(FocusEvent.EVENT_ID, FocusEvent.class,
  56. listener);
  57. }
  58. @Override
  59. @Deprecated
  60. public void removeFocusListener(FocusListener listener) {
  61. removeListener(FocusEvent.EVENT_ID, FocusEvent.class, listener);
  62. }
  63. @Override
  64. public void focus() {
  65. super.focus();
  66. }
  67. @Override
  68. public int getTabIndex() {
  69. return getState(false).tabIndex;
  70. }
  71. @Override
  72. public void setTabIndex(int tabIndex) {
  73. getState().tabIndex = tabIndex;
  74. }
  75. @Override
  76. protected TabIndexState getState() {
  77. return (TabIndexState) super.getState();
  78. }
  79. @Override
  80. protected TabIndexState getState(boolean markAsDirty) {
  81. return (TabIndexState) super.getState(markAsDirty);
  82. }
  83. }