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