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.8KB

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