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;
025import java.awt.event.MouseEvent;
026import java.io.Serializable;
027import java.util.List;
028
029import org.biojava.utils.AbstractChangeable;
030import org.biojava.utils.ChangeEvent;
031import org.biojava.utils.ChangeForwarder;
032import org.biojava.utils.ChangeSupport;
033import org.biojava.utils.ChangeType;
034import org.biojava.utils.ChangeVetoException;
035import org.biojava.utils.Changeable;
036
037/**
038 * This class wraps SequenceRenderer classes to present
039 * an Overlay Marker interface that will indicate to 
040 * LayeredRenderer that subsequent renderers should overdraw
041 * the same space as the wrapped renderer.
042 * <p>
043 * Extensively plagiarised from SequenceRendererWrapper.java.
044 *
045 * @author     David Huen
046 */
047public class OverlayRendererWrapper
048extends AbstractChangeable
049implements SequenceRenderer, Serializable, OverlayMarker {
050  public static ChangeType RENDERER = new ChangeType(
051    "The renderer used to render the filtered features has changed",
052    "org.biojava.bio.gui.sequence.OverlayRendererWrapper",
053    "RENDERER",
054    SequenceRenderContext.LAYOUT
055  );
056  
057  private SequenceRenderer renderer;
058  private transient ChangeForwarder rendForwarder;
059  
060  public OverlayRendererWrapper() {}
061  public OverlayRendererWrapper(SequenceRenderer renderer) {
062    this.renderer = renderer;
063  }
064  
065  protected ChangeSupport getChangeSupport(ChangeType ct) {
066    ChangeSupport cs = super.getChangeSupport(ct);
067    
068    if(rendForwarder == null) {
069      rendForwarder = new SequenceRenderer.RendererForwarder(this, cs);
070      if((renderer != null) && (renderer instanceof Changeable)) {
071        Changeable c = (Changeable) this.renderer;
072        c.addChangeListener(
073          rendForwarder,
074          SequenceRenderContext.REPAINT
075        );
076      }
077    }
078    
079    return cs;
080  }
081  
082  public void setRenderer(SequenceRenderer renderer)
083  throws ChangeVetoException {
084    if(hasListeners()) {
085      ChangeEvent ce = new ChangeEvent(
086        this, RENDERER,
087        renderer, this.renderer
088      );
089      ChangeSupport cs = getChangeSupport(RENDERER);
090      synchronized(cs) {
091        cs.firePreChangeEvent(ce);
092        if((this.renderer != null) && (this.renderer instanceof Changeable)) {
093          Changeable c = (Changeable) this.renderer;
094          c.removeChangeListener(rendForwarder);
095        }
096        this.renderer = renderer;
097        if(renderer instanceof Changeable) {
098          Changeable c = (Changeable) renderer;
099          c.removeChangeListener(rendForwarder);
100        }
101        cs.firePostChangeEvent(ce);
102      }
103    } else {
104      this.renderer = renderer;
105    }
106  }
107  
108  public SequenceRenderer getRenderer() {
109    return this.renderer;
110  }
111  
112  public double getDepth(SequenceRenderContext src) {
113    SequenceRenderer sr = getRenderer();
114    if(sr == null) {
115      return 0.0;
116    } else {
117      return sr.getDepth(src);
118    }
119  }
120  
121  public double getMinimumLeader(SequenceRenderContext src) {
122    SequenceRenderer sr = getRenderer();
123    if(sr == null) {
124      return 0.0;
125    } else {
126      return sr.getMinimumLeader(src);
127    }
128  }
129  
130  public double getMinimumTrailer(SequenceRenderContext src) {
131    SequenceRenderer sr = getRenderer();
132    if(sr == null) {
133      return 0.0;
134    } else {
135      return sr.getMinimumTrailer(src);
136    }
137  }
138  
139  public void paint(
140    Graphics2D g,
141    SequenceRenderContext src
142  ) {
143    SequenceRenderer sr = getRenderer();
144    if(sr != null) {
145      sr.paint(g, src);
146    }
147  }
148  
149  public SequenceViewerEvent processMouseEvent(
150    SequenceRenderContext src,
151    MouseEvent me,
152    List path
153  ) {
154    path.add(this);
155    return getRenderer().processMouseEvent(src, me, path);
156  }
157}
158