JAL-2808 pass FeatureSettings to controller as array of data bean
[jalview.git] / src / jalview / appletgui / FeatureSettings.java
index b0bb372..7db1fbe 100755 (executable)
@@ -25,6 +25,7 @@ import jalview.api.FeatureSettingsControllerI;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.SequenceI;
 import jalview.util.MessageManager;
+import jalview.viewmodel.seqfeatures.FeatureRendererModel.FeatureSettingsBean;
 
 import java.awt.BorderLayout;
 import java.awt.Button;
@@ -63,10 +64,16 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
-public class FeatureSettings extends Panel implements ItemListener,
-        MouseListener, MouseMotionListener, ActionListener,
-        AdjustmentListener, FeatureSettingsControllerI
+public class FeatureSettings extends Panel
+        implements ItemListener, MouseListener, MouseMotionListener,
+        ActionListener, AdjustmentListener, FeatureSettingsControllerI
 {
+  private static final int TYPE_COLUMN = 0;
+
+  private static final int COLOUR_COLUMN = 1;
+
+  private static final int SHOW_COLUMN = 2;
+
   FeatureRenderer fr;
 
   AlignmentPanel ap;
@@ -135,11 +142,18 @@ public class FeatureSettings extends Panel implements ItemListener,
 
     add(lowerPanel, BorderLayout.SOUTH);
 
-    groupPanel.setLayout(new GridLayout(
-            (fr.getFeatureGroupsSize()) / 4 + 1, 4)); // JBPNote - this was
-                                                      // scaled on number of
-                                                      // visible groups. seems
-                                                      // broken
+    groupPanel.setLayout(
+            new GridLayout((fr.getFeatureGroupsSize()) / 4 + 1, 4)); // JBPNote
+                                                                     // - this
+                                                                     // was
+                                                                     // scaled
+                                                                     // on
+                                                                     // number
+                                                                     // of
+                                                                     // visible
+                                                                     // groups.
+                                                                     // seems
+                                                                     // broken
     groupPanel.validate();
 
     add(groupPanel, BorderLayout.NORTH);
@@ -174,14 +188,12 @@ public class FeatureSettings extends Panel implements ItemListener,
   public void paint(Graphics g)
   {
     g.setColor(Color.black);
-    g.drawString(MessageManager
-            .getString("label.no_features_added_to_this_alignment"), 10, 20);
-    g.drawString(MessageManager
-            .getString("label.features_can_be_added_from_searches_1"), 10,
-            40);
-    g.drawString(MessageManager
-            .getString("label.features_can_be_added_from_searches_2"), 10,
-            60);
+    g.drawString(MessageManager.getString(
+            "label.no_features_added_to_this_alignment"), 10, 20);
+    g.drawString(MessageManager.getString(
+            "label.features_can_be_added_from_searches_1"), 10, 40);
+    g.drawString(MessageManager.getString(
+            "label.features_can_be_added_from_searches_2"), 10, 60);
   }
 
   protected void popupSort(final MyCheckbox check,
@@ -189,8 +201,9 @@ public class FeatureSettings extends Panel implements ItemListener,
   {
     final String type = check.type;
     final FeatureColourI typeCol = fr.getFeatureStyle(type);
-    PopupMenu men = new PopupMenu(MessageManager.formatMessage(
-            "label.settings_for_type", new String[] { type }));
+    PopupMenu men = new PopupMenu(MessageManager
+            .formatMessage("label.settings_for_type", new String[]
+            { type }));
     java.awt.MenuItem scr = new MenuItem(
             MessageManager.getString("label.sort_by_score"));
     men.add(scr);
@@ -201,8 +214,9 @@ public class FeatureSettings extends Panel implements ItemListener,
       @Override
       public void actionPerformed(ActionEvent e)
       {
-        me.ap.alignFrame.avc.sortAlignmentByFeatureScore(Arrays
-                .asList(new String[] { type }));
+        me.ap.alignFrame.avc
+                .sortAlignmentByFeatureScore(Arrays.asList(new String[]
+                { type }));
       }
 
     });
@@ -214,8 +228,9 @@ public class FeatureSettings extends Panel implements ItemListener,
       @Override
       public void actionPerformed(ActionEvent e)
       {
-        me.ap.alignFrame.avc.sortAlignmentByFeatureDensity(Arrays
-                .asList(new String[] { type }));
+        me.ap.alignFrame.avc
+                .sortAlignmentByFeatureDensity(Arrays.asList(new String[]
+                { type }));
       }
 
     });
@@ -279,8 +294,8 @@ public class FeatureSettings extends Panel implements ItemListener,
     });
     men.add(selectContaining);
 
