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.

GenericFontIcon.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.server;
  17. /**
  18. * A generic implementation of {@link FontIcon} interface.
  19. *
  20. * @since 7.5.0
  21. * @author Vaadin Ltd
  22. */
  23. @SuppressWarnings("serial")
  24. public class GenericFontIcon implements FontIcon {
  25. private final String fontFamily;
  26. private final int codePoint;
  27. /**
  28. * Creates a new instance of GenericFontIcon with given font family and
  29. * codepoint.
  30. *
  31. * @param fontFamily
  32. * Name of the type face that is used to display icons
  33. * @param codepoint
  34. * Numerical code point in the font
  35. */
  36. public GenericFontIcon(String fontFamily, int codepoint) {
  37. this.fontFamily = fontFamily;
  38. codePoint = codepoint;
  39. }
  40. /*
  41. * (non-Javadoc)
  42. *
  43. * @see com.vaadin.server.FontIcon#getFontFamily()
  44. */
  45. @Override
  46. public String getFontFamily() {
  47. return fontFamily;
  48. }
  49. /*
  50. * (non-Javadoc)
  51. *
  52. * @see com.vaadin.server.Resource#getMIMEType()
  53. */
  54. @Override
  55. public String getMIMEType() {
  56. throw new UnsupportedOperationException(FontIcon.class.getSimpleName()
  57. + " should not be used where a MIME type is needed.");
  58. }
  59. /*
  60. * (non-Javadoc)
  61. *
  62. * @see com.vaadin.server.FontIcon#getCodepoint()
  63. */
  64. @Override
  65. public int getCodepoint() {
  66. return codePoint;
  67. }
  68. /*
  69. * (non-Javadoc)
  70. *
  71. * @see com.vaadin.server.FontIcon#getHtml()
  72. */
  73. @Override
  74. public String getHtml() {
  75. return getHtml(fontFamily, codePoint);
  76. }
  77. /**
  78. * Utility method for generating HTML that displays an icon from specific
  79. * fontFamiliy with a given codePoint in the font.
  80. *
  81. * @param fontFamily
  82. * Name of the font family
  83. * @param codePoint
  84. * Icon's character code point in the font
  85. * @return
  86. */
  87. public static String getHtml(String fontFamily, int codePoint) {
  88. return "<span class=\"v-icon\" style=\"font-family: " + fontFamily
  89. + ";\">&#x" + Integer.toHexString(codePoint) + ";</span>";
  90. }
  91. /*
  92. * (non-Javadoc)
  93. *
  94. * @see java.lang.Object#hashCode()
  95. */
  96. @Override
  97. public int hashCode() {
  98. final int prime = 31;
  99. int result = 1;
  100. result = prime * result + codePoint;
  101. result = prime * result
  102. + ((fontFamily == null) ? 0 : fontFamily.hashCode());
  103. return result;
  104. }
  105. /*
  106. * (non-Javadoc)
  107. *
  108. * @see java.lang.Object#equals(java.lang.Object)
  109. */
  110. @Override
  111. public boolean equals(Object obj) {
  112. if (this == obj) {
  113. return true;
  114. }
  115. if (obj == null) {
  116. return false;
  117. }
  118. if (!(obj instanceof GenericFontIcon)) {
  119. return false;
  120. }
  121. GenericFontIcon other = (GenericFontIcon) obj;
  122. if (codePoint != other.codePoint) {
  123. return false;
  124. }
  125. if (fontFamily == null) {
  126. if (other.fontFamily != null) {
  127. return false;
  128. }
  129. } else if (!fontFamily.equals(other.fontFamily)) {
  130. return false;
  131. }
  132. return true;
  133. }
  134. }