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