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.

FontIcon.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.http.client.URL;
  18. import com.google.gwt.user.client.DOM;
  19. import com.vaadin.shared.ApplicationConstants;
  20. /**
  21. * A font-based icon implementation.
  22. * <p>
  23. * The icon represents a specific character (identified by codepoint,
  24. * {@link #getCodepoint()}, {@link #setCodepoint(int)}) within a specific font
  25. * (identified by font-family, {@link #getFontFamily()},
  26. * {@link #setFontFamily(String)}).
  27. * </p>
  28. *
  29. * @since 7.2
  30. * @author Vaadin Ltd
  31. */
  32. public class FontIcon extends Icon {
  33. private int codepoint;
  34. private String fontFamily;
  35. public FontIcon() {
  36. setElement(DOM.createSpan());
  37. setStyleName(CLASSNAME);
  38. }
  39. @Override
  40. public void setUri(String uri) {
  41. String[] parts = uri.substring(
  42. ApplicationConstants.FONTICON_PROTOCOL_PREFIX.length()).split(
  43. "/");
  44. setFontFamily(URL.decode(parts[0]));
  45. setCodepoint(Integer.parseInt(parts[1], 16));
  46. }
  47. /**
  48. * Not implemeted for {@link FontIcon} yet.
  49. *
  50. * @see com.vaadin.client.ui.Icon#setAlternateText(java.lang.String)
  51. */
  52. @Override
  53. public void setAlternateText(String alternateText) {
  54. // TODO this is mostly for WAI-ARIA and should be implemented in an
  55. // appropriate way.
  56. }
  57. /**
  58. * Sets the font-family from which this icon comes. Use
  59. * {@link #setCodepoint(int)} to specify a particular icon (character)
  60. * within the font.
  61. *
  62. * @param fontFamily
  63. * font-family name
  64. */
  65. protected void setFontFamily(String fontFamily) {
  66. if (this.fontFamily != null) {
  67. removeStyleName(getFontStylename());
  68. }
  69. this.fontFamily = fontFamily;
  70. if (fontFamily != null) {
  71. addStyleName(getFontStylename());
  72. }
  73. }
  74. /**
  75. * Gets the font-family from which this icon comes. Use
  76. * {@link #getCodepoint()} to find out which particular icon (character)
  77. * within the font this is.
  78. *
  79. * @return font-family name
  80. */
  81. public String getFontFamily() {
  82. return fontFamily;
  83. }
  84. /**
  85. * Sets the codepoint indicating which particular icon (character) within
  86. * the font-family this is.
  87. *
  88. * @param codepoint
  89. */
  90. protected void setCodepoint(int codepoint) {
  91. this.codepoint = codepoint;
  92. getElement().setInnerText(new String(Character.toChars(codepoint)));
  93. }
  94. /**
  95. * Gets the codepoint indicating which particular icon (character) within
  96. * the font-family this is.
  97. *
  98. * @return
  99. */
  100. public int getCodepoint() {
  101. return codepoint;
  102. }
  103. /**
  104. * Get the font-family based stylename used to apply the font-family.
  105. *
  106. * @since 7.2
  107. * @return stylename used to apply font-family
  108. */
  109. protected String getFontStylename() {
  110. if (fontFamily == null) {
  111. return null;
  112. }
  113. return fontFamily.replace(' ', '-');
  114. }
  115. /**
  116. * Checks whether or not the given uri is a font icon uri. Does not check
  117. * whether or not the font icon is available and can be rendered.
  118. *
  119. * @since 7.2
  120. * @param uri
  121. * @return true if it's a fonticon uri
  122. */
  123. public static boolean isFontIconUri(String uri) {
  124. return uri != null
  125. && uri.startsWith(ApplicationConstants.FONTICON_PROTOCOL_PREFIX);
  126. }
  127. @Override
  128. public String getUri() {
  129. if (fontFamily == null) {
  130. return null;
  131. }
  132. return ApplicationConstants.FONTICON_PROTOCOL_PREFIX + fontFamily + "/"
  133. + codepoint;
  134. }
  135. }