simplification of FeatureSettings inner classes
[jalview.git] / src / jalview / gui / FeatureSettings.java
index 3cd6c21..93b5f31 100644 (file)
@@ -36,7 +36,6 @@ import jalview.io.JalviewFileView;
 import jalview.schemes.FeatureColour;
 import jalview.util.MessageManager;
 import jalview.util.Platform;
-import jalview.util.dialogrunner.RunResponse;
 import jalview.viewmodel.seqfeatures.FeatureRendererModel.FeatureSettingsBean;
 import jalview.xml.binding.jalview.JalviewUserColours;
 import jalview.xml.binding.jalview.JalviewUserColours.Colour;
@@ -210,6 +209,33 @@ public class FeatureSettings extends JPanel
 
     table = new JTable()
     {
+       
+//     @Override
+//     public void repaint() {
+//       System.out.println("FS repaint");
+//       super.repaint();
+//       
+//     }
+//     
+//     @Override
+//        public void repaint(long tm, int x, int y, int width, int height) {
+//       System.out.println("FS repaint " + x  + " " + y + " " + width + " " + height);
+//       super.repaint(tm, x, y, width, height);
+//       
+//     }
+//     @Override
+//     public void invalidate() {
+//             if (isValid())
+//               System.out.println("FS invalidating ");
+//            super.invalidate();
+//     }
+//     
+//     @Override
+//     public void validate() {
+//            System.out.println("FS validating "  + isValid());
+//            super.validate();
+//     }
+
       @Override
       public String getToolTipText(MouseEvent e)
       {
@@ -267,18 +293,18 @@ public class FeatureSettings extends JPanel
     table.getTableHeader().setFont(new Font("Verdana", Font.PLAIN, 12));
     ToolTipManager.sharedInstance().registerComponent(table);
 
-    table.setDefaultEditor(FeatureColour.class, new ColorEditor(this));
+    table.setDefaultEditor(FeatureColour.class, new ColorEditor());
     table.setDefaultRenderer(FeatureColour.class, new ColorRenderer());
 
-    table.setDefaultEditor(FeatureMatcherSet.class, new FilterEditor(this));
+    table.setDefaultEditor(FeatureMatcherSet.class, new FilterEditor());
     table.setDefaultRenderer(FeatureMatcherSet.class, new FilterRenderer());
     
     TableColumn colourColumn = new TableColumn(COLOUR_COLUMN, 75,
-            new ColorRenderer(), new ColorEditor(this));
+            new ColorRenderer(), new ColorEditor());
     table.addColumn(colourColumn);
 
     TableColumn filterColumn = new TableColumn(FILTER_COLUMN, 75,
-            new FilterRenderer(), new FilterEditor(this));
+            new FilterRenderer(), new FilterEditor());
     table.addColumn(filterColumn);
 
     table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
@@ -293,8 +319,7 @@ public class FeatureSettings extends JPanel
         if (evt.isPopupTrigger())
         {
           Object colour = table.getValueAt(selectedRow, COLOUR_COLUMN);
-          popupSort(selectedRow, type, colour, fr.getMinMax(), evt.getX(),
-                  evt.getY());
+          showPopupMenu(selectedRow, type, colour, evt.getPoint());
         }
         else if (evt.getClickCount() == 2)
         {
@@ -315,8 +340,7 @@ public class FeatureSettings extends JPanel
         {
           String type = (String) table.getValueAt(selectedRow, TYPE_COLUMN);
           Object colour = table.getValueAt(selectedRow, COLOUR_COLUMN);
-          popupSort(selectedRow, type, colour, fr.getMinMax(), evt.getX(),
-                  evt.getY());
+          showPopupMenu(selectedRow, type, colour, evt.getPoint());
         }
       }
     });
@@ -344,7 +368,7 @@ public class FeatureSettings extends JPanel
             data[i + direction] = temp;
           }
           updateFeatureRenderer(data);
-          repaintTable();
+          table.repaint();
           selectedRow = newRow;
         }
       }
