From 761ad3f4f8408cee61c932d926532e186d71b95d Mon Sep 17 00:00:00 2001 From: gmungoc Date: Fri, 24 Nov 2017 14:46:42 +0000 Subject: [PATCH] JAL-2808 refactoring to better handler filter by Score or Label etc --- src/jalview/gui/FeatureTypeSettings.java | 175 +++++++++++++++++++++--------- 1 file changed, 123 insertions(+), 52 deletions(-) diff --git a/src/jalview/gui/FeatureTypeSettings.java b/src/jalview/gui/FeatureTypeSettings.java index 835f1fc..c356731 100644 --- a/src/jalview/gui/FeatureTypeSettings.java +++ b/src/jalview/gui/FeatureTypeSettings.java @@ -38,7 +38,7 @@ import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; -import java.awt.LayoutManager; +import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusAdapter; @@ -79,6 +79,12 @@ import javax.swing.plaf.basic.BasicArrowButton; */ public class FeatureTypeSettings extends JalviewDialog { + private final static String LABEL_18N = MessageManager + .getString("label.label"); + + private final static String SCORE_18N = MessageManager + .getString("label.score"); + private static final int RADIO_WIDTH = 130; private static final String COLON = ":"; @@ -294,8 +300,7 @@ public class FeatureTypeSettings extends JalviewDialog } else { - colourByTextCombo - .setSelectedItem(MessageManager.getString("label.label")); + colourByTextCombo.setSelectedItem(LABEL_18N); } } else @@ -336,8 +341,7 @@ public class FeatureTypeSettings extends JalviewDialog } else { - colourByRangeCombo - .setSelectedItem(MessageManager.getString("label.score")); + colourByRangeCombo.setSelectedItem(SCORE_18N); } Color noColour = fc.getNoColour(); if (noColour == null) @@ -444,8 +448,7 @@ public class FeatureTypeSettings extends JalviewDialog } String colourBy = (String) colourByRangeCombo.getSelectedItem(); - String[] attNames = fromAttributeDisplayName(colourBy); - float[] minMax = getMinMax(attNames); + float[] minMax = getMinMax(colourBy); if (minMax != null) { @@ -461,13 +464,13 @@ public class FeatureTypeSettings extends JalviewDialog *
  • else of the selected attribute
  • * * - * @param attNames + * @param attName * @return */ - private float[] getMinMax(String[] attNames) + private float[] getMinMax(String attName) { float[] minMax = null; - if (MessageManager.getString("label.score").equals(attNames[0])) + if (SCORE_18N.equals(attName)) { minMax = fr.getMinMax().get(featureType)[0]; } @@ -475,7 +478,7 @@ public class FeatureTypeSettings extends JalviewDialog { // colour by attribute range minMax = FeatureAttributes.getInstance().getMinMax(featureType, - attNames); + fromAttributeDisplayName(attName)); } return minMax; } @@ -902,7 +905,7 @@ public class FeatureTypeSettings extends JalviewDialog FeatureColourI fc = new FeatureColour(c, c, null, 0f, 0f); fc.setColourByLabel(true); String byWhat = (String) colourByTextCombo.getSelectedItem(); - if (!MessageManager.getString("label.label").equals(byWhat)) + if (!LABEL_18N.equals(byWhat)) { fc.setAttributeName(fromAttributeDisplayName(byWhat)); } @@ -959,7 +962,7 @@ public class FeatureTypeSettings extends JalviewDialog * set attribute to colour by if selected */ String byWhat = (String) colourByRangeCombo.getSelectedItem(); - if (!MessageManager.getString("label.score").equals(byWhat)) + if (!SCORE_18N.equals(byWhat)) { fc.setAttributeName(fromAttributeDisplayName(byWhat)); } @@ -1134,7 +1137,7 @@ public class FeatureTypeSettings extends JalviewDialog if (withText) { - displayAtts.add(MessageManager.getString("label.label")); + displayAtts.add(LABEL_18N); tooltips.add(MessageManager.getString("label.description")); } if (withRange) @@ -1142,8 +1145,8 @@ public class FeatureTypeSettings extends JalviewDialog float[][] minMax = fr.getMinMax().get(featureType); if (minMax != null && minMax[0][0] != minMax[0][1]) { - displayAtts.add(MessageManager.getString("label.score")); - tooltips.add(MessageManager.getString("label.score")); + displayAtts.add(SCORE_18N); + tooltips.add(SCORE_18N); } } @@ -1188,11 +1191,11 @@ public class FeatureTypeSettings extends JalviewDialog filtersPanel.add(andOrPanel); /* - * panel with filters - populated by refreshFiltersDisplay + * panel with filters - populated by refreshFiltersDisplay, + * which also sets the layout manager */ chooseFiltersPanel = new JPanel(); - LayoutManager box = new BoxLayout(chooseFiltersPanel, BoxLayout.Y_AXIS); - chooseFiltersPanel.setLayout(box); + chooseFiltersPanel.setBackground(Color.white); filtersPanel.add(chooseFiltersPanel); return filtersPanel; @@ -1267,9 +1270,21 @@ public class FeatureTypeSettings extends JalviewDialog /* * and an empty filter for the user to populate (add) */ - FeatureMatcherI noFilter = FeatureMatcher.byLabel(Condition.values()[0], - ""); - filters.add(noFilter); + filters.add(FeatureMatcher.NULL_MATCHER); + + /* + * use GridLayout to 'justify' rows to the top of the panel, until + * there are too many to fit in, then fall back on BoxLayout + */ + if (filters.size() <= 5) + { + chooseFiltersPanel.setLayout(new GridLayout(5, 1)); + } + else + { + chooseFiltersPanel.setLayout( + new BoxLayout(chooseFiltersPanel, BoxLayout.Y_AXIS)); + } /* * render the conditions in rows, each in its own JPanel @@ -1277,11 +1292,7 @@ public class FeatureTypeSettings extends JalviewDialog int filterIndex = 0; for (FeatureMatcherI filter : filters) { - String[] attName = filter.getKey(); - Condition condition = filter.getMatcher().getCondition(); - String pattern = filter.getMatcher().getPattern(); - JPanel row = addFilter(attName, attNames, condition, pattern, - filterIndex); + JPanel row = addFilter(filter, attNames, filterIndex); row.setBorder(BorderFactory.createLineBorder(debugBorderColour)); chooseFiltersPanel.add(row); filterIndex++; @@ -1294,25 +1305,36 @@ public class FeatureTypeSettings extends JalviewDialog /** * A helper method that constructs a row (panel) with one filter condition: * - * If attribute, condition or pattern are not null, they are set as defaults for - * the input fields. The 'remove' button is added unless the pattern is null or - * empty (incomplete filter condition). + * The filter values are set as defaults for the input fields. The 'remove' + * button is added unless the pattern is empty (incomplete filter condition). + *

    + * Action handlers on these fields provide for + *

    * - * @param attName + * @param filter * @param attNames - * @param cond - * @param pattern * @param filterIndex * @return */ - protected JPanel addFilter(String[] attName, List attNames, - Condition cond, String pattern, int filterIndex) + protected JPanel addFilter(FeatureMatcherI filter, + List attNames, int filterIndex) { + String[] attName = filter.getAttribute(); + Condition cond = filter.getMatcher().getCondition(); + String pattern = filter.getMatcher().getPattern(); + JPanel filterRow = new JPanel(new FlowLayout(FlowLayout.LEFT)); filterRow.setBackground(Color.white); @@ -1322,8 +1344,12 @@ public class FeatureTypeSettings extends JalviewDialog */ final JComboBox attCombo = populateAttributesDropdown(attNames, true, true); + String filterBy = setSelectedAttribute(attCombo, filter); + JComboBox condCombo = new JComboBox<>(); + JTextField patternField = new JTextField(8); + patternField.setText(pattern); /* * action handlers that validate and (if valid) apply changes @@ -1351,7 +1377,7 @@ public class FeatureTypeSettings extends JalviewDialog } }; - if (attName == null) // the 'add a condition' row + if (filter == FeatureMatcher.NULL_MATCHER) // the 'add a condition' row { attCombo.setSelectedIndex(0); } @@ -1369,7 +1395,8 @@ public class FeatureTypeSettings extends JalviewDialog * ensure it is appropriate for the attribute datatype */ populateConditions((String) attCombo.getSelectedItem(), - (Condition) condCombo.getSelectedItem(), condCombo); + (Condition) condCombo.getSelectedItem(), condCombo, + patternField); actionListener.actionPerformed(null); } }); @@ -1379,8 +1406,7 @@ public class FeatureTypeSettings extends JalviewDialog /* * drop-down choice of test condition */ - populateConditions((String) attCombo.getSelectedItem(), cond, - condCombo); + populateConditions(filterBy, cond, condCombo, patternField); condCombo.setPreferredSize(new Dimension(150, 20)); condCombo.addItemListener(itemListener); filterRow.add(condCombo); @@ -1388,7 +1414,6 @@ public class FeatureTypeSettings extends JalviewDialog /* * pattern to match against */ - patternField.setText(pattern); patternField.addActionListener(actionListener); patternField.addFocusListener(new FocusAdapter() { @@ -1413,7 +1438,7 @@ public class FeatureTypeSettings extends JalviewDialog * if a numeric condition is selected, show the value range * as a tooltip on the value input field */ - updatePatternTooltip(attName, selectedCondition, patternField); + setPatternTooltip(filterBy, selectedCondition, patternField); /* * add remove button if filter is populated (non-empty pattern) @@ -1441,6 +1466,33 @@ public class FeatureTypeSettings extends JalviewDialog } /** + * Sets the selected item in the Label/Score/Attribute drop-down to match the + * filter + * + * @param attCombo + * @param filter + */ + private String setSelectedAttribute(JComboBox attCombo, + FeatureMatcherI filter) + { + String item = null; + if (filter.isByScore()) + { + item = SCORE_18N; + } + else if (filter.isByLabel()) + { + item = LABEL_18N; + } + else + { + item = toAttributeDisplayName(filter.getAttribute()); + } + attCombo.setSelectedItem(item); + return item; + } + + /** * If a numeric comparison condition is selected, retrieve the min-max range for * the value (score or attribute), and set it as a tooltip on the value file * @@ -1448,7 +1500,7 @@ public class FeatureTypeSettings extends JalviewDialog * @param selectedCondition * @param patternField */ - private void updatePatternTooltip(String[] attName, + private void setPatternTooltip(String attName, Condition selectedCondition, JTextField patternField) { patternField.setToolTipText(""); @@ -1469,22 +1521,24 @@ public class FeatureTypeSettings extends JalviewDialog * Populates the drop-down list of comparison conditions for the given attribute * name. The conditions added depend on the datatype of the attribute values. * The supplied condition is set as the selected item in the list, provided it - * is in the list. + * is in the list. If the pattern is now invalid (non-numeric pattern for a + * numeric condition), it is cleared. * * @param attName * @param cond * @param condCombo + * @param patternField */ private void populateConditions(String attName, Condition cond, - JComboBox condCombo) + JComboBox condCombo, JTextField patternField) { Datatype type = FeatureAttributes.getInstance().getDatatype(featureType, fromAttributeDisplayName(attName)); - if (MessageManager.getString("label.label").equals(attName)) + if (LABEL_18N.equals(attName)) { type = Datatype.Character; } - else if (MessageManager.getString("label.score").equals(attName)) + else if (SCORE_18N.equals(attName)) { type = Datatype.Number; } @@ -1522,6 +1576,24 @@ public class FeatureTypeSettings extends JalviewDialog } condCombo.addItemListener(listener); + + /* + * clear pattern if it is now invalid for condition + */ + if (((Condition) condCombo.getSelectedItem()).isNumeric()) + { + try + { + String pattern = patternField.getText().trim(); + if (pattern.length() > 0) + { + Float.valueOf(pattern); + } + } catch (NumberFormatException e) + { + patternField.setText(""); + } + } } /** @@ -1544,7 +1616,7 @@ public class FeatureTypeSettings extends JalviewDialog } Condition cond = (Condition) condCombo.getSelectedItem(); - if (cond.needsAPattern()) + if (!cond.needsAPattern()) { return true; } @@ -1598,8 +1670,7 @@ public class FeatureTypeSettings extends JalviewDialog Condition cond = (Condition) condCombo.getSelectedItem(); String pattern = valueField.getText().trim(); - updatePatternTooltip(fromAttributeDisplayName(attName), cond, - valueField); + setPatternTooltip(attName, cond, valueField); if (pattern.length() == 0 && cond.needsAPattern()) { @@ -1611,11 +1682,11 @@ public class FeatureTypeSettings extends JalviewDialog * or named attribute */ FeatureMatcherI km = null; - if (MessageManager.getString("label.label").equals(attName)) + if (LABEL_18N.equals(attName)) { km = FeatureMatcher.byLabel(cond, pattern); } - else if (MessageManager.getString("label.score").equals(attName)) + else if (SCORE_18N.equals(attName)) { km = FeatureMatcher.byScore(cond, pattern); } -- 1.7.10.2