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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. * @version @VERSION@
  18. * @since 7.0.0
  19. */
  20. public abstract class AbstractExtension extends AbstractClientConnector
  21. implements Extension {
  22. private boolean previouslyAttached = false;
  23. /**
  24. * Gets a type that the parent must be an instance of. Override this if the
  25. * extension only support certain targets, e.g. if only TextFields can be
  26. * extended.
  27. *
  28. * @return a type that the parent must be an instance of
  29. */
  30. protected Class<? extends ClientConnector> getSupportedParentType() {
  31. return ClientConnector.class;
  32. }
  33. /**
  34. * Add this extension to the target connector. This method is protected to
  35. * allow subclasses to require a more specific type of target.
  36. *
  37. * @param target
  38. * the connector to attach this extension to
  39. */
  40. protected void extend(AbstractClientConnector target) {
  41. target.addExtension(this);
  42. }
  43. /**
  44. * Remove this extension from its target. After an extension has been
  45. * removed, it can not be attached again.
  46. */
  47. public void removeFromTarget() {
  48. getParent().removeExtension(this);
  49. }
  50. @Override
  51. public void setParent(ClientConnector parent) {
  52. if (previouslyAttached && parent != null) {
  53. throw new IllegalStateException(
  54. "An extension can not be set to extend a new target after getting detached from the previous.");
  55. }
  56. Class<? extends ClientConnector> supportedParentType = getSupportedParentType();
  57. if (parent == null || supportedParentType.isInstance(parent)) {
  58. super.setParent(parent);
  59. previouslyAttached = true;
  60. } else {
  61. throw new IllegalArgumentException(getClass().getName()
  62. + " can only be attached to targets of type "
  63. + supportedParentType.getName() + " but attach to "
  64. + parent.getClass().getName() + " was attempted.");
  65. }
  66. }
  67. }