Link inputs directly to MainWindow to avoid issue when loading new graph/redrawing.

This commit is contained in:
Holt59 2018-02-25 20:16:10 +01:00
parent b192bb4f07
commit 1f06fc1eec
4 changed files with 41 additions and 165 deletions

View File

@ -123,9 +123,6 @@ public class MainWindow extends JFrame {
// Current running thread // Current running thread
private ThreadWrapper currentThread; private ThreadWrapper currentThread;
// Multi point listener
private MultiPointsClickListener clickAdapter = null;
// Factory // Factory
private BlockingActionFactory baf; private BlockingActionFactory baf;
@ -138,10 +135,31 @@ public class MainWindow extends JFrame {
// Create drawing and action listeners... // Create drawing and action listeners...
this.drawing = new BasicDrawing(); 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.currentThread = new ThreadWrapper(this);
this.baf = new BlockingActionFactory(this); this.baf = new BlockingActionFactory(this);
this.baf.addAction(clickAdapter);
this.baf.addAction(currentThread); this.baf.addAction(currentThread);
// Click adapter // Click adapter
@ -176,6 +194,12 @@ public class MainWindow extends JFrame {
GridBagConstraints c = new GridBagConstraints(); GridBagConstraints c = new GridBagConstraints();
c.gridx = 0; c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.HORIZONTAL;
rightComponent.add(spPanel, c);
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 2; c.gridy = 2;
c.weightx = 1; c.weightx = 1;
c.weighty = 1; c.weighty = 1;
@ -335,7 +359,7 @@ public class MainWindow extends JFrame {
} }
private void addDrawingClickListeners() { private void addDrawingClickListeners() {
drawing.addDrawingClickListener(this.clickAdapter); drawing.addDrawingClickListener(spPanel.nodesInputPanel);
} }
private void updateDrawing(Class<? extends Drawing> newClass) { private void updateDrawing(Class<? extends Drawing> newClass) {
@ -544,36 +568,8 @@ public class MainWindow extends JFrame {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
int dividerLocation = mainPanel.getDividerLocation(); int dividerLocation = mainPanel.getDividerLocation();
spPanel = new ShortestPathPanel(drawing, graph); spPanel.setVisible(true);
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.HORIZONTAL;
((JPanel) mainPanel.getRightComponent()).add(spPanel, c);
mainPanel.setDividerLocation(dividerLocation); 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); graphLockItems.add(wccItem);

View File

@ -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<Node> nodes);
};
// Enable/Disable.
private boolean enabled = false;
// List of points.
private ArrayList<Node> points = new ArrayList<Node>();
// 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();
}
}

View File

