JAL-2388 Unpleasant but operational refactor into OverviewCanvas
[jalview.git] / src / jalview / gui / OverviewCanvas.java
1 package jalview.gui;
2
3 import jalview.api.AlignViewportI;
4 import jalview.datamodel.SequenceI;
5 import jalview.renderer.AnnotationRenderer;
6 import jalview.viewmodel.OverviewDimensions;
7
8 import java.awt.Color;
9 import java.awt.Dimension;
10 import java.awt.Graphics;
11 import java.awt.image.BufferedImage;
12
13 import javax.swing.JComponent;
14
15 public class OverviewCanvas extends JComponent
16 {
17   private static final Color TRANS_GREY = new Color(100, 100, 100, 25);
18
19   private BufferedImage miniMe;
20
21   private BufferedImage lastMiniMe = null;
22
23   public boolean resizing = false;
24
25   // This is set true if the user resizes whilst
26   // the overview is being calculated
27   public boolean resizeAgain = false;
28
29   // Can set different properties in this seqCanvas than
30   // main visible SeqCanvas
31   private SequenceRenderer sr;
32
33   private jalview.renderer.seqfeatures.FeatureRenderer fr;
34
35   private final AnnotationRenderer renderer = new AnnotationRenderer();
36
37   OverviewDimensions od;
38
39   OverviewPanel op;
40
41   AlignViewport av;
42
43   AlignmentPanel ap;
44
45   public OverviewCanvas(OverviewDimensions overviewDims,
46           AlignViewportI alignvp, AlignmentPanel alignp, OverviewPanel overp)
47   {
48     od = overviewDims;
49     av = alignp.av;
50     ap = alignp;
51     op = overp;
52
53     sr = new SequenceRenderer(av);
54     sr.renderGaps = false;
55     sr.forOverview = true;
56     fr = new FeatureRenderer(ap);
57   }
58
59   public void draw(boolean showSequenceFeatures, boolean showAnnotation)
60   {
61     miniMe = null;
62
63     if (showSequenceFeatures)
64     {
65       fr.transferSettings(ap.getSeqPanel().seqCanvas.getFeatureRenderer());
66     }
67
68     // why do we need to set preferred size again? was set in
69     // updateOverviewImage
70     setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
71
72     miniMe = new BufferedImage(od.getWidth(), od.getHeight(),
73             BufferedImage.TYPE_INT_RGB);
74
75     Graphics mg = miniMe.getGraphics();
76     mg.setColor(Color.orange);
77     mg.fillRect(0, 0, od.getWidth(), miniMe.getHeight());
78
79     // calculate sampleCol and sampleRow
80     // alignment width is max number of residues/bases
81     // alignment height is number of sequences
82     int alwidth = av.getAlignment().getWidth();
83     int alheight = av.getAlignment().getAbsoluteHeight();
84
85     // sampleCol or sampleRow is the width/height allocated to each residue
86     // in particular, sometimes we may need more than one row/col of the
87     // BufferedImage allocated
88     // sampleCol is how much of a residue to assign to each pixel
89     // sampleRow is how many sequences to assign to each pixel
90     float sampleCol = alwidth / (float) od.getWidth();
91     float sampleRow = alheight / (float) od.getSequencesHeight();
92
93     buildImage(sampleRow, sampleCol);
94
95     if (showAnnotation)
96     {
97       renderer.updateFromAlignViewport(av);
98       for (int col = 0; col < od.getWidth() && !resizeAgain; col++)
99       {
100         mg.translate(col, od.getSequencesHeight());
101         renderer.drawGraph(mg, av.getAlignmentConservationAnnotation(),
102                 av.getAlignmentConservationAnnotation().annotations,
103                 (int) (sampleCol) + 1, od.getGraphHeight(),
104                 (int) (col * sampleCol), (int) (col * sampleCol) + 1);
105         mg.translate(-col, -od.getSequencesHeight());
106
107       }
108     }
109     System.gc();
110
111     resizing = false;
112
113     if (resizeAgain)
114     {
115       resizeAgain = false;
116       op.updateOverviewImage();
117     }
118     else
119     {
120       lastMiniMe = miniMe;
121     }
122   }
123
124   @Override
125   public void paintComponent(Graphics g)
126   {
127     if (resizing || resizeAgain)
128     {
129       if (lastMiniMe == null)
130       {
131         g.setColor(Color.white);
132         g.fillRect(0, 0, getWidth(), getHeight());
133       }
134       else
135       {
136         g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
137       }
138       g.setColor(TRANS_GREY);
139       g.fillRect(0, 0, getWidth(), getHeight());
140     }
141     else if (lastMiniMe != null)
142     {
143       g.drawImage(lastMiniMe, 0, 0, this);
144       if (lastMiniMe != miniMe)
145       {
146         g.setColor(TRANS_GREY);
147         g.fillRect(0, 0, getWidth(), getHeight());
148       }
149     }
150
151     g.setColor(Color.red);
152     od.drawBox(g);
153   }
154
155   /*
156    * Build the overview panel image
157    */
158   private void buildImage(float sampleRow, float sampleCol)
159   {
160     int lastcol = -1;
161     int lastrow = -1;
162     int color = Color.white.getRGB();
163
164     SequenceI seq = null;
165
166     final boolean hasHiddenCols = av.hasHiddenColumns();
167     boolean hiddenRow = false;
168     // get hidden row and hidden column map once at beginning.
169     // clone featureRenderer settings to avoid race conditions... if state is
170     // updated just need to refresh again
171     for (int row = 0; row < od.getSequencesHeight() && !resizeAgain; row++)
172     {
173       boolean doCopy = true;
174       int currentrow = (int) (row * sampleRow);
175       if (currentrow != lastrow)
176       {
177         doCopy = false;
178
179         lastrow = currentrow;
180
181         // get the sequence which would be at alignment index 'lastrow' if no
182         // rows were hidden, and determine whether it is hidden or not
183         hiddenRow = av.getAlignment().isHidden(lastrow);
184         seq = av.getAlignment().getSequenceAtAbsoluteIndex(lastrow);
185       }
186
187       for (int col = 0; col < od.getWidth() && !resizeAgain; col++)
188       {
189         if (doCopy)
190         {
191           color = miniMe.getRGB(col, row - 1);
192         }
193         else if ((int) (col * sampleCol) != lastcol
194                 || (int) (row * sampleRow) != lastrow)
195         {
196           lastcol = (int) (col * sampleCol);
197           color = getColumnColourFromSequence(seq, hiddenRow,
198                   hasHiddenCols, lastcol);
199         }
200         // else we just use the color we already have , so don't need to set it
201
202         miniMe.setRGB(col, row, color);
203       }
204     }
205   }
206
207   /*
208    * Find the colour of a sequence at a specified column position
209    */
210   private int getColumnColourFromSequence(jalview.datamodel.SequenceI seq,
211           boolean hiddenRow, boolean hasHiddenCols, int lastcol)
212   {
213     int color;
214
215     if (seq == null)
216     {
217       color = Color.white.getRGB();
218     }
219     else if (seq.getLength() > lastcol)
220     {
221       color = sr.getResidueBoxColour(seq, lastcol).getRGB();
222
223       if (av.isShowSequenceFeatures())
224       {
225         color = fr.findFeatureColour(color, seq, lastcol);
226       }
227     }
228     else
229     {
230       color = Color.white.getRGB();
231     }
232
233     if (hiddenRow
234             || (hasHiddenCols && !av.getColumnSelection()
235                     .isVisible(lastcol)))
236     {
237       color = new Color(color).darker().darker().getRGB();
238     }
239
240     return color;
241   }
242
243 }