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.

HSSFHyperlink.java 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.usermodel;
  16. import org.apache.poi.common.usermodel.HyperlinkType;
  17. import org.apache.poi.hssf.record.HyperlinkRecord;
  18. import org.apache.poi.ss.usermodel.Hyperlink;
  19. import org.apache.poi.util.Internal;
  20. /**
  21. * Represents an Excel hyperlink.
  22. */
  23. public class HSSFHyperlink implements Hyperlink {
  24. /**
  25. * Low-level record object that stores the actual hyperlink data
  26. */
  27. final protected HyperlinkRecord record;
  28. /**
  29. * If we create a new hyperlink remember its type
  30. */
  31. final protected HyperlinkType link_type;
  32. /**
  33. * Construct a new hyperlink
  34. *
  35. * This method is internal to be used only by
  36. * {@link HSSFCreationHelper#createHyperlink(int)}.
  37. *
  38. * @param type the type of hyperlink to create
  39. * @deprecated POI 3.15 beta 3
  40. */
  41. @Internal(since="3.15 beta 3")
  42. protected HSSFHyperlink( int type )
  43. {
  44. this(HyperlinkType.forInt(type));
  45. }
  46. /**
  47. * Construct a new hyperlink
  48. *
  49. * This method is internal to be used only by
  50. * {@link HSSFCreationHelper#createHyperlink(HyperlinkType)}.
  51. *
  52. * @param type the type of hyperlink to create
  53. */
  54. @Internal(since="3.15 beta 3")
  55. protected HSSFHyperlink( HyperlinkType type )
  56. {
  57. this.link_type = type;
  58. record = new HyperlinkRecord();
  59. switch(type){
  60. case URL:
  61. case EMAIL:
  62. record.newUrlLink();
  63. break;
  64. case FILE:
  65. record.newFileLink();
  66. break;
  67. case DOCUMENT:
  68. record.newDocumentLink();
  69. break;
  70. default:
  71. throw new IllegalArgumentException("Invalid type: " + type);
  72. }
  73. }
  74. /**
  75. * Initialize the hyperlink by a <code>HyperlinkRecord</code> record
  76. *
  77. * @param record
  78. */
  79. protected HSSFHyperlink( HyperlinkRecord record )
  80. {
  81. this.record = record;
  82. link_type = getType(record);
  83. }
  84. private static HyperlinkType getType(HyperlinkRecord record) {
  85. HyperlinkType link_type;
  86. // Figure out the type
  87. if (record.isFileLink()) {
  88. link_type = HyperlinkType.FILE;
  89. } else if(record.isDocumentLink()) {
  90. link_type = HyperlinkType.DOCUMENT;
  91. } else {
  92. if(record.getAddress() != null &&
  93. record.getAddress().startsWith("mailto:")) {
  94. link_type = HyperlinkType.EMAIL;
  95. } else {
  96. link_type = HyperlinkType.URL;
  97. }
  98. }
  99. return link_type;
  100. }
  101. protected HSSFHyperlink(Hyperlink other) {
  102. if (other instanceof HSSFHyperlink) {
  103. HSSFHyperlink hlink = (HSSFHyperlink) other;
  104. record = hlink.record.clone();
  105. link_type = getType(record);
  106. }
  107. else {
  108. link_type = other.getTypeEnum();
  109. record = new HyperlinkRecord();
  110. setFirstRow(other.getFirstRow());
  111. setFirstColumn(other.getFirstColumn());
  112. setLastRow(other.getLastRow());
  113. setLastColumn(other.getLastColumn());
  114. }
  115. }
  116. /**
  117. * Return the row of the first cell that contains the hyperlink
  118. *
  119. * @return the 0-based row of the cell that contains the hyperlink
  120. */
  121. @Override
  122. public int getFirstRow(){
  123. return record.getFirstRow();
  124. }
  125. /**
  126. * Set the row of the first cell that contains the hyperlink
  127. *
  128. * @param row the 0-based row of the first cell that contains the hyperlink
  129. */
  130. @Override
  131. public void setFirstRow(int row){
  132. record.setFirstRow(row);
  133. }
  134. /**
  135. * Return the row of the last cell that contains the hyperlink
  136. *
  137. * @return the 0-based row of the last cell that contains the hyperlink
  138. */
  139. @Override
  140. public int getLastRow(){
  141. return record.getLastRow();
  142. }
  143. /**
  144. * Set the row of the last cell that contains the hyperlink
  145. *
  146. * @param row the 0-based row of the last cell that contains the hyperlink
  147. */
  148. @Override
  149. public void setLastRow(int row){
  150. record.setLastRow(row);
  151. }
  152. /**
  153. * Return the column of the first cell that contains the hyperlink
  154. *
  155. * @return the 0-based column of the first cell that contains the hyperlink
  156. */
  157. @Override
  158. public int getFirstColumn(){
  159. return record.getFirstColumn();
  160. }
  161. /**
  162. * Set the column of the first cell that contains the hyperlink
  163. *
  164. * @param col the 0-based column of the first cell that contains the hyperlink
  165. */
  166. @Override
  167. public void setFirstColumn(int col){
  168. record.setFirstColumn((short)col);
  169. }
  170. /**
  171. * Return the column of the last cell that contains the hyperlink
  172. *
  173. * @return the 0-based column of the last cell that contains the hyperlink
  174. */
  175. @Override
  176. public int getLastColumn(){
  177. return record.getLastColumn();
  178. }
  179. /**
  180. * Set the column of the last cell that contains the hyperlink
  181. *
  182. * @param col the 0-based column of the last cell that contains the hyperlink
  183. */
  184. @Override
  185. public void setLastColumn(int col){
  186. record.setLastColumn((short)col);
  187. }
  188. /**
  189. * Hyperlink address. Depending on the hyperlink type it can be URL, e-mail, path to a file, etc.
  190. *
  191. * @return the address of this hyperlink
  192. */
  193. @Override
  194. public String getAddress(){
  195. return record.getAddress();
  196. }
  197. public String getTextMark(){
  198. return record.getTextMark();
  199. }
  200. /**
  201. * Convenience method equivalent to {@link #setAddress(String)}
  202. *
  203. * @param textMark the place in worksheet this hyperlink refers to, e.g. 'Target Sheet'!A1'
  204. */
  205. public void setTextMark(String textMark) {
  206. record.setTextMark(textMark);
  207. }
  208. public String getShortFilename(){
  209. return record.getShortFilename();
  210. }
  211. /**
  212. * Convenience method equivalent to {@link #setAddress(String)}
  213. *
  214. * @param shortFilename the path to a file this hyperlink points to, e.g. 'readme.txt'
  215. */
  216. public void setShortFilename(String shortFilename) {
  217. record.setShortFilename(shortFilename);
  218. }
  219. /**
  220. * Hyperlink address. Depending on the hyperlink type it can be URL, e-mail, path to a file, etc.
  221. *
  222. * @param address the address of this hyperlink
  223. */
  224. @Override
  225. public void setAddress(String address){
  226. record.setAddress(address);
  227. }
  228. /**
  229. * Return text label for this hyperlink
  230. *
  231. * @return text to display
  232. */
  233. @Override
  234. public String getLabel(){
  235. return record.getLabel();
  236. }
  237. /**
  238. * Sets text label for this hyperlink
  239. *
  240. * @param label text label for this hyperlink
  241. */
  242. @Override
  243. public void setLabel(String label){
  244. record.setLabel(label);
  245. }
  246. /**
  247. * Return the type of this hyperlink
  248. *
  249. * @return the type of this hyperlink
  250. * @see HyperlinkType#forInt
  251. * @deprecated POI 3.15. Use {@link #getTypeEnum()} instead.
  252. * getType will return a HyperlinkType enum in the future.
  253. */
  254. @Override
  255. public int getType() {
  256. return link_type.getCode();
  257. }
  258. /**
  259. * Return the type of this hyperlink
  260. *
  261. * @return the type of this hyperlink
  262. */
  263. @Override
  264. public HyperlinkType getTypeEnum() {
  265. return link_type;
  266. }
  267. /**
  268. * @return whether the objects have the same HyperlinkRecord
  269. */
  270. @Override
  271. public boolean equals(Object other) {
  272. if (this == other) return true;
  273. if (!(other instanceof HSSFHyperlink)) return false;
  274. HSSFHyperlink otherLink = (HSSFHyperlink) other;
  275. return record == otherLink.record;
  276. }
  277. @Override
  278. public int hashCode() {
  279. return record.hashCode();
  280. }
  281. }