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.

AbstractExtension.java 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal;
  5. import com.vaadin.terminal.gwt.server.ClientConnector;
  6. /**
  7. * An extension is an entity that is attached to a Component or another
  8. * Extension and independently communicates between client and server.
  9. * <p>
  10. * Extensions can use shared state and RPC in the same way as components.
  11. * <p>
  12. * AbstractExtension adds a mechanism for adding the extension to any Connector
  13. * (extend). To let the Extension determine what kind target it can be added to,
  14. * the extend method is declared as protected.
  15. *
  16. * @author Vaadin Ltd
  17. * @since 7.0.0
  18. */
  19. public abstract class AbstractExtension extends AbstractClientConnector
  20. implements Extension {
  21. private boolean previouslyAttached = false;
  22. /**
  23. * Gets a type that the parent must be an instance of. Override this if the
  24. * extension only support certain targets, e.g. if only TextFields can be
  25. * extended.
  26. *
  27. * @return a type that the parent must be an instance of
  28. */
  29. protected Class<? extends ClientConnector> getSupportedParentType() {
  30. return ClientConnector.class;
  31. }
  32. /**
  33. * Add this extension to the target connector. This method is protected to
  34. * allow subclasses to require a more specific type of target.
  35. *
  36. * @param target
  37. * the connector to attach this extension to
  38. */
  39. protected void extend(AbstractClientConnector target) {
  40. target.addExtension(this);
  41. }
  42. /**
  43. * Remove this extension from its target. After an extension has been
  44. * removed, it can not be attached again.
  45. */
  46. public void removeFromTarget() {
  47. getParent().removeExtension(this);
  48. }
  49. @Override
  50. public void setParent(ClientConnector parent) {
  51. if (previouslyAttached && parent != null) {
  52. throw new IllegalStateException(
  53. "An extension can not be set to extend a new target after getting detached from the previous.");
  54. }
  55. Class<? extends ClientConnector> supportedParentType = getSupportedParentType();
  56. if (parent == null || supportedParentType.isInstance(parent)) {
  57. super.setParent(parent);
  58. previouslyAttached = true;
  59. } else {
  60. throw new IllegalArgumentException(getClass().getName()
  61. + " can only be attached to targets of type "
  62. + supportedParentType.getName() + " but attach to "
  63. + parent.getClass().getName() + " was attempted.");
  64. }
  65. }
  66. }