From 1f06fc1eec2cb8802fb5adc3c6b9bada45d16edc Mon Sep 17 00:00:00 2001 From: Holt59 Date: Sun, 25 Feb 2018 20:16:10 +0100 Subject: [PATCH] Link inputs directly to MainWindow to avoid issue when loading new graph/redrawing. --- src/main/org/insa/graphics/MainWindow.java | 66 +++++----- .../graphics/MultiPointsClickListener.java | 113 ------------------ .../org/insa/graphics/NodesInputPanel.java | 19 ++- .../org/insa/graphics/ShortestPathPanel.java | 8 +- 4 files changed, 41 insertions(+), 165 deletions(-) delete mode 100644 src/main/org/insa/graphics/MultiPointsClickListener.java diff --git a/src/main/org/insa/graphics/MainWindow.java b/src/main/org/insa/graphics/MainWindow.java index a8ea007..64a5f93 100644 --- a/src/main/org/insa/graphics/MainWindow.java +++ b/src/main/org/insa/graphics/MainWindow.java @@ -123,9 +123,6 @@ public class MainWindow extends JFrame { // Current running thread private ThreadWrapper currentThread; - // Multi point listener - private MultiPointsClickListener clickAdapter = null; - // Factory private BlockingActionFactory baf; @@ -138,10 +135,31 @@ public class MainWindow extends JFrame { // Create drawing and action listeners... this.drawing = new BasicDrawing(); - this.clickAdapter = new MultiPointsClickListener(this); + spPanel = new ShortestPathPanel(MainWindow.this); + spPanel.addStartActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + StartActionEvent evt = (StartActionEvent) e; + ShortestPathData data = new ShortestPathData(graph, evt.getOrigin(), evt.getDestination(), + evt.getMode()); + try { + ShortestPathAlgorithm spAlgorithm = ShortestPathAlgorithmFactory + .createAlgorithm(evt.getAlgorithmClass(), data); + spPanel.setEnabled(false); + launchShortestPathThread(spAlgorithm); + } + catch (Exception e1) { + JOptionPane.showMessageDialog(MainWindow.this, + "An error occurred while creating the specified algorithm.", + "Internal error: Algorithm instantiation failure", JOptionPane.ERROR_MESSAGE); + e1.printStackTrace(); + } + } + }); + spPanel.setVisible(false); + this.currentThread = new ThreadWrapper(this); this.baf = new BlockingActionFactory(this); - this.baf.addAction(clickAdapter); this.baf.addAction(currentThread); // Click adapter @@ -176,6 +194,12 @@ public class MainWindow extends JFrame { GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; + c.gridy = 0; + c.fill = GridBagConstraints.HORIZONTAL; + rightComponent.add(spPanel, c); + + c = new GridBagConstraints(); + c.gridx = 0; c.gridy = 2; c.weightx = 1; c.weighty = 1; @@ -335,7 +359,7 @@ public class MainWindow extends JFrame { } private void addDrawingClickListeners() { - drawing.addDrawingClickListener(this.clickAdapter); + drawing.addDrawingClickListener(spPanel.nodesInputPanel); } private void updateDrawing(Class newClass) { @@ -544,36 +568,8 @@ public class MainWindow extends JFrame { @Override public void actionPerformed(ActionEvent e) { int dividerLocation = mainPanel.getDividerLocation(); - spPanel = new ShortestPathPanel(drawing, graph); - - GridBagConstraints c = new GridBagConstraints(); - c.gridx = 0; - c.gridy = 0; - c.fill = GridBagConstraints.HORIZONTAL; - ((JPanel) mainPanel.getRightComponent()).add(spPanel, c); - + spPanel.setVisible(true); mainPanel.setDividerLocation(dividerLocation); - - spPanel.addStartActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - StartActionEvent evt = (StartActionEvent) e; - ShortestPathData data = new ShortestPathData(graph, evt.getOrigin(), evt.getDestination(), - evt.getMode()); - try { - ShortestPathAlgorithm spAlgorithm = ShortestPathAlgorithmFactory - .createAlgorithm(evt.getAlgorithmClass(), data); - spPanel.setEnabled(false); - launchShortestPathThread(spAlgorithm); - } - catch (Exception e1) { - JOptionPane.showMessageDialog(MainWindow.this, - "An error occurred while creating the specified algorithm.", - "Internal error: Algorithm instantiation failure", JOptionPane.ERROR_MESSAGE); - e1.printStackTrace(); - } - } - }); } })); graphLockItems.add(wccItem); diff --git a/src/main/org/insa/graphics/MultiPointsClickListener.java b/src/main/org/insa/graphics/MultiPointsClickListener.java deleted file mode 100644 index da2965d..0000000 --- a/src/main/org/insa/graphics/MultiPointsClickListener.java +++ /dev/null @@ -1,113 +0,0 @@ -package org.insa.graphics; - -import java.awt.Color; -import java.time.Duration; -import java.time.Instant; -import java.util.ArrayList; - -import org.insa.graph.Node; -import org.insa.graph.Point; -import org.insa.graphics.drawing.DrawingClickListener; - -public class MultiPointsClickListener implements DrawingClickListener, RunningAction { - - protected interface CallableWithNodes { - - /** - * Function called when the given number of nodes is reached. - * - * @param nodes - */ - void call(ArrayList nodes); - - }; - - // Enable/Disable. - private boolean enabled = false; - - // List of points. - private ArrayList points = new ArrayList(); - - // Starting time - private Instant startTime; - - // Number of points to find before running. - private int nTargetPoints = 0; - - // Callable to call when points are reached. - CallableWithNodes callable = null; - - // Graph - private final MainWindow mainWindow; - - public MultiPointsClickListener(MainWindow mainWindow) { - this.mainWindow = mainWindow; - } - - /** - * @return true if this listener is enabled. - */ - public boolean isEnabled() { - return enabled; - } - - /** - * Enable this listener. - * - * @param nTargetPoints Number of point to found before calling the callable. - */ - public void enable(int nTargetPoints, CallableWithNodes callable) { - this.enabled = true; - this.nTargetPoints = nTargetPoints; - this.points.clear(); - this.callable = callable; - this.startTime = Instant.now(); - } - - /** - * Disable this listener. - */ - public void disable() { - this.enabled = false; - } - - @Override - public void mouseClicked(Point lonlat) { - if (!isEnabled()) { - return; - } - Node node = mainWindow.graph.findClosestNode(lonlat); - mainWindow.drawing.drawMarker(node.getPoint(), Color.BLUE); - points.add(node); - if (points.size() == nTargetPoints) { - callable.call(points); - this.disable(); - } - } - - @Override - public boolean isRunning() { - return isEnabled(); - } - - @Override - public void interrupt() { - disable(); - } - - @Override - public Instant getStartingTime() { - return startTime; - } - - @Override - public Duration getDuration() { - return Duration.between(getStartingTime(), Instant.now()); - } - - @Override - public String getInformation() { - return getClass().getName(); - } - -} diff --git a/src/main/org/insa/graphics/NodesInputPanel.java b/src/main/org/insa/graphics/NodesInputPanel.java index 6326b8d..3787103 100644 --- a/src/main/org/insa/graphics/NodesInputPanel.java +++ b/src/main/org/insa/graphics/NodesInputPanel.java @@ -21,10 +21,8 @@ import javax.swing.JTextField; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; -import org.insa.graph.Graph; import org.insa.graph.Node; import org.insa.graph.Point; -import org.insa.graphics.drawing.Drawing; import org.insa.graphics.drawing.DrawingClickListener; import org.insa.graphics.drawing.overlays.MarkerOverlay; @@ -73,16 +71,13 @@ public class NodesInputPanel extends JPanel implements DrawingClickListener { // ActionListener called when all inputs are filled. private ArrayList inputChangeListeners = new ArrayList<>(); - // Graph & Drawing. - private Graph graph; - private Drawing drawing; + // Instance of mainwindow. + MainWindow mainWindow; - public NodesInputPanel(Drawing drawing, Graph graph) { + public NodesInputPanel(MainWindow mainWindow) { super(new GridBagLayout()); - this.graph = graph; - this.drawing = drawing; + this.mainWindow = mainWindow; initInputToFill(); - drawing.addDrawingClickListener(this); } public void addInputChangedListener(ActionListener listener) { @@ -179,7 +174,7 @@ public class NodesInputPanel extends JPanel implements DrawingClickListener { MarkerOverlay tracker = markerTrackers.getOrDefault(textField, null); if (curnode != null) { if (tracker == null) { - tracker = drawing.drawMarker(curnode.getPoint(), markerColor); + tracker = mainWindow.drawing.drawMarker(curnode.getPoint(), markerColor); markerTrackers.put(textField, tracker); } else { @@ -228,7 +223,7 @@ public class NodesInputPanel extends JPanel implements DrawingClickListener { */ protected Node getNodeForInput(JTextField textfield) { try { - Node node = graph.getNodes().get(Integer.valueOf(textfield.getText().trim())); + Node node = this.mainWindow.graph.getNodes().get(Integer.valueOf(textfield.getText().trim())); return node; } catch (IllegalArgumentException | IndexOutOfBoundsException ex) { @@ -297,7 +292,7 @@ public class NodesInputPanel extends JPanel implements DrawingClickListener { @Override public void mouseClicked(Point point) { - Node node = graph.findClosestNode(point); + Node node = this.mainWindow.graph.findClosestNode(point); JTextField input = getInputToFill(); if (input != null) { input.setText(String.valueOf(node.getId())); diff --git a/src/main/org/insa/graphics/ShortestPathPanel.java b/src/main/org/insa/graphics/ShortestPathPanel.java index 3f079dd..5d7c76a 100644 --- a/src/main/org/insa/graphics/ShortestPathPanel.java +++ b/src/main/org/insa/graphics/ShortestPathPanel.java @@ -22,10 +22,8 @@ import javax.swing.border.EmptyBorder; import org.insa.algo.shortestpath.ShortestPathAlgorithm; import org.insa.algo.shortestpath.ShortestPathAlgorithmFactory; import org.insa.algo.shortestpath.ShortestPathData.Mode; -import org.insa.graph.Graph; import org.insa.graph.Node; import org.insa.graphics.NodesInputPanel.InputChangedEvent; -import org.insa.graphics.drawing.Drawing; public class ShortestPathPanel extends JPanel { @@ -89,7 +87,7 @@ public class ShortestPathPanel extends JPanel { }; // Input panels for node. - private NodesInputPanel nodesInputPanel; + protected NodesInputPanel nodesInputPanel; // Component that can be enabled/disabled. private ArrayList components = new ArrayList<>(); @@ -97,7 +95,7 @@ public class ShortestPathPanel extends JPanel { // Start listeners List startActionListeners = new ArrayList<>(); - public ShortestPathPanel(Drawing drawing, Graph graph) { + public ShortestPathPanel(MainWindow mainWindow) { super(); setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); @@ -124,7 +122,7 @@ public class ShortestPathPanel extends JPanel { components.add(algoSelect); // Add inputs for node. - this.nodesInputPanel = new NodesInputPanel(drawing, graph); + this.nodesInputPanel = new NodesInputPanel(mainWindow); this.nodesInputPanel.setAlignmentX(Component.LEFT_ALIGNMENT); nodesInputPanel.addTextField("Origin: ", new Color(57, 172, 115)); nodesInputPanel.addTextField("Destination: ", new Color(255, 77, 77));