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