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.gui.sequence;
023
024import java.awt.Graphics2D;
025
026import org.biojava.bio.alignment.Alignment;
027import org.biojava.bio.seq.FeatureHolder;
028import org.biojava.bio.symbol.SymbolList;
029import org.biojava.utils.ChangeEvent;
030import org.biojava.utils.ChangeSupport;
031import org.biojava.utils.ChangeType;
032import org.biojava.utils.ChangeVetoException;
033
034/**
035 * @author Matthew Pocock
036 * @author Thomas Down
037 */
038public class AlignmentRenderer
039extends SequenceRendererWrapper {
040  public static ChangeType LABEL = new ChangeType(
041    "The label used to select the Alignment component to render has changed.",
042    "org.biojava.bio.gui.sequence.AlignmentRenderer",
043    "LABEL",
044    SequenceRenderContext.LAYOUT
045  );
046  
047  private String label;
048  
049  public void setLabel(String label)
050  throws ChangeVetoException {
051    if(hasListeners()) {
052      ChangeEvent ce = new ChangeEvent(
053        this, LABEL,
054        label, this.label
055      );
056      ChangeSupport cs = getChangeSupport(LABEL);
057      synchronized(cs) {
058        cs.firePreChangeEvent(ce);
059        this.label = label;
060        cs.firePostChangeEvent(ce);
061      }
062    } else {
063      this.label = label;
064    }
065  }
066  
067  public String getLabel() {
068    return this.label;
069  }
070  
071  public double getDepth(SequenceRenderContext ctx) {
072    if(getLabel() != null) {
073      SequenceRenderContext subctx = contextForLabel(
074        ctx, getLabel()
075      );
076      return super.getDepth(subctx);
077    } else {
078      return 0.0;
079    }
080  }
081  
082  public double getMinimumLeader(SequenceRenderContext ctx) {
083    if(getLabel() != null) {
084      SequenceRenderContext subctx = contextForLabel(
085        ctx, getLabel()
086      );
087      return super.getMinimumLeader(subctx);
088    } else {
089      return 0.0;
090    }
091  }
092  
093  public double getMinimumTrailer(SequenceRenderContext ctx) {
094    if(getLabel() != null) {
095      SequenceRenderContext subctx = contextForLabel(
096        ctx, getLabel()
097      );
098      return super.getMinimumTrailer(subctx);
099    } else {
100      return 0.0;
101    }
102  }
103  
104  public void paint(
105        Graphics2D g,
106        SequenceRenderContext ctx
107  ) {
108    if(getLabel() != null) {
109      SequenceRenderContext subctx = contextForLabel(
110        ctx, getLabel()
111      );
112      super.paint(g, subctx);
113    }
114  }
115
116  public SequenceRenderContext contextForLabel(
117    SequenceRenderContext src, String label
118  ) {
119    Alignment ali = (Alignment) src.getSymbols();
120    SymbolList sl = ali.symbolListForLabel(label);
121    FeatureHolder features = null;
122    if(sl instanceof FeatureHolder) {
123      features = (FeatureHolder) sl;
124    }
125    
126    return new SubSequenceRenderContext(src, sl, features, null);
127  }
128}