Merge branch 'develop' into trialMerge
[jalview.git] / src / jalview / gui / FeatureSettings.java
index 4526517..33c22f4 100644 (file)
@@ -29,6 +29,7 @@ import jalview.datamodel.features.FeatureMatcherI;
 import jalview.datamodel.features.FeatureMatcherSet;
 import jalview.datamodel.features.FeatureMatcherSetI;
 import jalview.gui.Help.HelpId;
+import jalview.gui.JalviewColourChooser.ColourChooserListener;
 import jalview.io.JalviewFileChooser;
 import jalview.io.JalviewFileView;
 import jalview.schemes.FeatureColour;
@@ -80,8 +81,7 @@ import javax.swing.BorderFactory;
 import javax.swing.Icon;
 import javax.swing.JButton;
 import javax.swing.JCheckBox;
-import javax.swing.JColorChooser;
-import javax.swing.JDialog;
+import javax.swing.JCheckBoxMenuItem;
 import javax.swing.JInternalFrame;
 import javax.swing.JLabel;
 import javax.swing.JLayeredPane;
@@ -93,6 +93,7 @@ import javax.swing.JSlider;
 import javax.swing.JTable;
 import javax.swing.ListSelectionModel;
 import javax.swing.SwingConstants;
+import javax.swing.ToolTipManager;
 import javax.swing.border.Border;
 import javax.swing.event.ChangeEvent;
 import javax.swing.event.ChangeListener;
@@ -141,9 +142,9 @@ public class FeatureSettings extends JPanel
    */
   Object[][] originalData;
 
-  private float originalTransparency;
+  float originalTransparency;
 
-  private Map<String, FeatureMatcherSetI> originalFilters;
+  Map<String, FeatureMatcherSetI> originalFilters;
 
   final JInternalFrame frame;
 
@@ -167,7 +168,7 @@ public class FeatureSettings extends JPanel
   /*
    * true when Feature Settings are updating from feature renderer
    */
-  private boolean handlingUpdate = false;
+  boolean handlingUpdate = false;
 
   /*
    * holds {featureCount, totalExtent} for each feature type
@@ -233,6 +234,7 @@ public class FeatureSettings extends JPanel
         
         return tip;
       }
+      
 
       /**
        * Position the tooltip near the bottom edge of, and half way across, the
@@ -253,19 +255,18 @@ public class FeatureSettings extends JPanel
     tableHeader.setFont(new Font("Verdana", Font.PLAIN, 12));
     tableHeader.setReorderingAllowed(false);
     table.setFont(new Font("Verdana", Font.PLAIN, 12));
-
-    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);
@@ -280,8 +281,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)
         {
@@ -302,8 +302,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());
         }
       }
     });
@@ -366,18 +365,9 @@ public class FeatureSettings extends JPanel
 
     frame = new JInternalFrame();
     frame.setContentPane(this);
-    if (Platform.isAMac())
-    {
-      Desktop.addInternalFrame(frame,
-              MessageManager.getString("label.sequence_feature_settings"),
-              600, 480);
-    }
-    else
-    {
-      Desktop.addInternalFrame(frame,
-              MessageManager.getString("label.sequence_feature_settings"),
-              600, 450);
-    }
+    Desktop.addInternalFrame(frame,
+            MessageManager.getString("label.sequence_feature_settings"),
+            600, Platform.isAMacAndNotJS() ? 480 : 450);
     frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
 
     frame.addInternalFrameListener(
@@ -394,30 +384,107 @@ 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;
-
     JPopupMenu men = new JPopupMenu(MessageManager
             .formatMessage("label.settings_for_param", new String[]
             { type }));
+    final FeatureColourI featureColour = (FeatureColourI) typeCol;
+
+    /*
+     * menu option to select (or deselect) variable colour
+     */
+    final JCheckBoxMenuItem variableColourCB = new JCheckBoxMenuItem(
+            MessageManager.getString("label.variable_colour"));
+    variableColourCB.setSelected(!featureColour.isSimpleColour());
+    men.add(variableColourCB);
+    
+    /*
+     * checkbox action listener doubles up as listener to OK
+     * from the variable colour / filters dialog
+     */
+    variableColourCB.addActionListener(new ActionListener()
+    {
+      @Override
+      public void actionPerformed(ActionEvent e)
+      {
+        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())
+          {
+            /*
+             * toggle simple colour to variable colour - show dialog
+             */
+            FeatureTypeSettings fc = new FeatureTypeSettings(fr, type);
+            fc.addActionListener(this);
+          }
+          else
+          {
+            /*
+             * toggle variable to simple colour - show colour chooser
+             */
+            String title = MessageManager.formatMessage("label.select_colour_for", type);
+            ColourChooserListener listener = new ColourChooserListener()
+            {
+              @Override
+              public void colourSelected(Color c)
+              {
+                table.setValueAt(new FeatureColour(c), rowSelected,
+                        COLOUR_COLUMN);
+                table.validate();
+                updateFeatureRenderer(
+                        ((FeatureTableModel) table.getModel()).getData(),
+                        false);
+              }
+            };
+            JalviewColourChooser.showColourChooser(FeatureSettings.this, title,
+               featureColour.getMaxColour(), listener);
+          }
+        }
+        else    
+        {
+          if (e.getSource() instanceof FeatureTypeSettings)
+          {
+            /*
+             * update after OK in feature colour dialog; the updated
+             * colour will have already been set in the FeatureRenderer
+             */
+            FeatureColourI fci = fr.getFeatureColours().get(type);
+            table.setValueAt(fci, rowSelected, COLOUR_COLUMN);
+            // BH 2018 setting a table value does not invalidate it.
+//                 System.out.println("FeatureSettings is valied" + table.isValid());
+//                 table.validate();
+          }
+        }
+      }
+    });
+    
+    men.addSeparator();
+
     JMenuItem scr = new JMenuItem(
             MessageManager.getString("label.sort_by_score"));
     men.add(scr);
