Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

VLink.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import com.google.gwt.event.dom.client.ClickEvent;
  6. import com.google.gwt.event.dom.client.ClickHandler;
  7. import com.google.gwt.user.client.DOM;
  8. import com.google.gwt.user.client.Element;
  9. import com.google.gwt.user.client.Event;
  10. import com.google.gwt.user.client.Window;
  11. import com.google.gwt.user.client.ui.HTML;
  12. import com.google.gwt.user.client.ui.Widget;
  13. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  14. import com.vaadin.terminal.gwt.client.VPaintableWidget;
  15. import com.vaadin.terminal.gwt.client.UIDL;
  16. import com.vaadin.terminal.gwt.client.Util;
  17. import com.vaadin.terminal.gwt.client.VTooltip;
  18. public class VLink extends HTML implements VPaintableWidget, ClickHandler {
  19. public static final String CLASSNAME = "v-link";
  20. private static final int BORDER_STYLE_DEFAULT = 0;
  21. private static final int BORDER_STYLE_MINIMAL = 1;
  22. private static final int BORDER_STYLE_NONE = 2;
  23. private String src;
  24. private String target;
  25. private int borderStyle = BORDER_STYLE_DEFAULT;
  26. private boolean enabled;
  27. private boolean readonly;
  28. private int targetWidth;
  29. private int targetHeight;
  30. private Element errorIndicatorElement;
  31. private final Element anchor = DOM.createAnchor();
  32. private final Element captionElement = DOM.createSpan();
  33. private Icon icon;
  34. private ApplicationConnection client;
  35. public VLink() {
  36. super();
  37. getElement().appendChild(anchor);
  38. anchor.appendChild(captionElement);
  39. addClickHandler(this);
  40. sinkEvents(VTooltip.TOOLTIP_EVENTS);
  41. setStyleName(CLASSNAME);
  42. }
  43. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  44. // Ensure correct implementation,
  45. // but don't let container manage caption etc.
  46. if (client.updateComponent(this, uidl, false)) {
  47. return;
  48. }
  49. this.client = client;
  50. enabled = uidl.hasAttribute("disabled") ? false : true;
  51. readonly = uidl.hasAttribute("readonly") ? true : false;
  52. if (uidl.hasAttribute("name")) {
  53. target = uidl.getStringAttribute("name");
  54. anchor.setAttribute("target", target);
  55. }
  56. if (uidl.hasAttribute("src")) {
  57. src = client.translateVaadinUri(uidl.getStringAttribute("src"));
  58. anchor.setAttribute("href", src);
  59. }
  60. if (uidl.hasAttribute("border")) {
  61. if ("none".equals(uidl.getStringAttribute("border"))) {
  62. borderStyle = BORDER_STYLE_NONE;
  63. } else {
  64. borderStyle = BORDER_STYLE_MINIMAL;
  65. }
  66. } else {
  67. borderStyle = BORDER_STYLE_DEFAULT;
  68. }
  69. targetHeight = uidl.hasAttribute("targetHeight") ? uidl
  70. .getIntAttribute("targetHeight") : -1;
  71. targetWidth = uidl.hasAttribute("targetWidth") ? uidl
  72. .getIntAttribute("targetWidth") : -1;
  73. // Set link caption
  74. captionElement.setInnerText(uidl.getStringAttribute("caption"));
  75. // handle error
  76. if (uidl.hasAttribute("error")) {
  77. if (errorIndicatorElement == null) {
  78. errorIndicatorElement = DOM.createDiv();
  79. DOM.setElementProperty(errorIndicatorElement, "className",
  80. "v-errorindicator");
  81. }
  82. DOM.insertChild(getElement(), errorIndicatorElement, 0);
  83. } else if (errorIndicatorElement != null) {
  84. DOM.setStyleAttribute(errorIndicatorElement, "display", "none");
  85. }
  86. if (uidl.hasAttribute("icon")) {
  87. if (icon == null) {
  88. icon = new Icon(client);
  89. anchor.insertBefore(icon.getElement(), captionElement);
  90. }
  91. icon.setUri(uidl.getStringAttribute("icon"));
  92. }
  93. }
  94. public void onClick(ClickEvent event) {
  95. if (enabled && !readonly) {
  96. if (target == null) {
  97. target = "_self";
  98. }
  99. String features;
  100. switch (borderStyle) {
  101. case BORDER_STYLE_NONE:
  102. features = "menubar=no,location=no,status=no";
  103. break;
  104. case BORDER_STYLE_MINIMAL:
  105. features = "menubar=yes,location=no,status=no";
  106. break;
  107. default:
  108. features = "";
  109. break;
  110. }
  111. if (targetWidth > 0) {
  112. features += (features.length() > 0 ? "," : "") + "width="
  113. + targetWidth;
  114. }
  115. if (targetHeight > 0) {
  116. features += (features.length() > 0 ? "," : "") + "height="
  117. + targetHeight;
  118. }
  119. if (features.length() > 0) {
  120. // if 'special features' are set, use window.open(), unless
  121. // a modifier key is held (ctrl to open in new tab etc)
  122. Event e = DOM.eventGetCurrentEvent();
  123. if (!e.getCtrlKey() && !e.getAltKey() && !e.getShiftKey()
  124. && !e.getMetaKey()) {
  125. Window.open(src, target, features);
  126. e.preventDefault();
  127. }
  128. }
  129. }
  130. }
  131. @Override
  132. public void onBrowserEvent(Event event) {
  133. final Element target = DOM.eventGetTarget(event);
  134. if (event.getTypeInt() == Event.ONLOAD) {
  135. Util.notifyParentOfSizeChange(this, true);
  136. }
  137. if (client != null) {
  138. client.handleTooltipEvent(event, this);
  139. }
  140. if (target == captionElement || target == anchor
  141. || (icon != null && target == icon.getElement())) {
  142. super.onBrowserEvent(event);
  143. }
  144. if (!enabled) {
  145. event.preventDefault();
  146. }
  147. }
  148. public Widget getWidgetForPaintable() {
  149. return this;
  150. }
  151. }