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