convert space to gapchar
[jalview.git] / src / jalview / gui / FeatureRenderer.java
index 10f91b0..a1de62e 100755 (executable)
@@ -32,12 +32,13 @@ import jalview.datamodel.*;
 
 /**
  * DOCUMENT ME!
- * 
+ *
  * @author $author$
  * @version $Revision$
  */
 public class FeatureRenderer
 {
+  AlignmentPanel ap;
   AlignViewport av;
   Color resBoxColour;
   float transparency = 1.0f;
@@ -48,7 +49,7 @@ public class FeatureRenderer
 
   // A higher level for grouping features of a
   // particular type
-  Hashtable featureGroups = null;
+  Hashtable featureGroups = new Hashtable();
 
   // This is actually an Integer held in the hashtable,
   // Retrieved using the key feature type
@@ -57,24 +58,28 @@ public class FeatureRenderer
   String[] renderOrder;
   PropertyChangeSupport changeSupport=new PropertyChangeSupport(this);
 
+  Vector allfeatures;
+
   /**
    * Creates a new FeatureRenderer object.
-   * 
+   *
    * @param av
    *          DOCUMENT ME!
    */
-  public FeatureRenderer(AlignViewport av)
+  public FeatureRenderer(AlignmentPanel ap)
   {
-    this.av = av;
+    this.ap = ap;
+    this.av = ap.av;
   }
 
+
   public void transferSettings(FeatureRenderer fr)
   {
-    renderOrder = fr.renderOrder;
-    featureGroups = fr.featureGroups;
-    featureColours = fr.featureColours;
-    transparency = fr.transparency;
-    featureOrder = fr.featureOrder;
+    this.renderOrder = fr.renderOrder;
+    this.featureGroups = fr.featureGroups;
+    this.featureColours = fr.featureColours;
+    this.transparency = fr.transparency;
+    this.featureOrder = fr.featureOrder;
   }
 
   BufferedImage offscreenImage;
@@ -100,21 +105,21 @@ public class FeatureRenderer
     {
       lastSeq = seq;
       sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
-      if (sequenceFeatures!=null) 
+      if (sequenceFeatures!=null)
       {
         sfSize = sequenceFeatures.length;
       }
     }
-    
+
     if (sequenceFeatures!=lastSeq.getDatasetSequence().getSequenceFeatures()) {
       sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
       if (sequenceFeatures != null)
       {
         sfSize = sequenceFeatures.length;
-      }      
+      }
     }
-    
-    if (sequenceFeatures == null)
+
+    if (sequenceFeatures == null || sfSize==0)
     {
       return initialCol;
     }
@@ -165,7 +170,7 @@ public class FeatureRenderer
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param g
    *          DOCUMENT ME!
    * @param seq
@@ -191,9 +196,10 @@ public class FeatureRenderer
   SequenceFeature[] sequenceFeatures;
   int sfSize, sfindex, spos, epos;
 
