JAL-3187 use the recommended way of catching tab selection change events
authorJim Procter <jprocter@issues.jalview.org>
Thu, 20 Feb 2020 17:24:08 +0000 (17:24 +0000)
committerJim Procter <jprocter@issues.jalview.org>
Thu, 20 Feb 2020 17:24:08 +0000 (17:24 +0000)
src/jalview/gui/SplitFrame.java

index 8e5c2de..1fad9ae 100644 (file)
@@ -37,8 +37,6 @@ import java.awt.Component;
 import java.awt.Dimension;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
-import java.awt.event.FocusEvent;
-import java.awt.event.FocusListener;
 import java.awt.event.KeyAdapter;
 import java.awt.event.KeyEvent;
 import java.awt.event.KeyListener;
@@ -58,6 +56,8 @@ import javax.swing.JMenuItem;
 import javax.swing.JPanel;
 import javax.swing.JTabbedPane;
 import javax.swing.KeyStroke;
+import javax.swing.event.ChangeEvent;
+import javax.swing.event.ChangeListener;
 import javax.swing.event.InternalFrameAdapter;
 import javax.swing.event.InternalFrameEvent;
 
@@ -867,6 +867,30 @@ public class SplitFrame extends GSplitFrame implements SplitContainerI
     {
       showInternalFrame = true;
       featureSettingsPanels = new JTabbedPane();
+      featureSettingsPanels.addChangeListener(new ChangeListener()
+      {
+
+        @Override
+        public void stateChanged(ChangeEvent e)
+        {
+          if (e.getSource() != featureSettingsPanels
+                  || featureSettingsUI == null
+                  || featureSettingsUI.isClosed()
+                  || !featureSettingsUI.isVisible())
+          {
+            // not our tabbed pane
+            return;
+          }
+          int tab = featureSettingsPanels.getSelectedIndex();
+          if (tab < 0 || featureSettingsPanels
+                  .getSelectedComponent() instanceof FeatureSettingsControllerGuiI)
+          {
+            // no tab selected or already showing a feature settings GUI
+            return;
+          }
+          getAlignFrames().get(tab).showFeatureSettingsUI();
+        }
+      });
       featureSettingsUI = new JInternalFrame(MessageManager.getString(
               "label.sequence_feature_settings_for_CDS_and_Protein"));
       featureSettingsPanels.setOpaque(true);
@@ -998,33 +1022,13 @@ public class SplitFrame extends GSplitFrame implements SplitContainerI
                     }
                   }
                   featureSettingsPanels = null;
+                  featureSettingsUI = null;
                 };
               });
       featureSettingsUI.setLayer(JLayeredPane.PALETTE_LAYER);
     }
   }
 
-  /*
-   * for materialising feature settings for a tab when clicked on
-   */
-  private FocusListener fl1 = new FocusListener()
-  {
-
-    @Override
-    public void focusLost(FocusEvent e)
-    {
-      // TODO Auto-generated method stub
-
-    }
-
-    @Override
-    public void focusGained(FocusEvent e)
-    {
-      int tab = featureSettingsPanels.getSelectedIndex();
-      getAlignFrames().get(tab).showFeatureSettingsUI();
-    }
-  };
-
   /**
    * tab names for feature settings
    */
@@ -1041,7 +1045,6 @@ public class SplitFrame extends GSplitFrame implements SplitContainerI
     for (int tabIndex = 0; tabIndex < 2; tabIndex++)
     {
       JPanel dummyTab = new JPanel();
-      dummyTab.addFocusListener(fl1);
       featureSettingsPanels.addTab(tabName[tabIndex], dummyTab);
     }
   }
@@ -1052,26 +1055,25 @@ public class SplitFrame extends GSplitFrame implements SplitContainerI
     for (int tabIndex = 0; tabIndex < 2; tabIndex++)
     {
       if (featureSettingsPanels.getTabCount() > tabIndex)
-    {
+      {
         dummyTab = featureSettingsPanels.getTabComponentAt(tabIndex);
         if (dummyTab instanceof FeatureSettingsControllerGuiI
                 && !dummyTab.isVisible())
-      {
+        {
           featureSettingsPanels.removeTabAt(tabIndex);
           // close the feature Settings tab
           ((FeatureSettingsControllerGuiI) dummyTab)
                   .featureSettings_isClosed();
           // create a dummy tab in its place
-        dummyTab = new JPanel();
-        dummyTab.addFocusListener(fl1);
+          dummyTab = new JPanel();
           featureSettingsPanels.insertTab(tabName[tabIndex], null, dummyTab,
                   MessageManager.formatMessage(
                           "label.sequence_feature_settings_for",
                           tabName[tabIndex]),
                   tabIndex);
+        }
       }
     }
-    }
   }
 
   @Override