JAL-2527 Added default setting/resetting and first pass at Spanish
[jalview.git] / src / jalview / gui / OverviewCanvas.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.gui;
22
23 import jalview.api.AlignViewportI;
24 import jalview.bin.Cache;
25 import jalview.renderer.OverviewRenderer;
26 import jalview.renderer.OverviewResColourFinder;
27 import jalview.viewmodel.OverviewDimensions;
28
29 import java.awt.Color;
30 import java.awt.Dimension;
31 import java.awt.Graphics;
32 import java.awt.image.BufferedImage;
33
34 import javax.swing.JComponent;
35
36 public class OverviewCanvas extends JComponent
37 {
38   private static final Color TRANS_GREY = new Color(100, 100, 100, 25);
39
40   // This is set true if the alignment view changes whilst
41   // the overview is being calculated
42   private volatile boolean restart = false;
43
44   private volatile boolean updaterunning = false;
45
46   private BufferedImage miniMe;
47
48   private BufferedImage lastMiniMe = null;
49
50   // Can set different properties in this seqCanvas than
51   // main visible SeqCanvas
52   private SequenceRenderer sr;
53
54   private jalview.renderer.seqfeatures.FeatureRenderer fr;
55
56   private OverviewDimensions od;
57
58   private OverviewRenderer or = null;
59
60   private AlignViewportI av;
61
62   public OverviewCanvas(OverviewDimensions overviewDims,
63           AlignViewportI alignvp)
64   {
65     od = overviewDims;
66     av = alignvp;
67
68     sr = new SequenceRenderer(av);
69     sr.renderGaps = false;
70     fr = new jalview.renderer.seqfeatures.FeatureRenderer(av);
71   }
72
73   /**
74    * Update the overview dimensions object used by the canvas (e.g. if we change
75    * from showing hidden columns to hiding them or vice versa)
76    * 
77    * @param overviewDims
78    */
79   public void resetOviewDims(OverviewDimensions overviewDims)
80   {
81     od = overviewDims;
82   }
83
84   /**
85    * Signals to drawing code that the associated alignment viewport has changed
86    * and a redraw will be required
87    */
88   public boolean restartDraw()
89   {
90     synchronized (this)
91     {
92       if (updaterunning)
93       {
94         restart = true;
95         if (or != null)
96         {
97           or.setRedraw(true);
98         }
99       }
100       else
101       {
102         updaterunning = true;
103       }
104       return restart;
105     }
106   }
107
108   /**
109    * Draw the overview sequences
110    * 
111    * @param showSequenceFeatures
112    *          true if sequence features are to be shown
113    * @param showAnnotation
114    *          true if the annotation is to be shown
115    * @param transferRenderer
116    *          the renderer to transfer feature colouring from
117    */
118   public void draw(boolean showSequenceFeatures, boolean showAnnotation,
119           FeatureRenderer transferRenderer)
120   {
121     miniMe = null;
122
123     if (showSequenceFeatures)
124     {
125       fr.transferSettings(transferRenderer);
126     }
127
128     setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
129
130     boolean useLegacy = Cache.getDefault("USE_LEGACY_GAP", false);
131     Color gapCol = Cache.getDefaultColour("GAP_COLOUR", Color.lightGray);
132     Color hiddenCol = Cache.getDefaultColour("HIDDEN_COLOUR", Color.DARK_GRAY.darker());
133     OverviewResColourFinder cf = new OverviewResColourFinder(useLegacy,
134             gapCol, hiddenCol);
135
136     or = new OverviewRenderer(fr, od, av.getAlignment(),
137             av.getResidueShading(), cf);
138     miniMe = or.draw(od.getRows(av.getAlignment()),
139             od.getColumns(av.getAlignment()));
140
141     Graphics mg = miniMe.getGraphics();
142
143     if (showAnnotation)
144     {
145       mg.translate(0, od.getSequencesHeight());
146       or.drawGraph(mg, av.getAlignmentConservationAnnotation(),
147               av.getCharWidth(), od.getGraphHeight(),
148               od.getColumns(av.getAlignment()));
149       mg.translate(0, -od.getSequencesHeight());
150     }
151     System.gc();
152
153     if (restart)
154     {
155       restart = false;
156       draw(showSequenceFeatures, showAnnotation, transferRenderer);
157     }
158     else
159     {
160       updaterunning = false;
161       lastMiniMe = miniMe;
162     }
163   }
164
165   @Override
166   public void paintComponent(Graphics g)
167   {
168     if (restart)
169     {
170       if (lastMiniMe == null)
171       {
172         g.setColor(Color.white);
173         g.fillRect(0, 0, getWidth(), getHeight());
174       }
175       else
176       {
177         g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
178       }
179       g.setColor(TRANS_GREY);
180       g.fillRect(0, 0, getWidth(), getHeight());
181     }
182     else if (lastMiniMe != null)
183     {
184       g.drawImage(lastMiniMe, 0, 0, this);
185       if (lastMiniMe != miniMe)
186       {
187         g.setColor(TRANS_GREY);
188         g.fillRect(0, 0, getWidth(), getHeight());
189       }
190     }
191
192     g.setColor(Color.red);
193     od.drawBox(g);
194   }
195
196 }