From 84af78ccf403b216724163f7cbc1bfa03bec7ca5 Mon Sep 17 00:00:00 2001 From: kruland2607 Date: Tue, 10 Sep 2013 21:46:38 -0500 Subject: [PATCH] Extract the MotorRowFilter out of the ThrustCurveMotorSelectionPanel to facilitate adding more filters. --- .../motor/thrustcurve/MotorRowFilter.java | 83 +++++++++++++++++ .../ThrustCurveMotorSelectionPanel.java | 92 +++---------------- 2 files changed, 94 insertions(+), 81 deletions(-) create mode 100644 core/src/net/sf/openrocket/gui/dialogs/motor/thrustcurve/MotorRowFilter.java diff --git a/core/src/net/sf/openrocket/gui/dialogs/motor/thrustcurve/MotorRowFilter.java b/core/src/net/sf/openrocket/gui/dialogs/motor/thrustcurve/MotorRowFilter.java new file mode 100644 index 000000000..142571e04 --- /dev/null +++ b/core/src/net/sf/openrocket/gui/dialogs/motor/thrustcurve/MotorRowFilter.java @@ -0,0 +1,83 @@ +package net.sf.openrocket.gui.dialogs.motor.thrustcurve; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Locale; + +import javax.swing.RowFilter; +import javax.swing.table.TableModel; + +import net.sf.openrocket.database.motor.ThrustCurveMotorSet; + +//////// Row filters + +/** + * Abstract adapter class. + */ +class MotorRowFilter extends RowFilter { + + public enum DiameterFilterControl { + ALL, + EXACT, + SMALLER + }; + + private final ThrustCurveMotorDatabaseModel model; + private final double diameter; + + private List searchTerms = Collections. emptyList(); + private DiameterFilterControl diameterControl = DiameterFilterControl.ALL; + + public MotorRowFilter(ThrustCurveMotorDatabaseModel model, double diameter) { + super(); + this.model = model; + this.diameter = diameter; + } + + public void setSearchTerms(final List searchTerms) { + this.searchTerms = new ArrayList(); + for (String s : searchTerms) { + s = s.trim().toLowerCase(Locale.getDefault()); + if (s.length() > 0) { + this.searchTerms.add(s); + } + } + } + + void setDiameterControl(DiameterFilterControl diameterControl) { + this.diameterControl = diameterControl; + } + + @Override + public boolean include(RowFilter.Entry entry) { + int index = entry.getIdentifier(); + ThrustCurveMotorSet m = model.getMotorSet(index); + return filterByDiameter(m) && filterByString(m); + } + + public boolean filterByDiameter(ThrustCurveMotorSet m) { + switch (diameterControl) { + default: + case ALL: + return true; + case EXACT: + return ((m.getDiameter() <= diameter + 0.0004) && (m.getDiameter() >= diameter - 0.0015)); + case SMALLER: + return (m.getDiameter() <= diameter + 0.0004); + } + } + + + public boolean filterByString(ThrustCurveMotorSet m) { + main: for (String s : searchTerms) { + for (ThrustCurveMotorColumns col : ThrustCurveMotorColumns.values()) { + String str = col.getValue(m).toString().toLowerCase(Locale.getDefault()); + if (str.indexOf(s) >= 0) + continue main; + } + return false; + } + return true; + } +} diff --git a/swing/src/net/sf/openrocket/gui/dialogs/motor/thrustcurve/ThrustCurveMotorSelectionPanel.java b/swing/src/net/sf/openrocket/gui/dialogs/motor/thrustcurve/ThrustCurveMotorSelectionPanel.java index e9781b210..3456fa65b 100644 --- a/swing/src/net/sf/openrocket/gui/dialogs/motor/thrustcurve/ThrustCurveMotorSelectionPanel.java +++ b/swing/src/net/sf/openrocket/gui/dialogs/motor/thrustcurve/ThrustCurveMotorSelectionPanel.java @@ -34,7 +34,6 @@ import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.ListCellRenderer; import javax.swing.ListSelectionModel; -import javax.swing.RowFilter; import javax.swing.RowSorter; import javax.swing.SortOrder; import javax.swing.SwingUtilities; @@ -109,18 +108,17 @@ public class ThrustCurveMotorSelectionPanel extends JPanel implements MotorSelec private final List database; - private final double diameter; private CloseableDialog dialog = null; - private final ThrustCurveMotorDatabaseModel model; + final ThrustCurveMotorDatabaseModel model; private final JTable table; private final TableRowSorter sorter; private final JCheckBox hideSimilarBox; private final JTextField searchField; - private String[] searchTerms = new String[0]; + String[] searchTerms = new String[0]; private final JLabel curveSelectionLabel; @@ -162,7 +160,6 @@ public class ThrustCurveMotorSelectionPanel extends JPanel implements MotorSelec public ThrustCurveMotorSelectionPanel(ThrustCurveMotor current, double delay, double diameter) { super(new MigLayout("fill", "[grow][]")); - this.diameter = diameter; // Construct the database (adding the current motor if not in the db already) @@ -189,6 +186,8 @@ public class ThrustCurveMotorSelectionPanel extends JPanel implements MotorSelec } database = db; + model = new ThrustCurveMotorDatabaseModel(database); + final MotorRowFilter rowFilter = new MotorRowFilter(model, diameter); //// GUI @@ -217,20 +216,21 @@ public class ThrustCurveMotorSelectionPanel extends JPanel implements MotorSelec sel = SHOW_ALL; switch (sel) { case SHOW_ALL: - sorter.setRowFilter(new MotorRowFilterAll()); + rowFilter.setDiameterControl(MotorRowFilter.DiameterFilterControl.ALL); break; case SHOW_SMALLER: - sorter.setRowFilter(new MotorRowFilterSmaller()); + rowFilter.setDiameterControl(MotorRowFilter.DiameterFilterControl.SMALLER); break; case SHOW_EXACT: - sorter.setRowFilter(new MotorRowFilterExact()); + rowFilter.setDiameterControl(MotorRowFilter.DiameterFilterControl.EXACT); break; default: throw new BugException("Invalid selection mode sel=" + sel); } + sorter.sort(); Application.getPreferences().putChoice("MotorDiameterMatch", sel); scrollSelectionVisible(); } @@ -252,7 +252,6 @@ public class ThrustCurveMotorSelectionPanel extends JPanel implements MotorSelec // Motor selection table - model = new ThrustCurveMotorDatabaseModel(database); table = new JTable(model); @@ -275,6 +274,8 @@ public class ThrustCurveMotorSelectionPanel extends JPanel implements MotorSelec sorter.setSortKeys(Arrays.asList(sortKeys)); } + sorter.setRowFilter(rowFilter); + // Set selection and double-click listeners table.getSelectionModel().addListSelectionListener(new ListSelectionListener() { @Override @@ -344,14 +345,7 @@ public class ThrustCurveMotorSelectionPanel extends JPanel implements MotorSelec private void update() { String text = searchField.getText().trim(); String[] split = text.split("\\s+"); - ArrayList list = new ArrayList(); - for (String s : split) { - s = s.trim().toLowerCase(Locale.getDefault()); - if (s.length() > 0) { - list.add(s); - } - } - searchTerms = list.toArray(new String[0]); + rowFilter.setSearchTerms( Arrays.asList(split) ); sorter.sort(); scrollSelectionVisible(); } @@ -386,10 +380,6 @@ public class ThrustCurveMotorSelectionPanel extends JPanel implements MotorSelec }); panel.add(curveSelectionBox, "growx, wrap para"); - - - - // Ejection charge delay: panel.add(new JLabel(trans.get("TCMotorSelPan.lbl.Ejectionchargedelay"))); @@ -936,66 +926,6 @@ public class ThrustCurveMotorSelectionPanel extends JPanel implements MotorSelec } - //////// Row filters - - /** - * Abstract adapter class. - */ - private abstract class MotorRowFilter extends RowFilter { - @Override - public boolean include(RowFilter.Entry entry) { - int index = entry.getIdentifier(); - ThrustCurveMotorSet m = model.getMotorSet(index); - return filterByDiameter(m) && filterByString(m); - } - - public abstract boolean filterByDiameter(ThrustCurveMotorSet m); - - - public boolean filterByString(ThrustCurveMotorSet m) { - main: for (String s : searchTerms) { - for (ThrustCurveMotorColumns col : ThrustCurveMotorColumns.values()) { - String str = col.getValue(m).toString().toLowerCase(Locale.getDefault()); - if (str.indexOf(s) >= 0) - continue main; - } - return false; - } - return true; - } - } - - /** - * Show all motors. - */ - private class MotorRowFilterAll extends MotorRowFilter { - @Override - public boolean filterByDiameter(ThrustCurveMotorSet m) { - return true; - } - } - - /** - * Show motors smaller than the mount. - */ - private class MotorRowFilterSmaller extends MotorRowFilter { - @Override - public boolean filterByDiameter(ThrustCurveMotorSet m) { - return (m.getDiameter() <= diameter + 0.0004); - } - } - - /** - * Show motors that fit the mount. - */ - private class MotorRowFilterExact extends MotorRowFilter { - @Override - public boolean filterByDiameter(ThrustCurveMotorSet m) { - return ((m.getDiameter() <= diameter + 0.0004) && (m.getDiameter() >= diameter - 0.0015)); - } - } - - /** * Custom layered pane that sets the bounds of the components on every layout. */