From 7e06e1c4bfbd639d5636c4428f01fc248d1a592a Mon Sep 17 00:00:00 2001 From: Holt59 Date: Sun, 4 Mar 2018 22:53:47 +0100 Subject: [PATCH] Create an ArcFilter factory. --- src/main/org/insa/algo/ArcFilterFactory.java | 55 +++++++++++++++++++ .../org/insa/graphics/AlgorithmPanel.java | 32 +---------- 2 files changed, 58 insertions(+), 29 deletions(-) create mode 100644 src/main/org/insa/algo/ArcFilterFactory.java diff --git a/src/main/org/insa/algo/ArcFilterFactory.java b/src/main/org/insa/algo/ArcFilterFactory.java new file mode 100644 index 0000000..76bfefb --- /dev/null +++ b/src/main/org/insa/algo/ArcFilterFactory.java @@ -0,0 +1,55 @@ +package org.insa.algo; + +import java.util.ArrayList; +import java.util.EnumSet; +import java.util.List; + +import org.insa.algo.AbstractInputData.ArcFilter; +import org.insa.graph.AccessRestrictions.AccessMode; +import org.insa.graph.AccessRestrictions.AccessRestriction; +import org.insa.graph.Arc; + +public class ArcFilterFactory { + + /** + * @return List of all arc filters in this factory. + */ + public static List getAllFilters() { + List filters = new ArrayList<>(); + + // Common filters: + + // 1. No filter (all arcs allowed): + filters.add(new ArcFilter() { + @Override + public boolean isAllowed(Arc arc) { + return true; + } + + @Override + public String toString() { + return "All roads are allowed."; + } + }); + + // 2. Only road allowed for cars: + filters.add(new ArcFilter() { + @Override + public boolean isAllowed(Arc arc) { + return arc.getRoadInformation().getAccessRestrictions().isAllowedForAny(AccessMode.MOTORCAR, + EnumSet.complementOf(EnumSet.of(AccessRestriction.FORBIDDEN, AccessRestriction.PRIVATE))); + } + + @Override + public String toString() { + return "Only roads open for cars."; + } + }); + + // 3. Add your own filters here (do not forget to implement toString() to get an + // understandable output!): + + return filters; + } + +} diff --git a/src/main/org/insa/graphics/AlgorithmPanel.java b/src/main/org/insa/graphics/AlgorithmPanel.java index e901a2a..7e280f1 100644 --- a/src/main/org/insa/graphics/AlgorithmPanel.java +++ b/src/main/org/insa/graphics/AlgorithmPanel.java @@ -10,7 +10,6 @@ import java.awt.event.ActionListener; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.util.ArrayList; -import java.util.EnumSet; import java.util.List; import javax.swing.Box; @@ -30,10 +29,7 @@ import org.insa.algo.AbstractInputData; import org.insa.algo.AbstractInputData.ArcFilter; import org.insa.algo.AbstractInputData.Mode; import org.insa.algo.AlgorithmFactory; -import org.insa.graph.AccessRestrictions; -import org.insa.graph.AccessRestrictions.AccessMode; -import org.insa.graph.AccessRestrictions.AccessRestriction; -import org.insa.graph.Arc; +import org.insa.algo.ArcFilterFactory; import org.insa.graph.Node; import org.insa.graphics.NodesInputPanel.InputChangedEvent; @@ -171,30 +167,8 @@ public class AlgorithmPanel extends JPanel { add(this.nodesInputPanel); components.add(this.nodesInputPanel); - JComboBox arcFilterSelect = new JComboBox<>( - new AbstractInputData.ArcFilter[] { new AbstractInputData.ArcFilter() { - @Override - public boolean isAllowed(Arc arc) { - return true; - } - - @Override - public String toString() { - return "All arcs are allowed"; - } - }, new AbstractInputData.ArcFilter() { - @Override - public boolean isAllowed(Arc arc) { - AccessRestrictions restrictions = arc.getRoadInformation().getAccessRestrictions(); - return restrictions.isAllowedForAny(AccessMode.MOTORCAR, EnumSet - .complementOf(EnumSet.of(AccessRestriction.PRIVATE, AccessRestriction.FORBIDDEN))); - } - - @Override - public String toString() { - return "Only non-private roads allowed for motorcars"; - } - } }); + JComboBox arcFilterSelect = new JComboBox<>( + ArcFilterFactory.getAllFilters().toArray(new ArcFilter[0])); arcFilterSelect.setBackground(Color.WHITE); // Add mode selection