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.

VLink.java 5.7KB

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