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