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.

Link.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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.ui;
  17. import java.util.Collection;
  18. import org.jsoup.nodes.Element;
  19. import com.vaadin.server.Resource;
  20. import com.vaadin.shared.ui.BorderStyle;
  21. import com.vaadin.shared.ui.link.LinkConstants;
  22. import com.vaadin.shared.ui.link.LinkState;
  23. import com.vaadin.ui.declarative.DesignAttributeHandler;
  24. import com.vaadin.ui.declarative.DesignContext;
  25. /**
  26. * Link is used to create external or internal URL links.
  27. *
  28. * @author Vaadin Ltd.
  29. * @since 3.0
  30. */
  31. @SuppressWarnings("serial")
  32. public class Link extends AbstractComponent {
  33. /**
  34. * @deprecated As of 7.0, use {@link BorderStyle#NONE} instead
  35. */
  36. @Deprecated
  37. public static final BorderStyle TARGET_BORDER_NONE = BorderStyle.NONE;
  38. /**
  39. * @deprecated As of 7.0, use {@link BorderStyle#MINIMAL} instead
  40. */
  41. @Deprecated
  42. public static final BorderStyle TARGET_BORDER_MINIMAL = BorderStyle.MINIMAL;
  43. /**
  44. * @deprecated As of 7.0, use {@link BorderStyle#DEFAULT} instead
  45. */
  46. @Deprecated
  47. public static final BorderStyle TARGET_BORDER_DEFAULT = BorderStyle.DEFAULT;
  48. /**
  49. * Creates a new link.
  50. */
  51. public Link() {
  52. }
  53. /**
  54. * Creates a new instance of Link.
  55. *
  56. * @param caption
  57. * @param resource
  58. */
  59. public Link(String caption, Resource resource) {
  60. setCaption(caption);
  61. setResource(resource);
  62. }
  63. /**
  64. * Creates a new instance of Link that opens a new window.
  65. *
  66. *
  67. * @param caption
  68. * the Link text.
  69. * @param targetName
  70. * the name of the target window where the link opens to. Empty
  71. * name of null implies that the target is opened to the window
  72. * containing the link.
  73. * @param width
  74. * the Width of the target window.
  75. * @param height
  76. * the Height of the target window.
  77. * @param border
  78. * the Border style of the target window.
  79. *
  80. */
  81. public Link(String caption, Resource resource, String targetName, int width,
  82. int height, BorderStyle border) {
  83. setCaption(caption);
  84. setResource(resource);
  85. setTargetName(targetName);
  86. setTargetWidth(width);
  87. setTargetHeight(height);
  88. setTargetBorder(border);
  89. }
  90. @Override
  91. protected LinkState getState() {
  92. return (LinkState) super.getState();
  93. }
  94. @Override
  95. protected LinkState getState(boolean markAsDirty) {
  96. return (LinkState) super.getState(markAsDirty);
  97. }
  98. /**
  99. * Returns the target window border.
  100. *
  101. * @return the target window border.
  102. */
  103. public BorderStyle getTargetBorder() {
  104. return getState(false).targetBorder;
  105. }
  106. /**
  107. * Returns the target window height or -1 if not set.
  108. *
  109. * @return the target window height.
  110. */
  111. public int getTargetHeight() {
  112. return getState(false).targetHeight < 0 ? -1
  113. : getState(false).targetHeight;
  114. }
  115. /**
  116. * Returns the target window name. Empty name of null implies that the
  117. * target is opened to the window containing the link.
  118. *
  119. * @return the target window name.
  120. */
  121. public String getTargetName() {
  122. return getState(false).target;
  123. }
  124. /**
  125. * Returns the target window width or -1 if not set.
  126. *
  127. * @return the target window width.
  128. */
  129. public int getTargetWidth() {
  130. return getState(false).targetWidth < 0 ? -1
  131. : getState(false).targetWidth;
  132. }
  133. /**
  134. * Sets the border of the target window.
  135. *
  136. * @param targetBorder
  137. * the targetBorder to set.
  138. */
  139. public void setTargetBorder(BorderStyle targetBorder) {
  140. getState().targetBorder = targetBorder;
  141. }
  142. /**
  143. * Sets the target window height.
  144. *
  145. * @param targetHeight
  146. * the targetHeight to set.
  147. */
  148. public void setTargetHeight(int targetHeight) {
  149. getState().targetHeight = targetHeight;
  150. }
  151. /**
  152. * Sets the target window name.
  153. *
  154. * @param targetName
  155. * the targetName to set.
  156. */
  157. public void setTargetName(String targetName) {
  158. getState().target = targetName;
  159. }
  160. /**
  161. * Sets the target window width.
  162. *
  163. * @param targetWidth
  164. * the targetWidth to set.
  165. */
  166. public void setTargetWidth(int targetWidth) {
  167. getState().targetWidth = targetWidth;
  168. }
  169. /**
  170. * Returns the resource this link opens.
  171. *
  172. * @return the Resource.
  173. */
  174. public Resource getResource() {
  175. return getResource(LinkConstants.HREF_RESOURCE);
  176. }
  177. /**
  178. * Sets the resource this link opens.
  179. *
  180. * @param resource
  181. * the resource to set.
  182. */
  183. public void setResource(Resource resource) {
  184. setResource(LinkConstants.HREF_RESOURCE, resource);
  185. }
  186. @Override
  187. public void readDesign(Element design, DesignContext designContext) {
  188. super.readDesign(design, designContext);
  189. if (design.hasAttr("target")) {
  190. setTargetName(DesignAttributeHandler.getFormatter()
  191. .parse(design.attr("target"), String.class));
  192. }
  193. if (design.hasAttr("href")) {
  194. setResource(DesignAttributeHandler.getFormatter()
  195. .parse(design.attr("href"), Resource.class));
  196. }
  197. }
  198. @Override
  199. public void writeDesign(Element design, DesignContext designContext) {
  200. super.writeDesign(design, designContext);
  201. Link def = designContext.getDefaultInstance(this);
  202. DesignAttributeHandler.writeAttribute("target", design.attributes(),
  203. getTargetName(), def.getTargetName(), String.class,
  204. designContext);
  205. DesignAttributeHandler.writeAttribute("href", design.attributes(),
  206. getResource(), def.getResource(), Resource.class,
  207. designContext);
  208. }
  209. @Override
  210. protected Collection<String> getCustomAttributes() {
  211. Collection<String> a = super.getCustomAttributes();
  212. a.add("target-name");
  213. a.add("resource");
  214. // Add custom attributes, see #19107
  215. a.add("target");
  216. a.add("href");
  217. return a;
  218. }
  219. }