Move solution panel inside shortest-path panel.

This commit is contained in:
Mikael Capelle 2018-03-02 11:35:02 +01:00
parent d442a7e910
commit 19832321aa
3 changed files with 58 additions and 32 deletions

View File

@ -98,7 +98,6 @@ public class MainWindow extends JFrame {
// Shortest path panel
private ShortestPathPanel spPanel;
private ShortestPathSolutionPanel spSolPanel;
// List of items that cannot be used without a graph
private ArrayList<JMenuItem> graphLockItems = new ArrayList<JMenuItem>();
@ -139,7 +138,7 @@ public class MainWindow extends JFrame {
this.drawing = this.basicDrawing;
spPanel = new ShortestPathPanel();
spPanel = new ShortestPathPanel(this);
spPanel.addStartActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
@ -163,17 +162,14 @@ public class MainWindow extends JFrame {
});
spPanel.setVisible(false);
spSolPanel = new ShortestPathSolutionPanel(this, drawing);
spSolPanel.setVisible(false);
// Add click listeners to both drawing.
basicDrawing.addDrawingClickListener(spPanel.nodesInputPanel);
mapViewDrawing.addDrawingClickListener(spPanel.nodesInputPanel);
this.graphChangeListeneres.add(spPanel.nodesInputPanel);
this.graphChangeListeneres.add(spSolPanel);
this.graphChangeListeneres.add(spPanel.solutionPanel);
this.drawingChangeListeners.add(spPanel.nodesInputPanel);
this.drawingChangeListeners.add(spSolPanel);
this.drawingChangeListeners.add(spPanel.solutionPanel);
// Create action factory.
this.currentThread = new ThreadWrapper(this);
@ -219,12 +215,6 @@ public class MainWindow extends JFrame {
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL;
rightComponent.add(spSolPanel, c);
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 2;
c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
@ -279,8 +269,8 @@ public class MainWindow extends JFrame {
}
private void displayShortestPathSolution(ShortestPathSolution solution) {
spSolPanel.addSolution(solution);
spSolPanel.setVisible(true);
spPanel.solutionPanel.addSolution(solution);
spPanel.solutionPanel.setVisible(true);
}
private void launchShortestPathThread(ShortestPathAlgorithm spAlgorithm) {

View File

@ -49,8 +49,8 @@ public class ShortestPathPanel extends JPanel {
private final Mode mode;
private final Class<? extends ShortestPathAlgorithm> algoClass;
public StartActionEvent(Class<? extends ShortestPathAlgorithm> algoClass, Node origin, Node destination,
Mode mode) {
public StartActionEvent(Class<? extends ShortestPathAlgorithm> algoClass, Node origin,
Node destination, Mode mode) {
super(ShortestPathPanel.this, START_EVENT_ID, START_EVENT_COMMAND);
this.origin = origin;
this.destination = destination;
@ -91,6 +91,9 @@ public class ShortestPathPanel extends JPanel {
// Input panels for node.
protected NodesInputPanel nodesInputPanel;
// Solution
protected ShortestPathSolutionPanel solutionPanel;
// Component that can be enabled/disabled.
private ArrayList<JComponent> components = new ArrayList<>();
@ -101,7 +104,7 @@ public class ShortestPathPanel extends JPanel {
/**
*/
public ShortestPathPanel() {
public ShortestPathPanel(Component parent) {
super();
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
@ -156,6 +159,12 @@ public class ShortestPathPanel extends JPanel {
components.add(timeModeButton);
components.add(lengthModeButton);
solutionPanel = new ShortestPathSolutionPanel(parent);
solutionPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
solutionPanel.setVisible(false);
add(Box.createVerticalStrut(10));
add(solutionPanel);
// Bottom panel
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.LINE_AXIS));
@ -171,7 +180,8 @@ public class ShortestPathPanel extends JPanel {
for (ActionListener lis: startActionListeners) {
lis.actionPerformed(new StartActionEvent(
ShortestPathAlgorithmFactory.getAlgorithmClass((String) algoSelect.getSelectedItem()),
ShortestPathAlgorithmFactory
.getAlgorithmClass((String) algoSelect.getSelectedItem()),
origin, destination, mode));
}
}
@ -236,6 +246,7 @@ public class ShortestPathPanel extends JPanel {
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
nodesInputPanel.setEnabled(enabled);
solutionPanel.setEnabled(enabled);
for (JComponent component: components) {
component.setEnabled(enabled);
}

View File

@ -19,6 +19,8 @@ import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
@ -112,27 +114,32 @@ public class ShortestPathSolutionPanel extends JPanel
private final JComboBox<ShortestPathBundle> solutionSelect;
// Map solution -> panel
private final JLabel informationPanel;
private final JTextArea informationPanel;
// Current bundle
private ShortestPathBundle currentBundle = null;
public ShortestPathSolutionPanel(Component parent, Drawing drawing) {
public ShortestPathSolutionPanel(Component parent) {
super();
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
setBorder(new CompoundBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.BLACK),
new EmptyBorder(15, 15, 15, 15)));
this.drawing = drawing;
setBorder(new CompoundBorder(BorderFactory.createMatteBorder(1, 0, 1, 0, Color.LIGHT_GRAY),
new EmptyBorder(10, 0, 10, 0)));
solutionSelect = new JComboBox<>();
solutionSelect.setBackground(Color.WHITE);
solutionSelect.setAlignmentX(Component.LEFT_ALIGNMENT);
add(solutionSelect);
informationPanel = new JLabel();
informationPanel = new JTextArea();
informationPanel.setWrapStyleWord(true);
informationPanel.setLineWrap(true);
informationPanel.setOpaque(true);
informationPanel.setFocusable(false);
informationPanel.setEditable(false);
informationPanel.setBackground(UIManager.getColor("Label.background"));
informationPanel.setFont(UIManager.getFont("Label.font"));
informationPanel.setBorder(UIManager.getBorder("Label.border"));
informationPanel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
informationPanel.setHorizontalAlignment(JLabel.LEFT);
add(informationPanel);
JButton clearButton = new JButton("Hide");
@ -230,24 +237,42 @@ public class ShortestPathSolutionPanel extends JPanel
ShortestPathData data = bundle.getData();
String info = null;
if (!bundle.getSolution().isFeasible()) {
info = String.format("Shortest path: No path found from node #%d to node #%d.",
info = String.format("No path found from node #%d to node #%d.",
data.getOrigin().getId(), data.getDestination().getId());
}
else {
info = String.format("Shortest path: Found a path from node #%d to node #%d",
data.getOrigin().getId(), data.getDestination().getId());
info = String.format("Found a path from node #%d to node #%d", data.getOrigin().getId(),
data.getDestination().getId());
if (data.getMode() == Mode.LENGTH) {
info = String.format("%s, %.2f kilometers.", info,
info = String.format("%s, %.4f kilometers.", info,
(bundle.getSolution().getPath().getLength() / 1000.0));
}
else {
info = String.format("%s, %.2f minutes.", info,
info = String.format("%s, %.4f minutes.", info,
(bundle.getSolution().getPath().getMinimumTravelTime() / 60.0));
}
}
informationPanel.setText(info);
}
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
solutionSelect.setEnabled(enabled);
if (enabled) {
// Trigger event
solutionSelect.actionPerformed(null);
}
else {
ShortestPathBundle bundle = (ShortestPathBundle) this.solutionSelect.getSelectedItem();
if (bundle != null && bundle.getOverlay() != null) {
bundle.getOverlay().setVisible(false);
}
}
}
@Override
public void newGraphLoaded(Graph graph) {
this.solutionSelect.removeAllItems();