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