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

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