IconsFactory need a nicer generated HTML file

This forum is used by users to request and discuss new product features. Please do not use this forum for technical support including bug reports.

Moderator: JIDE Support

Forum rules
Product suggestions only. Please do not use this forum for technical support including bug reports.

IconsFactory need a nicer generated HTML file

Postby zartc » Fri Sep 11, 2009 4:31 am

Hi all,

Don't you feel the need for a better/nicer HTML page generated from IconsFactory ?
I did, so I helped myself and extracted the code from IconsFactory to create this nice little class (below) that generate a more modern HTML page. Nothing spectacular, just a better/nicer generated HTML page.

Hope this please someone.

:arrow: JIDESoft, you are welcome to copy the code and reintegrate it into the JIDE framework. :)

Code: Select all
package wathever;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.StringTokenizer;


/**
 * Inspired from {@link com.jidesoft.icons.IconsFactory#generateHTML(Class)} but
 * generates a better/nicer html file.
 */
public class IconFactoryHtmlGenerator {
   /**
    * Generates HTML that lists all icons in IconsFactory.
    *
    * @param clazz the IconsFactory class
    */
   public static void generateHTML(Class<?> clazz) {
      String fullClassName = clazz.getName();
      String className = getClassName(fullClassName);
      File file = new File(fullClassName + ".html");

      try {
         FileWriter writer = new FileWriter(file);
         writer.write("<html>\n");
         writer.write("<body>\n");
         writer.write("<h2>Icons in " + fullClassName + "</h2>\n");
         writer.write("<p style=\"font: smaller;color: gray;\">Generated by JIDE Icons</p>");
         writer.write("<ul style=\"color: maroon;\">\n");
         writer.write("<li>If you cannot view the images in this page, make sure the file is in the same directory as " + className
               + ".java</li>\n");
         writer.write("<li>To get a particular icon in your code, call &nbsp;&nbsp;<code>"
               + className
               + ".getImageIcon(<i>FULL_CONSTANT_NAME</i>)</code> replacing <code><i>FULL_CONSTANT_NAME</i></code> with the actual full constant name as in the table below</li>");
         writer.write("</ul>\n");
         generate(clazz, writer, className);
         writer.write("</body>\n");
         writer.write("</html>\n");
         writer.close();
         System.out.println("File was generated at \"" + file.getAbsolutePath() + "\". Please copy it to the same directory as "
               + className + ".java");
      }
      catch(IOException e) {
         System.err.println(e);
      }
   }

   private static String getClassName(String fullName) {
      int last = fullName.lastIndexOf(".");
      if(last != -1) {
         fullName = fullName.substring(last + 1);
      }
      StringTokenizer tokenizer = new StringTokenizer(fullName, "$");
      StringBuffer buffer = new StringBuffer();
      while(tokenizer.hasMoreTokens()) {
         buffer.append(tokenizer.nextToken());
         buffer.append(".");
      }
      return buffer.substring(0, buffer.length() - 1);
   }

   private static void generate(Class<?> aClass, FileWriter writer, String prefix) throws IOException {
      Class<?>[] classes = aClass.getDeclaredClasses();
      // don't know why but the order is exactly the reverse of the order of definitions.
      for(int i = classes.length - 1; i >= 0; i--) {
         Class<?> clazz = classes[i];
         generate(clazz, writer, getClassName(clazz.getName()));
      }

      Field[] fields = aClass.getFields();
      writer.write("<label style=\"font-size: medium;font-weight: bold;color: green;\">" + prefix + "</label>\n");
      writer.write("<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\" bordercolor=\"#CCCCCC\" width=\"100%\">\n");
      writer.write("<thead style=\"color: navy\">\n");
      writer.write("<tr>\n");
      writer.write("<th>Name</th>\n");
      writer.write("<th>Image</th>\n");
      writer.write("<th>File Name</th>\n");
      writer.write("<th>Full Constant Name</th>\n");
      writer.write("</tr>\n");
      writer.write("</thead>\n");
      writer.write("<tbody>\n");
      for(Field field : fields) {
         try {
            Object name = field.getName();
            Object value = field.get(aClass);
            writer.write("<tr>\n");
            writer.write("<td><code>" + name + "</code></td>\n");
            writer.write("<td style=\"text-align: center;\"><img src=\"" + value + "\"></td>\n");
            writer.write("<td>" + value + "</td>\n");
            writer.write("<td><code>" + prefix + "." + name + "</code></td>\n");
            writer.write("</tr>\n");
         }
         catch(IllegalArgumentException e) {
            e.printStackTrace();
         }
         catch(IllegalAccessException e) {
            e.printStackTrace();
         }
      }
      writer.write("</tbody>\n");
      writer.write("</table>\n");
      writer.write("<p />\n");
   }
}

/* EOF */
zartc
 
Posts: 19
Joined: Tue Dec 09, 2008 4:23 am

Re: IconsFactory need a nicer generated HTML file

Postby JIDE Support » Fri Sep 11, 2009 10:52 am

Thanks for sharing :)
JIDE Software Technical Support Team
JIDE Support
Site Admin
 
Posts: 37219
Joined: Sun Sep 14, 2003 10:49 am


Return to Product Suggestions

Who is online

Users browsing this forum: No registered users and 8 guests

cron