Skip to content

Commit

Permalink
Fix for declarative FontIcon support (#17275)
Browse files Browse the repository at this point in the history
Change-Id: I5d61ed7003811f95bba4ded71937bb08742936c5
  • Loading branch information
jdahlstrom authored and Vaadin Code Review committed May 6, 2015
1 parent 0fcac70 commit 49ded4a
Show file tree
Hide file tree
Showing 8 changed files with 420 additions and 33 deletions.
25 changes: 21 additions & 4 deletions server/src/com/vaadin/server/FontAwesome.java
Expand Up @@ -538,7 +538,7 @@ public enum FontAwesome implements FontIcon {
YOUTUBE_PLAY(0XF16A), // YOUTUBE_PLAY(0XF16A), //
YOUTUBE_SQUARE(0XF166); YOUTUBE_SQUARE(0XF166);


private static final String fontFamily = "FontAwesome"; public static final String FONT_FAMILY = "FontAwesome";
private int codepoint; private int codepoint;


FontAwesome(int codepoint) { FontAwesome(int codepoint) {
Expand All @@ -563,7 +563,7 @@ public String getMIMEType() {
*/ */
@Override @Override
public String getFontFamily() { public String getFontFamily() {
return fontFamily; return FontAwesome.FONT_FAMILY;
} }


/* /*
Expand All @@ -578,8 +578,25 @@ public int getCodepoint() {


@Override @Override
public String getHtml() { public String getHtml() {
return "<span class=\"v-icon\" style=\"font-family: " + fontFamily return GenericFontIcon.getHtml(FontAwesome.FONT_FAMILY, codepoint);
+ ";\">&#x" + Integer.toHexString(codepoint) + ";</span>"; }

/**
* Finds an instance of FontAwesome with given codepoint
*
* @since 7.5
* @param codepoint
* @return FontAwesome instance with a specific codepoint or null if there
* isn't any
*/
public static FontAwesome fromCodepoint(final int codepoint) {
for (FontAwesome f : values()) {
if (f.getCodepoint() == codepoint) {
return f;
}
}
throw new IllegalArgumentException("Codepoint " + codepoint
+ " not found in FontAwesome");
} }


} }
144 changes: 144 additions & 0 deletions server/src/com/vaadin/server/GenericFontIcon.java
@@ -0,0 +1,144 @@
/*
* Copyright 2000-2014 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.server;

/**
* A generic implementation of {@link FontIcon} interface
*
* @since
* @author Vaadin Ltd
*/
@SuppressWarnings("serial")
public class GenericFontIcon implements FontIcon {
private final String fontFamily;
private final int codePoint;

/**
* Creates a new instance of GenericFontIcon with given font family and
* codepoint
*
* @param fontFamily
* Name of the type face that is used to display icons
* @param codepoint
* Numerical code point in the font
*/
public GenericFontIcon(String fontFamily, int codepoint) {
this.fontFamily = fontFamily;
codePoint = codepoint;
}

/*
* (non-Javadoc)
*
* @see com.vaadin.server.FontIcon#getFontFamily()
*/
@Override
public String getFontFamily() {
return fontFamily;
}

/*
* (non-Javadoc)
*
* @see com.vaadin.server.Resource#getMIMEType()
*/
@Override
public String getMIMEType() {
throw new UnsupportedOperationException(FontIcon.class.getSimpleName()
+ " should not be used where a MIME type is needed.");
}

/*
* (non-Javadoc)
*
* @see com.vaadin.server.FontIcon#getCodepoint()
*/
@Override
public int getCodepoint() {
return codePoint;
}

/*
* (non-Javadoc)
*
* @see com.vaadin.server.FontIcon#getHtml()
*/
@Override
public String getHtml() {
return getHtml(fontFamily, codePoint);
}

/**
* Utility method for generating HTML that displays an icon from specific
* fontFamiliy with a given codePoint in the font
*
* @since 7.5
* @param fontFamily
* Name of the font family
* @param codePoint
* Icon's character code point in the font
* @return
*/
public static String getHtml(String fontFamily, int codePoint) {
return "<span class=\"v-icon\" style=\"font-family: " + fontFamily
+ ";\">&#x" + Integer.toHexString(codePoint) + ";</span>";
}

/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + codePoint;
result = prime * result
+ ((fontFamily == null) ? 0 : fontFamily.hashCode());
return result;
}

/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof GenericFontIcon)) {
return false;
}
GenericFontIcon other = (GenericFontIcon) obj;
if (codePoint != other.codePoint) {
return false;
}
if (fontFamily == null) {
if (other.fontFamily != null) {
return false;
}
} else if (!fontFamily.equals(other.fontFamily)) {
return false;
}
return true;
}
}
2 changes: 1 addition & 1 deletion server/src/com/vaadin/server/ResourceReference.java
Expand Up @@ -64,7 +64,7 @@ public String getURL() {
String uri = getConnectorResourceBase(prefix, connector); String uri = getConnectorResourceBase(prefix, connector);
return uri; return uri;
} else if (resource instanceof ThemeResource) { } else if (resource instanceof ThemeResource) {
final String uri = "theme://" final String uri = ApplicationConstants.THEME_PROTOCOL_PREFIX
+ ((ThemeResource) resource).getResourceId(); + ((ThemeResource) resource).getResourceId();
return uri; return uri;
} else if (resource instanceof FontIcon) { } else if (resource instanceof FontIcon) {
Expand Down
9 changes: 6 additions & 3 deletions server/src/com/vaadin/ui/declarative/DesignFormatter.java
Expand Up @@ -325,9 +325,8 @@ protected <T> Converter<String, T> findConverterFor(
// component. // component.
return (Converter<String, T>) stringObjectConverter; return (Converter<String, T>) stringObjectConverter;
} }
if (sourceType.isEnum()) {
return (Converter<String, T>) stringEnumConverter; if (converterMap.containsKey(sourceType)) {
} else if (converterMap.containsKey(sourceType)) {
return ((Converter<String, T>) converterMap.get(sourceType)); return ((Converter<String, T>) converterMap.get(sourceType));
} else if (!strict) { } else if (!strict) {
for (Class<?> supported : converterMap.keySet()) { for (Class<?> supported : converterMap.keySet()) {
Expand All @@ -336,6 +335,10 @@ protected <T> Converter<String, T> findConverterFor(
} }
} }
} }

if (sourceType.isEnum()) {
return (Converter<String, T>) stringEnumConverter;
}
return null; return null;
} }


Expand Down

0 comments on commit 49ded4a

Please sign in to comment.