From 19832321aa71ec76180e79924913f7a6f7f53b5a Mon Sep 17 00:00:00 2001 From: Mikael Capelle Date: Fri, 2 Mar 2018 11:35:02 +0100 Subject: [PATCH] Move solution panel inside shortest-path panel. --- src/main/org/insa/graphics/MainWindow.java | 20 ++------ .../org/insa/graphics/ShortestPathPanel.java | 19 +++++-- .../graphics/ShortestPathSolutionPanel.java | 51 ++++++++++++++----- 3 files changed, 58 insertions(+), 32 deletions(-) diff --git a/src/main/org/insa/graphics/MainWindow.java b/src/main/org/insa/graphics/MainWindow.java index ec03786..aeb9853 100644 --- a/src/main/org/insa/graphics/MainWindow.java +++ b/src/main/org/insa/graphics/MainWindow.java @@ -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 graphLockItems = new ArrayList(); @@ -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) { diff --git a/src/main/org/insa/graphics/ShortestPathPanel.java b/src/main/org/insa/graphics/ShortestPathPanel.java index 57ba0ec..9636056 100644 --- a/src/main/org/insa/graphics/ShortestPathPanel.java +++ b/src/main/org/insa/graphics/ShortestPathPanel.java @@ -49,8 +49,8 @@ public class ShortestPathPanel extends JPanel { private final Mode mode; private final Class algoClass; - public StartActionEvent(Class algoClass, Node origin, Node destination, - Mode mode) { + public StartActionEvent(Class 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 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); } diff --git a/src/main/org/insa/graphics/ShortestPathSolutionPanel.java b/src/main/org/insa/graphics/ShortestPathSolutionPanel.java index 7a9c9b5..e507ab6 100644 --- a/src/main/org/insa/graphics/ShortestPathSolutionPanel.java +++ b/src/main/org/insa/graphics/ShortestPathSolutionPanel.java @@ -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 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();