1af491c97afef2d63a2ed7f7ba1d28fcf583def7
[jalview.git] / src / jalview / viewmodel / seqfeatures / FeatureRendererModel.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.viewmodel.seqfeatures;
22
23 import jalview.api.AlignViewportI;
24 import jalview.api.FeatureColourI;
25 import jalview.api.FeaturesDisplayedI;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.SequenceFeature;
28 import jalview.datamodel.SequenceI;
29 import jalview.renderer.seqfeatures.FeatureRenderer;
30 import jalview.schemes.FeatureColour;
31 import jalview.util.ColorUtils;
32
33 import java.awt.Color;
34 import java.beans.PropertyChangeListener;
35 import java.beans.PropertyChangeSupport;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.HashMap;
39 import java.util.HashSet;
40 import java.util.Hashtable;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.Set;
44 import java.util.concurrent.ConcurrentHashMap;
45
46 public abstract class FeatureRendererModel implements
47         jalview.api.FeatureRenderer
48 {
49
50   /**
51    * global transparency for feature
52    */
53   protected float transparency = 1.0f;
54
55   protected Map<String, FeatureColourI> featureColours = new ConcurrentHashMap<String, FeatureColourI>();
56
57   protected Map<String, Boolean> featureGroups = new ConcurrentHashMap<String, Boolean>();
58
59   protected String[] renderOrder;
60
61   Map<String, Float> featureOrder = null;
62
63   protected PropertyChangeSupport changeSupport = new PropertyChangeSupport(
64           this);
65
66   protected AlignViewportI av;
67
68   @Override
69   public AlignViewportI getViewport()
70   {
71     return av;
72   }
73
74   public FeatureRendererSettings getSettings()
75   {
76     return new FeatureRendererSettings(this);
77   }
78
79   public void transferSettings(FeatureRendererSettings fr)
80   {
81     this.renderOrder = fr.renderOrder;
82     this.featureGroups = fr.featureGroups;
83     this.featureColours = fr.featureColours;
84     this.transparency = fr.transparency;
85     this.featureOrder = fr.featureOrder;
86   }
87
88   /**
89    * update from another feature renderer
90    * 
91    * @param fr
92    *          settings to copy
93    */
94   public void transferSettings(jalview.api.FeatureRenderer _fr)
95   {
96     FeatureRenderer fr = (FeatureRenderer) _fr;
97     FeatureRendererSettings frs = new FeatureRendererSettings(fr);
98     this.renderOrder = frs.renderOrder;
99     this.featureGroups = frs.featureGroups;
100     this.featureColours = frs.featureColours;
101     this.transparency = frs.transparency;
102     this.featureOrder = frs.featureOrder;
103     if (av != null && av != fr.getViewport())
104     {
105       // copy over the displayed feature settings
106       if (_fr.getFeaturesDisplayed() != null)
107       {
108         FeaturesDisplayedI fd = getFeaturesDisplayed();
109         if (fd == null)
110         {
111           setFeaturesDisplayedFrom(_fr.getFeaturesDisplayed());
112         }
113         else
114         {
115           synchronized (fd)
116           {
117             fd.clear();
118             for (String type : _fr.getFeaturesDisplayed()
119                     .getVisibleFeatures())
120             {
121               fd.setVisible(type);
122             }
123           }
124         }
125       }
126     }
127   }
128
129   public void setFeaturesDisplayedFrom(FeaturesDisplayedI featuresDisplayed)
130   {
131     av.setFeaturesDisplayed(new FeaturesDisplayed(featuresDisplayed));
132   }
133
134   @Override
135   public void setVisible(String featureType)
136   {
137     FeaturesDisplayedI fdi = av.getFeaturesDisplayed();
138     if (fdi == null)
139     {
140       av.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
141     }
142     if (!fdi.isRegistered(featureType))
143     {
144       pushFeatureType(Arrays.asList(new String[] { featureType }));
145     }
146     fdi.setVisible(featureType);
147   }
148
149   @Override
150   public void setAllVisible(List<String> featureTypes)
151   {
152     FeaturesDisplayedI fdi = av.getFeaturesDisplayed();
153     if (fdi == null)
154     {
155       av.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
156     }
157     List<String> nft = new ArrayList<String>();
158     for (String featureType : featureTypes)
159     {
160       if (!fdi.isRegistered(featureType))
161       {
162         nft.add(featureType);
163       }
164     }
165     if (nft.size() > 0)
166     {
167       pushFeatureType(nft);
168     }
169     fdi.setAllVisible(featureTypes);
170   }
171
172   /**
173    * push a set of new types onto the render order stack. Note - this is a
174    * direct mechanism rather than the one employed in updateRenderOrder
175    * 
176    * @param types
177    */
178   private void pushFeatureType(List<String> types)
179   {
180
181     int ts = types.size();
182     String neworder[] = new String[(renderOrder == null ? 0
183             : renderOrder.length) + ts];
184     types.toArray(neworder);
185     if (renderOrder != null)
186     {
187       System.arraycopy(neworder, 0, neworder, renderOrder.length, ts);
188       System.arraycopy(renderOrder, 0, neworder, 0, renderOrder.length);
189     }
190     renderOrder = neworder;
191   }
192
193   protected Map<String, float[][]> minmax = new Hashtable<String, float[][]>();
194
195   public Map<String, float[][]> getMinMax()
196   {
197     return minmax;
198   }
199
200   /**
201    * normalise a score against the max/min bounds for the feature type.
202    * 
203    * @param sequenceFeature
204    * @return byte[] { signed, normalised signed (-127 to 127) or unsigned
205    *         (0-255) value.
206    */
207   protected final byte[] normaliseScore(SequenceFeature sequenceFeature)
208   {
209     float[] mm = minmax.get(sequenceFeature.type)[0];
210     final byte[] r = new byte[] { 0, (byte) 255 };
211     if (mm != null)
212     {
213       if (r[0] != 0 || mm[0] < 0.0)
214       {
215         r[0] = 1;
216         r[1] = (byte) ((int) 128.0 + 127.0 * (sequenceFeature.score / mm[1]));
217       }
218       else
219       {
220         r[1] = (byte) ((int) 255.0 * (sequenceFeature.score / mm[1]));
221       }
222     }
223     return r;
224   }
225
226   boolean newFeatureAdded = false;
227
228   boolean findingFeatures = false;
229
230   protected boolean updateFeatures()
231   {
232     if (av.getFeaturesDisplayed() == null || renderOrder == null
233             || newFeatureAdded)
234     {
235       findAllFeatures();
236       if (av.getFeaturesDisplayed().getVisibleFeatureCount() < 1)
237       {
238         return false;
239       }
240     }
241     // TODO: decide if we should check for the visible feature count first
242     return true;
243   }
244
245   /**
246    * search the alignment for all new features, give them a colour and display
247    * them. Then fires a PropertyChangeEvent on the changeSupport object.
248    * 
249    */
250   protected void findAllFeatures()
251   {
252     synchronized (firing)
253     {
254       if (firing.equals(Boolean.FALSE))
255       {
256         firing = Boolean.TRUE;
257         findAllFeatures(true); // add all new features as visible
258         changeSupport.firePropertyChange("changeSupport", null, null);
259         firing = Boolean.FALSE;
260       }
261     }
262   }
263
264   @Override
265   public List<SequenceFeature> findFeaturesAtRes(SequenceI sequence, int res)
266   {
267     List<SequenceFeature> result = new ArrayList<SequenceFeature>();
268     if (!av.areFeaturesDisplayed())
269     {
270       return result;
271     }
272
273     Set<String> visibleFeatures = getFeaturesDisplayed()
274             .getVisibleFeatures();
275     String[] visibleTypes = visibleFeatures
276             .toArray(new String[visibleFeatures.size()]);
277
278     /*
279      * include features at the position provided their feature type is 
280      * displayed, and feature group is null or marked for display
281      */
282     List<SequenceFeature> features = sequence.findFeatures(res, res,
283             visibleTypes);
284
285     for (SequenceFeature sf : features)
286     {
287       if (!featureGroupNotShown(sf))
288       {
289         result.add(sf);
290       }
291     }
292     return result;
293   }
294
295   /**
296    * Searches alignment for all features and updates colours
297    * 
298    * @param newMadeVisible
299    *          if true newly added feature types will be rendered immediately
300    *          TODO: check to see if this method should actually be proxied so
301    *          repaint events can be propagated by the renderer code
302    */
303   @Override
304   public synchronized void findAllFeatures(boolean newMadeVisible)
305   {
306     newFeatureAdded = false;
307
308     if (findingFeatures)
309     {
310       newFeatureAdded = true;
311       return;
312     }
313
314     findingFeatures = true;
315     if (av.getFeaturesDisplayed() == null)
316     {
317       av.setFeaturesDisplayed(new FeaturesDisplayed());
318     }
319     FeaturesDisplayedI featuresDisplayed = av.getFeaturesDisplayed();
320
321     Set<String> oldfeatures = new HashSet<String>();
322     if (renderOrder != null)
323     {
324       for (int i = 0; i < renderOrder.length; i++)
325       {
326         if (renderOrder[i] != null)
327         {
328           oldfeatures.add(renderOrder[i]);
329         }
330       }
331     }
332
333     AlignmentI alignment = av.getAlignment();
334     List<String> allfeatures = new ArrayList<String>();
335
336     for (int i = 0; i < alignment.getHeight(); i++)
337     {
338       SequenceI asq = alignment.getSequenceAt(i);
339       for (String group : asq.getFeatures().getFeatureGroups(true))
340       {
341         boolean groupDisplayed = true;
342         if (group != null)
343         {
344           if (featureGroups.containsKey(group))
345           {
346             groupDisplayed = featureGroups.get(group);
347           }
348           else
349           {
350             groupDisplayed = newMadeVisible;
351             featureGroups.put(group, groupDisplayed);
352           }
353         }
354         if (groupDisplayed)
355         {
356           Set<String> types = asq.getFeatures().getFeatureTypesForGroups(
357                   true, group);
358           for (String type : types)
359           {
360             if (!allfeatures.contains(type)) // or use HashSet and no test?
361             {
362               allfeatures.add(type);
363             }
364             updateMinMax(asq, type, true); // todo: for all features?
365           }
366         }
367       }
368     }
369
370     // uncomment to add new features in alphebetical order (but JAL-2575)
371     // Collections.sort(allfeatures, String.CASE_INSENSITIVE_ORDER);
372     if (newMadeVisible)
373     {
374       for (String type : allfeatures)
375       {
376         if (!oldfeatures.contains(type))
377         {
378           featuresDisplayed.setVisible(type);
379           setOrder(type, 0);
380         }
381       }
382     }
383
384     updateRenderOrder(allfeatures);
385     findingFeatures = false;
386   }
387
388   /**
389    * Updates the global (alignment) min and max values for a feature type from
390    * the score for a sequence, if the score is not NaN. Values are stored
391    * separately for positional and non-positional features.
392    * 
393    * @param seq
394    * @param featureType
395    * @param positional
396    */
397   protected void updateMinMax(SequenceI seq, String featureType,
398           boolean positional)
399   {
400     float min = seq.getFeatures().getMinimumScore(featureType, positional);
401     if (Float.isNaN(min))
402     {
403       return;
404     }
405
406     float max = seq.getFeatures().getMaximumScore(featureType, positional);
407
408     /*
409      * stored values are 
410      * { {positionalMin, positionalMax}, {nonPositionalMin, nonPositionalMax} }
411      */
412     if (minmax == null)
413     {
414       minmax = new Hashtable<String, float[][]>();
415     }
416     synchronized (minmax)
417     {
418       float[][] mm = minmax.get(featureType);
419       int index = positional ? 0 : 1;
420       if (mm == null)
421       {
422         mm = new float[][] { null, null };
423         minmax.put(featureType, mm);
424       }
425       if (mm[index] == null)
426       {
427         mm[index] = new float[] { min, max };
428       }
429       else
430       {
431         mm[index][0] = Math.min(mm[index][0], min);
432         mm[index][1] = Math.max(mm[index][1], max);
433       }
434     }
435   }
436   protected Boolean firing = Boolean.FALSE;
437
438   /**
439    * replaces the current renderOrder with the unordered features in
440    * allfeatures. The ordering of any types in both renderOrder and allfeatures
441    * is preserved, and all new feature types are rendered on top of the existing
442    * types, in the order given by getOrder or the order given in allFeatures.
443    * Note. this operates directly on the featureOrder hash for efficiency. TODO:
444    * eliminate the float storage for computing/recalling the persistent ordering
445    * New Cability: updates min/max for colourscheme range if its dynamic
446    * 
447    * @param allFeatures
448    */
449   private void updateRenderOrder(List<String> allFeatures)
450   {
451     List<String> allfeatures = new ArrayList<String>(allFeatures);
452     String[] oldRender = renderOrder;
453     renderOrder = new String[allfeatures.size()];
454     boolean initOrders = (featureOrder == null);
455     int opos = 0;
456     if (oldRender != null && oldRender.length > 0)
457     {
458       for (int j = 0; j < oldRender.length; j++)
459       {
460         if (oldRender[j] != null)
461         {
462           if (initOrders)
463           {
464             setOrder(oldRender[j], (1 - (1 + (float) j) / oldRender.length));
465           }
466           if (allfeatures.contains(oldRender[j]))
467           {
468             renderOrder[opos++] = oldRender[j]; // existing features always
469             // appear below new features
470             allfeatures.remove(oldRender[j]);
471             if (minmax != null)
472             {
473               float[][] mmrange = minmax.get(oldRender[j]);
474               if (mmrange != null)
475               {
476                 FeatureColourI fc = featureColours.get(oldRender[j]);
477                 if (fc != null && !fc.isSimpleColour() && fc.isAutoScaled())
478                 {
479                   fc.updateBounds(mmrange[0][0], mmrange[0][1]);
480                 }
481               }
482             }
483           }
484         }
485       }
486     }
487     if (allfeatures.size() == 0)
488     {
489       // no new features - leave order unchanged.
490       return;
491     }
492     int i = allfeatures.size() - 1;
493     int iSize = i;
494     boolean sort = false;
495     String[] newf = new String[allfeatures.size()];
496     float[] sortOrder = new float[allfeatures.size()];
497     for (String newfeat : allfeatures)
498     {
499       newf[i] = newfeat;
500       if (minmax != null)
501       {
502         // update from new features minmax if necessary
503         float[][] mmrange = minmax.get(newf[i]);
504         if (mmrange != null)
505         {
506           FeatureColourI fc = featureColours.get(newf[i]);
507           if (fc != null && !fc.isSimpleColour() && fc.isAutoScaled())
508           {
509             fc.updateBounds(mmrange[0][0], mmrange[0][1]);
510           }
511         }
512       }
513       if (initOrders || !featureOrder.containsKey(newf[i]))
514       {
515         int denom = initOrders ? allfeatures.size() : featureOrder.size();
516         // new unordered feature - compute persistent ordering at head of
517         // existing features.
518         setOrder(newf[i], i / (float) denom);
519       }
520       // set order from newly found feature from persisted ordering.
521       sortOrder[i] = 2 - featureOrder.get(newf[i]).floatValue();
522       if (i < iSize)
523       {
524         // only sort if we need to
525         sort = sort || sortOrder[i] > sortOrder[i + 1];
526       }
527       i--;
528     }
529     if (iSize > 1 && sort)
530     {
531       jalview.util.QuickSort.sort(sortOrder, newf);
532     }
533     sortOrder = null;
534     System.arraycopy(newf, 0, renderOrder, opos, newf.length);
535   }
536
537   /**
538    * get a feature style object for the given type string. Creates a
539    * java.awt.Color for a featureType with no existing colourscheme.
540    * 
541    * @param featureType
542    * @return
543    */
544   @Override
545   public FeatureColourI getFeatureStyle(String featureType)
546   {
547     FeatureColourI fc = featureColours.get(featureType);
548     if (fc == null)
549     {
550       Color col = ColorUtils.createColourFromName(featureType);
551       fc = new FeatureColour(col);
552       featureColours.put(featureType, fc);
553     }
554     return fc;
555   }
556
557   /**
558    * Returns the configured colour for a particular feature instance. This
559    * includes calculation of 'colour by label', or of a graduated score colour,
560    * if applicable. It does not take into account feature visibility or colour
561    * transparency.
562    * 
563    * @param feature
564    * @return
565    */
566   public Color getColour(SequenceFeature feature)
567   {
568     FeatureColourI fc = getFeatureStyle(feature.getType());
569     return fc.getColor(feature);
570   }
571
572   /**
573    * Answers true unless the feature has a graduated colour scheme and the
574    * feature value lies outside the current threshold for display
575    * 
576    * @param sequenceFeature
577    * @return
578    */
579   protected boolean showFeature(SequenceFeature sequenceFeature)
580   {
581     FeatureColourI fc = getFeatureStyle(sequenceFeature.type);
582     return fc.isColored(sequenceFeature);
583   }
584
585   /**
586    * Answers true if the feature type is currently selected to be displayed,
587    * else false
588    * 
589    * @param type
590    * @return
591    */
592   protected boolean showFeatureOfType(String type)
593   {
594     return type == null ? false : av.getFeaturesDisplayed().isVisible(type);
595   }
596
597   @Override
598   public void setColour(String featureType, FeatureColourI col)
599   {
600     featureColours.put(featureType, col);
601   }
602
603   @Override
604   public void setTransparency(float value)
605   {
606     transparency = value;
607   }
608
609   @Override
610   public float getTransparency()
611   {
612     return transparency;
613   }
614
615   /**
616    * analogous to colour - store a normalized ordering for all feature types in
617    * this rendering context.
618    * 
619    * @param type
620    *          Feature type string
621    * @param position
622    *          normalized priority - 0 means always appears on top, 1 means
623    *          always last.
624    */
625   public float setOrder(String type, float position)
626   {
627     if (featureOrder == null)
628     {
629       featureOrder = new Hashtable<String, Float>();
630     }
631     featureOrder.put(type, new Float(position));
632     return position;
633   }
634
635   /**
636    * get the global priority (0 (top) to 1 (bottom))
637    * 
638    * @param type
639    * @return [0,1] or -1 for a type without a priority
640    */
641   public float getOrder(String type)
642   {
643     if (featureOrder != null)
644     {
645       if (featureOrder.containsKey(type))
646       {
647         return featureOrder.get(type).floatValue();
648       }
649     }
650     return -1;
651   }
652
653   @Override
654   public Map<String, FeatureColourI> getFeatureColours()
655   {
656     return featureColours;
657   }
658
659   /**
660    * Replace current ordering with new ordering
661    * 
662    * @param data
663    *          { String(Type), Colour(Type), Boolean(Displayed) }
664    * @return true if any visible features have been reordered, else false
665    */
666   public boolean setFeaturePriority(Object[][] data)
667   {
668     return setFeaturePriority(data, true);
669   }
670
671   /**
672    * Sets the priority order for features, with the highest priority (displayed
673    * on top) at the start of the data array
674    * 
675    * @param data
676    *          { String(Type), Colour(Type), Boolean(Displayed) }
677    * @param visibleNew
678    *          when true current featureDisplay list will be cleared
679    * @return true if any visible features have been reordered or recoloured,
680    *         else false (i.e. no need to repaint)
681    */
682   public boolean setFeaturePriority(Object[][] data, boolean visibleNew)
683   {
684     /*
685      * note visible feature ordering and colours before update
686      */
687     List<String> visibleFeatures = getDisplayedFeatureTypes();
688     Map<String, FeatureColourI> visibleColours = new HashMap<String, FeatureColourI>(
689             getFeatureColours());
690
691     FeaturesDisplayedI av_featuresdisplayed = null;
692     if (visibleNew)
693     {
694       if ((av_featuresdisplayed = av.getFeaturesDisplayed()) != null)
695       {
696         av.getFeaturesDisplayed().clear();
697       }
698       else
699       {
700         av.setFeaturesDisplayed(av_featuresdisplayed = new FeaturesDisplayed());
701       }
702     }
703     else
704     {
705       av_featuresdisplayed = av.getFeaturesDisplayed();
706     }
707     if (data == null)
708     {
709       return false;
710     }
711     // The feature table will display high priority
712     // features at the top, but these are the ones
713     // we need to render last, so invert the data
714     renderOrder = new String[data.length];
715
716     if (data.length > 0)
717     {
718       for (int i = 0; i < data.length; i++)
719       {
720         String type = data[i][0].toString();
721         setColour(type, (FeatureColourI) data[i][1]);
722         if (((Boolean) data[i][2]).booleanValue())
723         {
724           av_featuresdisplayed.setVisible(type);
725         }
726
727         renderOrder[data.length - i - 1] = type;
728       }
729     }
730
731     /*
732      * get the new visible ordering and return true if it has changed
733      * order or any colour has changed
734      */
735     List<String> reorderedVisibleFeatures = getDisplayedFeatureTypes();
736     if (!visibleFeatures.equals(reorderedVisibleFeatures))
737     {
738       /*
739        * the list of ordered visible features has changed
740        */
741       return true;
742     }
743
744     /*
745      * return true if any feature colour has changed
746      */
747     for (String feature : visibleFeatures)
748     {
749       if (visibleColours.get(feature) != getFeatureStyle(feature))
750       {
751         return true;
752       }
753     }
754     return false;
755   }
756
757   /**
758    * @param listener
759    * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
760    */
761   public void addPropertyChangeListener(PropertyChangeListener listener)
762   {
763     changeSupport.addPropertyChangeListener(listener);
764   }
765
766   /**
767    * @param listener
768    * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
769    */
770   public void removePropertyChangeListener(PropertyChangeListener listener)
771   {
772     changeSupport.removePropertyChangeListener(listener);
773   }
774
775   public Set<String> getAllFeatureColours()
776   {
777     return featureColours.keySet();
778   }
779
780   public void clearRenderOrder()
781   {
782     renderOrder = null;
783   }
784
785   public boolean hasRenderOrder()
786   {
787     return renderOrder != null;
788   }
789
790   /**
791    * Returns feature types in ordering of rendering, where last means on top
792    */
793   public List<String> getRenderOrder()
794   {
795     if (renderOrder == null)
796     {
797       return Arrays.asList(new String[] {});
798     }
799     return Arrays.asList(renderOrder);
800   }
801
802   public int getFeatureGroupsSize()
803   {
804     return featureGroups != null ? 0 : featureGroups.size();
805   }
806
807   @Override
808   public List<String> getFeatureGroups()
809   {
810     // conflict between applet and desktop - featureGroups returns the map in
811     // the desktop featureRenderer
812     return (featureGroups == null) ? Arrays.asList(new String[0]) : Arrays
813             .asList(featureGroups.keySet().toArray(new String[0]));
814   }
815
816   public boolean checkGroupVisibility(String group, boolean newGroupsVisible)
817   {
818     if (featureGroups == null)
819     {
820       // then an exception happens next..
821     }
822     if (featureGroups.containsKey(group))
823     {
824       return featureGroups.get(group).booleanValue();
825     }
826     if (newGroupsVisible)
827     {
828       featureGroups.put(group, new Boolean(true));
829       return true;
830     }
831     return false;
832   }
833
834   /**
835    * get visible or invisible groups
836    * 
837    * @param visible
838    *          true to return visible groups, false to return hidden ones.
839    * @return list of groups
840    */
841   @Override
842   public List<String> getGroups(boolean visible)
843   {
844     if (featureGroups != null)
845     {
846       List<String> gp = new ArrayList<String>();
847
848       for (String grp : featureGroups.keySet())
849       {
850         Boolean state = featureGroups.get(grp);
851         if (state.booleanValue() == visible)
852         {
853           gp.add(grp);
854         }
855       }
856       return gp;
857     }
858     return null;
859   }
860
861   @Override
862   public void setGroupVisibility(String group, boolean visible)
863   {
864     featureGroups.put(group, new Boolean(visible));
865   }
866
867   @Override
868   public void setGroupVisibility(List<String> toset, boolean visible)
869   {
870     if (toset != null && toset.size() > 0 && featureGroups != null)
871     {
872       boolean rdrw = false;
873       for (String gst : toset)
874       {
875         Boolean st = featureGroups.get(gst);
876         featureGroups.put(gst, new Boolean(visible));
877         if (st != null)
878         {
879           rdrw = rdrw || (visible != st.booleanValue());
880         }
881       }
882       if (rdrw)
883       {
884         // set local flag indicating redraw needed ?
885       }
886     }
887   }
888
889   @Override
890   public Map<String, FeatureColourI> getDisplayedFeatureCols()
891   {
892     Map<String, FeatureColourI> fcols = new Hashtable<String, FeatureColourI>();
893     if (getViewport().getFeaturesDisplayed() == null)
894     {
895       return fcols;
896     }
897     Set<String> features = getViewport().getFeaturesDisplayed()
898             .getVisibleFeatures();
899     for (String feature : features)
900     {
901       fcols.put(feature, getFeatureStyle(feature));
902     }
903     return fcols;
904   }
905
906   @Override
907   public FeaturesDisplayedI getFeaturesDisplayed()
908   {
909     return av.getFeaturesDisplayed();
910   }
911
912   /**
913    * Returns a (possibly empty) list of visible feature types, in render order
914    * (last is on top)
915    */
916   @Override
917   public List<String> getDisplayedFeatureTypes()
918   {
919     List<String> typ = getRenderOrder();
920     List<String> displayed = new ArrayList<String>();
921     FeaturesDisplayedI feature_disp = av.getFeaturesDisplayed();
922     if (feature_disp != null)
923     {
924       synchronized (feature_disp)
925       {
926         for (String type : typ)
927         {
928           if (feature_disp.isVisible(type))
929           {
930             displayed.add(type);
931           }
932         }
933       }
934     }
935     return displayed;
936   }
937
938   @Override
939   public List<String> getDisplayedFeatureGroups()
940   {
941     List<String> _gps = new ArrayList<String>();
942     for (String gp : getFeatureGroups())
943     {
944       if (checkGroupVisibility(gp, false))
945       {
946         _gps.add(gp);
947       }
948     }
949     return _gps;
950   }
951
952   /**
953    * Answers true if the feature belongs to a feature group which is not
954    * currently displayed, else false
955    * 
956    * @param sequenceFeature
957    * @return
958    */
959   protected boolean featureGroupNotShown(final SequenceFeature sequenceFeature)
960   {
961     return featureGroups != null
962             && sequenceFeature.featureGroup != null
963             && sequenceFeature.featureGroup.length() != 0
964             && featureGroups.containsKey(sequenceFeature.featureGroup)
965             && !featureGroups.get(sequenceFeature.featureGroup)
966                     .booleanValue();
967   }
968
969 }