fixed deadlock
[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 = new Hashtable();
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 || sfSize==0)
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   synchronized public void drawSequence(Graphics g, SequenceI seq,
200                            int start, int end, int y1)
201   {
202
203     if (seq.getDatasetSequence().getSequenceFeatures() == null
204         || seq.getDatasetSequence().getSequenceFeatures().length == 0)
205     {
206       return;
207     }
208
209     if (g != null)
210     {
211       fm = g.getFontMetrics();
212     }
213
214     if (av.featuresDisplayed == null
215         || renderOrder == null
216         || newFeatureAdded)
217     {
218       findAllFeatures();
219       if (av.featuresDisplayed.size() < 1)
220       {
221         return;
222       }
223
224       sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
225     }
226
227     if (lastSeq == null || seq != lastSeq
228         || seq.getDatasetSequence().getSequenceFeatures()!=sequenceFeatures)
229     {
230       lastSeq = seq;
231       sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
232     }
233
234     if (transparency != 1 && g != null)
235     {
236       Graphics2D g2 = (Graphics2D) g;
237       g2.setComposite(
238           AlphaComposite.getInstance(
239               AlphaComposite.SRC_OVER, transparency));
240     }
241
242     if (!offscreenRender)
243     {
244       spos = lastSeq.findPosition(start);
245       epos = lastSeq.findPosition(end);
246     }
247
248     sfSize = sequenceFeatures.length;
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[sfindex].type.equals(type))
264         {
265           continue;
266         }
267
268         if (featureGroups != null
269             && sequenceFeatures[sfindex].featureGroup != null
270             &&
271             sequenceFeatures[sfindex].featureGroup.length()!=0
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   void findAllFeatures()
401   {
402     synchronized (firing)
403     {
404         if (firing.equals(Boolean.FALSE)) {
405           firing=Boolean.TRUE;
406           findAllFeatures(true); // add all new features as visible
407           changeSupport.firePropertyChange("changeSupport",null,null);
408           firing=Boolean.FALSE;
409       }
410     }
411   }
412   /**
413    * Searches alignment for all features and updates colours
414    *
415    * @param newMadeVisible
416    *          if true newly added feature types will be rendered immediatly
417    */
418   synchronized void findAllFeatures(boolean newMadeVisible) {
419     newFeatureAdded = false;
420
421     if (findingFeatures)
422     {
423       newFeatureAdded = true;
424       return;
425     }
426
427     findingFeatures = true;
428
429     if (av.featuresDisplayed == null)
430     {
431       av.featuresDisplayed = new Hashtable();
432     }
433
434     allfeatures = new Vector();
435     Vector oldfeatures = new Vector();
436     if (renderOrder!=null)
437     {
438       for (int i=0; i<renderOrder.length; i++) {
439         if (renderOrder[i]!=null)
440         {
441           oldfeatures.addElement(renderOrder[i]);
442         }
443       }
444     }
445     for (int i = 0; i < av.alignment.getHeight(); i++)
446     {
447       SequenceFeature[] features
448           = av.alignment.getSequenceAt(i).getDatasetSequence().
449           getSequenceFeatures();
450
451       if (features == null)
452       {
453         continue;
454       }
455
456       int index = 0;
457       while (index < features.length)
458       {
459         if (!av.featuresDisplayed.containsKey(features[index].getType()))
460         {
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               index++;
470               continue;
471             }
472           }
473
474
475           if (! (features[index].begin == 0 && features[index].end == 0))
476           {
477             // If beginning and end are 0, the feature is for the whole sequence
478             // and we don't want to render the feature in the normal way
479
480             if (newMadeVisible && !oldfeatures.contains(features[index].getType())) {
481               // this is a new feature type on the alignment. Mark it for
482               // display.
483               av.featuresDisplayed.put(features[index].getType(),
484                                      new Integer(getColour(features[index].
485                 getType()).getRGB()));
486               setOrder(features[index].getType(),0);
487             }
488            }
489         }
490         if (!allfeatures.contains(features[index].getType()))
491         {
492           allfeatures.addElement(features[index].getType());
493         }
494         index++;
495       }
496     }
497     updateRenderOrder(allfeatures);
498     findingFeatures = false;
499   }
500   protected Boolean firing=Boolean.FALSE;
501   /**
502    * replaces the current renderOrder with the unordered features in allfeatures.
503    * The ordering of any types in both renderOrder and allfeatures is preserved,
504    * and all new feature types are rendered on top of the existing types, in
505    * the order given by getOrder or the order given in allFeatures.
506    * Note. this operates directly on the featureOrder hash for efficiency. TODO:
507    * eliminate the float storage for computing/recalling the persistent ordering
508    *
509    * @param allFeatures
510    */
511   private void updateRenderOrder(Vector allFeatures) {
512     Vector allfeatures = new Vector(allFeatures);
513     String[] oldRender = renderOrder;
514     renderOrder = new String[allfeatures.size()];
515     boolean initOrders=(featureOrder==null);
516     int opos=0;
517     if (oldRender!=null && oldRender.length>0)
518     {
519       for (int j=0; j<oldRender.length; j++)
520       {
521         if (oldRender[j]!=null)
522           {
523             if (initOrders)
524             {
525               setOrder(oldRender[j], (1-(1+(float)j)/(float) oldRender.length));
526             }
527             if (allfeatures.contains(oldRender[j])) {
528               renderOrder[opos++]  = oldRender[j]; // existing features always
529                                                     // appear below new features
530               allfeatures.removeElement(oldRender[j]);
531             }
532           }
533         }
534     }
535     if (allfeatures.size()==0) {
536       // no new features - leave order unchanged.
537       return;
538     }
539     int i=allfeatures.size()-1;
540     int iSize=i;
541     boolean sort=false;
542     String[] newf = new String[allfeatures.size()];
543     float[] sortOrder = new float[allfeatures.size()];
544     Enumeration en = allfeatures.elements();
545     // sort remaining elements
546     while (en.hasMoreElements())
547     {
548       newf[i] = en.nextElement().toString();
549       if (initOrders || !featureOrder.containsKey(newf[i]))
550       {
551         int denom = initOrders ? allfeatures.size() : featureOrder.size();
552           // new unordered feature - compute persistent ordering at head of
553           // existing features.
554         setOrder(newf[i], i/(float) denom);
555       }
556       // set order from newly found feature from persisted ordering.
557       sortOrder[i] = 2-((Float) featureOrder.get(newf[i])).floatValue();
558       if (i<iSize)
559       {
560         // only sort if we need to
561         sort = sort || sortOrder[i]>sortOrder[i+1];
562       }
563       i--;
564     }
565     if (iSize>1 && sort)
566       jalview.util.QuickSort.sort(sortOrder, newf);
567     sortOrder=null;
568     System.arraycopy(newf, 0, renderOrder, opos, newf.length);
569   }
570   public Color getColour(String featureType)
571   {
572     if (!featureColours.containsKey(featureType))
573     {
574       jalview.schemes.UserColourScheme ucs = new
575           jalview.schemes.UserColourScheme();
576       Color col = ucs.createColourFromName(featureType);
577       featureColours.put(featureType, col);
578       return col;
579     }
580     else
581       return (Color) featureColours.get(featureType);
582   }
583
584   static String lastFeatureAdded;
585   static String lastFeatureGroupAdded;
586   static String lastDescriptionAdded;
587
588   int featureIndex = 0;
589   boolean amendFeatures(final SequenceI[] sequences,
590                         final SequenceFeature[] features,
591                         boolean newFeatures,
592                         final AlignmentPanel ap)
593   {
594
595     featureIndex = 0;
596
597     JPanel bigPanel = new JPanel(new BorderLayout());
598     final JComboBox overlaps;
599     final JTextField name = new JTextField(25);
600     final JTextField source = new JTextField(25);
601     final JTextArea description = new JTextArea(3, 25);
602     final JSpinner start = new JSpinner();
603     final JSpinner end = new JSpinner();
604     start.setPreferredSize(new Dimension(80, 20));
605     end.setPreferredSize(new Dimension(80, 20));
606
607     final JPanel colour = new JPanel();
608     colour.setBorder(BorderFactory.createEtchedBorder());
609     colour.setMaximumSize(new Dimension(40, 10));
610     colour.addMouseListener(new MouseAdapter()
611     {
612       public void mousePressed(MouseEvent evt)
613       {
614         Color col = JColorChooser.showDialog(Desktop.desktop,
615                                              "Select Feature Colour",
616                                              colour.getBackground());
617         if (col != null)
618           colour.setBackground(col);
619
620       }
621     });
622
623     JPanel tmp = new JPanel();
624     JPanel panel = new JPanel(new GridLayout(3, 1));
625
626     ///////////////////////////////////////
627     ///MULTIPLE FEATURES AT SELECTED RESIDUE
628     if(!newFeatures && features.length>1)
629     {
630      panel = new JPanel(new GridLayout(4, 1));
631      tmp = new JPanel();
632      tmp.add(new JLabel("Select Feature: "));
633      overlaps = new JComboBox();
634      for(int i=0; i<features.length; i++)
635      {
636        overlaps.addItem(features[i].getType()
637         +"/"+features[i].getBegin()+"-"+features[i].getEnd()
638         +" ("+features[i].getFeatureGroup()+")");
639      }
640
641      tmp.add(overlaps);
642
643      overlaps.addItemListener(new ItemListener()
644      {
645        public void itemStateChanged(ItemEvent e)
646        {
647          int index = overlaps.getSelectedIndex();
648          if (index != -1)
649          {
650            featureIndex = index;
651            name.setText(features[index].getType());
652            description.setText(features[index].getDescription());
653            source.setText(features[index].getFeatureGroup());
654            start.setValue(new Integer(features[index].getBegin()));
655            end.setValue(new Integer(features[index].getEnd()));
656
657            SearchResults highlight = new SearchResults();
658            highlight.addResult(sequences[0],
659                                features[index].getBegin(),
660                                features[index].getEnd());
661
662            ap.seqPanel.seqCanvas.highlightSearchResults(highlight);
663
664          }
665          Color col = getColour(name.getText());
666          if (col == null)
667          {
668            col = new
669                jalview.schemes.UserColourScheme()
670                .createColourFromName(name.getText());
671          }
672
673          colour.setBackground(col);
674        }
675      });
676
677
678      panel.add(tmp);
679     }
680     //////////
681     //////////////////////////////////////
682
683     tmp = new JPanel();
684     panel.add(tmp);
685     tmp.add(new JLabel("Name: ", JLabel.RIGHT));
686     tmp.add(name);
687
688     tmp = new JPanel();
689     panel.add(tmp);
690     tmp.add(new JLabel("Group: ", JLabel.RIGHT));
691     tmp.add(source);
692
693     tmp = new JPanel();
694     panel.add(tmp);
695     tmp.add(new JLabel("Colour: ", JLabel.RIGHT));
696     tmp.add(colour);
697     colour.setPreferredSize(new Dimension(150, 15));
698
699     bigPanel.add(panel, BorderLayout.NORTH);
700
701     panel = new JPanel();
702     panel.add(new JLabel("Description: ", JLabel.RIGHT));
703     description.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
704     description.setLineWrap(true);
705     panel.add(new JScrollPane(description));
706
707     if (!newFeatures)
708     {
709       bigPanel.add(panel, BorderLayout.SOUTH);
710
711       panel = new JPanel();
712       panel.add(new JLabel(" Start:", JLabel.RIGHT));
713       panel.add(start);
714       panel.add(new JLabel("  End:", JLabel.RIGHT));
715       panel.add(end);
716       bigPanel.add(panel, BorderLayout.CENTER);
717     }
718     else
719     {
720       bigPanel.add(panel, BorderLayout.CENTER);
721     }
722
723     if (lastFeatureAdded == null)
724     {
725       if (features[0].type != null)
726       {
727         lastFeatureAdded = features[0].type;
728       }
729       else
730       {
731         lastFeatureAdded = "feature_1";
732       }
733     }
734
735     if (lastFeatureGroupAdded == null)
736     {
737       if (features[0].featureGroup != null)
738       {
739         lastFeatureGroupAdded = features[0].featureGroup;
740       }
741       else
742       {
743         lastFeatureGroupAdded = "Jalview";
744       }
745     }
746
747     if(newFeatures)
748     {
749       name.setText(lastFeatureAdded);
750       source.setText(lastFeatureGroupAdded);
751     }
752     else
753     {
754       name.setText(features[0].getType());
755       source.setText(features[0].getFeatureGroup());
756     }
757
758     start.setValue(new Integer(features[0].getBegin()));
759     end.setValue(new Integer(features[0].getEnd()));
760     description.setText(features[0].getDescription());
761     colour.setBackground(getColour(name.getText()));
762
763
764     Object[] options;
765     if (!newFeatures)
766     {
767       options = new Object[]
768           {
769           "Amend", "Delete", "Cancel"};
770     }
771     else
772     {
773       options = new Object[]
774           {
775           "OK", "Cancel"};
776     }
777
778     String title = newFeatures ? "Create New Sequence Feature(s)" :
779         "Amend/Delete Features for "
780         + sequences[0].getName();
781
782     int reply = JOptionPane.showInternalOptionDialog(Desktop.desktop,
783         bigPanel,
784         title,
785         JOptionPane.YES_NO_CANCEL_OPTION,
786         JOptionPane.QUESTION_MESSAGE,
787         null,
788         options, "OK");
789
790     jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
791
792     if (reply == JOptionPane.OK_OPTION && name.getText().length()>0)
793     {
794       // This ensures that the last sequence
795       // is refreshed and new features are rendered
796       lastSeq = null;
797       lastFeatureAdded = name.getText().trim();
798       lastFeatureGroupAdded = source.getText().trim();
799       lastDescriptionAdded = description.getText().replaceAll("\n", " ");
800
801       if(lastFeatureGroupAdded.length()<1)
802         lastFeatureGroupAdded = null;
803     }
804
805     if (!newFeatures)
806     {
807       SequenceFeature sf = features[featureIndex];
808
809       if (reply == JOptionPane.NO_OPTION)
810       {
811         sequences[0].getDatasetSequence().deleteFeature(sf);
812       }
813       else if (reply == JOptionPane.YES_OPTION)
814       {
815         sf.type = lastFeatureAdded;
816         sf.featureGroup = lastFeatureGroupAdded;
817         sf.description = lastDescriptionAdded;
818
819         setColour(sf.type, colour.getBackground());
820         av.featuresDisplayed.put(sf.type,
821                                  new Integer(colour.getBackground().getRGB()));
822
823         try
824         {
825           sf.begin = ( (Integer) start.getValue()).intValue();
826           sf.end = ( (Integer) end.getValue()).intValue();
827         }
828         catch (NumberFormatException ex)
829         {}
830
831         ffile.parseDescriptionHTML(sf, false);
832       }
833     }
834     else //NEW FEATURES ADDED
835     {
836       if (reply == JOptionPane.OK_OPTION
837           && lastFeatureAdded.length()>0)
838       {
839         for (int i = 0; i < sequences.length; i++)
840         {
841           features[i].type = lastFeatureAdded;
842           if (lastFeatureGroupAdded!=null)
843             features[i].featureGroup = lastFeatureGroupAdded;
844           features[i].description = lastDescriptionAdded;
845           sequences[i].addSequenceFeature(features[i]);
846           ffile.parseDescriptionHTML(features[i], false);
847         }
848
849         if (av.featuresDisplayed == null)
850         {
851           av.featuresDisplayed = new Hashtable();
852         }
853
854         if (lastFeatureGroupAdded != null)
855         {
856           if (featureGroups == null)
857             featureGroups = new Hashtable();
858           featureGroups.put(lastFeatureGroupAdded, new Boolean(true));
859         }
860
861         Color col = colour.getBackground();
862         setColour(lastFeatureAdded, colour.getBackground());
863         av.featuresDisplayed.put(lastFeatureAdded,
864                                    new Integer(col.getRGB()));
865
866         findAllFeatures(false);
867
868         ap.paintAlignment(true);
869
870
871         return true;
872       }
873       else
874       {
875         return false;
876       }
877     }
878
879     ap.paintAlignment(true);
880
881     return true;
882   }
883
884   public void setColour(String featureType, Color col)
885   {
886     featureColours.put(featureType, col);
887   }
888
889   public void setTransparency(float value)
890   {
891     transparency = value;
892   }
893
894   public float getTransparency()
895   {
896     return transparency;
897   }
898   /**
899    * Replace current ordering with new ordering
900    * @param data { String(Type), Colour(Type), Boolean(Displayed) }
901    */
902   public void setFeaturePriority(Object[][] data)
903   {
904     setFeaturePriority(data, true);
905   }
906   /**
907    *
908    * @param data { String(Type), Colour(Type), Boolean(Displayed) }
909    * @param visibleNew when true current featureDisplay list will be cleared
910    */
911   public void setFeaturePriority(Object[][] data, boolean visibleNew)
912   {
913     if (visibleNew)
914       {
915       if (av.featuresDisplayed != null)
916       {
917         av.featuresDisplayed.clear();
918       }
919       else
920       {
921         av.featuresDisplayed = new Hashtable();
922       }
923     }
924     if (data==null)
925     {
926       return;
927     }
928
929     // The feature table will display high priority
930     // features at the top, but theses are the ones
931     // we need to render last, so invert the data
932     renderOrder = new String[data.length];
933
934     if (data.length > 0)
935     {
936       for (int i = 0; i < data.length; i++)
937       {
938         String type = data[i][0].toString();
939         setColour(type, (Color) data[i][1]);
940         if ( ( (Boolean) data[i][2]).booleanValue())
941         {
942           av.featuresDisplayed.put(type, new Integer(getColour(type).getRGB()));
943         }
944
945         renderOrder[data.length - i - 1] = type;
946       }
947     }
948
949   }
950   Hashtable featureOrder=null;
951   /**
952    * analogous to colour - store a normalized ordering for all feature types in
953    * this rendering context.
954    *
955    * @param type
956    *          Feature type string
957    * @param position
958    *          normalized priority - 0 means always appears on top, 1 means
959    *          always last.
960    */
961   public float setOrder(String type, float position)
962   {
963     if (featureOrder==null)
964     {
965       featureOrder = new Hashtable();
966     }
967     featureOrder.put(type, new Float(position));
968     return position;
969   }
970   /**
971    * get the global priority (0 (top) to 1 (bottom))
972    *
973    * @param type
974    * @return [0,1] or -1 for a type without a priority
975    */
976   public float getOrder(String type) {
977     if (featureOrder!=null)
978     {
979       if (featureOrder.containsKey(type))
980       {
981         return ((Float)featureOrder.get(type)).floatValue();
982       }
983     }
984     return -1;
985   }
986
987   /**
988    * @param listener
989    * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
990    */
991   public void addPropertyChangeListener(PropertyChangeListener listener)
992   {
993     changeSupport.addPropertyChangeListener(listener);
994   }
995
996   /**
997    * @param listener
998    * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
999    */
1000   public void removePropertyChangeListener(PropertyChangeListener listener)
1001   {
1002     changeSupport.removePropertyChangeListener(listener);
1003   }
1004 }