From 8e9cee1be3194bacb353d94a2166fff1afe8029b Mon Sep 17 00:00:00 2001 From: gmungoc Date: Wed, 5 Apr 2017 15:47:10 +0100 Subject: [PATCH] JAL-2361 simplify constructors, select unsaved but applied user defined colour scheme in menu --- src/jalview/gui/AlignFrame.java | 12 ++---- src/jalview/gui/ColourMenuHelper.java | 66 ++++++++++++++++++------------- src/jalview/gui/PopupMenu.java | 34 ++++++---------- src/jalview/gui/UserDefinedColours.java | 52 ++++-------------------- 4 files changed, 61 insertions(+), 103 deletions(-) diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index 507bfab..d4c87d8 100644 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -3335,7 +3335,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, */ if (ResidueColourScheme.USER_DEFINED.equals(name)) { - new UserDefinedColours(alignPanel, null); + new UserDefinedColours(alignPanel); return; } @@ -3356,10 +3356,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, public void changeColour(ColourSchemeI cs) { // TODO: pull up to controller method - if (cs != null) - { - ColourMenuHelper.setColourSelected(colourMenu, cs.getSchemeName()); - } + ColourMenuHelper.setColourSelected(colourMenu, cs); viewport.setGlobalColourScheme(cs); @@ -5708,10 +5705,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, colourMenu.add(annotationColour); ColourSchemeI colourScheme = viewport.getGlobalColourScheme(); - String schemeName = colourScheme == null ? null : colourScheme - .getSchemeName(); - - ColourMenuHelper.setColourSelected(colourMenu, schemeName); + ColourMenuHelper.setColourSelected(colourMenu, colourScheme); } } diff --git a/src/jalview/gui/ColourMenuHelper.java b/src/jalview/gui/ColourMenuHelper.java index 801615c..b2b9574 100644 --- a/src/jalview/gui/ColourMenuHelper.java +++ b/src/jalview/gui/ColourMenuHelper.java @@ -23,6 +23,12 @@ public class ColourMenuHelper { public interface ColourChangeListener { + /** + * Change colour scheme to the selected scheme + * + * @param name + * the registered (unique) name of a colour scheme + */ void changeColour_actionPerformed(String name); } @@ -37,7 +43,7 @@ public class ColourMenuHelper *
  • Clustal
  • *
  • ...other 'built-in' colours
  • *
  • ...any user-defined colours
  • - *
  • User Defined..
  • + *
  • User Defined..(only for AlignFrame menu)
  • * * * @param colourMenu @@ -77,7 +83,7 @@ public class ColourMenuHelper } /* - * scan registered colour schemes (built-in or user-defined + * scan registered colour schemes (built-in or user-defined) * and add them to the menu (in the order they were registered) */ Iterable colourSchemes = ColourSchemes.getInstance() @@ -171,6 +177,7 @@ public class ColourMenuHelper final String label = MessageManager.getString("action.user_defined"); JRadioButtonMenuItem userDefinedColour = new JRadioButtonMenuItem( label); + userDefinedColour.setName(ResidueColourScheme.USER_DEFINED); userDefinedColour.addActionListener(new ActionListener() { @Override @@ -187,20 +194,22 @@ public class ColourMenuHelper } /** - * Marks as selected the colour menu item matching the given name, or the - * first item ('None') if no match is found + * Marks as selected the colour menu item matching the given colour scheme, or + * the first item ('None') if no match is found. If the colour scheme is a + * user defined scheme, but not in the menu (this arises if a new scheme is + * defined and applied but not saved to file), then menu option + * "User Defined.." is selected. * * @param colourMenu - * @param colourName + * @param cs */ - public static void setColourSelected(JMenu colourMenu, String colourName) + public static void setColourSelected(JMenu colourMenu, ColourSchemeI cs) { - if (colourName == null) - { - return; - } + String colourName = cs == null ? ResidueColourScheme.NONE : cs + .getSchemeName(); JRadioButtonMenuItem none = null; + JRadioButtonMenuItem userDefined = null; /* * select the radio button whose name matches the colour name @@ -210,38 +219,39 @@ public class ColourMenuHelper { if (menuItem instanceof JRadioButtonMenuItem) { - String buttonName = ((JRadioButtonMenuItem) menuItem).getName(); - if (colourName.equals(buttonName)) + JRadioButtonMenuItem radioButton = (JRadioButtonMenuItem) menuItem; + String buttonName = radioButton.getName(); + if (buttonName.equals(colourName)) { - ((JRadioButtonMenuItem) menuItem).setSelected(true); + radioButton.setSelected(true); return; } if (ResidueColourScheme.NONE.equals(buttonName)) { - none = (JRadioButtonMenuItem) menuItem; + none = radioButton; + } + if (ResidueColourScheme.USER_DEFINED.equals(buttonName)) + { + userDefined = radioButton; } } } - if (none != null) + + /* + * no match by name; select User Defined.. if current scheme is a + * user defined one, else select None + */ + if (cs instanceof UserColourScheme && userDefined != null) + { + userDefined.setSelected(true); + } + else if (none != null) { none.setSelected(true); } } /** - * Marks as selected the colour menu item matching the given colour scheme, or - * the first item ('None') if no match is found - * - * @param colourMenu - * @param cs - */ - public static void setColourSelected(JMenu colourMenu, ColourSchemeI cs) - { - setColourSelected(colourMenu, cs == null ? ResidueColourScheme.NONE - : cs.getSchemeName()); - } - - /** * Updates the USER_DEFINE_COLOURS preference to remove any de-registered * colour scheme */ diff --git a/src/jalview/gui/PopupMenu.java b/src/jalview/gui/PopupMenu.java index d91fa70..660c651 100644 --- a/src/jalview/gui/PopupMenu.java +++ b/src/jalview/gui/PopupMenu.java @@ -47,7 +47,6 @@ import jalview.schemes.Blosum62ColourScheme; import jalview.schemes.ColourSchemeI; import jalview.schemes.ColourSchemes; import jalview.schemes.PIDColourScheme; -import jalview.schemes.ResidueColourScheme; import jalview.util.GroupUrlLink; import jalview.util.GroupUrlLink.UrlStringTooLongException; import jalview.util.MessageManager; @@ -1984,28 +1983,19 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener public void changeColour_actionPerformed(String colourSchemeName) { SequenceGroup sg = getGroup(); - if (ResidueColourScheme.USER_DEFINED.equals(colourSchemeName)) - { - /* - * open a panel to load or configure a user-defined colour scheme - */ - new UserDefinedColours(ap, sg); - } - else + /* + * switch to the chosen colour scheme (or null for None) + */ + ColourSchemeI colourScheme = ColourSchemes.getInstance() + .getColourScheme(colourSchemeName, sg, + ap.av.getHiddenRepSequences()); + sg.setColourScheme(colourScheme); + if (colourScheme instanceof Blosum62ColourScheme + || colourScheme instanceof PIDColourScheme) { - /* - * switch to the chosen colour scheme (or null for None) - */ - ColourSchemeI colourScheme = ColourSchemes.getInstance().getColourScheme( - colourSchemeName, sg, ap.av.getHiddenRepSequences()); - sg.setColourScheme(colourScheme); - if (colourScheme instanceof Blosum62ColourScheme - || colourScheme instanceof PIDColourScheme) - { - sg.cs.setConsensus(AAFrequency.calculate( - sg.getSequences(ap.av.getHiddenRepSequences()), - sg.getStartRes(), sg.getEndRes() + 1)); - } + sg.cs.setConsensus(AAFrequency.calculate( + sg.getSequences(ap.av.getHiddenRepSequences()), + sg.getStartRes(), sg.getEndRes() + 1)); } refresh(); diff --git a/src/jalview/gui/UserDefinedColours.java b/src/jalview/gui/UserDefinedColours.java index 5ec95ed..369dde6 100755 --- a/src/jalview/gui/UserDefinedColours.java +++ b/src/jalview/gui/UserDefinedColours.java @@ -21,7 +21,6 @@ package jalview.gui; import jalview.bin.Cache; -import jalview.datamodel.SequenceGroup; import jalview.io.JalviewFileChooser; import jalview.io.JalviewFileView; import jalview.jbgui.GUserDefinedColours; @@ -78,8 +77,6 @@ public class UserDefinedColours extends GUserDefinedColours implements AlignmentPanel ap; - SequenceGroup seqGroup; - ColourSchemeI oldColourScheme; JInternalFrame frame; @@ -89,28 +86,19 @@ public class UserDefinedColours extends GUserDefinedColours implements List lowerCaseButtons; /** - * Creates a new UserDefinedColours object. + * Creates and displays a new UserDefinedColours panel * - * @param ap - * @param sg + * @param alignPanel */ - public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg) + public UserDefinedColours(AlignmentPanel alignPanel) { this(); lcaseColour.setEnabled(false); - this.ap = ap; - seqGroup = sg; + this.ap = alignPanel; - if (seqGroup != null) - { - oldColourScheme = seqGroup.getColourScheme(); - } - else - { - oldColourScheme = ap.av.getGlobalColourScheme(); - } + oldColourScheme = alignPanel.av.getGlobalColourScheme(); if (oldColourScheme instanceof UserColourScheme) { @@ -148,11 +136,6 @@ public class UserDefinedColours extends GUserDefinedColours implements Desktop.addInternalFrame(frame, MessageManager.getString("label.user_defined_colours"), MY_FRAME_WIDTH, MY_FRAME_HEIGHT, true); - - if (seqGroup != null) - { - frame.setTitle(frame.getTitle() + " (" + seqGroup.getName() + ")"); - } } /** @@ -496,15 +479,7 @@ public class UserDefinedColours extends GUserDefinedColours implements } UserColourScheme ucs = getSchemeFromButtons(); - if (seqGroup != null) - { - seqGroup.setColourScheme(ucs); - ap.paintAlignment(true); - } - else if (ap != null) - { - ap.alignFrame.changeColour(ucs); - } + ap.alignFrame.changeColour(ucs); } /** @@ -695,7 +670,6 @@ public class UserDefinedColours extends GUserDefinedColours implements { return; } - ColourSchemes.getInstance().removeColourScheme(name); } JalviewFileChooser chooser = new JalviewFileChooser("jc", "Jalview User Colours"); @@ -809,18 +783,8 @@ public class UserDefinedColours extends GUserDefinedColours implements @Override protected void cancelButton_actionPerformed() { - if (ap != null) - { - if (seqGroup != null) - { - seqGroup.setColourScheme(oldColourScheme); - } - else - { - ap.alignFrame.changeColour(oldColourScheme); - } - ap.paintAlignment(true); - } + ap.alignFrame.changeColour(oldColourScheme); + ap.paintAlignment(true); try { -- 1.7.10.2