JAL-2418 source formatting
[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.Iterator;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.Set;
45 import java.util.concurrent.ConcurrentHashMap;
46
47 public abstract class FeatureRendererModel
48         implements jalview.api.FeatureRenderer
49 {
50
51   /**
52    * global transparency for feature
53    */
54   protected float transparency = 1.0f;
55
56   protected Map<String, FeatureColourI> featureColours = new ConcurrentHashMap<String, FeatureColourI>();
57
58   protected Map<String, Boolean> featureGroups = new ConcurrentHashMap<String, Boolean>();
59
60   protected String[] renderOrder;
61
62   Map<String, Float> featureOrder = null;
63
64   protected PropertyChangeSupport changeSupport = new PropertyChangeSupport(
65           this);
66
67   protected AlignViewportI av;
68
69   @Override
70   public AlignViewportI getViewport()
71   {
72     return av;
73   }
74
75   public FeatureRendererSettings getSettings()
76   {
77     return new FeatureRendererSettings(this);
78   }
79
80   public void transferSettings(FeatureRendererSettings fr)
81   {
82     this.renderOrder = fr.renderOrder;
83     this.featureGroups = fr.featureGroups;
84     this.featureColours = fr.featureColours;
85     this.transparency = fr.transparency;
86     this.featureOrder = fr.featureOrder;
87   }
88
89   /**
90    * update from another feature renderer
91    * 
92    * @param fr
93    *          settings to copy
94    */
95   public void transferSettings(jalview.api.FeatureRenderer _fr)
96   {
97     FeatureRenderer fr = (FeatureRenderer) _fr;
98     FeatureRendererSettings frs = new FeatureRendererSettings(fr);
99     this.renderOrder = frs.renderOrder;
100     this.featureGroups = frs.featureGroups;
101     this.featureColours = frs.featureColours;
102     this.transparency = frs.transparency;
103     this.featureOrder = frs.featureOrder;
104     if (av != null && av != fr.getViewport())
105     {
106       // copy over the displayed feature settings
107       if (_fr.getFeaturesDisplayed() != null)
108       {
109         FeaturesDisplayedI fd = getFeaturesDisplayed();
110         if (fd == null)
111         {
112           setFeaturesDisplayedFrom(_fr.getFeaturesDisplayed());
113         }
114         else
115         {
116           synchronized (fd)
117           {
118             fd.clear();
119             java.util.Iterator<String> fdisp = _fr.getFeaturesDisplayed()
120                     .getVisibleFeatures();
121             while (fdisp.hasNext())
122             {
123               fd.setVisible(fdisp.next());
124             }
125           }
126         }
127       }
128     }
129   }
130
131   public void setFeaturesDisplayedFrom(FeaturesDisplayedI featuresDisplayed)
132   {
133     av.setFeaturesDisplayed(new FeaturesDisplayed(featuresDisplayed));
134   }
135
136   @Override
137   public void setVisible(String featureType)
138   {
139     FeaturesDisplayedI fdi = av.getFeaturesDisplayed();
140     if (fdi == null)
141     {
142       av.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
143     }
144     if (!fdi.isRegistered(featureType))
145     {
146       pushFeatureType(Arrays.asList(new String[] { featureType }));
147     }
148     fdi.setVisible(featureType);
149   }
150
151   @Override
152   public void setAllVisible(List<String> featureTypes)
153   {
154     FeaturesDisplayedI fdi = av.getFeaturesDisplayed();
155     if (fdi == null)
156     {
157       av.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
158     }
159     List<String> nft = new ArrayList<String>();
160     for (String featureType : featureTypes)
161     {
162       if (!fdi.isRegistered(featureType))
163       {
164         nft.add(featureType);
165       }
166     }
167     if (nft.size() > 0)
168     {
169       pushFeatureType(nft);
170     }
171     fdi.setAllVisible(featureTypes);
172   }
173
174   /**
175    * push a set of new types onto the render order stack. Note - this is a
176    * direct mechanism rather than the one employed in updateRenderOrder
177    * 
178    * @param types
179    */
180   private void pushFeatureType(List<String> types)
181   {
182
183     int ts = types.size();
184     String neworder[] = new String[(renderOrder == null ? 0
185             : renderOrder.length) + ts];
186     types.toArray(neworder);
187     if (renderOrder != null)
188     {
189       System.arraycopy(neworder, 0, neworder, renderOrder.length, ts);
190       System.arraycopy(renderOrder, 0, neworder, 0, renderOrder.length);
191     }
192     renderOrder = neworder;
193   }
194
195   protected Map<String, float[][]> minmax = new Hashtable<String, float[][]>();
196
197   public Map<String, float[][]> getMinMax()
198   {
199     return minmax;
200   }
201
202   /**
203    * normalise a score against the max/min bounds for the feature type.
204    * 
205    * @param sequenceFeature
206    * @return byte[] { signed, normalised signed (-127 to 127) or unsigned
207    *         (0-255) value.
208    */
209   protected final byte[] normaliseScore(SequenceFeature sequenceFeature)
210   {
211     float[] mm = minmax.get(sequenceFeature.type)[0];
212     final byte[] r = new byte[] { 0, (byte) 255 };
213     if (mm != null)
214     {
215       if (r[0] != 0 || mm[0] < 0.0)
216       {
217         r[0] = 1;
218         r[1] = (byte) ((int) 128.0
219                 + 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,
269           int res)
270   {
271     ArrayList<SequenceFeature> tmp = new ArrayList<SequenceFeature>();
272     SequenceFeature[] features = sequence.getSequenceFeatures();
273
274     if (features != null)
275     {
276       for (int i = 0; i < features.length; i++)
277       {
278         if (!av.areFeaturesDisplayed() || !av.getFeaturesDisplayed()
279                 .isVisible(features[i].getType()))
280         {
281           continue;
282         }
283
284         if (features[i].featureGroup != null && featureGroups != null
285                 && featureGroups.containsKey(features[i].featureGroup)
286                 && !featureGroups.get(features[i].featureGroup)
287                         .booleanValue())
288         {
289           continue;
290         }
291
292         // check if start/end are at res, and if not a contact feature, that res
293         // lies between start and end
294         if ((features[i].getBegin() == res || features[i].getEnd() == res)
295                 || (!features[i].isContactFeature()
296                         && (features[i].getBegin() < res)
297                         && (features[i].getEnd() >= res)))
298         {
299           tmp.add(features[i]);
300         }
301       }
302     }
303     return tmp;
304   }
305
306   /**
307    * Searches alignment for all features and updates colours
308    * 
309    * @param newMadeVisible
310    *          if true newly added feature types will be rendered immediatly
311    *          TODO: check to see if this method should actually be proxied so
312    *          repaint events can be propagated by the renderer code
313    */
314   @Override
315   public synchronized void findAllFeatures(boolean newMadeVisible)
316   {
317     newFeatureAdded = false;
318
319     if (findingFeatures)
320     {
321       newFeatureAdded = true;
322       return;
323     }
324
325     findingFeatures = true;
326     if (av.getFeaturesDisplayed() == null)
327     {
328       av.setFeaturesDisplayed(new FeaturesDisplayed());
329     }
330     FeaturesDisplayedI featuresDisplayed = av.getFeaturesDisplayed();
331
332     ArrayList<String> allfeatures = new ArrayList<String>();
333     ArrayList<String> oldfeatures = new ArrayList<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     if (minmax == null)
345     {
346       minmax = new Hashtable<String, float[][]>();
347     }
348
349     Set<String> oldGroups = new HashSet<String>(featureGroups.keySet());
350     AlignmentI alignment = av.getAlignment();
351     for (int i = 0; i < alignment.getHeight(); i++)
352     {
353       SequenceI asq = alignment.getSequenceAt(i);
354       SequenceFeature[] features = asq.getSequenceFeatures();
355
356       if (features == null)
357       {
358         continue;
359       }
360
361       int index = 0;
362       while (index < features.length)
363       {
364         String fgrp = features[index].getFeatureGroup();
365         oldGroups.remove(fgrp);
366         if (!featuresDisplayed.isRegistered(features[index].getType()))
367         {
368           if (fgrp != null)
369           {
370             Boolean groupDisplayed = featureGroups.get(fgrp);
371             if (groupDisplayed == null)
372             {
373               groupDisplayed = Boolean.valueOf(newMadeVisible);
374               featureGroups.put(fgrp, groupDisplayed);
375             }
376             if (!groupDisplayed.booleanValue())
377             {
378               index++;
379               continue;
380             }
381           }
382           if (!(features[index].begin == 0 && features[index].end == 0))
383           {
384             // If beginning and end are 0, the feature is for the whole sequence
385             // and we don't want to render the feature in the normal way
386
387             if (newMadeVisible
388                     && !oldfeatures.contains(features[index].getType()))
389             {
390               // this is a new feature type on the alignment. Mark it for
391               // display.
392               featuresDisplayed.setVisible(features[index].getType());
393               setOrder(features[index].getType(), 0);
394             }
395           }
396         }
397         if (!allfeatures.contains(features[index].getType()))
398         {
399           allfeatures.add(features[index].getType());
400         }
401         if (!Float.isNaN(features[index].score))
402         {
403           int nonpos = features[index].getBegin() >= 1 ? 0 : 1;
404           float[][] mm = minmax.get(features[index].getType());
405           if (mm == null)
406           {
407             mm = new float[][] { null, null };
408             minmax.put(features[index].getType(), mm);
409           }
410           if (mm[nonpos] == null)
411           {
412             mm[nonpos] = new float[] { features[index].score,
413                 features[index].score };
414
415           }
416           else
417           {
418             if (mm[nonpos][0] > features[index].score)
419             {
420               mm[nonpos][0] = features[index].score;
421             }
422             if (mm[nonpos][1] < features[index].score)
423             {
424               mm[nonpos][1] = features[index].score;
425             }
426           }
427         }
428         index++;
429       }
430     }
431
432     /*
433      * oldGroups now consists of groups that no longer 
434      * have any feature in them - remove these
435      */
436     for (String grp : oldGroups)
437     {
438       featureGroups.remove(grp);
439     }
440
441     updateRenderOrder(allfeatures);
442     findingFeatures = false;
443   }
444
445   protected Boolean firing = Boolean.FALSE;
446
447   /**
448    * replaces the current renderOrder with the unordered features in
449    * allfeatures. The ordering of any types in both renderOrder and allfeatures
450    * is preserved, and all new feature types are rendered on top of the existing
451    * types, in the order given by getOrder or the order given in allFeatures.
452    * Note. this operates directly on the featureOrder hash for efficiency. TODO:
453    * eliminate the float storage for computing/recalling the persistent ordering
454    * New Cability: updates min/max for colourscheme range if its dynamic
455    * 
456    * @param allFeatures
457    */
458   private void updateRenderOrder(List<String> allFeatures)
459   {
460     List<String> allfeatures = new ArrayList<String>(allFeatures);
461     String[] oldRender = renderOrder;
462     renderOrder = new String[allfeatures.size()];
463     boolean initOrders = (featureOrder == null);
464     int opos = 0;
465     if (oldRender != null && oldRender.length > 0)
466     {
467       for (int j = 0; j < oldRender.length; j++)
468       {
469         if (oldRender[j] != null)
470         {
471           if (initOrders)
472           {
473             setOrder(oldRender[j],
474                     (1 - (1 + (float) j) / oldRender.length));
475           }
476           if (allfeatures.contains(oldRender[j]))
477           {
478             renderOrder[opos++] = oldRender[j]; // existing features always
479             // appear below new features
480             allfeatures.remove(oldRender[j]);
481             if (minmax != null)
482             {
483               float[][] mmrange = minmax.get(oldRender[j]);
484               if (mmrange != null)
485               {
486                 FeatureColourI fc = featureColours.get(oldRender[j]);
487                 if (fc != null && !fc.isSimpleColour() && fc.isAutoScaled())
488                 {
489                   fc.updateBounds(mmrange[0][0], mmrange[0][1]);
490                 }
491               }
492             }
493           }
494         }
495       }
496     }
497     if (allfeatures.size() == 0)
498     {
499       // no new features - leave order unchanged.
500       return;
501     }
502     int i = allfeatures.size() - 1;
503     int iSize = i;
504     boolean sort = false;
505     String[] newf = new String[allfeatures.size()];
506     float[] sortOrder = new float[allfeatures.size()];
507     for (String newfeat : allfeatures)
508     {
509       newf[i] = newfeat;
510       if (minmax != null)
511       {
512         // update from new features minmax if necessary
513         float[][] mmrange = minmax.get(newf[i]);
514         if (mmrange != null)
515         {
516           FeatureColourI fc = featureColours.get(newf[i]);
517           if (fc != null && !fc.isSimpleColour() && fc.isAutoScaled())
518           {
519             fc.updateBounds(mmrange[0][0], mmrange[0][1]);
520           }
521         }
522       }
523       if (initOrders || !featureOrder.containsKey(newf[i]))
524       {
525         int denom = initOrders ? allfeatures.size() : featureOrder.size();
526         // new unordered feature - compute persistent ordering at head of
527         // existing features.
528         setOrder(newf[i], i / (float) denom);
529       }
530       // set order from newly found feature from persisted ordering.
531       sortOrder[i] = 2 - featureOrder.get(newf[i]).floatValue();
532       if (i < iSize)
533       {
534         // only sort if we need to
535         sort = sort || sortOrder[i] > sortOrder[i + 1];
536       }
537       i--;
538     }
539     if (iSize > 1 && sort)
540     {
541       jalview.util.QuickSort.sort(sortOrder, newf);
542     }
543     sortOrder = null;
544     System.arraycopy(newf, 0, renderOrder, opos, newf.length);
545   }
546
547   /**
548    * get a feature style object for the given type string. Creates a
549    * java.awt.Color for a featureType with no existing colourscheme.
550    * 
551    * @param featureType
552    * @return
553    */
554   @Override
555   public FeatureColourI getFeatureStyle(String featureType)
556   {
557     FeatureColourI fc = featureColours.get(featureType);
558     if (fc == null)
559     {
560       Color col = ColorUtils.createColourFromName(featureType);
561       fc = new FeatureColour(col);
562       featureColours.put(featureType, fc);
563     }
564     return fc;
565   }
566
567   /**
568    * Returns the configured colour for a particular feature instance. This
569    * includes calculation of 'colour by label', or of a graduated score colour,
570    * if applicable. It does not take into account feature visibility or colour
571    * transparency.
572    * 
573    * @param feature
574    * @return
575    */
576   public Color getColour(SequenceFeature feature)
577   {
578     FeatureColourI fc = getFeatureStyle(feature.getType());
579     return fc.isColored(feature) ? fc.getColor(feature) : null;
580   }
581
582   /**
583    * Answers true unless the feature has a score value which lies outside a
584    * minimum or maximum threshold configured for colouring. This method does not
585    * check feature type or group visibility.
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
684    * 
685    * @param data
686    *          { String(Type), Colour(Type), Boolean(Displayed) }
687    * @param visibleNew
688    *          when true current featureDisplay list will be cleared
689    * @return true if any visible features have been reordered or recoloured,
690    *         else false (i.e. no need to repaint)
691    */
692   public boolean setFeaturePriority(Object[][] data, boolean visibleNew)
693   {
694     /*
695      * note visible feature ordering and colours before update
696      */
697     List<String> visibleFeatures = getDisplayedFeatureTypes();
698     Map<String, FeatureColourI> visibleColours = new HashMap<String, FeatureColourI>(
699             getFeatureColours());
700
701     FeaturesDisplayedI av_featuresdisplayed = null;
702     if (visibleNew)
703     {
704       if ((av_featuresdisplayed = av.getFeaturesDisplayed()) != null)
705       {
706         av.getFeaturesDisplayed().clear();
707       }
708       else
709       {
710         av.setFeaturesDisplayed(
711                 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])
824             : Arrays.asList(featureGroups.keySet().toArray(new String[0]));
825   }
826
827   public boolean checkGroupVisibility(String group,
828           boolean newGroupsVisible)
829   {
830     if (featureGroups == null)
831     {
832       // then an exception happens next..
833     }
834     if (featureGroups.containsKey(group))
835     {
836       return featureGroups.get(group).booleanValue();
837     }
838     if (newGroupsVisible)
839     {
840       featureGroups.put(group, new Boolean(true));
841       return true;
842     }
843     return false;
844   }
845
846   /**
847    * get visible or invisible groups
848    * 
849    * @param visible
850    *          true to return visible groups, false to return hidden ones.
851    * @return list of groups
852    */
853   @Override
854   public List<String> getGroups(boolean visible)
855   {
856     if (featureGroups != null)
857     {
858       List<String> gp = new ArrayList<String>();
859
860       for (String grp : featureGroups.keySet())
861       {
862         Boolean state = featureGroups.get(grp);
863         if (state.booleanValue() == visible)
864         {
865           gp.add(grp);
866         }
867       }
868       return gp;
869     }
870     return null;
871   }
872
873   @Override
874   public void setGroupVisibility(String group, boolean visible)
875   {
876     featureGroups.put(group, new Boolean(visible));
877   }
878
879   @Override
880   public void setGroupVisibility(List<String> toset, boolean visible)
881   {
882     if (toset != null && toset.size() > 0 && featureGroups != null)
883     {
884       boolean rdrw = false;
885       for (String gst : toset)
886       {
887         Boolean st = featureGroups.get(gst);
888         featureGroups.put(gst, new Boolean(visible));
889         if (st != null)
890         {
891           rdrw = rdrw || (visible != st.booleanValue());
892         }
893       }
894       if (rdrw)
895       {
896         // set local flag indicating redraw needed ?
897       }
898     }
899   }
900
901   @Override
902   public Map<String, FeatureColourI> getDisplayedFeatureCols()
903   {
904     Map<String, FeatureColourI> fcols = new Hashtable<String, FeatureColourI>();
905     if (getViewport().getFeaturesDisplayed() == null)
906     {
907       return fcols;
908     }
909     Iterator<String> features = getViewport().getFeaturesDisplayed()
910             .getVisibleFeatures();
911     while (features.hasNext())
912     {
913       String feature = features.next();
914       fcols.put(feature, getFeatureStyle(feature));
915     }
916     return fcols;
917   }
918
919   @Override
920   public FeaturesDisplayedI getFeaturesDisplayed()
921   {
922     return av.getFeaturesDisplayed();
923   }
924
925   /**
926    * Returns a (possibly empty) list of visible feature types, in render order
927    * (last is on top)
928    */
929   @Override
930   public List<String> getDisplayedFeatureTypes()
931   {
932     List<String> typ = getRenderOrder();
933     List<String> displayed = new ArrayList<String>();
934     FeaturesDisplayedI feature_disp = av.getFeaturesDisplayed();
935     if (feature_disp != null)
936     {
937       synchronized (feature_disp)
938       {
939         for (String type : typ)
940         {
941           if (feature_disp.isVisible(type))
942           {
943             displayed.add(type);
944           }
945         }
946       }
947     }
948     return displayed;
949   }
950
951   @Override
952   public List<String> getDisplayedFeatureGroups()
953   {
954     List<String> _gps = new ArrayList<String>();
955     boolean valid = false;
956     for (String gp : getFeatureGroups())
957     {
958       if (checkGroupVisibility(gp, false))
959       {
960         valid = true;
961         _gps.add(gp);
962       }
963       if (!valid)
964       {
965         return null;
966       }
967       else
968       {
969         // gps = new String[_gps.size()];
970         // _gps.toArray(gps);
971       }
972     }
973     return _gps;
974   }
975
976 }