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 4.7KB

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