pickmanager on sequence and alignment position, vamsas pick broadcast and refactor...
[jalview.git] / src / jalview / gui / FeatureSettings.java
index 895c235..de61be3 100755 (executable)
@@ -37,7 +37,7 @@ public class FeatureSettings
     extends JPanel
 {
   DasSourceBrowser dassourceBrowser;
-  jalview.io.DasSequenceFeatureFetcher dasFeatureFetcher;
+  jalview.ws.DasSequenceFeatureFetcher dasFeatureFetcher;
   JPanel settingsPane = new JPanel();
   JPanel dasSettingsPane = new JPanel();
 
@@ -128,18 +128,17 @@ public class FeatureSettings
     final PropertyChangeListener change;
     final FeatureSettings fs=this;
     fr.addPropertyChangeListener(change=new PropertyChangeListener() {
-      private boolean firing=false;
       public void propertyChange(PropertyChangeEvent evt)
       {
-        if (!fs.resettingTable && !firing) {
-          firing=true;
-          resetTable(null);
-          firing=false;
+        if (!fs.resettingTable && !fs.handlingUpdate) {
+          fs.handlingUpdate=true;
+          fs.resetTable(null); // new groups may be added with new seuqence feature types only
+          fs.handlingUpdate=false;
         }
       }
-      
+
     });
-    
+
     frame = new JInternalFrame();
     frame.setContentPane(this);
     Desktop.addInternalFrame(frame, "Sequence Feature Settings", 400, 450);
@@ -155,6 +154,11 @@ public class FeatureSettings
     frame.setLayer(JLayeredPane.PALETTE_LAYER);
   }
   /**
+   * true when Feature Settings are updating from feature renderer
+   */
+  private boolean handlingUpdate=false;
+
+  /**
    * contains a float[3] for each feature type string. created by setTableData
    */
   Hashtable typeWidth=null;
@@ -194,57 +198,10 @@ public class FeatureSettings
           if (!allGroups.contains(group))
           {
             allGroups.addElement(group);
-
-            boolean visible;
-            if (fr.featureGroups.containsKey(group))
-            {
-              visible = ( (Boolean) fr.featureGroups.get(group)).booleanValue();
-            } else {
-              visible=true; //initGroups || fr.av.featuresDisplayed.containsKey(tmpfeatures[index].getType());
-            }
-
-            if (groupPanel == null)
-            {
-              groupPanel = new JPanel();
-            }
-
-            boolean alreadyAdded = false;
-            for (int g = 0; g < groupPanel.getComponentCount(); g++)
-            {
-              if ( ( (JCheckBox) groupPanel.getComponent(g))
-                  .getText().equals(group))
-              {
-                alreadyAdded = true;
-                break;
-              }
-            }
-
-            if (alreadyAdded)
+            if (group!=null)
             {
-              
-              continue;
+              checkGroupState(group);
             }
-
-            fr.featureGroups.put(group, new Boolean(visible));
-            final String grp = group;
-            final JCheckBox check = new JCheckBox(group, visible);
-            check.setFont(new Font("Serif", Font.BOLD, 12));
-            check.addItemListener(new ItemListener()
-            {
-              public void itemStateChanged(ItemEvent evt)
-              {
-                fr.featureGroups.put(check.getText(),
-                                     new Boolean(check.isSelected()));
-                af.alignPanel.seqPanel.seqCanvas.repaint();
-                if (af.alignPanel.overviewPanel != null)
-                {
-                  af.alignPanel.overviewPanel.updateOverviewImage();
-                }
-
-                resetTable(new String[] { grp } );
-              }
-            });
-            groupPanel.add(check);
           }
         }
 
@@ -255,17 +212,79 @@ public class FeatureSettings
         index++;
       }
     }
-    
+
     resetTable(null);
 
     validate();
   }
+  /**
+   *
+   * @param group
+   * @return true if group has been seen before and is already added to set.
+   */
+  private boolean checkGroupState(String group) {
+    boolean visible;
+    if (fr.featureGroups.containsKey(group))
+    {
+      visible = ( (Boolean) fr.featureGroups.get(group)).booleanValue();
+        } else {
+        visible=true; // new group is always made visible
+      }
+
+      if (groupPanel == null)
+      {
+        groupPanel = new JPanel();
+      }
+
+      boolean alreadyAdded = false;
+      for (int g = 0; g < groupPanel.getComponentCount(); g++)
+      {
+        if ( ( (JCheckBox) groupPanel.getComponent(g))
+            .getText().equals(group))
+        {
+          alreadyAdded = true;
+          ((JCheckBox)groupPanel.getComponent(g)).setSelected(visible);
+          break;
+        }
+      }
+
+      if (alreadyAdded)
+      {
+
+        return true;
+      }
+
+      fr.featureGroups.put(group, new Boolean(visible));
+      final String grp = group;
+      final JCheckBox check = new JCheckBox(group, visible);
+      check.setFont(new Font("Serif", Font.BOLD, 12));
+      check.addItemListener(new ItemListener()
+      {
+        public void itemStateChanged(ItemEvent evt)
+        {
+          fr.featureGroups.put(check.getText(),
+                               new Boolean(check.isSelected()));
+          af.alignPanel.seqPanel.seqCanvas.repaint();
+          if (af.alignPanel.overviewPanel != null)
+          {
+            af.alignPanel.overviewPanel.updateOverviewImage();
+          }
+
+          resetTable(new String[] { grp } );
+        }
+      });
+      groupPanel.add(check);
+      return false;
+  }
   boolean resettingTable=false;
