如何在vaadin中为自定义图标字体生成字体枚举

时间:2016-01-07 03:43:46

标签: java vaadin

我正在尝试在我的vaadin应用程序中使用自定义图标字体。我遵循this页面中提供的指南。

我遇到的麻烦是为字体生成java enum。

在这个页面中,它说,

  

(请注意,您可以从中的图标列表轻松生成枚举   从IcoMoon下载的zip。)

但是我不知道如何用我下载的icomoon zip文件生成这个枚举。可能是我错过了页面假定其读者所拥有的一些基本知识。

有人可以告诉我如何生成图标枚举吗?

1 个答案:

答案 0 :(得分:3)

我遇到了同样的挑战并为此编写了一个类,它自动将selection.js从IcoMoon zip转换为完全和完整的Vaadin兼容的Java-enum; - )

随意重复使用。它只需要来自https://code.google.com/archive/p/json-simple/downloads

的json-simple jar
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class JSONTools {

    public static void convertIcoMoonJSON2EnumClass(String filename) {

        JSONParser parser = new JSONParser();

        StringBuilder returnTxt = new StringBuilder();

        returnTxt.append("public enum IcoMoon implements FontIcon {\n\n");

        try {

            Object obj = parser.parse(new FileReader(filename));

            JSONObject jsonObject = (JSONObject) obj;
            // loop array
            JSONArray msg = (JSONArray) jsonObject.get("icons");
            Iterator<JSONObject> iterator = msg.iterator();
            int i = 0;
            while (iterator.hasNext()) {
                JSONObject iterator_current = iterator.next();

                JSONObject icon = (JSONObject) iterator_current.get("icon");
                // System.out.println(icon);

                JSONArray tags = (JSONArray) icon.get("tags");
                returnTxt.append(((String) tags.get(0)).toUpperCase().replace("-", "_"));

                returnTxt.append("(");

                JSONObject properties = (JSONObject) iterator_current.get("properties");
                long code = (Long) properties.get("code");
                returnTxt.append(code);
                returnTxt.append(")");

                returnTxt.append(",\n");

                // System.out.println(icon);
            }
            returnTxt.append(";\n");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        returnTxt.append("\r\n" + 
                "   private final int codepoint;\r\n" + 
                "   // This must match (S)CSS\r\n" + 
                "   private final String fontFamily = \"IcoMoon\";\r\n" + 
                "\r\n" + 
                "   IcoMoon(int codepoint) {\r\n" + 
                "       this.codepoint = codepoint;\r\n" + 
                "   }\r\n" + 
                "\r\n" + 
                "   @Override\r\n" + 
                "   public String getFontFamily() {\r\n" + 
                "       return fontFamily;\r\n" + 
                "   }\r\n" + 
                "\r\n" + 
                "   @Override\r\n" + 
                "   public int getCodepoint() {\r\n" + 
                "       return codepoint;\r\n" + 
                "   }\r\n" + 
                "\r\n" + 
                "   @Override\r\n" + 
                "   public String getHtml() {\r\n" + 
                "       return \"<span class=\\\"v-icon IcoMoon\\\">&#x\" + Integer.toHexString(codepoint) + \";</span>\";\r\n" + 
                "   }\r\n" + 
                "\r\n" + 
                "   @Override\r\n" + 
                "   public String getMIMEType() {\r\n" + 
                "       // Font icons are not real resources\r\n" + 
                "       throw new UnsupportedOperationException(\r\n" + 
                "               FontIcon.class.getSimpleName() + \" should not be used where a MIME type is needed.\");\r\n" + 
                "   }\r\n" + 
                "\r\n" + 
                "}");

        System.out.println(returnTxt);

    }

    public static void main(String[] args) {
        convertIcoMoonJSON2EnumClass("path_to_your_selection.json");
    }

}