From 575eb71e64a9d3bd8718287093c0f88ce6da67db Mon Sep 17 00:00:00 2001 From: Holt59 Date: Tue, 27 Feb 2018 23:35:06 +0100 Subject: [PATCH] Fix issue with startAlgoButton when enabling the ShortestPathPanel. --- .../org/insa/graphics/ShortestPathPanel.java | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/main/org/insa/graphics/ShortestPathPanel.java b/src/main/org/insa/graphics/ShortestPathPanel.java index a52c11c..57ba0ec 100644 --- a/src/main/org/insa/graphics/ShortestPathPanel.java +++ b/src/main/org/insa/graphics/ShortestPathPanel.java @@ -94,6 +94,8 @@ public class ShortestPathPanel extends JPanel { // Component that can be enabled/disabled. private ArrayList components = new ArrayList<>(); + private JButton startAlgoButton; + // Start listeners List startActionListeners = new ArrayList<>(); @@ -157,7 +159,8 @@ public class ShortestPathPanel extends JPanel { // Bottom panel JPanel bottomPanel = new JPanel(); bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.LINE_AXIS)); - JButton startAlgoButton = new JButton("Start"); + + startAlgoButton = new JButton("Start"); startAlgoButton.setEnabled(false); startAlgoButton.addActionListener(new ActionListener() { @Override @@ -187,7 +190,6 @@ public class ShortestPathPanel extends JPanel { bottomPanel.add(Box.createHorizontalGlue()); bottomPanel.add(hideButton); - components.add(startAlgoButton); components.add(hideButton); bottomPanel.setAlignmentX(Component.LEFT_ALIGNMENT); @@ -199,13 +201,7 @@ public class ShortestPathPanel extends JPanel { @Override public void actionPerformed(ActionEvent e) { InputChangedEvent evt = (InputChangedEvent) e; - boolean allNotNull = true; - for (Node node: evt.getNodes()) { - if (node == null) { - allNotNull = false; - } - } - startAlgoButton.setEnabled(allNotNull); + startAlgoButton.setEnabled(allNotNull(evt.getNodes())); } }); @@ -228,6 +224,14 @@ public class ShortestPathPanel extends JPanel { setEnabled(false); } + protected boolean allNotNull(List nodes) { + boolean allNotNull = true; + for (Node node: nodes) { + allNotNull = allNotNull && node != null; + } + return allNotNull; + } + @Override public void setEnabled(boolean enabled) { super.setEnabled(enabled); @@ -235,6 +239,8 @@ public class ShortestPathPanel extends JPanel { for (JComponent component: components) { component.setEnabled(enabled); } + enabled = enabled && allNotNull(this.nodesInputPanel.getNodeForInputs()); + startAlgoButton.setEnabled(enabled); } /**