@@ -407,9 +431,17 @@ public class FeatureSettings extends JPanel
     inConstruction = false;
   }
 
-  protected void popupSort(final int rowSelected, final String type,
-          final Object typeCol, final Map<String, float[][]> minmax, int x,
-          int y)
+       /**
+        * Constructs and shows a popup menu of possible actions on the selected row and
+        * feature type
+        * 
+        * @param rowSelected
+        * @param type
+        * @param typeCol
+        * @param pt
+        */
+  protected void showPopupMenu(final int rowSelected, final String type,
+          final Object typeCol, final Point pt)
   {
     final FeatureColourI featureColour = (FeatureColourI) typeCol;
 
@@ -430,7 +462,6 @@ public class FeatureSettings extends JPanel
                 .sortAlignmentByFeatureScore(Arrays.asList(new String[]
                 { type }));
       }
-
     });
     JMenuItem dens = new JMenuItem(
             MessageManager.getString("label.sort_by_density"));
@@ -444,7 +475,6 @@ public class FeatureSettings extends JPanel
                 .sortAlignmentByFeatureDensity(Arrays.asList(new String[]
                 { type }));
       }
-
     });
     men.add(dens);
 
@@ -468,6 +498,8 @@ public class FeatureSettings extends JPanel
       {
         if (e.getSource() == variableColourCB)
         {
+          men.setVisible(true); // BH 2018 for JavaScript because this is a checkbox
+          men.setVisible(false); // BH 2018 for JavaScript because this is a checkbox
           if (featureColour.isSimpleColour())
           {
             /*
@@ -481,7 +513,7 @@ public class FeatureSettings extends JPanel
             /*
              * toggle variable to simple colour - show colour chooser
              */
-            String title = MessageManager.getString("label.select_colour");
+            String title = MessageManager.formatMessage("label.select_colour_for", type);
             ColourChooserListener listener = new ColourChooserListener()
             {
               @Override
@@ -498,7 +530,8 @@ public class FeatureSettings extends JPanel
             JalviewColourChooser.showColourChooser(me, title, featureColour.getMaxColour(), listener);
           }
         }
-        else    {
+        else    
+        {
           if (e.getSource() instanceof FeatureTypeSettings)
           {
             /*
@@ -507,11 +540,12 @@ public class FeatureSettings extends JPanel
              */
             FeatureColourI fci = fr.getFeatureColours().get(type);
             table.setValueAt(fci, rowSelected, COLOUR_COLUMN);
-            table.validate();
+            // BH 2018 setting a table value does not invalidate it.
+            System.out.println("FeatureSettings is valied" + table.isValid());
+//            table.validate();
           }
         }
       }
-
     });
 
     JMenuItem selCols = new JMenuItem(
@@ -560,7 +594,7 @@ public class FeatureSettings extends JPanel
     men.add(clearCols);
     men.add(hideCols);
     men.add(hideOtherCols);
-    men.show(table, x, y);
+    men.show(table, pt.x, pt.y);
   }
 
   @Override
@@ -894,13 +928,15 @@ public class FeatureSettings extends JPanel
     chooser.setDialogTitle(
             MessageManager.getString("label.load_feature_colours"));
     chooser.setToolTipText(MessageManager.getString("action.load"));
-    chooser.response(new RunResponse(JalviewFileChooser.APPROVE_OPTION){
-
-               @Override
-               public void run() {
-                     File file = chooser.getSelectedFile();
-                     load(file);
-               }});
+    chooser.setResponseHandler(0, new Runnable()
+    {
+         @Override
+         public void run() 
+         {
+           File file = chooser.getSelectedFile();
+               load(file);
+         }
+       });
     chooser.showOpenDialog(this);
   }
 
@@ -965,7 +1001,7 @@ public class FeatureSettings extends JPanel
                 .getData();
         ensureOrder(data);
         updateFeatureRenderer(data, false);