-    MenuItem selectNotContaining = new MenuItem(
-            MessageManager.getString("label.select_columns_not_containing"));
+    MenuItem selectNotContaining = new MenuItem(MessageManager
+            .getString("label.select_columns_not_containing"));
     selectNotContaining.addActionListener(new ActionListener()
     {
       @Override
@@ -369,8 +384,8 @@ public class FeatureSettings extends Panel implements ItemListener,
   // Group selection states
   void resetTable(boolean groupsChanged)
   {
-    List<String> displayableTypes = new ArrayList<String>();
-    Set<String> foundGroups = new HashSet<String>();
+    List<String> displayableTypes = new ArrayList<>();
+    Set<String> foundGroups = new HashSet<>();
 
     AlignmentI alignment = av.getAlignment();
 
@@ -383,14 +398,16 @@ public class FeatureSettings extends Panel implements ItemListener,
        * and keep track of which groups are visible
        */
       Set<String> groups = seq.getFeatures().getFeatureGroups(true);
-      Set<String> visibleGroups = new HashSet<String>();
+      Set<String> visibleGroups = new HashSet<>();
       for (String group : groups)
       {
-        if (group == null || fr.checkGroupVisibility(group, true))
+        // if (group == null || fr.checkGroupVisibility(group, true))
+        if (group == null || checkGroupState(group))
         {
           visibleGroups.add(group);
         }
       }
+      foundGroups.addAll(groups);
 
       /*
        * get distinct feature types for visible groups
@@ -452,8 +469,8 @@ public class FeatureSettings extends Panel implements ItemListener,
       addCheck(groupsChanged, type);
     }
 
-    featurePanel.setLayout(new GridLayout(featurePanel.getComponentCount(),
-            1, 10, 5));
+    featurePanel.setLayout(
+            new GridLayout(featurePanel.getComponentCount(), 1, 10, 5));
     featurePanel.validate();
 
     if (scrollPane != null)
@@ -543,7 +560,7 @@ public class FeatureSettings extends Panel implements ItemListener,
       Checkbox check = (Checkbox) featurePanel.getComponent(i);
       check.setState(!check.getState());
     }
-    selectionChanged();
+    selectionChanged(true);
   }
 
   private ItemListener groupItemListener = new ItemListener()
@@ -566,33 +583,54 @@ public class FeatureSettings extends Panel implements ItemListener,
   @Override
   public void itemStateChanged(ItemEvent evt)
   {
-    selectionChanged();
+    selectionChanged(true);
   }
 
-  void selectionChanged()
+  void selectionChanged(boolean updateOverview)
   {
     Component[] comps = featurePanel.getComponents();
     int cSize = comps.length;
 
-    Object[][] tmp = new Object[cSize][3];
+    /*
+     * temporary! leave column[2] empty - used for Filter in
+     * gui.FeatureSettings
+     */
+    int columnCount = 4;
+    Object[][] tmp = new Object[cSize][columnCount];
     int tmpSize = 0;
     for (int i = 0; i < cSize; i++)
     {
       MyCheckbox check = (MyCheckbox) comps[i];
-      tmp[tmpSize][0] = check.type;
-      tmp[tmpSize][1] = fr.getFeatureStyle(check.type);
-      tmp[tmpSize][2] = new Boolean(check.getState());
+      tmp[tmpSize][TYPE_COLUMN /* 0 */] = check.type;
+      tmp[tmpSize][COLOUR_COLUMN /* 1 */] = fr.getFeatureStyle(check.type);
+      tmp[tmpSize][SHOW_COLUMN /* 3 */] = new Boolean(check.getState());
       tmpSize++;
     }
 
-    Object[][] data = new Object[tmpSize][3];
+    Object[][] data = new Object[tmpSize][columnCount];
     System.arraycopy(tmp, 0, data, 0, tmpSize);
 
-    fr.setFeaturePriority(data);
+    fr.setFeaturePriority(getTableAsBeans(data));
 
-    ap.paintAlignment(true);
+    ap.paintAlignment(updateOverview, updateOverview);
   }
 
+  /**
+   * Converts table data into an array of data beans
+   */
+  private FeatureSettingsBean[] getTableAsBeans(Object[][] data)
+  {
+    FeatureSettingsBean[] rowData = new FeatureSettingsBean[data.length];
+    for (int i = 0; i < data.length; i++)
+    {
+      String type = (String) data[i][TYPE_COLUMN];
+      FeatureColourI colour = (FeatureColourI) data[i][COLOUR_COLUMN];
+      Boolean isShown = (Boolean) data[i][SHOW_COLUMN];
+      // feature filter set to null as not (yet) offered in applet
+      rowData[i] = new FeatureSettingsBean(type, colour, null, isShown);
+    }
+    return rowData;
+  }
   MyCheckbox selectedCheck;
 
   boolean dragging = false;
@@ -632,8 +670,8 @@ public class FeatureSettings extends Panel implements ItemListener,
     }
     else
     {
-      comp = featurePanel.getComponentAt(evt.getX(), evt.getY()
-              + evt.getComponent().getLocation().y);
+      comp = featurePanel.getComponentAt(evt.getX(),
+              evt.getY() + evt.getComponent().getLocation().y);
     }
 
     if (comp != null && comp instanceof Checkbox)
@@ -670,7 +708,7 @@ public class FeatureSettings extends Panel implements ItemListener,
   {
     featurePanel.removeAll();
     resetTable(false);
-    ap.paintAlignment(true);
+    ap.paintAlignment(true, true);
   }
 
   @Override
@@ -722,7 +760,7 @@ public class FeatureSettings extends Panel implements ItemListener,
   public void adjustmentValueChanged(AdjustmentEvent evt)
   {
     fr.setTransparency((100 - transparency.getValue()) / 100f);
-    ap.paintAlignment(true);
+    ap.paintAlignment(true, true);
   }
 
   class MyCheckbox extends Checkbox