-  public void drawSequence(Graphics g, SequenceI seq,
+  synchronized public void drawSequence(Graphics g, SequenceI seq,
                            int start, int end, int y1)
   {
+
     if (seq.getDatasetSequence().getSequenceFeatures() == null
         || seq.getDatasetSequence().getSequenceFeatures().length == 0)
     {
@@ -216,7 +222,6 @@ public class FeatureRenderer
       }
 
       sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
-      sfSize = sequenceFeatures.length;
     }
 
     if (lastSeq == null || seq != lastSeq
@@ -224,7 +229,6 @@ public class FeatureRenderer
     {
       lastSeq = seq;
       sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
-      sfSize = sequenceFeatures.length;
     }
 
     if (transparency != 1 && g != null)
@@ -241,6 +245,7 @@ public class FeatureRenderer
       epos = lastSeq.findPosition(end);
     }
 
+    sfSize = sequenceFeatures.length;
     String type;
     for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
     {
@@ -255,10 +260,6 @@ public class FeatureRenderer
       // current feature to render
       for (sfindex = 0; sfindex < sfSize; sfindex++)
       {
-        if (sequenceFeatures.length <= sfindex)
-        {
-          continue;
-        }
         if (!sequenceFeatures[sfindex].type.equals(type))
         {
           continue;
@@ -267,7 +268,8 @@ public class FeatureRenderer
         if (featureGroups != null
             && sequenceFeatures[sfindex].featureGroup != null
             &&
-            featureGroups.containsKey(sequenceFeatures[sfindex].featureGroup)
+            sequenceFeatures[sfindex].featureGroup.length()!=0
+            && featureGroups.containsKey(sequenceFeatures[sfindex].featureGroup)
             &&
             ! ( (Boolean) featureGroups.get(sequenceFeatures[sfindex].
                                             featureGroup)).
@@ -393,20 +395,23 @@ public class FeatureRenderer
   /**
    * search the alignment for all new features, give them a colour and display
    * them. Then fires a PropertyChangeEvent on the changeSupport object.
-   * 
+   *
    */
-  synchronized void findAllFeatures()
+  void findAllFeatures()
   {
-    findAllFeatures(true); // add all new features as visible
-    if (!firing) {
-      firing=true;
-      changeSupport.firePropertyChange("changeSupport",null,null);
-      firing=false;
+    synchronized (firing)
+    {
+        if (firing.equals(Boolean.FALSE)) {
+          firing=Boolean.TRUE;
+          findAllFeatures(true); // add all new features as visible
+          changeSupport.firePropertyChange("changeSupport",null,null);
+          firing=Boolean.FALSE;
+      }
     }
   }
   /**
    * Searches alignment for all features and updates colours
-   * 
+   *
    * @param newMadeVisible
    *          if true newly added feature types will be rendered immediatly
    */
@@ -426,9 +431,9 @@ public class FeatureRenderer
       av.featuresDisplayed = new Hashtable();
     }
 
-    Vector allfeatures = new Vector();
+    allfeatures = new Vector();
     Vector oldfeatures = new Vector();
-    if (renderOrder!=null) 
+    if (renderOrder!=null)
     {
       for (int i=0; i<renderOrder.length; i++) {
         if (renderOrder[i]!=null)
@@ -453,6 +458,20 @@ public class FeatureRenderer
       {
         if (!av.featuresDisplayed.containsKey(features[index].getType()))
         {
+
+          if(featureGroups.containsKey(features[index].getType()))
+          {
+            boolean visible = ( (Boolean) featureGroups.get(
+                features[index].featureGroup)).booleanValue();
+
+            if(!visible)
+            {
+              index++;
+              continue;
+            }
+          }
+
+
           if (! (features[index].begin == 0 && features[index].end == 0))
           {
             // If beginning and end are 0, the feature is for the whole sequence
@@ -478,7 +497,7 @@ public class FeatureRenderer
     updateRenderOrder(allfeatures);
     findingFeatures = false;
   }
-  protected boolean firing=false;
+  protected Boolean firing=Boolean.FALSE;
   /**
    * replaces the current renderOrder with the unordered features in allfeatures.
    * The ordering of any types in both renderOrder and allfeatures is preserved,
@@ -486,7 +505,7 @@ public class FeatureRenderer
    * the order given by getOrder or the order given in allFeatures.
    * Note. this operates directly on the featureOrder hash for efficiency. TODO:
    * eliminate the float storage for computing/recalling the persistent ordering
-   * 
+   *
    * @param allFeatures
    */
   private void updateRenderOrder(Vector allFeatures) {
@@ -504,7 +523,7 @@ public class FeatureRenderer
             if (initOrders)
             {
               setOrder(oldRender[j], (1-(1+(float)j)/(float) oldRender.length));
-            } 
+            }
             if (allfeatures.contains(oldRender[j])) {
               renderOrder[opos++]  = oldRender[j]; // existing features always
                                                     // appear below new features
@@ -520,14 +539,14 @@ public class FeatureRenderer
     int i=allfeatures.size()-1;
     int iSize=i;
     boolean sort=false;
-    String[] newf = new String[allfeatures.size()]; 
+    String[] newf = new String[allfeatures.size()];
     float[] sortOrder = new float[allfeatures.size()];
     Enumeration en = allfeatures.elements();
     // sort remaining elements
     while (en.hasMoreElements())
     {
       newf[i] = en.nextElement().toString();
-      if (initOrders || !featureOrder.containsKey(newf[i])) 
+      if (initOrders || !featureOrder.containsKey(newf[i]))
       {
         int denom = initOrders ? allfeatures.size() : featureOrder.size();
           // new unordered feature - compute persistent ordering at head of
@@ -550,41 +569,41 @@ public class FeatureRenderer
   }
   public Color getColour(String featureType)
   {
-    Color colour = (Color) featureColours.get(featureType);
-    if (colour == null)
+    if (!featureColours.containsKey(featureType))
     {
       jalview.schemes.UserColourScheme ucs = new
-        jalview.schemes.UserColourScheme();
-      featureColours.put(featureType,
-                         colour=ucs.createColourFromName(featureType));
+          jalview.schemes.UserColourScheme();
+      Color col = ucs.createColourFromName(featureType);
+      featureColours.put(featureType, col);
+      return col;
     }
-    return colour;
+    else
+      return (Color) featureColours.get(featureType);
   }
 
   static String lastFeatureAdded;
   static String lastFeatureGroupAdded;
   static String lastDescriptionAdded;
 
-  public boolean createNewFeatures(SequenceI[] sequences,
-                                   SequenceFeature[] features)
-  {
-    return amendFeatures(sequences, features, true, null);
-  }
-
   int featureIndex = 0;
   boolean amendFeatures(final SequenceI[] sequences,
                         final SequenceFeature[] features,
                         boolean newFeatures,
                         final AlignmentPanel ap)
   {
+
+    featureIndex = 0;
+
     JPanel bigPanel = new JPanel(new BorderLayout());
-    final JComboBox name = new JComboBox();
-    final JComboBox source = new JComboBox();
+    final JComboBox overlaps;
+    final JTextField name = new JTextField(25);
+    final JTextField source = new JTextField(25);
     final JTextArea description = new JTextArea(3, 25);
     final JSpinner start = new JSpinner();
     final JSpinner end = new JSpinner();
     start.setPreferredSize(new Dimension(80, 20));
     end.setPreferredSize(new Dimension(80, 20));
+
     final JPanel colour = new JPanel();
     colour.setBorder(BorderFactory.createEtchedBorder());
     colour.setMaximumSize(new Dimension(40, 10));
@@ -592,27 +611,93 @@ public class FeatureRenderer
     {
       public void mousePressed(MouseEvent evt)
       {
-        colour.setBackground(
-            JColorChooser.showDialog(Desktop.desktop,
-                                     "Select Feature Colour",
-                                     colour.getBackground()));
+        Color col = JColorChooser.showDialog(Desktop.desktop,
+                                             "Select Feature Colour",
+                                             colour.getBackground());
+        if (col != null)
+          colour.setBackground(col);
+
       }
     });
 
-    JPanel panel = new JPanel(new GridLayout(3, 2));
-    panel.add(new JLabel("Sequence Feature Name: ", JLabel.RIGHT));
-    panel.add(name);
-    panel.add(new JLabel("Feature Group: ", JLabel.RIGHT));
-    panel.add(source);
-    panel.add(new JLabel("Feature Colour: ", JLabel.RIGHT));
     JPanel tmp = new JPanel();
+    JPanel panel = new JPanel(new GridLayout(3, 1));
+
+    ///////////////////////////////////////
+    ///MULTIPLE FEATURES AT SELECTED RESIDUE
+    if(!newFeatures && features.length>1)
+    {
+     panel = new JPanel(new GridLayout(4, 1));
+     tmp = new JPanel();
+     tmp.add(new JLabel("Select Feature: "));
+     overlaps = new JComboBox();
+     for(int i=0; i<features.length; i++)
+     {
+       overlaps.addItem(features[i].getType()
+        +"/"+features[i].getBegin()+"-"+features[i].getEnd()
+        +" ("+features[i].getFeatureGroup()+")");
+     }
+
+     tmp.add(overlaps);
+
+     overlaps.addItemListener(new ItemListener()
+     {
+       public void itemStateChanged(ItemEvent e)
+       {
+         int index = overlaps.getSelectedIndex();
+         if (index != -1)
+         {
+           featureIndex = index;
+           name.setText(features[index].getType());
+           description.setText(features[index].getDescription());
+           source.setText(features[index].getFeatureGroup());
+           start.setValue(new Integer(features[index].getBegin()));
+           end.setValue(new Integer(features[index].getEnd()));
+
+           SearchResults highlight = new SearchResults();
+           highlight.addResult(sequences[0],
+                               features[index].getBegin(),
+                               features[index].getEnd());
+
+           ap.seqPanel.seqCanvas.highlightSearchResults(highlight);
+
+         }
+         Color col = getColour(name.getText());
+         if (col == null)
+         {
+           col = new
+               jalview.schemes.UserColourScheme()
+               .createColourFromName(name.getText());
+         }
+
+         colour.setBackground(col);
+       }
+     });
+
+
+     panel.add(tmp);
+    }
+    //////////
+    //////////////////////////////////////
+
+    tmp = new JPanel();
+    panel.add(tmp);
+    tmp.add(new JLabel("Name: ", JLabel.RIGHT));
+    tmp.add(name);
+
+    tmp = new JPanel();
+    panel.add(tmp);
+    tmp.add(new JLabel("Group: ", JLabel.RIGHT));
+    tmp.add(source);
+
+    tmp = new JPanel();
+    panel.add(tmp);
+    tmp.add(new JLabel("Colour: ", JLabel.RIGHT));
     tmp.add(colour);
     colour.setPreferredSize(new Dimension(150, 15));
-    panel.add(tmp);
-    name.setEditable(true);
-    source.setEditable(true);
 
     bigPanel.add(panel, BorderLayout.NORTH);
+
     panel = new JPanel();
     panel.add(new JLabel("Description: ", JLabel.RIGHT));
     description.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
@@ -655,97 +740,26 @@ public class FeatureRenderer
       }
       else
       {
-        lastFeatureAdded = "Jalview";
+        lastFeatureGroupAdded = "Jalview";
       }
     }
 
-    Enumeration en;
-    if (featureGroups != null)
+    if(newFeatures)
     {
-      en = featureGroups.keys();
-      while (en.hasMoreElements())
-      {
-        source.addItem(en.nextElement().toString());
-      }
+      name.setText(lastFeatureAdded);
+      source.setText(lastFeatureGroupAdded);
     }
-
-    if (newFeatures)
+    else
     {
-      if (av.featuresDisplayed != null)
-      {
-        en = av.featuresDisplayed.keys();
-        while (en.hasMoreElements())
-        {
-          name.addItem(en.nextElement().toString());
-        }
-      }
-
-      name.setSelectedItem(lastFeatureAdded);
-      source.setSelectedItem(lastFeatureGroupAdded);
-      description.setText(
-          lastDescriptionAdded == null ?
-          features[0].description : lastDescriptionAdded);
-
-      if (getColour(lastFeatureAdded) != null)
-      {
-        colour.setBackground(getColour(lastFeatureAdded));
-      }
-      else
-      {
-        colour.setBackground(new Color(60, 160, 115));
-      }
-
+      name.setText(features[0].getType());
+      source.setText(features[0].getFeatureGroup());
     }
-    else if (!newFeatures)
-    {
-      featureIndex = 0;
-      for (int f = 0; f < features.length; f++)
-      {
-        name.addItem(features[f].getType().toString());
-      }
-
-      description.setText(features[0].getDescription());
-      source.setSelectedItem(features[0].getFeatureGroup());
-      start.setValue(new Integer(features[0].getBegin()));
-      end.setValue(new Integer(features[0].getEnd()));
-      colour.setBackground(
-          getColour(name.getSelectedItem().toString()));
-      name.addItemListener(new ItemListener()
-      {
-        public void itemStateChanged(ItemEvent e)
-        {
-          int index = name.getSelectedIndex();
-          if (index != -1)
-          {
-            featureIndex = index;
-            description.setText(features[index].getDescription());
-            source.setSelectedItem(features[index].getFeatureGroup());
-            start.setValue(new Integer(features[index].getBegin()));
-            end.setValue(new Integer(features[index].getEnd()));
-            colour.setBackground(
-                getColour(name.getSelectedItem().toString()));
 
-            SearchResults highlight = new SearchResults();
-            highlight.addResult(sequences[0],
-                                features[index].getBegin(),
-                                features[index].getEnd());
+    start.setValue(new Integer(features[0].getBegin()));
+    end.setValue(new Integer(features[0].getEnd()));
+    description.setText(features[0].getDescription());
+    colour.setBackground(getColour(name.getText()));
 
-            ap.seqPanel.seqCanvas.highlightSearchResults(highlight);
-
-          }
-          Color col = getColour(name.getSelectedItem().toString());
-          if (col == null)
-          {
-            col = new
-                jalview.schemes.UserColourScheme()
-                .createColourFromName(name.getSelectedItem().toString());
-          }
-
-          colour.setBackground(col);
-        }
-      });
-
-    }
 
     Object[] options;
     if (!newFeatures)
@@ -775,16 +789,17 @@ public class FeatureRenderer
 
     jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
 
-    if (reply == JOptionPane.OK_OPTION
-        && name.getSelectedItem() != null
-        && source.getSelectedItem() != null)
+    if (reply == JOptionPane.OK_OPTION && name.getText().length()>0)
     {
       // This ensures that the last sequence
       // is refreshed and new features are rendered
       lastSeq = null;
-      lastFeatureAdded = name.getSelectedItem().toString();
-      lastFeatureGroupAdded = source.getSelectedItem().toString();
+      lastFeatureAdded = name.getText().trim();
+      lastFeatureGroupAdded = source.getText().trim();
       lastDescriptionAdded = description.getText().replaceAll("\n", " ");
+
+      if(lastFeatureGroupAdded.length()<1)
+        lastFeatureGroupAdded = null;
     }
 
     if (!newFeatures)
@@ -800,7 +815,11 @@ public class FeatureRenderer
         sf.type = lastFeatureAdded;
         sf.featureGroup = lastFeatureGroupAdded;
         sf.description = lastDescriptionAdded;
+
         setColour(sf.type, colour.getBackground());
+        av.featuresDisplayed.put(sf.type,
+                                 new Integer(colour.getBackground().getRGB()));
+
         try
         {
           sf.begin = ( (Integer) start.getValue()).intValue();
@@ -812,16 +831,16 @@ public class FeatureRenderer
         ffile.parseDescriptionHTML(sf, false);
       }
     }
-    else
+    else //NEW FEATURES ADDED
     {
       if (reply == JOptionPane.OK_OPTION
-          && name.getSelectedItem() != null
-          && source.getSelectedItem() != null)
+          && lastFeatureAdded.length()>0)
       {
         for (int i = 0; i < sequences.length; i++)
         {
           features[i].type = lastFeatureAdded;
-          features[i].featureGroup = lastFeatureGroupAdded;
+          if (lastFeatureGroupAdded!=null)
+            features[i].featureGroup = lastFeatureGroupAdded;
           features[i].description = lastDescriptionAdded;
           sequences[i].addSequenceFeature(features[i]);
           ffile.parseDescriptionHTML(features[i], false);
@@ -832,20 +851,22 @@ public class FeatureRenderer
           av.featuresDisplayed = new Hashtable();
         }
 
-        if (featureGroups == null)
+        if (lastFeatureGroupAdded != null)
         {
-          featureGroups = new Hashtable();
+          if (featureGroups == null)
+            featureGroups = new Hashtable();
+          featureGroups.put(lastFeatureGroupAdded, new Boolean(true));
         }
 
-        featureGroups.put(lastFeatureGroupAdded, new Boolean(true));
-
         Color col = colour.getBackground();
         setColour(lastFeatureAdded, colour.getBackground());
+        av.featuresDisplayed.put(lastFeatureAdded,
+                                   new Integer(col.getRGB()));
 
-        av.featuresDisplayed.put(lastFeatureGroupAdded,
-                                 new Integer(col.getRGB()));
+        findAllFeatures(false);
+
+        ap.paintAlignment(true);
 
-        findAllFeatures();
 
         return true;
       }
@@ -855,10 +876,7 @@ public class FeatureRenderer
       }
     }
 
-    if (name.getSelectedIndex() == -1)
-    {
-      findAllFeatures();
-    }
+    ap.paintAlignment(true);
 
     return true;
   }
@@ -886,7 +904,7 @@ public class FeatureRenderer
     setFeaturePriority(data, true);
   }
   /**
-   * 
+   *
    * @param data { String(Type), Colour(Type), Boolean(Displayed) }
    * @param visibleNew when true current featureDisplay list will be cleared
    */
@@ -903,11 +921,11 @@ public class FeatureRenderer
         av.featuresDisplayed = new Hashtable();
       }
     }
-    if (data==null) 
+    if (data==null)
     {
       return;
     }
-    
+
     // The feature table will display high priority
     // features at the top, but theses are the ones
     // we need to render last, so invert the data
@@ -933,7 +951,7 @@ public class FeatureRenderer
   /**
    * analogous to colour - store a normalized ordering for all feature types in
    * this rendering context.
-   * 
+   *
    * @param type
    *          Feature type string
    * @param position
@@ -942,7 +960,7 @@ public class FeatureRenderer
    */
   public float setOrder(String type, float position)
   {
-    if (featureOrder==null) 
+    if (featureOrder==null)
     {
       featureOrder = new Hashtable();
     }
@@ -951,12 +969,12 @@ public class FeatureRenderer
   }
   /**
    * get the global priority (0 (top) to 1 (bottom))
-   * 
+   *
    * @param type
    * @return [0,1] or -1 for a type without a priority
    */
   public float getOrder(String type) {
-    if (featureOrder!=null) 
+    if (featureOrder!=null)
     {
       if (featureOrder.containsKey(type))
       {