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