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