-        repaintTable();
+        table.repaint();
       }
     } catch (Exception ex)
     {
@@ -1079,7 +1115,7 @@ public class FeatureSettings extends JPanel
       data[i][SHOW_COLUMN] = !(Boolean) data[i][SHOW_COLUMN];
     }
     updateFeatureRenderer(data, true);
-    repaintTable();
+    table.repaint();
   }
 
   public void orderByAvWidth()
@@ -1142,28 +1178,7 @@ public class FeatureSettings extends JPanel
     }
 
     updateFeatureRenderer(data, false);
-    repaintTable();
-  }
-
-  /**
-   * Repaints the table using alternative code for Java and J2S
-   */
-  private void repaintTable() 
-  {
-       // BH 2018
-       // Here is a needed intervention
-       // because generally we don't "repaint"
-       // the table. We re-create the HTML divs
-       // that is associated with it. A better
-       // way to do this would be to fire a property change.
-       @SuppressWarnings("unused")
-       TableUI ui = table.getUI();
-       /**
-        * @j2sNative ui.repaintTable$();
-        */
-       {
-         table.repaint();
-       }
+    table.repaint();
   }
 
   public void close()
@@ -1694,10 +1709,11 @@ public class FeatureSettings extends JPanel
     renderGraduatedColor(comp, gcol, w, h);
   }
 
-  class ColorEditor extends AbstractCellEditor
+  @SuppressWarnings("serial")
+class ColorEditor extends AbstractCellEditor
           implements TableCellEditor, ActionListener
   {
-    FeatureSettings me;
+//    FeatureSettings me;
 
     FeatureColourI currentColor;
 
@@ -1711,9 +1727,8 @@ public class FeatureSettings extends JPanel
 
     int rowSelected = 0;
 
-    public ColorEditor(FeatureSettings fs)
+    public ColorEditor()
     {
-      this.me = fs;
       // Set up the editor (from the table's point of view),
       // which is a button.
       // This button brings up the color chooser dialog,
@@ -1738,14 +1753,21 @@ public class FeatureSettings extends JPanel
           /*
            * simple colour chooser
            */
-          String ttl = MessageManager.getString("label.select_colour");
-          ColourChooserListener listener = new ColourChooserListener() {
+          String ttl = MessageManager.formatMessage("label.select_colour_for", type);
+          ColourChooserListener listener = new ColourChooserListener() 
+          {
             @Override
             public void colourSelected(Color c)
             {
               currentColor = new FeatureColour(c);
-              me.table.setValueAt(currentColor, rowSelected, COLOUR_COLUMN);
+              table.setValueAt(currentColor, rowSelected, COLOUR_COLUMN);
+              fireEditingStopped();
             }
+                       @Override
+                       public void cancel() 
+                       {
+                 fireEditingStopped();
+                       }
           };
           JalviewColourChooser.showColourChooser(button,  ttl,  currentColor.getColour(), listener);
         }
@@ -1754,14 +1776,13 @@ public class FeatureSettings extends JPanel
           /*
            * variable colour and filters dialog
            */
-          chooser = new FeatureTypeSettings(me.fr, type);
+          chooser = new FeatureTypeSettings(fr, type);
           if (!Jalview.isJS())
           {
             chooser.setRequestFocusEnabled(true);
             chooser.requestFocus();
           }
           chooser.addActionListener(this);
-          // Make the renderer reappear.
           fireEditingStopped();
         }
       }
@@ -1773,7 +1794,7 @@ public class FeatureSettings extends JPanel
          * update table data without triggering updateFeatureRenderer
          */
         currentColor = fr.getFeatureColours().get(type);
-        FeatureMatcherSetI currentFilter = me.fr.getFeatureFilter(type);
+        FeatureMatcherSetI currentFilter = fr.getFeatureFilter(type);
         if (currentFilter == null)
         {
           currentFilter = new FeatureMatcherSet();
@@ -1782,9 +1803,13 @@ public class FeatureSettings extends JPanel
                 .getData()[rowSelected];
         data[COLOUR_COLUMN] = currentColor;
         data[FILTER_COLUMN] = currentFilter;
-
         fireEditingStopped();
-        me.table.validate();
+        // SwingJS needs an explicit repaint() here, 
+        // rather than relying upon no validation having
+        // occurred since the stopEditing call was made.
+        // Its laying out has not been stopped by the modal frame
+        table.validate();
+        table.repaint();
       }
     }
 
