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