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