@@ -1802,9 +1827,9 @@ public class FeatureSettings extends JPanel
     {
       currentColor = (FeatureColourI) value;
       this.rowSelected = row;
-      type = me.table.getValueAt(row, TYPE_COLUMN).toString();
+      type = table.getValueAt(row, TYPE_COLUMN).toString();
       button.setOpaque(true);
-      button.setBackground(me.getBackground());
+      button.setBackground(getBackground());
       if (!currentColor.isSimpleColour())
       {
         JLabel btn = new JLabel();
@@ -1830,10 +1855,10 @@ public class FeatureSettings extends JPanel
    * as display text). On click in the cell, opens the Feature Display Settings
    * dialog at the Filters tab.
    */
-  class FilterEditor extends AbstractCellEditor
+  @SuppressWarnings("serial")
+class FilterEditor extends AbstractCellEditor
           implements TableCellEditor, ActionListener
   {
-    FeatureSettings me;
 
     FeatureMatcherSetI currentFilter;
 
@@ -1847,9 +1872,8 @@ public class FeatureSettings extends JPanel
 
     int rowSelected = 0;
 
-    public FilterEditor(FeatureSettings me)
+    public FilterEditor()
     {
-      this.me = me;
       button = new JButton();
       button.setActionCommand(EDIT);
       button.addActionListener(this);
@@ -1864,7 +1888,7 @@ public class FeatureSettings extends JPanel
     {
       if (button == e.getSource())
       {
-        FeatureTypeSettings chooser = new FeatureTypeSettings(me.fr, type);
+        FeatureTypeSettings chooser = new FeatureTypeSettings(fr, type);
         chooser.addActionListener(this);
         chooser.setRequestFocusEnabled(true);
         chooser.requestFocus();
@@ -1886,17 +1910,23 @@ public class FeatureSettings extends JPanel
          * update table data without triggering updateFeatureRenderer
          */
         FeatureColourI currentColor = fr.getFeatureColours().get(type);
-        currentFilter = me.fr.getFeatureFilter(type);
+        currentFilter = fr.getFeatureFilter(type);
         if (currentFilter == null)
         {
           currentFilter = new FeatureMatcherSet();
         }
+        
         Object[] data = ((FeatureTableModel) table.getModel())
                 .getData()[rowSelected];
         data[COLOUR_COLUMN] = currentColor;
         data[FILTER_COLUMN] = currentFilter;
         fireEditingStopped();
-        me.table.validate();
+        // SwingJS needs an explicit repaint() here, 
+        // rather than relying upon no validation having
+        // occurred since the stopEditing call was made.
+        // Its laying out has not been stopped by the modal frame
+        table.validate();
+        table.repaint();
       }
     }
 
@@ -1912,9 +1942,9 @@ public class FeatureSettings extends JPanel
     {
       currentFilter = (FeatureMatcherSetI) value;
       this.rowSelected = row;
-      type = me.table.getValueAt(row, TYPE_COLUMN).toString();
+      type = table.getValueAt(row, TYPE_COLUMN).toString();
       button.setOpaque(true);
-      button.setBackground(me.getBackground());
+      button.setBackground(getBackground());
       button.setText(currentFilter.toString());
       button.setIcon(null);
       return button;
@@ -1999,7 +2029,8 @@ class FeatureIcon implements Icon
         g.fillRect(s1, 0, e1 - s1, height);
       }
       g.setColor(gcol.getMaxColour());
-      g.fillRect(0, e1, width - e1, height);
+//      g.fillRect(0, e1, width - e1, height);  // BH 2018
+      g.fillRect(e1, 0, width - e1, height);
     }
   }
 }