-  void resetTable(String[] groupChanged)
+  synchronized void resetTable(String[] groupChanged)
   {
+    if (resettingTable==true)
+    {
+      return;
+    }
     resettingTable=true;
     typeWidth=new Hashtable();
-    Vector groupchanged = new Vector();
     // TODO: change avWidth calculation to 'per-sequence' average and use long rather than float
     float[] avWidth=null;
     SequenceFeature[] tmpfeatures;
@@ -299,10 +318,9 @@ public class FeatureSettings
         if (group == null || fr.featureGroups.get(group) == null ||
             ( (Boolean) fr.featureGroups.get(group)).booleanValue())
         {
+          if (group!=null)
+            checkGroupState(group);
           type = tmpfeatures[index].getType();
-          if (groupchanged==null || groupchanged.contains(group)) {
-            //af.getViewport().featuresDisplayed.put(type, fr.getColour(type));
-          }
           if (!visibleChecks.contains(type))
           {
             visibleChecks.addElement(type);
@@ -330,7 +348,8 @@ public class FeatureSettings
 
     if (fr.renderOrder != null)
     {
-      fr.findAllFeatures(groupChanged!=null); // prod to update colourschemes. but don't affect display
+      if (!handlingUpdate)
+        fr.findAllFeatures(groupChanged!=null); // prod to update colourschemes. but don't affect display
       //First add the checks in the previous render order,
       //in case the window has been closed and reopened
       for (int ro = fr.renderOrder.length - 1; ro > -1; ro--)
@@ -438,17 +457,21 @@ public class FeatureSettings
             JalviewUserColours();
         jucs = (jalview.binding.JalviewUserColours) jucs.unmarshal(in);
 
-        for (int i = 0; i < jucs.getColourCount(); i++)
+        for (int i = jucs.getColourCount()-1; i >=0; i--)
         {
           String name;
           fr.setColour(name=jucs.getColour(i).getName(),
                        new Color(Integer.parseInt(jucs.getColour(i).getRGB(),
                                                   16)));
-          fr.setOrder(name,i/jucs.getColourCount());
+          fr.setOrder(name,(i==0) ? 0 : i/jucs.getColourCount());
+        }
+        if (table!=null) {
+          resetTable(null);
+          Object[][] data=((FeatureTableModel) table.getModel()).getData();
+          ensureOrder(data);
+          updateFeatureRenderer(data,false);
+          table.repaint();
         }
-
-        setTableData();
-        af.alignPanel.paintAlignment(true);
       }
       catch (Exception ex)
       {
@@ -483,15 +506,24 @@ public class FeatureSettings
             new FileOutputStream(choice), "UTF-8"));
 
         Enumeration e = fr.featureColours.keys();
+        float[] sortOrder = new float[fr.featureColours.size()];
+        String[] sortTypes = new String[fr.featureColours.size()];
+        int i=0;
         while (e.hasMoreElements())
         {
+          sortTypes[i] = e.nextElement().toString();
+          sortOrder[i]  = fr.getOrder(sortTypes[i]);
+          i++;
+        }
+        jalview.util.QuickSort.sort(sortOrder, sortTypes);
+        sortOrder=null;
+        for (i=0; i<sortTypes.length; i++) {
           jalview.binding.Colour col = new jalview.binding.Colour();
-          col.setName(e.nextElement().toString());
+          col.setName(sortTypes[i]);
           col.setRGB(jalview.util.Format.getHexString(
               fr.getColour(col.getName())));
           ucs.addColour(col);
         }
-
         ucs.marshal(out);
         out.close();
       }
@@ -536,16 +568,16 @@ public class FeatureSettings
     boolean sort=false;
     for (int i=0;i<width.length; i++) {
       //awidth = (float[]) typeWidth.get(data[i][0]);
-      if (width[i]==0) 
+      if (width[i]==0)
       {
         width[i] = fr.getOrder(data[i][0].toString());
         if (width[i]<0)
         {
           width[i] = fr.setOrder(data[i][0].toString(), i/data.length);
         }
-      } else { 
+      } else {
         width[i] /=max; // normalize
-        fr.setOrder(data[i][0].toString(), width[i]); // store for later 
+        fr.setOrder(data[i][0].toString(), width[i]); // store for later
       }
       if (i>0)
         sort = sort || width[i-1]>width[i];
@@ -553,7 +585,7 @@ public class FeatureSettings
     if (sort)
       jalview.util.QuickSort.sort(width, data);
     // update global priority order
-    
+
     updateFeatureRenderer(data,false);
     table.repaint();
   }
@@ -576,11 +608,6 @@ public class FeatureSettings
   {
     fr.setFeaturePriority(data, visibleNew);
     af.alignPanel.paintAlignment(true);
-
-    if (af.alignPanel.overviewPanel != null)
-    {
-      af.alignPanel.overviewPanel.updateOverviewImage();
-    }
   }
 
   int selectedRow = -1;
@@ -751,7 +778,7 @@ public class FeatureSettings
     }
 
     dasFeatureFetcher =
-        new jalview.io.DasSequenceFeatureFetcher(
+        new jalview.ws.DasSequenceFeatureFetcher(
             dataset,
             this,
             selectedSources);
@@ -777,6 +804,17 @@ public class FeatureSettings
     fetchDAS.setEnabled(true);
     cancelDAS.setEnabled(false);
   }
+  public void noDasSourceActive()
+  {
+    JOptionPane.showInternalConfirmDialog(Desktop.desktop,
+            "No das sources were selected.\n"
+            + "Please select some sources and\n"
+            +" try again.",
+            "No Sources Selected",
+            JOptionPane.DEFAULT_OPTION,
+            JOptionPane.INFORMATION_MESSAGE);
+    complete();
+  }
 
   /////////////////////////////////////////////////////////////////////////
   // http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
@@ -891,6 +929,7 @@ public class FeatureSettings
       return this;
     }
   }
+
 }
 
 class ColorEditor