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.

ActiveLink.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package com.vaadin.demo.sampler;
  2. import java.io.Serializable;
  3. import java.lang.reflect.Method;
  4. import java.util.HashSet;
  5. import java.util.Map;
  6. import com.vaadin.terminal.PaintException;
  7. import com.vaadin.terminal.PaintTarget;
  8. import com.vaadin.terminal.Resource;
  9. import com.vaadin.ui.Component;
  10. import com.vaadin.ui.Link;
  11. import com.vaadin.ui.Button.ClickEvent;
  12. import com.vaadin.ui.Button.ClickListener;
  13. @SuppressWarnings("serial")
  14. public class ActiveLink extends Link {
  15. private static final String TAG = "activelink";
  16. private static final Method LINK_FOLLOWED_METHOD;
  17. private HashSet listeners = new HashSet();
  18. public ActiveLink() {
  19. super();
  20. }
  21. public ActiveLink(String caption, Resource resource, String targetName,
  22. int width, int height, int border) {
  23. super(caption, resource, targetName, width, height, border);
  24. }
  25. public ActiveLink(String caption, Resource resource) {
  26. super(caption, resource);
  27. }
  28. @Override
  29. public String getTag() {
  30. return TAG;
  31. }
  32. static {
  33. try {
  34. LINK_FOLLOWED_METHOD = LinkActivatedListener.class
  35. .getDeclaredMethod("linkActivated",
  36. new Class[] { LinkActivatedEvent.class });
  37. } catch (final java.lang.NoSuchMethodException e) {
  38. // This should never happen
  39. throw new java.lang.RuntimeException(
  40. "Internal error finding methods in ActiveLink");
  41. }
  42. }
  43. /**
  44. * Adds the link activated listener.
  45. *
  46. * @param listener
  47. * the Listener to be added.
  48. */
  49. public void addListener(LinkActivatedListener listener) {
  50. listeners.add(listener);
  51. addListener(LinkActivatedEvent.class, listener, LINK_FOLLOWED_METHOD);
  52. if (listeners.size() == 1) {
  53. requestRepaint();
  54. }
  55. }
  56. /**
  57. * Removes the link activated listener.
  58. *
  59. * @param listener
  60. * the Listener to be removed.
  61. */
  62. public void removeListener(ClickListener listener) {
  63. listeners.remove(listener);
  64. removeListener(ClickEvent.class, listener, LINK_FOLLOWED_METHOD);
  65. if (listeners.size() == 0) {
  66. requestRepaint();
  67. }
  68. }
  69. /**
  70. * Emits the options change event.
  71. */
  72. protected void fireClick(boolean linkOpened) {
  73. fireEvent(new LinkActivatedEvent(this, linkOpened));
  74. }
  75. @Override
  76. public void paintContent(PaintTarget target) throws PaintException {
  77. super.paintContent(target);
  78. if (listeners.size() > 0) {
  79. target.addVariable(this, "activated", false);
  80. target.addVariable(this, "opened", false);
  81. }
  82. }
  83. @Override
  84. public void changeVariables(Object source, Map variables) {
  85. super.changeVariables(source, variables);
  86. if (!isReadOnly() && variables.containsKey("activated")) {
  87. final Boolean activated = (Boolean) variables.get("activated");
  88. final Boolean opened = (Boolean) variables.get("opened");
  89. if (activated != null && activated.booleanValue() && !isReadOnly()) {
  90. fireClick((opened != null && opened.booleanValue() ? true
  91. : false));
  92. }
  93. }
  94. }
  95. public class LinkActivatedEvent extends Component.Event {
  96. private boolean linkOpened;
  97. /**
  98. * New instance of text change event.
  99. *
  100. * @param source
  101. * the Source of the event.
  102. */
  103. public LinkActivatedEvent(Component source, boolean linkOpened) {
  104. super(source);
  105. this.linkOpened = linkOpened;
  106. }
  107. /**
  108. * Gets the ActiveLink where the event occurred.
  109. *
  110. * @return the Source of the event.
  111. */
  112. public ActiveLink getActiveLink() {
  113. return (ActiveLink) getSource();
  114. }
  115. /**
  116. * Indicates whether or not the link was opened on the client, i.e in a
  117. * new window/tab. If the link was not opened, the listener should react
  118. * to the event and "do something", otherwise the link does nothing.
  119. *
  120. * @return true if the link was opened on the client
  121. */
  122. public boolean isLinkOpened() {
  123. return linkOpened;
  124. }
  125. }
  126. /**
  127. * ActiveLink click listener
  128. */
  129. public interface LinkActivatedListener extends Serializable {
  130. /**
  131. * ActiveLink has been activated.
  132. *
  133. * @param event
  134. * ActiveLink click event.
  135. */
  136. public void linkActivated(LinkActivatedEvent event);
  137. }
  138. }