gov.nih.nci.cma.web.graphing.LegendCreator.java Source code

Java tutorial

Introduction

Here is the source code for gov.nih.nci.cma.web.graphing.LegendCreator.java

Source

/*L
 *  Copyright SAIC
 *
 *  Distributed under the OSI-approved BSD 3-Clause License.
 *  See http://ncip.github.com/cma/LICENSE.txt for details.
 */

package gov.nih.nci.cma.web.graphing;

import java.awt.Color;

import org.jfree.chart.LegendItemCollection;

public class LegendCreator {

    /**
     * creates and HTML legend and returns a string of HTML
     * @param lic - a jFreechart LegendItemCollection
     * @return
     */
    public static String buildLegend(LegendItemCollection lic, String legendTitle) {

        //TODO: take annotation link as a string param

        String html = new String();
        Color p = null;
        //html = "<fieldset style='display:table;width:600; border:1px solid gray; text-align:left; padding:5px;'>";
        //html += "<legend>Legend: "+ legendTitle+"</legend>";

        for (int i = 0; i < lic.getItemCount(); i++) {
            p = (Color) lic.get(i).getFillPaint();
            //html += "<div style='margin:10px; padding:10px;border:1px solid red;'><label style='width:30px; height:10px; background-color: "+ c2hex(p)+"; border:1px solid black;'>&nbsp;&nbsp;&nbsp;</label><b style='color: "+ c2hex(p)+"'>"+lic.get(i).getLabel()+"</b></div>\n";
            html += "<table style=\"display:inline;\"><tr><td style=\"width:10px; height:10px; background-color: "
                    + c2hex(p)
                    + "; border:1px solid black;\">&nbsp;&nbsp;&nbsp;</td><td><a style=\"text-decoration:none\" "
                    + "href=\"javascript:spawnAnnot('" + lic.get(i).getLabel() + "\');\"><b style=\"color: "
                    + c2hex(p) + "\">" + lic.get(i).getLabel() + "</b></a></td></tr></table>";

            //html += "<table style=\"display:inline;\"><tr><td style=\"width:10px; height:10px; background-color: "+ c2hex(p)+"; border:1px solid black;\">&nbsp;&nbsp;&nbsp;</td><td><a style=\"text-decoration:none\" href=\"javascript:alert(\'some annotation for:"+lic.get(i).getLabel()+"\');\"><b style=\"color: "+ c2hex(p)+"\">"+lic.get(i).getLabel()+"</b></a></td></tr></table>";
        }
        //html += "</fieldset>";
        //System.out.println(html);
        String js = "";
        //js = "<script language=\"javascript\">document.getElementById('legend').innerHTML = \""+ html +" \"; </script>";
        return html;
    }

    /**
     * creates and HTML legend and returns a string of HTML
     * @param lic - a jFreechart LegendItemCollection
     * @return
     */
    public static String buildSmartLegend(String chipType, LegendItemCollection lic, String legendTitle) {

        //TODO: take annotation link as a string param

        String html = new String();
        Color p = null;
        //html = "<fieldset style='display:table;width:600; border:1px solid gray; text-align:left; padding:5px;'>";
        //html += "<legend>Legend: "+ legendTitle+"</legend>";

        for (int i = 0; i < lic.getItemCount(); i++) {
            p = (Color) lic.get(i).getFillPaint();
            //html += "<div style='margin:10px; padding:10px;border:1px solid red;'><label style='width:30px; height:10px; background-color: "+ c2hex(p)+"; border:1px solid black;'>&nbsp;&nbsp;&nbsp;</label><b style='color: "+ c2hex(p)+"'>"+lic.get(i).getLabel()+"</b></div>\n";

            // JB: GF [#21548] Issue for annotation query in GeneView
            // The LPG application that these probeset links redirect the user to, only support the HG-133 Chip.
            // So, when non-HG133 chips were selected for the GeneView search, those links probeset
            // links are NOT hyperlinked to the LPG site.
            if (chipType.equals("AFFY_HT_HG-U133A") || chipType.equals("AFFY_HGU133P2")) {
                html += "<table style=\"display:inline;\"><tr><td style=\"width:10px; height:10px; background-color: "
                        + c2hex(p)
                        + "; border:1px solid black;\">&nbsp;&nbsp;&nbsp;</td><td><a style=\"text-decoration:none\" "
                        + "href=\"javascript:spawnAnnot('" + lic.get(i).getLabel() + "\');\"><b style=\"color: "
                        + c2hex(p) + "\">" + lic.get(i).getLabel() + "</b></a></td></tr></table>";
            } else {
                html += "<table style=\"display:inline;\"><tr><td style=\"width:10px; height:10px; background-color: "
                        + c2hex(p) + "; border:1px solid black;\">&nbsp;&nbsp;&nbsp;</td><td><b style=\"color: "
                        + c2hex(p) + "\">" + lic.get(i).getLabel() + "</b></td></tr></table>";
            }
            //html += "<table style=\"display:inline;\"><tr><td style=\"width:10px; height:10px; background-color: "+ c2hex(p)+"; border:1px solid black;\">&nbsp;&nbsp;&nbsp;</td><td><a style=\"text-decoration:none\" href=\"javascript:alert(\'some annotation for:"+lic.get(i).getLabel()+"\');\"><b style=\"color: "+ c2hex(p)+"\">"+lic.get(i).getLabel()+"</b></a></td></tr></table>";
        }
        //html += "</fieldset>";
        //System.out.println(html);
        String js = "";
        //js = "<script language=\"javascript\">document.getElementById('legend').innerHTML = \""+ html +" \"; </script>";
        return html;
    }

    /**
     * converts an awt.Color to a hex color string
     * in the format: #zzzzzz for use in HTML
     * @param c
     * @return
     */
    public static String c2hex(Color c) {
        //http://rsb.info.nih.gov/ij/developer/source/index.html
        final char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

        int i = c.getRGB();
        char[] buf7 = new char[7];
        buf7[0] = '#';
        for (int pos = 6; pos >= 1; pos--) {
            buf7[pos] = hexDigits[i & 0xf];
            i >>>= 4;
        }
        return new String(buf7);
    }
}