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 022 023package org.biojava.bio.gui; 024 025import java.awt.Color; 026import java.awt.Paint; 027import java.util.HashMap; 028import java.util.Iterator; 029import java.util.Map; 030 031import org.biojava.bio.BioError; 032import org.biojava.bio.seq.DNATools; 033import org.biojava.bio.symbol.Alphabet; 034import org.biojava.bio.symbol.FiniteAlphabet; 035import org.biojava.bio.symbol.IllegalSymbolException; 036import org.biojava.bio.symbol.Symbol; 037 038/** 039 * A no-frills implementation of SymbolStyle. 040 * 041 * @author Matthew Pocock 042 */ 043public class SimpleSymbolStyle implements SymbolStyle { 044 private final Map outlinePaint; 045 private final Map fillPaint; 046 private final FiniteAlphabet alphabet; 047 048 { 049 outlinePaint = new HashMap(); 050 fillPaint = new HashMap(); 051 } 052 053 public Alphabet getAlphabet() { 054 return alphabet; 055 } 056 057 public SimpleSymbolStyle(FiniteAlphabet alphabet) { 058 this.alphabet = alphabet; 059 Map outline = getStandardOutlinePaints(alphabet); 060 Map fill = getStandardFillPaints(alphabet); 061 try { 062 if(fill == null || outline == null) { 063 for(Iterator i = alphabet.iterator(); i.hasNext(); ) { 064 Symbol s = (Symbol) i.next(); 065 if(outline == null) { 066 setOutlinePaint(s, Color.black); 067 } else { 068 setOutlinePaint(s, (Paint) outline.get(s)); 069 } 070 if(fill == null) { 071 setFillPaint(s, Color.gray); 072 } else { 073 setOutlinePaint(s, (Paint) fill.get(s)); 074 } 075 } 076 } 077 } catch (IllegalSymbolException ire) { 078 throw new BioError("Symbol dissapeared from my alphabet", ire); 079 } 080 } 081 082 public Paint outlinePaint(Symbol s) throws IllegalSymbolException { 083 getAlphabet().validate(s); 084 return (Paint) outlinePaint.get(s); 085 } 086 087 public Paint fillPaint(Symbol s) throws IllegalSymbolException { 088 getAlphabet().validate(s); 089 return (Paint) fillPaint.get(s); 090 } 091 092 public void setOutlinePaint(Symbol s, Paint paint) 093 throws IllegalSymbolException { 094 getAlphabet().validate(s); 095 outlinePaint.put(s, paint); 096 } 097 098 public void setFillPaint(Symbol s, Paint paint) 099 throws IllegalSymbolException { 100 getAlphabet().validate(s); 101 fillPaint.put(s, paint); 102 } 103 104 private static Map standardFillPaints; 105 private static Map standardOutlinePaints; 106 107 public static Map getStandardFillPaints(Alphabet alpha) { 108 return (Map) standardFillPaints.get(alpha); 109 } 110 111 public static Map getStandardOutlinePaints(Alphabet alpha) { 112 return (Map) standardOutlinePaints.get(alpha); 113 } 114 115 static { 116 standardFillPaints = new HashMap(); 117 standardOutlinePaints = new HashMap(); 118 119 Map dnaFill = new HashMap(); 120 dnaFill.put(DNATools.t(), Color.red); 121 dnaFill.put(DNATools.g(), Color.blue); 122 dnaFill.put(DNATools.c(), Color.yellow); 123 dnaFill.put(DNATools.a(), Color.green); 124 standardFillPaints.put(DNATools.getDNA(), dnaFill); 125 126 Map dnaOutline = new HashMap(); 127 dnaOutline.put(DNATools.t(), Color.black); 128 dnaOutline.put(DNATools.a(), Color.black); 129 dnaOutline.put(DNATools.g(), Color.black); 130 dnaOutline.put(DNATools.c(), Color.black); 131 standardOutlinePaints.put(DNATools.getDNA(), dnaOutline); 132 } 133}