Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

VLink.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright 2000-2018 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.client.ui;
  17. import com.google.gwt.dom.client.Element;
  18. import com.google.gwt.event.dom.client.ClickEvent;
  19. import com.google.gwt.event.dom.client.ClickHandler;
  20. import com.google.gwt.user.client.DOM;
  21. import com.google.gwt.user.client.Event;
  22. import com.google.gwt.user.client.Window;
  23. import com.google.gwt.user.client.ui.HTML;
  24. import com.google.gwt.user.client.ui.HasEnabled;
  25. import com.vaadin.client.Util;
  26. import com.vaadin.client.WidgetUtil.ErrorUtil;
  27. import com.vaadin.shared.ui.BorderStyle;
  28. public class VLink extends HTML
  29. implements ClickHandler, HasEnabled, HasErrorIndicatorElement {
  30. public static final String CLASSNAME = "v-link";
  31. @Deprecated
  32. protected static final BorderStyle BORDER_STYLE_DEFAULT = BorderStyle.DEFAULT;
  33. @Deprecated
  34. protected static final BorderStyle BORDER_STYLE_MINIMAL = BorderStyle.MINIMAL;
  35. @Deprecated
  36. protected static final BorderStyle BORDER_STYLE_NONE = BorderStyle.NONE;
  37. /** For internal use only. May be removed or replaced in the future. */
  38. public String src;
  39. /** For internal use only. May be removed or replaced in the future. */
  40. public String target;
  41. /** For internal use only. May be removed or replaced in the future. */
  42. public BorderStyle borderStyle = BorderStyle.DEFAULT;
  43. /** For internal use only. May be removed or replaced in the future. */
  44. public boolean enabled;
  45. /** For internal use only. May be removed or replaced in the future. */
  46. public int targetWidth;
  47. /** For internal use only. May be removed or replaced in the future. */
  48. public int targetHeight;
  49. /** For internal use only. May be removed or replaced in the future. */
  50. private Element errorIndicatorElement;
  51. /** For internal use only. May be removed or replaced in the future. */
  52. public final Element anchor = DOM.createAnchor();
  53. /** For internal use only. May be removed or replaced in the future. */
  54. public final Element captionElement = DOM.createSpan();
  55. /** For internal use only. May be removed or replaced in the future. */
  56. public Icon icon;
  57. public VLink() {
  58. super();
  59. getElement().appendChild(anchor);
  60. anchor.appendChild(captionElement);
  61. addClickHandler(this);
  62. setStyleName(CLASSNAME);
  63. }
  64. @Override
  65. public void onClick(ClickEvent event) {
  66. if (enabled) {
  67. if (target == null) {
  68. target = "_self";
  69. }
  70. String features;
  71. switch (borderStyle) {
  72. case NONE:
  73. features = "menubar=no,location=no,status=no";
  74. break;
  75. case MINIMAL:
  76. features = "menubar=yes,location=no,status=no";
  77. break;
  78. default:
  79. features = "";
  80. break;
  81. }
  82. if (targetWidth > 0) {
  83. features += (features.isEmpty() ? "" : ",") + "width="
  84. + targetWidth;
  85. }
  86. if (targetHeight > 0) {
  87. features += (features.isEmpty() ? "" : ",") + "height="
  88. + targetHeight;
  89. }
  90. if (!features.isEmpty()) {
  91. // if 'special features' are set, use window.open(), unless
  92. // a modifier key is held (ctrl to open in new tab etc)
  93. Event e = DOM.eventGetCurrentEvent();
  94. if (!e.getCtrlKey() && !e.getAltKey() && !e.getShiftKey()
  95. && !e.getMetaKey()) {
  96. Window.open(src, target, features);
  97. e.preventDefault();
  98. }
  99. }
  100. }
  101. }
  102. @Override
  103. public void onBrowserEvent(Event event) {
  104. final Element target = DOM.eventGetTarget(event);
  105. if (event.getTypeInt() == Event.ONLOAD) {
  106. Util.notifyParentOfSizeChange(this, true);
  107. }
  108. if (target == captionElement || target == anchor
  109. || (icon != null && target == icon.getElement())) {
  110. super.onBrowserEvent(event);
  111. }
  112. if (!enabled) {
  113. event.preventDefault();
  114. }
  115. }
  116. @Override
  117. public boolean isEnabled() {
  118. return enabled;
  119. }
  120. @Override
  121. public void setEnabled(boolean enabled) {
  122. this.enabled = enabled;
  123. }
  124. @Override
  125. public Element getErrorIndicatorElement() {
  126. return errorIndicatorElement;
  127. }
  128. @Override
  129. public void setErrorIndicatorElementVisible(boolean visible) {
  130. if (visible) {
  131. if (errorIndicatorElement == null) {
  132. errorIndicatorElement = ErrorUtil.createErrorIndicatorElement();
  133. getElement().insertFirst(errorIndicatorElement);
  134. }
  135. } else if (errorIndicatorElement != null) {
  136. getElement().removeChild(errorIndicatorElement);
  137. errorIndicatorElement = null;
  138. }
  139. }
  140. }