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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright 2000-2018 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. return addListener(BlurEvent.EVENT_ID, BlurEvent.class, listener,
  42. BlurListener.blurMethod);
  43. }
  44. @Override
  45. public Registration addFocusListener(FocusListener listener) {
  46. return addListener(FocusEvent.EVENT_ID, FocusEvent.class, listener,
  47. FocusListener.focusMethod);
  48. }
  49. @Override
  50. public void focus() {
  51. super.focus();
  52. }
  53. @Override
  54. public int getTabIndex() {
  55. return getState(false).tabIndex;
  56. }
  57. @Override
  58. public void setTabIndex(int tabIndex) {
  59. getState().tabIndex = tabIndex;
  60. }
  61. @Override
  62. protected TabIndexState getState() {
  63. return (TabIndexState) super.getState();
  64. }
  65. @Override
  66. protected TabIndexState getState(boolean markAsDirty) {
  67. return (TabIndexState) super.getState(markAsDirty);
  68. }
  69. }