JAL-2435 copy font changes across split screen
[jalview.git] / src / jalview / appletgui / AlignViewport.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.appletgui;
22
23 import jalview.analysis.NJTree;
24 import jalview.api.AlignViewportI;
25 import jalview.api.FeatureSettingsModelI;
26 import jalview.bin.JalviewLite;
27 import jalview.commands.CommandI;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.ColumnSelection;
30 import jalview.datamodel.SearchResults;
31 import jalview.datamodel.SearchResultsI;
32 import jalview.datamodel.Sequence;
33 import jalview.datamodel.SequenceGroup;
34 import jalview.datamodel.SequenceI;
35 import jalview.renderer.ResidueShader;
36 import jalview.schemes.ColourSchemeProperty;
37 import jalview.schemes.UserColourScheme;
38 import jalview.structure.CommandListener;
39 import jalview.structure.SelectionSource;
40 import jalview.structure.StructureSelectionManager;
41 import jalview.structure.VamsasSource;
42 import jalview.viewmodel.AlignmentViewport;
43
44 import java.awt.Font;
45 import java.awt.FontMetrics;
46
47 public class AlignViewport extends AlignmentViewport implements
48         SelectionSource, VamsasSource, CommandListener
49 {
50   boolean cursorMode = false;
51
52   Font font = new Font("SansSerif", Font.PLAIN, 10);
53
54   boolean validCharWidth = true;
55
56   NJTree currentTree = null;
57
58   public jalview.bin.JalviewLite applet;
59
60   boolean MAC = false;
61
62   private AnnotationColumnChooser annotationColumnSelectionState;
63
64   @Override
65   public void finalize()
66   {
67     applet = null;
68     quality = null;
69     alignment = null;
70     colSel = null;
71   }
72
73   public AlignViewport(AlignmentI al, JalviewLite applet)
74   {
75     super();
76     calculator = new jalview.workers.AlignCalcManager();
77     this.applet = applet;
78     alignment = al;
79     // we always pad gaps
80     this.setPadGaps(true);
81     this.startRes = 0;
82     this.endRes = al.getWidth() - 1;
83     this.startSeq = 0;
84     this.endSeq = al.getHeight() - 1;
85     if (applet != null)
86     {
87       // get the width and height scaling factors if they were specified
88       String param = applet.getParameter("widthScale");
89       if (param != null)
90       {
91         try
92         {
93           widthScale = new Float(param).floatValue();
94         } catch (Exception e)
95         {
96         }
97         if (widthScale <= 1.0)
98         {
99           System.err
100                   .println("Invalid alignment character width scaling factor ("
101                           + widthScale + "). Ignoring.");
102           widthScale = 1;
103         }
104         if (JalviewLite.debug)
105         {
106           System.err
107                   .println("Alignment character width scaling factor is now "
108                           + widthScale);
109         }
110       }
111       param = applet.getParameter("heightScale");
112       if (param != null)
113       {
114         try
115         {
116           heightScale = new Float(param).floatValue();
117         } catch (Exception e)
118         {
119         }
120         if (heightScale <= 1.0)
121         {
122           System.err
123                   .println("Invalid alignment character height scaling factor ("
124                           + heightScale + "). Ignoring.");
125           heightScale = 1;
126         }
127         if (JalviewLite.debug)
128         {
129           System.err
130                   .println("Alignment character height scaling factor is now "
131                           + heightScale);
132         }
133       }
134     }
135     setFont(font, true);
136
137     MAC = new jalview.util.Platform().isAMac();
138
139     if (applet != null)
140     {
141       setShowJVSuffix(applet.getDefaultParameter("showFullId",
142               getShowJVSuffix()));
143
144       setShowAnnotation(applet.getDefaultParameter("showAnnotation",
145               isShowAnnotation()));
146
147       showConservation = applet.getDefaultParameter("showConservation",
148               showConservation);
149
150       showQuality = applet.getDefaultParameter("showQuality", showQuality);
151
152       showConsensus = applet.getDefaultParameter("showConsensus",
153               showConsensus);
154
155       setShowUnconserved(applet.getDefaultParameter("showUnconserved",
156               getShowUnconserved()));
157
158       setScaleProteinAsCdna(applet.getDefaultParameter(
159               "scaleProteinAsCdna", isScaleProteinAsCdna()));
160
161       String param = applet.getParameter("upperCase");
162       if (param != null)
163       {
164         if (param.equalsIgnoreCase("bold"))
165         {
166           setUpperCasebold(true);
167         }
168       }
169       sortByTree = applet.getDefaultParameter("sortByTree", sortByTree);
170
171       setFollowHighlight(applet.getDefaultParameter("automaticScrolling",
172               isFollowHighlight()));
173       followSelection = isFollowHighlight();
174
175       showSequenceLogo = applet.getDefaultParameter("showSequenceLogo",
176               showSequenceLogo);
177
178       normaliseSequenceLogo = applet.getDefaultParameter(
179               "normaliseSequenceLogo", applet.getDefaultParameter(
180                       "normaliseLogo", normaliseSequenceLogo));
181
182       showGroupConsensus = applet.getDefaultParameter("showGroupConsensus",
183               showGroupConsensus);
184
185       showGroupConservation = applet.getDefaultParameter(
186               "showGroupConservation", showGroupConservation);
187
188       showConsensusHistogram = applet.getDefaultParameter(
189               "showConsensusHistogram", showConsensusHistogram);
190
191     }
192
193     if (applet != null)
194     {
195       String colour = al.isNucleotide() ? applet
196               .getParameter("defaultColourNuc") : applet
197               .getParameter("defaultColourProt");
198       if (colour == null)
199       {
200         colour = applet.getParameter("defaultColour");
201       }
202       if (colour == null)
203       {
204         colour = applet.getParameter("userDefinedColour");
205         if (colour != null)
206         {
207           colour = "User Defined";
208         }
209       }
210
211       if (colour != null)
212       {
213         residueShading = new ResidueShader(
214                 ColourSchemeProperty.getColourScheme(alignment, colour));
215         if (residueShading != null)
216         {
217           residueShading.setConsensus(hconsensus);
218         }
219       }
220
221       if (applet.getParameter("userDefinedColour") != null)
222       {
223         residueShading = new ResidueShader(
224                 new UserColourScheme(
225                         applet.getParameter("userDefinedColour")));
226       }
227     }
228     initAutoAnnotation();
229
230   }
231
232   /**
233    * get the consensus sequence as displayed under the PID consensus annotation
234    * row.
235    * 
236    * @return consensus sequence as a new sequence object
237    */
238   public SequenceI getConsensusSeq()
239   {
240     if (consensus == null)
241     {
242       updateConsensus(null);
243     }
244     if (consensus == null)
245     {
246       return null;
247     }
248     StringBuilder seqs = new StringBuilder(consensus.annotations.length);
249     for (int i = 0; i < consensus.annotations.length; i++)
250     {
251       if (consensus.annotations[i] != null)
252       {
253         if (consensus.annotations[i].description.charAt(0) == '[')
254         {
255           seqs.append(consensus.annotations[i].description.charAt(1));
256         }
257         else
258         {
259           seqs.append(consensus.annotations[i].displayCharacter);
260         }
261       }
262     }
263     SequenceI sq = new Sequence("Consensus", seqs.toString());
264     sq.setDescription("Percentage Identity Consensus "
265             + ((ignoreGapsInConsensusCalculation) ? " without gaps" : ""));
266     return sq;
267   }
268
269   java.awt.Frame nullFrame;
270
271   protected FeatureSettings featureSettings = null;
272
273   private float heightScale = 1, widthScale = 1;
274
275   /**
276    * {@inheritDoc}
277    */
278   @Override
279   public void setFont(Font f, boolean setGrid)
280   {
281     font = f;
282     if (nullFrame == null)
283     {
284       nullFrame = new java.awt.Frame();
285       nullFrame.addNotify();
286     }
287
288     if (setGrid)
289     {
290       FontMetrics fm = nullFrame.getGraphics().getFontMetrics(font);
291       setCharHeight((int) (heightScale * fm.getHeight()));
292       setCharWidth((int) (widthScale * fm.charWidth('M')));
293     }
294
295     if (isUpperCasebold())
296     {
297       Font f2 = new Font(f.getName(), Font.BOLD, f.getSize());
298       FontMetrics fm = nullFrame.getGraphics().getFontMetrics(f2);
299       setCharWidth((int) (widthScale * (fm.stringWidth("MMMMMMMMMMM") / 10)));
300     }
301   }
302
303   public Font getFont()
304   {
305     return font;
306   }
307
308   public void resetSeqLimits(int height)
309   {
310     setEndSeq(height / getCharHeight());
311   }
312
313   public void setCurrentTree(NJTree tree)
314   {
315     currentTree = tree;
316   }
317
318   public NJTree getCurrentTree()
319   {
320     return currentTree;
321   }
322
323   boolean centreColumnLabels;
324
325   public boolean getCentreColumnLabels()
326   {
327     return centreColumnLabels;
328   }
329
330   public boolean followSelection = true;
331
332   /**
333    * @return true if view selection should always follow the selections
334    *         broadcast by other selection sources
335    */
336   public boolean getFollowSelection()
337   {
338     return followSelection;
339   }
340
341   @Override
342   public void sendSelection()
343   {
344     getStructureSelectionManager().sendSelection(
345             new SequenceGroup(getSelectionGroup()),
346             new ColumnSelection(getColumnSelection()), this);
347   }
348
349   /**
350    * Returns an instance of the StructureSelectionManager scoped to this applet
351    * instance.
352    * 
353    * @return
354    */
355   @Override
356   public StructureSelectionManager getStructureSelectionManager()
357   {
358     return jalview.structure.StructureSelectionManager
359             .getStructureSelectionManager(applet);
360   }
361
362   @Override
363   public boolean isNormaliseSequenceLogo()
364   {
365     return normaliseSequenceLogo;
366   }
367
368   public void setNormaliseSequenceLogo(boolean state)
369   {
370     normaliseSequenceLogo = state;
371   }
372
373   /**
374    * 
375    * @return true if alignment characters should be displayed
376    */
377   @Override
378   public boolean isValidCharWidth()
379   {
380     return validCharWidth;
381   }
382
383   public AnnotationColumnChooser getAnnotationColumnSelectionState()
384   {
385     return annotationColumnSelectionState;
386   }
387
388   public void setAnnotationColumnSelectionState(
389           AnnotationColumnChooser annotationColumnSelectionState)
390   {
391     this.annotationColumnSelectionState = annotationColumnSelectionState;
392   }
393
394   @Override
395   public void mirrorCommand(CommandI command, boolean undo,
396           StructureSelectionManager ssm, VamsasSource source)
397   {
398     // TODO refactor so this can be pulled up to superclass or controller
399     /*
400      * Do nothing unless we are a 'complement' of the source. May replace this
401      * with direct calls not via SSM.
402      */
403     if (source instanceof AlignViewportI
404             && ((AlignViewportI) source).getCodingComplement() == this)
405     {
406       // ok to continue;
407     }
408     else
409     {
410       return;
411     }
412
413     CommandI mappedCommand = ssm.mapCommand(command, undo, getAlignment(),
414             getGapCharacter());
415     if (mappedCommand != null)
416     {
417       mappedCommand.doCommand(null);
418       firePropertyChange("alignment", null, getAlignment().getSequences());
419
420       // ap.scalePanelHolder.repaint();
421       // ap.repaint();
422     }
423   }
424
425   @Override
426   public VamsasSource getVamsasSource()
427   {
428     return this;
429   }
430
431   /**
432    * If this viewport has a (Protein/cDNA) complement, then scroll the
433    * complementary alignment to match this one.
434    */
435   public void scrollComplementaryAlignment(AlignmentPanel complementPanel)
436   {
437     if (complementPanel == null)
438     {
439       return;
440     }
441
442     /*
443      * Populate a SearchResults object with the mapped location to scroll to. If
444      * there is no complement, or it is not following highlights, or no mapping
445      * is found, the result will be empty.
446      */
447     SearchResultsI sr = new SearchResults();
448     int seqOffset = findComplementScrollTarget(sr);
449     if (!sr.isEmpty())
450     {
451       complementPanel.setFollowingComplementScroll(true);
452       complementPanel.scrollToCentre(sr, seqOffset);
453     }
454   }
455
456   /**
457    * Applies the supplied feature settings descriptor to currently known
458    * features. This supports an 'initial configuration' of feature colouring
459    * based on a preset or user favourite. This may then be modified in the usual
460    * way using the Feature Settings dialogue.
461    * 
462    * @param featureSettings
463    */
464   @Override
465   public void applyFeaturesStyle(FeatureSettingsModelI featureSettings)
466   {
467     // TODO implement for applet
468   }
469
470 }