bugfix + create/amend whilst feature panel open (still fixing)
[jalview.git] / src / jalview / gui / FeatureRenderer.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.gui;
20
21 import java.util.*;
22
23 import java.awt.*;
24 import java.awt.event.*;
25 import java.awt.image.*;
26 import java.beans.PropertyChangeListener;
27 import java.beans.PropertyChangeSupport;
28
29 import javax.swing.*;
30
31 import jalview.datamodel.*;
32
33 /**
34  * DOCUMENT ME!
35  * 
36  * @author $author$
37  * @version $Revision$
38  */
39 public class FeatureRenderer
40 {
41   AlignViewport av;
42   Color resBoxColour;
43   float transparency = 1.0f;
44   FontMetrics fm;
45   int charOffset;
46
47   Hashtable featureColours = new Hashtable();
48
49   // A higher level for grouping features of a
50   // particular type
51   Hashtable featureGroups = null;
52
53   // This is actually an Integer held in the hashtable,
54   // Retrieved using the key feature type
55   Object currentColour;
56
57   String[] renderOrder;
58   PropertyChangeSupport changeSupport=new PropertyChangeSupport(this);
59
60   /**
61    * Creates a new FeatureRenderer object.
62    * 
63    * @param av
64    *          DOCUMENT ME!
65    */
66   public FeatureRenderer(AlignViewport av)
67   {
68     this.av = av;
69   }
70
71   public void transferSettings(FeatureRenderer fr)
72   {
73     renderOrder = fr.renderOrder;
74     featureGroups = fr.featureGroups;
75     featureColours = fr.featureColours;
76     transparency = fr.transparency;
77     featureOrder = fr.featureOrder;
78   }
79
80   BufferedImage offscreenImage;
81   boolean offscreenRender = false;
82   public Color findFeatureColour(Color initialCol, SequenceI seq, int res)
83   {
84     return new Color(findFeatureColour(initialCol.getRGB(),
85                                        seq, res));
86   }
87
88   /**
89    * This is used by the Molecule Viewer and Overview to get the accurate
90    * colourof the rendered sequence
91    */
92   public int findFeatureColour(int initialCol, SequenceI seq, int column)
93   {
94     if (!av.showSequenceFeatures)
95     {
96       return initialCol;
97     }
98
99     if (seq != lastSeq)
100     {
101       lastSeq = seq;
102       sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
103       if (sequenceFeatures!=null) 
104       {
105         sfSize = sequenceFeatures.length;
106       }
107     }
108     
109     if (sequenceFeatures!=lastSeq.getDatasetSequence().getSequenceFeatures()) {
110       sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
111       if (sequenceFeatures != null)
112       {
113         sfSize = sequenceFeatures.length;
114       }      
115     }
116     
117     if (sequenceFeatures == null)
118     {
119       return initialCol;
120     }
121
122
123     if (jalview.util.Comparison.isGap(lastSeq.getCharAt(column)))
124     {
125       return Color.white.getRGB();
126     }
127
128     // Only bother making an offscreen image if transparency is applied
129     if (transparency != 1.0f && offscreenImage == null)
130     {
131       offscreenImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
132     }
133
134     currentColour = null;
135
136     offscreenRender = true;
137
138     if (offscreenImage != null)
139     {
140       offscreenImage.setRGB(0, 0, initialCol);
141       drawSequence(offscreenImage.getGraphics(),
142                    lastSeq,
143                    column, column, 0);
144
145       return offscreenImage.getRGB(0, 0);
146     }
147     else
148     {
149       drawSequence(null,
150                    lastSeq,
151                    lastSeq.findPosition(column),
152                    -1, -1);
153
154       if (currentColour == null)
155       {
156         return initialCol;
157       }
158       else
159       {
160         return ( (Integer) currentColour).intValue();
161       }
162     }
163
164   }
165
166   /**
167    * DOCUMENT ME!
168    * 
169    * @param g
170    *          DOCUMENT ME!
171    * @param seq
172    *          DOCUMENT ME!
173    * @param sg
174    *          DOCUMENT ME!
175    * @param start
176    *          DOCUMENT ME!
177    * @param end
178    *          DOCUMENT ME!
179    * @param x1
180    *          DOCUMENT ME!
181    * @param y1
182    *          DOCUMENT ME!
183    * @param width
184    *          DOCUMENT ME!
185    * @param height
186    *          DOCUMENT ME!
187    */
188   // String type;
189   // SequenceFeature sf;
190   SequenceI lastSeq;
191   SequenceFeature[] sequenceFeatures;
192   int sfSize, sfindex, spos, epos;
193
194   public void drawSequence(Graphics g, SequenceI seq,
195                            int start, int end, int y1)
196   {
197     if (seq.getDatasetSequence().getSequenceFeatures() == null
198         || seq.getDatasetSequence().getSequenceFeatures().length == 0)
199     {
200       return;
201     }
202
203     if (g != null)
204     {
205       fm = g.getFontMetrics();
206     }
207
208     if (av.featuresDisplayed == null
209         || renderOrder == null
210         || newFeatureAdded)
211     {
212       findAllFeatures();
213       if (av.featuresDisplayed.size() < 1)
214       {
215         return;
216       }
217
218       sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
219       sfSize = sequenceFeatures.length;
220     }
221
222     if (lastSeq == null || seq != lastSeq
223         || seq.getDatasetSequence().getSequenceFeatures()!=sequenceFeatures)
224     {
225       lastSeq = seq;
226       sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
227       sfSize = sequenceFeatures.length;
228     }
229
230     if (transparency != 1 && g != null)
231     {
232       Graphics2D g2 = (Graphics2D) g;
233       g2.setComposite(
234           AlphaComposite.getInstance(
235               AlphaComposite.SRC_OVER, transparency));
236     }
237
238     if (!offscreenRender)
239     {
240       spos = lastSeq.findPosition(start);
241       epos = lastSeq.findPosition(end);
242     }
243
244     String type;
245     for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
246     {
247       type = renderOrder[renderIndex];
248
249       if (type == null || !av.featuresDisplayed.containsKey(type))
250       {
251         continue;
252       }
253
254       // loop through all features in sequence to find
255       // current feature to render
256       for (sfindex = 0; sfindex < sfSize; sfindex++)
257       {
258         if (sequenceFeatures.length <= sfindex)
259         {
260           continue;
261         }
262         if (!sequenceFeatures[sfindex].type.equals(type))
263         {
264           continue;
265         }
266
267         if (featureGroups != null
268             && sequenceFeatures[sfindex].featureGroup != null
269             &&
270             featureGroups.containsKey(sequenceFeatures[sfindex].featureGroup)
271             &&
272             ! ( (Boolean) featureGroups.get(sequenceFeatures[sfindex].
273                                             featureGroup)).
274             booleanValue())
275         {
276           continue;
277         }
278
279         if (!offscreenRender && (sequenceFeatures[sfindex].getBegin() > epos
280                                  || sequenceFeatures[sfindex].getEnd() < spos))
281         {
282           continue;
283         }
284
285         if (offscreenRender && offscreenImage == null)
286         {
287           if (sequenceFeatures[sfindex].begin <= start &&
288               sequenceFeatures[sfindex].end >= start)
289           {
290             currentColour = av.featuresDisplayed.get(sequenceFeatures[sfindex].
291                 type);
292           }
293         }
294         else if (sequenceFeatures[sfindex].type.equals("disulfide bond"))
295         {
296
297           renderFeature(g, seq,
298                         seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
299                         seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
300                         new Color( ( (Integer) av.featuresDisplayed.get(
301                             sequenceFeatures[sfindex].type)).intValue()),
302                         start, end, y1);
303           renderFeature(g, seq,
304                         seq.findIndex(sequenceFeatures[sfindex].end) - 1,
305                         seq.findIndex(sequenceFeatures[sfindex].end) - 1,
306                         new Color( ( (Integer) av.featuresDisplayed.get(
307                             sequenceFeatures[sfindex].type)).intValue()),
308                         start, end, y1);
309
310         }
311         else
312         {
313           renderFeature(g, seq,
314                         seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
315                         seq.findIndex(sequenceFeatures[sfindex].end) - 1,
316                         getColour(sequenceFeatures[sfindex].type),
317                         start, end, y1);
318         }
319
320       }
321
322     }
323
324     if (transparency != 1.0f && g != null)
325     {
326       Graphics2D g2 = (Graphics2D) g;
327       g2.setComposite(
328           AlphaComposite.getInstance(
329               AlphaComposite.SRC_OVER, 1.0f));
330     }
331   }
332
333   char s;
334   int i;
335   void renderFeature(Graphics g, SequenceI seq,
336                      int fstart, int fend, Color featureColour, int start,
337                      int end, int y1)
338   {
339
340     if ( ( (fstart <= end) && (fend >= start)))
341     {
342       if (fstart < start)
343       { // fix for if the feature we have starts before the sequence start,
344         fstart = start; // but the feature end is still valid!!
345       }
346
347       if (fend >= end)
348       {
349         fend = end;
350       }
351       int pady = (y1 + av.charHeight) - av.charHeight / 5;
352       for (i = fstart; i <= fend; i++)
353       {
354         s = seq.getCharAt(i);
355
356         if (jalview.util.Comparison.isGap(s))
357         {
358           continue;
359         }
360
361         g.setColor(featureColour);
362
363         g.fillRect( (i - start) * av.charWidth, y1, av.charWidth, av.charHeight);
364
365         if (offscreenRender || !av.validCharWidth)
366         {
367           continue;
368         }
369
370         g.setColor(Color.white);
371         charOffset = (av.charWidth - fm.charWidth(s)) / 2;
372         g.drawString(String.valueOf(s),
373                      charOffset + (av.charWidth * (i - start)),
374                      pady);
375
376       }
377     }
378   }
379
380   boolean newFeatureAdded = false;
381   /**
382    * Called when alignment in associated view has new/modified features
383    * to discover and display.
384    *
385    */
386   public void featuresAdded()
387   {
388     lastSeq=null;
389     findAllFeatures();
390   }
391
392   boolean findingFeatures = false;
393   /**
394    * search the alignment for all new features, give them a colour and display
395    * them. Then fires a PropertyChangeEvent on the changeSupport object.
396    * 
397    */
398   synchronized void findAllFeatures()
399   {
400     findAllFeatures(true); // add all new features as visible
401     if (!firing) {
402       firing=true;
403       changeSupport.firePropertyChange("changeSupport",null,null);
404       firing=false;
405     }
406   }
407   /**
408    * Searches alignment for all features and updates colours
409    * 
410    * @param newMadeVisible
411    *          if true newly added feature types will be rendered immediatly
412    */
413   synchronized void findAllFeatures(boolean newMadeVisible) {
414     newFeatureAdded = false;
415
416     if (findingFeatures)
417     {
418       newFeatureAdded = true;
419       return;
420     }
421
422     findingFeatures = true;
423
424     if (av.featuresDisplayed == null)
425     {
426       av.featuresDisplayed = new Hashtable();
427     }
428
429     Vector allfeatures = new Vector();
430     Vector oldfeatures = new Vector();
431     if (renderOrder!=null) 
432     {
433       for (int i=0; i<renderOrder.length; i++) {
434         if (renderOrder[i]!=null)
435         {
436           oldfeatures.addElement(renderOrder[i]);
437         }
438       }
439     }
440     for (int i = 0; i < av.alignment.getHeight(); i++)
441     {
442       SequenceFeature[] features
443           = av.alignment.getSequenceAt(i).getDatasetSequence().
444           getSequenceFeatures();
445
446       if (features == null)
447       {
448         continue;
449       }
450
451       int index = 0;
452       while (index < features.length)
453       {
454         if (!av.featuresDisplayed.containsKey(features[index].getType()))
455         {
456           if (! (features[index].begin == 0 && features[index].end == 0))
457           {
458             // If beginning and end are 0, the feature is for the whole sequence
459             // and we don't want to render the feature in the normal way
460
461             if (newMadeVisible && !oldfeatures.contains(features[index].getType())) {
462               // this is a new feature type on the alignment. Mark it for
463               // display.
464               av.featuresDisplayed.put(features[index].getType(),
465                                      new Integer(getColour(features[index].
466                 getType()).getRGB()));
467               setOrder(features[index].getType(),0);
468             }
469            }
470         }
471         if (!allfeatures.contains(features[index].getType()))
472         {
473           allfeatures.addElement(features[index].getType());
474         }
475         index++;
476       }
477     }
478     updateRenderOrder(allfeatures);
479     findingFeatures = false;
480   }
481   protected boolean firing=false;
482   /**
483    * replaces the current renderOrder with the unordered features in allfeatures.
484    * The ordering of any types in both renderOrder and allfeatures is preserved,
485    * and all new feature types are rendered on top of the existing types, in
486    * the order given by getOrder or the order given in allFeatures.
487    * Note. this operates directly on the featureOrder hash for efficiency. TODO:
488    * eliminate the float storage for computing/recalling the persistent ordering
489    * 
490    * @param allFeatures
491    */
492   private void updateRenderOrder(Vector allFeatures) {
493     Vector allfeatures = new Vector(allFeatures);
494     String[] oldRender = renderOrder;
495     renderOrder = new String[allfeatures.size()];
496     boolean initOrders=(featureOrder==null);
497     int opos=0;
498     if (oldRender!=null && oldRender.length>0)
499     {
500       for (int j=0; j<oldRender.length; j++)
501       {
502         if (oldRender[j]!=null)
503           {
504             if (initOrders)
505             {
506               setOrder(oldRender[j], (1-(1+(float)j)/(float) oldRender.length));
507             } 
508             if (allfeatures.contains(oldRender[j])) {
509               renderOrder[opos++]  = oldRender[j]; // existing features always
510                                                     // appear below new features
511               allfeatures.removeElement(oldRender[j]);
512             }
513           }
514         }
515     }
516     if (allfeatures.size()==0) {
517       // no new features - leave order unchanged.
518       return;
519     }
520     int i=allfeatures.size()-1;
521     int iSize=i;
522     boolean sort=false;
523     String[] newf = new String[allfeatures.size()]; 
524     float[] sortOrder = new float[allfeatures.size()];
525     Enumeration en = allfeatures.elements();
526     // sort remaining elements
527     while (en.hasMoreElements())
528     {
529       newf[i] = en.nextElement().toString();
530       if (initOrders || !featureOrder.containsKey(newf[i])) 
531       {
532         int denom = initOrders ? allfeatures.size() : featureOrder.size();
533           // new unordered feature - compute persistent ordering at head of
534           // existing features.
535         setOrder(newf[i], i/(float) denom);
536       }
537       // set order from newly found feature from persisted ordering.
538       sortOrder[i] = 2-((Float) featureOrder.get(newf[i])).floatValue();
539       if (i<iSize)
540       {
541         // only sort if we need to
542         sort = sort || sortOrder[i]>sortOrder[i+1];
543       }
544       i--;
545     }
546     if (iSize>1 && sort)
547       jalview.util.QuickSort.sort(sortOrder, newf);
548     sortOrder=null;
549     System.arraycopy(newf, 0, renderOrder, opos, newf.length);
550   }
551   public Color getColour(String featureType)
552   {
553     Color colour = (Color) featureColours.get(featureType);
554     if (colour == null)
555     {
556       jalview.schemes.UserColourScheme ucs = new
557         jalview.schemes.UserColourScheme();
558       featureColours.put(featureType,
559                          colour=ucs.createColourFromName(featureType));
560     }
561     return colour;
562   }
563
564   static String lastFeatureAdded;
565   static String lastFeatureGroupAdded;
566   static String lastDescriptionAdded;
567
568   public boolean createNewFeatures(SequenceI[] sequences,
569                                    SequenceFeature[] features)
570   {
571     return amendFeatures(sequences, features, true, null);
572   }
573
574   int featureIndex = 0;
575   boolean amendFeatures(final SequenceI[] sequences,
576                         final SequenceFeature[] features,
577                         boolean newFeatures,
578                         final AlignmentPanel ap)
579   {
580     JPanel bigPanel = new JPanel(new BorderLayout());
581     final JComboBox name = new JComboBox();
582     final JComboBox source = new JComboBox();
583     final JTextArea description = new JTextArea(3, 25);
584     final JSpinner start = new JSpinner();
585     final JSpinner end = new JSpinner();
586     start.setPreferredSize(new Dimension(80, 20));
587     end.setPreferredSize(new Dimension(80, 20));
588     final JPanel colour = new JPanel();
589     colour.setBorder(BorderFactory.createEtchedBorder());
590     colour.setMaximumSize(new Dimension(40, 10));
591     colour.addMouseListener(new MouseAdapter()
592     {
593       public void mousePressed(MouseEvent evt)
594       {
595         colour.setBackground(
596             JColorChooser.showDialog(Desktop.desktop,
597                                      "Select Feature Colour",
598                                      colour.getBackground()));
599       }
600     });
601
602     JPanel panel = new JPanel(new GridLayout(3, 2));
603     panel.add(new JLabel("Sequence Feature Name: ", JLabel.RIGHT));
604     panel.add(name);
605     panel.add(new JLabel("Feature Group: ", JLabel.RIGHT));
606     panel.add(source);
607     panel.add(new JLabel("Feature Colour: ", JLabel.RIGHT));
608     JPanel tmp = new JPanel();
609     tmp.add(colour);
610     colour.setPreferredSize(new Dimension(150, 15));
611     panel.add(tmp);
612     name.setEditable(true);
613     source.setEditable(true);
614
615     bigPanel.add(panel, BorderLayout.NORTH);
616     panel = new JPanel();
617     panel.add(new JLabel("Description: ", JLabel.RIGHT));
618     description.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
619     description.setLineWrap(true);
620     panel.add(new JScrollPane(description));
621
622     if (!newFeatures)
623     {
624       bigPanel.add(panel, BorderLayout.SOUTH);
625
626       panel = new JPanel();
627       panel.add(new JLabel(" Start:", JLabel.RIGHT));
628       panel.add(start);
629       panel.add(new JLabel("  End:", JLabel.RIGHT));
630       panel.add(end);
631       bigPanel.add(panel, BorderLayout.CENTER);
632     }
633     else
634     {
635       bigPanel.add(panel, BorderLayout.CENTER);
636     }
637
638     if (lastFeatureAdded == null)
639     {
640       if (features[0].type != null)
641       {
642         lastFeatureAdded = features[0].type;
643       }
644       else
645       {
646         lastFeatureAdded = "feature_1";
647       }
648     }
649
650     if (lastFeatureGroupAdded == null)
651     {
652       if (features[0].featureGroup != null)
653       {
654         lastFeatureGroupAdded = features[0].featureGroup;
655       }
656       else
657       {
658         lastFeatureAdded = "Jalview";
659       }
660     }
661
662     Enumeration en;
663     if (featureGroups != null)
664     {
665       en = featureGroups.keys();
666       while (en.hasMoreElements())
667       {
668         source.addItem(en.nextElement().toString());
669       }
670     }
671
672     if (newFeatures)
673     {
674       if (av.featuresDisplayed != null)
675       {
676         en = av.featuresDisplayed.keys();
677         while (en.hasMoreElements())
678         {
679           name.addItem(en.nextElement().toString());
680         }
681       }
682
683       name.setSelectedItem(lastFeatureAdded);
684       source.setSelectedItem(lastFeatureGroupAdded);
685       description.setText(
686           lastDescriptionAdded == null ?
687           features[0].description : lastDescriptionAdded);
688
689       if (getColour(lastFeatureAdded) != null)
690       {
691         colour.setBackground(getColour(lastFeatureAdded));
692       }
693       else
694       {
695         colour.setBackground(new Color(60, 160, 115));
696       }
697
698     }
699     else if (!newFeatures)
700     {
701       featureIndex = 0;
702       for (int f = 0; f < features.length; f++)
703       {
704         name.addItem(features[f].getType().toString());
705       }
706
707       description.setText(features[0].getDescription());
708       source.setSelectedItem(features[0].getFeatureGroup());
709       start.setValue(new Integer(features[0].getBegin()));
710       end.setValue(new Integer(features[0].getEnd()));
711       colour.setBackground(
712           getColour(name.getSelectedItem().toString()));
713       name.addItemListener(new ItemListener()
714       {
715         public void itemStateChanged(ItemEvent e)
716         {
717           int index = name.getSelectedIndex();
718           if (index != -1)
719           {
720             featureIndex = index;
721             description.setText(features[index].getDescription());
722             source.setSelectedItem(features[index].getFeatureGroup());
723             start.setValue(new Integer(features[index].getBegin()));
724             end.setValue(new Integer(features[index].getEnd()));
725             colour.setBackground(
726                 getColour(name.getSelectedItem().toString()));
727
728             SearchResults highlight = new SearchResults();
729             highlight.addResult(sequences[0],
730                                 features[index].getBegin(),
731                                 features[index].getEnd());
732
733             ap.seqPanel.seqCanvas.highlightSearchResults(highlight);
734
735           }
736           Color col = getColour(name.getSelectedItem().toString());
737           if (col == null)
738           {
739             col = new
740                 jalview.schemes.UserColourScheme()
741                 .createColourFromName(name.getSelectedItem().toString());
742           }
743
744           colour.setBackground(col);
745         }
746       });
747
748     }
749
750     Object[] options;
751     if (!newFeatures)
752     {
753       options = new Object[]
754           {
755           "Amend", "Delete", "Cancel"};
756     }
757     else
758     {
759       options = new Object[]
760           {
761           "OK", "Cancel"};
762     }
763
764     String title = newFeatures ? "Create New Sequence Feature(s)" :
765         "Amend/Delete Features for "
766         + sequences[0].getName();
767
768     int reply = JOptionPane.showInternalOptionDialog(Desktop.desktop,
769         bigPanel,
770         title,
771         JOptionPane.YES_NO_CANCEL_OPTION,
772         JOptionPane.QUESTION_MESSAGE,
773         null,
774         options, "OK");
775
776     jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
777
778     if (reply == JOptionPane.OK_OPTION
779         && name.getSelectedItem() != null
780         && source.getSelectedItem() != null)
781     {
782       // This ensures that the last sequence
783       // is refreshed and new features are rendered
784       lastSeq = null;
785       lastFeatureAdded = name.getSelectedItem().toString();
786       lastFeatureGroupAdded = source.getSelectedItem().toString();
787       lastDescriptionAdded = description.getText().replaceAll("\n", " ");
788     }
789
790     if (!newFeatures)
791     {
792       SequenceFeature sf = features[featureIndex];
793
794       if (reply == JOptionPane.NO_OPTION)
795       {
796         sequences[0].getDatasetSequence().deleteFeature(sf);
797       }
798       else if (reply == JOptionPane.YES_OPTION)
799       {
800         sf.type = lastFeatureAdded;
801         sf.featureGroup = lastFeatureGroupAdded;
802         sf.description = lastDescriptionAdded;
803         setColour(sf.type, colour.getBackground());
804         try
805         {
806           sf.begin = ( (Integer) start.getValue()).intValue();
807           sf.end = ( (Integer) end.getValue()).intValue();
808         }
809         catch (NumberFormatException ex)
810         {}
811
812         ffile.parseDescriptionHTML(sf, false);
813       }
814     }
815     else
816     {
817       if (reply == JOptionPane.OK_OPTION
818           && name.getSelectedItem() != null
819           && source.getSelectedItem() != null)
820       {
821         for (int i = 0; i < sequences.length; i++)
822         {
823           features[i].type = lastFeatureAdded;
824           features[i].featureGroup = lastFeatureGroupAdded;
825           features[i].description = lastDescriptionAdded;
826           sequences[i].addSequenceFeature(features[i]);
827           ffile.parseDescriptionHTML(features[i], false);
828         }
829
830         if (av.featuresDisplayed == null)
831         {
832           av.featuresDisplayed = new Hashtable();
833         }
834
835         if (featureGroups == null)
836         {
837           featureGroups = new Hashtable();
838         }
839
840         featureGroups.put(lastFeatureGroupAdded, new Boolean(true));
841
842         Color col = colour.getBackground();
843         setColour(lastFeatureAdded, colour.getBackground());
844
845         av.featuresDisplayed.put(lastFeatureGroupAdded,
846                                  new Integer(col.getRGB()));
847
848         findAllFeatures();
849
850         return true;
851       }
852       else
853       {
854         return false;
855       }
856     }
857
858     if (name.getSelectedIndex() == -1)
859     {
860       findAllFeatures();
861     }
862
863     return true;
864   }
865
866   public void setColour(String featureType, Color col)
867   {
868     featureColours.put(featureType, col);
869   }
870
871   public void setTransparency(float value)
872   {
873     transparency = value;
874   }
875
876   public float getTransparency()
877   {
878     return transparency;
879   }
880   /**
881    * Replace current ordering with new ordering
882    * @param data { String(Type), Colour(Type), Boolean(Displayed) }
883    */
884   public void setFeaturePriority(Object[][] data)
885   {
886     setFeaturePriority(data, true);
887   }
888   /**
889    * 
890    * @param data { String(Type), Colour(Type), Boolean(Displayed) }
891    * @param visibleNew when true current featureDisplay list will be cleared
892    */
893   public void setFeaturePriority(Object[][] data, boolean visibleNew)
894   {
895     if (visibleNew)
896       {
897       if (av.featuresDisplayed != null)
898       {
899         av.featuresDisplayed.clear();
900       }
901       else
902       {
903         av.featuresDisplayed = new Hashtable();
904       }
905     }
906     if (data==null) 
907     {
908       return;
909     }
910     
911     // The feature table will display high priority
912     // features at the top, but theses are the ones
913     // we need to render last, so invert the data
914     renderOrder = new String[data.length];
915
916     if (data.length > 0)
917     {
918       for (int i = 0; i < data.length; i++)
919       {
920         String type = data[i][0].toString();
921         setColour(type, (Color) data[i][1]);
922         if ( ( (Boolean) data[i][2]).booleanValue())
923         {
924           av.featuresDisplayed.put(type, new Integer(getColour(type).getRGB()));
925         }
926
927         renderOrder[data.length - i - 1] = type;
928       }
929     }
930
931   }
932   Hashtable featureOrder=null;
933   /**
934    * analogous to colour - store a normalized ordering for all feature types in
935    * this rendering context.
936    * 
937    * @param type
938    *          Feature type string
939    * @param position
940    *          normalized priority - 0 means always appears on top, 1 means
941    *          always last.
942    */
943   public float setOrder(String type, float position)
944   {
945     if (featureOrder==null) 
946     {
947       featureOrder = new Hashtable();
948     }
949     featureOrder.put(type, new Float(position));
950     return position;
951   }
952   /**
953    * get the global priority (0 (top) to 1 (bottom))
954    * 
955    * @param type
956    * @return [0,1] or -1 for a type without a priority
957    */
958   public float getOrder(String type) {
959     if (featureOrder!=null) 
960     {
961       if (featureOrder.containsKey(type))
962       {
963         return ((Float)featureOrder.get(type)).floatValue();
964       }
965     }
966     return -1;
967   }
968
969   /**
970    * @param listener
971    * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
972    */
973   public void addPropertyChangeListener(PropertyChangeListener listener)
974   {
975     changeSupport.addPropertyChangeListener(listener);
976   }
977
978   /**
979    * @param listener
980    * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
981    */
982   public void removePropertyChangeListener(PropertyChangeListener listener)
983   {
984     changeSupport.removePropertyChangeListener(listener);
985   }
986 }