001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021
022package org.biojava.bio.program.blast2html;
023
024import java.util.HashMap;
025import java.util.HashSet;
026import java.util.Iterator;
027
028/**
029 * <p>
030 * Abstract implementation of <code>AlignmentStyler</code>, contains
031 * utility methods for generating a set of HTML styles from a list of
032 * RGB colours.
033 * </p>
034 *
035 * <p>
036 * Thus <code>getAlignmentStyles()</code> is implemented and all that
037 * remains to be implemented is the <code>getStyle</code> method.
038 *
039 * <pre>
040 * Primary author -
041 *                 Colin Hardman      (CAT)
042 * Other authors  -
043 *                 Tim Dilks          (CAT)
044 *                 Simon Brocklehurst (CAT)
045 *                 Stuart Johnston    (CAT)
046 *                 Lawerence Bower    (CAT)
047 *                 Derek Crockford    (CAT)
048 *                 Neil Benn          (CAT)
049 *
050 * Copyright 2001 Cambridge Antibody Technology Group plc.
051 * </pre>
052 * </p>
053 *
054 * <p>
055 * This code released to the biojava project, May 2001
056 * under the LGPL license.
057 * </p>
058 *
059 * @author Cambridge Antibody Technology Group plc
060 * @author Greg Cox
061 * @version 1.0
062 *
063 */
064public abstract class AbstractAlignmentStyler implements AlignmentStyler {
065
066    /**
067     * Store the unique colours for markup.
068     */
069    protected HashSet oColourSet = new HashSet();
070
071
072    /**
073     * <p>
074     * Stores mapping from a Colour to a FONT Class.
075     * </p>
076     *
077     * <p>
078     * For example:
079     * <PRE>
080     *
081     * Key      Value
082     * ---      -----
083     * #000000  C1-S
084     *
085     * </PRE>
086     * </p>
087     */
088    protected HashMap oColourClassMap = new HashMap();
089
090
091
092    /**
093     * The number of unique colours.
094     */
095    protected int iNumberOfColours = 0;
096
097    /**
098     * <p>
099     * Map between Char and the Colour class.
100     * </p>
101     *
102     * <p>
103     * Eg.
104     * <PRE>
105     *
106     * Key      Value
107     * ---      -----
108     * A        C1-S
109     *
110     * </PRE>
111     * </p>
112     */
113    protected HashMap oColourMap = new HashMap();
114
115
116    /**
117     * <p>
118     * Returns a fragment of HTML that defines the FONT
119     * styles to be used in the alignment markup.
120     * </p>
121     *
122     * <p>
123     * For example:
124     * <PRE>
125     * FONT.C2-S{background-color:#FFFC50;color:#000000}
126     * FONT.C4-S{background-color:#FC50FF;color:#000000}
127     * FONT.C3-S{background-color:#FF7272;color:#000000}
128     * FONT.C0-S{background-color:#50FF78;color:#000000}
129     * FONT.C1-S{background-color:#FFCA50;color:#000000}
130     * FONT.C5-S{background-color:#A5A5FF;color:#000000}
131     * </PRE>
132     * </p>
133     *
134     * @return String - the HTML
135     */
136    public String getAlignmentStyles() {
137
138        StringBuffer sb = new StringBuffer();
139
140        if ( oColourSet.size() == 0 ) return "";
141
142        //      sb.append("<STYLE TYPE=\"text/css\">\n");
143        //      sb.append("<!--\n");
144
145        Iterator it = oColourSet.iterator();
146        while ( it.hasNext() ) {
147
148            sb.append( (String)it.next() );
149        }
150
151        //      sb.append( "-->\n</STYLE>\n" );
152        return sb.substring(0);
153    }
154
155    /**
156     * <p>
157     * Return the styles for the two aligned characters.
158     * (in the form of predefined font classes).
159     * </p>
160     *
161     * <p>
162     * Null is acceptable value for no style.
163     * </p>
164     *
165     * @param poFirst - the first char in the alignment
166     * @param poSecond - the second char in the alignment
167     * @param poStyleHolder - an array to hold the styles, [0] = first etc
168     */
169    public abstract void getStyle( String poFirst, String poSecond,
170                                   String[] poStyleHolder );
171
172
173    /**
174     * Add a colour style to this Styler.
175     *
176     * @param poChar the char for which this colour applies.
177     * @param poColour the color in hex eg 'FFA2A2' for a nice red
178     *                 ( R = FF, G = A2 and B = A2 )
179     */
180    public void addStyle ( String poChar, String poColour ) {
181
182        String oColourClass  = this.getColourClass
183            ( poColour );
184
185        oColourMap.put( poChar, oColourClass );
186    }
187
188    /**
189     * <p>Returns the colour class for the specified colour (in hex).
190     * If one is not already defined for that colour then a new class
191     * is created and returned.</p>
192     *
193     * <p>
194     * Colour specification is R G B in hex ie
195     * FF00FF is r = 255, g = 0, b = 255.
196     * </p>
197     *
198     * @param poColour - a colour, eg 'C8FFC8'
199     * @return String - the colour class, eg 'C1-S'
200     */
201    protected String getColourClass( String poColour ) {
202
203        String oColourClass = (String)oColourClassMap.get( poColour );
204        if ( oColourClass == null ) {
205            // otherwise create a new one
206            oColourClass  = "C" + iNumberOfColours + "-S" ;
207
208            StringBuffer sb = new StringBuffer( 50 );
209
210            sb.append ( "FONT." );
211            sb.append ( oColourClass );
212            sb.append ( "{background-color:#" );
213            sb.append ( poColour );
214            sb.append ( ";color:#000000}\n" );
215
216            oColourSet.add( sb.substring(0) );
217            oColourClassMap.put( poColour, oColourClass );
218
219            iNumberOfColours++;
220        }
221        return oColourClass;
222    }
223
224}