Update panel.

This commit is contained in:
Holt59 2018-03-03 22:14:02 +01:00
parent a5764948f9
commit 85025f6189

View File

@ -10,16 +10,15 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JCheckBox;
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.EmptyBorder;
import org.insa.graph.Graph;
@ -35,7 +34,12 @@ public class PathsPanel extends JPanel implements DrawingChangeListener, GraphCh
*/
private static final long serialVersionUID = 1L;
private class PathBundle {
private class PathPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
// Solution
private final Path path;
@ -50,23 +54,77 @@ public class PathsPanel extends JPanel implements DrawingChangeListener, GraphCh
* @param path Path for this bundle, must not be null.
*
*/
public PathBundle(Path path) {
public PathPanel(Path path) {
super();
setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
setBorder(BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.GRAY),
new EmptyBorder(5, 0, 5, 0)));
this.path = path;
this.overlay = drawing.drawPath(this.path);
}
/**
* @return Path associated with this bundle.
*/
public Path getPath() {
return this.path;
}
JCheckBox checkbox = new JCheckBox();
checkbox.setSelected(true);
checkbox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
overlay.setVisible(checkbox.isSelected());
}
});
JLabel infoPanel = new JLabel();
String info = "";
info += String.format("Length = %.3f kilometers, duration = ", path.getLength() / 1000.);
double time = path.getMinimumTravelTime();
int hours = (int) (time / 3600);
int minutes = (int) (time / 60) % 60;
int seconds = ((int) time) % 60;
info += String.format("%d hours, %d minutes, %d seconds.", hours, minutes, seconds);
infoPanel.setText("<html>" + toString() + "<br/>" + info + "</html>");
JButton saveButton = new JButton("Save");
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String filepath = System.getProperty("user.dir");
filepath += File.separator + String.format("path_%s_%d_%d.path",
path.getGraph().getMapId().toLowerCase().replaceAll("[^a-z0-9_]", "_"),
path.getOrigin().getId(), path.getDestination().getId());
JFileChooser fileChooser = new JFileChooser();
fileChooser.setSelectedFile(new File(filepath));
fileChooser.setApproveButtonText("Save");
if (fileChooser.showOpenDialog(getTopLevelAncestor()) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
BinaryPathWriter writer = new BinaryPathWriter(
new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file))));
writer.writePath(path);
}
catch (IOException e1) {
JOptionPane.showMessageDialog(getTopLevelAncestor(),
"Unable to write path to the selected file.");
e1.printStackTrace();
}
}
}
});
JButton deleteButton = new JButton("Delete");
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
overlay.delete();
PathsPanel.this.removePath(PathPanel.this);
}
});
add(checkbox);
add(Box.createHorizontalStrut(5));
add(infoPanel);
add(Box.createHorizontalGlue());
add(saveButton);
add(deleteButton);
/**
* @return Overlay associated with this bundle (never null).
*/
public PathOverlay getOverlay() {
return this.overlay;
}
/**
@ -94,164 +152,34 @@ public class PathsPanel extends JPanel implements DrawingChangeListener, GraphCh
// Solution
private Drawing drawing;
// Solution selector
private final JComboBox<PathBundle> solutionSelect;
// Map solution -> panel
private final JTextArea informationPanel;
// Current bundle
private PathBundle currentBundle = null;
public PathsPanel(Component parent) {
super();
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
setBorder(new EmptyBorder(15, 15, 15, 15));
solutionSelect = new JComboBox<>();
solutionSelect.setBackground(Color.WHITE);
solutionSelect.setAlignmentX(Component.LEFT_ALIGNMENT);
add(solutionSelect);
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);
add(Box.createVerticalStrut(8));
add(informationPanel);
JButton clearButton = new JButton("Hide");
clearButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (currentBundle != null) {
if (currentBundle.getOverlay().isVisible()) {
currentBundle.getOverlay().setVisible(false);
clearButton.setText("Show");
}
else {
currentBundle.getOverlay().setVisible(true);
clearButton.setText("Hide");
}
}
}
});
JButton saveButton = new JButton("Save");
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String filepath = System.getProperty("user.dir");
filepath += File.separator + String.format("path_%s_%d_%d.path",
currentBundle.getPath().getGraph().getMapId().toLowerCase().replaceAll("[^a-z0-9_]", "_"),
currentBundle.getPath().getOrigin().getId(), currentBundle.getPath().getDestination().getId());
JFileChooser fileChooser = new JFileChooser();
fileChooser.setSelectedFile(new File(filepath));
fileChooser.setApproveButtonText("Save");
if (fileChooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
BinaryPathWriter writer = new BinaryPathWriter(
new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file))));
writer.writePath(currentBundle.getPath());
}
catch (IOException e1) {
JOptionPane.showMessageDialog(parent, "Unable to write path to the selected file.");
e1.printStackTrace();
}
}
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
buttonPanel.add(Box.createHorizontalGlue());
buttonPanel.add(clearButton);
buttonPanel.add(saveButton);
buttonPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
add(Box.createVerticalStrut(4));
add(buttonPanel);
solutionSelect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
PathBundle bundle = (PathBundle) solutionSelect.getSelectedItem();
// Handle case when the JComboBox is empty.
if (bundle == null) {
return;
}
if (currentBundle != null) {
currentBundle.getOverlay().setVisible(false);
}
updateInformationLabel(bundle);
clearButton.setText("Hide");
bundle.getOverlay().setVisible(true);
currentBundle = bundle;
}
});
// Default hidden
this.setVisible(false);
}
public void addPath(Path path) {
PathBundle bundle = new PathBundle(path);
solutionSelect.addItem(bundle);
solutionSelect.setSelectedItem(bundle);
this.add(new PathPanel(path));
this.setVisible(true);
this.revalidate();
}
protected void updateInformationLabel(PathBundle bundle) {
String info = "";
info += String.format("Length = %.3f kilometers, duration = ", bundle.getPath().getLength() / 1000.);
double time = bundle.getPath().getMinimumTravelTime();
int hours = (int) (time / 3600);
int minutes = (int) (time / 60) % 60;
int seconds = ((int) time) % 60;
info += String.format("%d hours, %d minutes, %d seconds.", hours, minutes, seconds);
informationPanel.setText(info);
}
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
solutionSelect.setEnabled(enabled);
if (enabled) {
// Trigger event
solutionSelect.actionPerformed(null);
}
else {
PathBundle bundle = (PathBundle) this.solutionSelect.getSelectedItem();
if (bundle != null) {
bundle.getOverlay().setVisible(false);
}
protected void removePath(PathPanel panel) {
PathsPanel.this.remove(panel);
PathsPanel.this.validate();
PathsPanel.this.repaint();
if (this.getComponentCount() == 0) {
this.setVisible(false);
}
}
@Override
public void newGraphLoaded(Graph graph) {
for (int i = 0; i < this.solutionSelect.getItemCount(); ++i) {
this.solutionSelect.getItemAt(i).getOverlay().delete();
}
this.solutionSelect.removeAllItems();
this.currentBundle = null;
this.removeAll();
this.setVisible(false);
}
@ -264,8 +192,10 @@ public class PathsPanel extends JPanel implements DrawingChangeListener, GraphCh
@Override
public void onRedrawRequest() {
for (int i = 0; i < this.solutionSelect.getItemCount(); ++i) {
this.solutionSelect.getItemAt(i).updateOverlay();
for (Component c: this.getComponents()) {
if (c instanceof PathPanel) {
((PathPanel) c).updateOverlay();
}
}
}