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

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