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