@ -21,10 +21,8 @@ import javax.swing.JTextField;
import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener; import javax.swing.event.DocumentListener;
import org.insa.graph.Graph;
import org.insa.graph.Node; import org.insa.graph.Node;
import org.insa.graph.Point; import org.insa.graph.Point;
import org.insa.graphics.drawing.Drawing;
import org.insa.graphics.drawing.DrawingClickListener; import org.insa.graphics.drawing.DrawingClickListener;
import org.insa.graphics.drawing.overlays.MarkerOverlay; 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. // ActionListener called when all inputs are filled.
private ArrayList<ActionListener> inputChangeListeners = new ArrayList<>(); private ArrayList<ActionListener> inputChangeListeners = new ArrayList<>();
// Graph & Drawing. // Instance of mainwindow.
private Graph graph; MainWindow mainWindow;
private Drawing drawing;
public NodesInputPanel(Drawing drawing, Graph graph) { public NodesInputPanel(MainWindow mainWindow) {
super(new GridBagLayout()); super(new GridBagLayout());
this.graph = graph; this.mainWindow = mainWindow;
this.drawing = drawing;
initInputToFill(); initInputToFill();
drawing.addDrawingClickListener(this);
} }
public void addInputChangedListener(ActionListener listener) { public void addInputChangedListener(ActionListener listener) {
@ -179,7 +174,7 @@ public class NodesInputPanel extends JPanel implements DrawingClickListener {
MarkerOverlay tracker = markerTrackers.getOrDefault(textField, null); MarkerOverlay tracker = markerTrackers.getOrDefault(textField, null);
if (curnode != null) { if (curnode != null) {
if (tracker == null) { if (tracker == null) {
tracker = drawing.drawMarker(curnode.getPoint(), markerColor); tracker = mainWindow.drawing.drawMarker(curnode.getPoint(), markerColor);
markerTrackers.put(textField, tracker); markerTrackers.put(textField, tracker);
} }
else { else {
@ -228,7 +223,7 @@ public class NodesInputPanel extends JPanel implements DrawingClickListener {
*/ */
protected Node getNodeForInput(JTextField textfield) { protected Node getNodeForInput(JTextField textfield) {
try { 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; return node;
} }
catch (IllegalArgumentException | IndexOutOfBoundsException ex) { catch (IllegalArgumentException | IndexOutOfBoundsException ex) {
@ -297,7 +292,7 @@ public class NodesInputPanel extends JPanel implements DrawingClickListener {
@Override @Override
public void mouseClicked(Point point) { public void mouseClicked(Point point) {
Node node = graph.findClosestNode(point); Node node = this.mainWindow.graph.findClosestNode(point);
JTextField input = getInputToFill(); JTextField input = getInputToFill();
if (input != null) { if (input != null) {
input.setText(String.valueOf(node.getId())); input.setText(String.valueOf(node.getId()));

View File

@ -22,10 +22,8 @@ import javax.swing.border.EmptyBorder;
import org.insa.algo.shortestpath.ShortestPathAlgorithm; import org.insa.algo.shortestpath.ShortestPathAlgorithm;
import org.insa.algo.shortestpath.ShortestPathAlgorithmFactory; import org.insa.algo.shortestpath.ShortestPathAlgorithmFactory;
import org.insa.algo.shortestpath.ShortestPathData.Mode; import org.insa.algo.shortestpath.ShortestPathData.Mode;
import org.insa.graph.Graph;
import org.insa.graph.Node; import org.insa.graph.Node;
import org.insa.graphics.NodesInputPanel.InputChangedEvent; import org.insa.graphics.NodesInputPanel.InputChangedEvent;
import org.insa.graphics.drawing.Drawing;
public class ShortestPathPanel extends JPanel { public class ShortestPathPanel extends JPanel {
@ -89,7 +87,7 @@ public class ShortestPathPanel extends JPanel {
}; };
// Input panels for node. // Input panels for node.
private NodesInputPanel nodesInputPanel; protected NodesInputPanel nodesInputPanel;
// Component that can be enabled/disabled. // Component that can be enabled/disabled.
private ArrayList<JComponent> components = new ArrayList<>(); private ArrayList<JComponent> components = new ArrayList<>();
@ -97,7 +95,7 @@ public class ShortestPathPanel extends JPanel {
// Start listeners // Start listeners
List<ActionListener> startActionListeners = new ArrayList<>(); List<ActionListener> startActionListeners = new ArrayList<>();
public ShortestPathPanel(Drawing drawing, Graph graph) { public ShortestPathPanel(MainWindow mainWindow) {
super(); super();
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
@ -124,7 +122,7 @@ public class ShortestPathPanel extends JPanel {
components.add(algoSelect); components.add(algoSelect);
// Add inputs for node. // Add inputs for node.
this.nodesInputPanel = new NodesInputPanel(drawing, graph); this.nodesInputPanel = new NodesInputPanel(mainWindow);
this.nodesInputPanel.setAlignmentX(Component.LEFT_ALIGNMENT); this.nodesInputPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
nodesInputPanel.addTextField("Origin: ", new Color(57, 172, 115)); nodesInputPanel.addTextField("Origin: ", new Color(57, 172, 115));
nodesInputPanel.addTextField("Destination: ", new Color(255, 77, 77)); nodesInputPanel.addTextField("Destination: ", new Color(255, 77, 77));