-    final FeatureSettings me = this;
     scr.addActionListener(new ActionListener()
     {
 
       @Override
       public void actionPerformed(ActionEvent e)
       {
-        me.af.avc
-                .sortAlignmentByFeatureScore(Arrays.asList(new String[]
+        af.avc.sortAlignmentByFeatureScore(Arrays.asList(new String[]
                 { type }));
       }
-
     });
     JMenuItem dens = new JMenuItem(
             MessageManager.getString("label.sort_by_density"));
@@ -427,11 +494,9 @@ public class FeatureSettings extends JPanel
       @Override
       public void actionPerformed(ActionEvent e)
       {
-        me.af.avc
-                .sortAlignmentByFeatureDensity(Arrays.asList(new String[]
+        af.avc.sortAlignmentByFeatureDensity(Arrays.asList(new String[]
                 { type }));
       }
-
     });
     men.add(dens);
 
@@ -481,7 +546,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
@@ -815,14 +880,16 @@ public class FeatureSettings extends JPanel
     chooser.setDialogTitle(
             MessageManager.getString("label.load_feature_colours"));
     chooser.setToolTipText(MessageManager.getString("action.load"));
-
-    int value = chooser.showOpenDialog(this);
-
-    if (value == JalviewFileChooser.APPROVE_OPTION)
-    {
-      File file = chooser.getSelectedFile();
-      load(file);
-    }
+    chooser.setResponseHandler(0, new Runnable()
+    {
+         @Override
+         public void run() 
+         {
+           File file = chooser.getSelectedFile();
+               load(file);
+         }
+       });
+    chooser.showOpenDialog(this);
   }
 
   /**
@@ -906,13 +973,12 @@ public class FeatureSettings extends JPanel
     chooser.setDialogTitle(
             MessageManager.getString("label.save_feature_colours"));
     chooser.setToolTipText(MessageManager.getString("action.save"));
-
-    int value = chooser.showSaveDialog(this);
-
-    if (value == JalviewFileChooser.APPROVE_OPTION)
-    {
-      save(chooser.getSelectedFile());
-    }
+    int option = chooser.showSaveDialog(this);
+       if (option == JalviewFileChooser.APPROVE_OPTION) 
+       {
+         File file = chooser.getSelectedFile();
+         save(file);
+       }
   }
 
   /**
@@ -1090,7 +1156,7 @@ public class FeatureSettings extends JPanel
    * @param data
    * @param visibleNew
    */
-  private void updateFeatureRenderer(Object[][] data, boolean visibleNew)
+  void updateFeatureRenderer(Object[][] data, boolean visibleNew)
   {
     FeatureSettingsBean[] rowData = getTableAsBeans(data);
 
@@ -1579,11 +1645,10 @@ 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;
-
     FeatureColourI currentColor;
 
     FeatureTypeSettings chooser;
@@ -1592,17 +1657,12 @@ public class FeatureSettings extends JPanel
 
     JButton button;
 
-    JColorChooser colorChooser;
-
-    JDialog dialog;
-
     protected static final String EDIT = "edit";
 
     int rowSelected = 0;
 
-    public ColorEditor(FeatureSettings me)
+    public ColorEditor()
     {
-      this.me = me;
       // Set up the editor (from the table's point of view),
       // which is a button.
       // This button brings up the color chooser dialog,
@@ -1611,82 +1671,92 @@ public class FeatureSettings extends JPanel
       button.setActionCommand(EDIT);
       button.addActionListener(this);
       button.setBorderPainted(false);
-      // Set up the dialog that the button brings up.
-      colorChooser = new JColorChooser();
-      dialog = JColorChooser.createDialog(button,
-              MessageManager.getString("label.select_colour"), true, // modal
-              colorChooser, this, // OK button handler
-              null); // no CANCEL button handler
     }
 
     /**
-     * Handles events from the editor button and from the dialog's OK button.
+     * Handles events from the editor button, and from the colour/filters
+     * dialog's OK button
      */
     @Override
     public void actionPerformed(ActionEvent e)
     {
-      // todo test e.getSource() instead here
-      if (EDIT.equals(e.getActionCommand()))
+      if (button == e.getSource())
       {
-        // The user has clicked the cell, so
-        // bring up the dialog.
         if (currentColor.isSimpleColour())
         {
-          // bring up simple color chooser
-          button.setBackground(currentColor.getColour());
-          colorChooser.setColor(currentColor.getColour());
-          dialog.setVisible(true);
+          /*
+           * simple colour chooser
+           */
+          String ttl = MessageManager.formatMessage("label.select_colour_for", type);
+          ColourChooserListener listener = new ColourChooserListener() 
+          {
+            @Override
+            public void colourSelected(Color c)
+            {
+              currentColor = new FeatureColour(c);
+              table.setValueAt(currentColor, rowSelected, COLOUR_COLUMN);
+              fireEditingStopped();
+            }
+                       @Override
+                       public void cancel() 
+                       {
+                 fireEditingStopped();
+                       }
+          };
+          JalviewColourChooser.showColourChooser(button,  ttl,  currentColor.getColour(), listener);
         }
         else
         {
-          // bring up graduated chooser.
-          chooser = new FeatureTypeSettings(me.fr, type);
-          /**
-           * @j2sNative
+          /*
+           * variable colour and filters dialog
            */
+          chooser = new FeatureTypeSettings(fr, type);
+          if (!Platform.isJS())
           {
             chooser.setRequestFocusEnabled(true);
             chooser.requestFocus();
           }
           chooser.addActionListener(this);
-          // Make the renderer reappear.
           fireEditingStopped();
         }
       }
       else
       {
-        if (currentColor.isSimpleColour())
-        {
-          /*
-           * read off colour picked in colour chooser after OK pressed
-           */
-          currentColor = new FeatureColour(colorChooser.getColor());
-          me.table.setValueAt(currentColor, rowSelected, COLOUR_COLUMN);
-        }
-        else
+        /*
+         * after OK in variable colour dialog, any changes to colour 
+         * (or filters!) are already set in FeatureRenderer, so just
+         * update table data without triggering updateFeatureRenderer
+         */
+        currentColor = fr.getFeatureColours().get(type);
+        FeatureMatcherSetI currentFilter = fr.getFeatureFilter(type);
+        if (currentFilter == null)
         {
-          /*
-           * after OK in variable colour dialog, any changes to colour 
-           * (or filters!) are already set in FeatureRenderer, so just
-           * update table data without triggering updateFeatureRenderer
-           */
-          currentColor = fr.getFeatureColours().get(type);
-          FeatureMatcherSetI currentFilter = me.fr.getFeatureFilter(type);
-          if (currentFilter == null)
-          {
-            currentFilter = new FeatureMatcherSet();
-          }
-          Object[] data = ((FeatureTableModel) table.getModel())
-                  .getData()[rowSelected];
-          data[COLOUR_COLUMN] = currentColor;
-          data[FILTER_COLUMN] = currentFilter;
+          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();
       }
     }
 
-    // Implement the one CellEditor method that AbstractCellEditor doesn't.
+    /**
+     * Override allows access to this method from anonymous inner classes 
+     */
+    @Override
+       protected void fireEditingStopped() 
+    {
+           super.fireEditingStopped();
+       }
+
+       // Implement the one CellEditor method that AbstractCellEditor doesn't.
     @Override
     public Object getCellEditorValue()
     {
@@ -1700,9 +1770,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(FeatureSettings.this.getBackground());
       if (!currentColor.isSimpleColour())
       {
         JLabel btn = new JLabel();
@@ -1728,10 +1798,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;
 
@@ -1745,9 +1815,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);
@@ -1762,7 +1831,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();
@@ -1784,17 +1853,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();
       }
     }
 
@@ -1810,9 +1885,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(FeatureSettings.this.getBackground());
       button.setText(currentFilter.toString());
       button.setIcon(null);
       return button;
@@ -1897,7 +1972,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);
     }
   }
 }