Clean code + Fix issue with right-to-left map names.

This commit is contained in:
Mikael Capelle 2018-03-05 17:57:06 +01:00
parent 6be3493bba
commit 3252a6faba

View File

@ -156,18 +156,19 @@ public class MainWindow extends JFrame {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
StartActionEvent evt = (StartActionEvent) e; StartActionEvent evt = (StartActionEvent) e;
ShortestPathData data = new ShortestPathData(graph, evt.getNodes().get(0), evt.getNodes().get(1), ShortestPathData data = new ShortestPathData(graph, evt.getNodes().get(0),
evt.getMode(), evt.getArcFilter()); evt.getNodes().get(1), evt.getMode(), evt.getArcFilter());
ShortestPathAlgorithm spAlgorithm = null; ShortestPathAlgorithm spAlgorithm = null;
try { try {
spAlgorithm = (ShortestPathAlgorithm) AlgorithmFactory.createAlgorithm(evt.getAlgorithmClass(), spAlgorithm = (ShortestPathAlgorithm) AlgorithmFactory
data); .createAlgorithm(evt.getAlgorithmClass(), data);
} }
catch (Exception e1) { catch (Exception e1) {
JOptionPane.showMessageDialog(MainWindow.this, JOptionPane.showMessageDialog(MainWindow.this,
"An error occurred while creating the specified algorithm.", "An error occurred while creating the specified algorithm.",
"Internal error: Algorithm instantiation failure", JOptionPane.ERROR_MESSAGE); "Internal error: Algorithm instantiation failure",
JOptionPane.ERROR_MESSAGE);
e1.printStackTrace(); e1.printStackTrace();
return; return;
} }
@ -303,7 +304,8 @@ public class MainWindow extends JFrame {
} }
private void displayShortestPathSolution(ShortestPathSolution solution) { private void displayShortestPathSolution(ShortestPathSolution solution) {
spPanel.solutionPanel.addSolution(solution, false); // Do not add overlay in the solution panel. spPanel.solutionPanel.addSolution(solution, false); // Do not add overlay in the solution
// panel.
if (solution.isFeasible()) { if (solution.isFeasible()) {
pathPanel.addPath(solution.getPath()); pathPanel.addPath(solution.getPath());
} }
@ -365,12 +367,14 @@ public class MainWindow extends JFrame {
// We need to draw MapView, we have to check if the file exists. // We need to draw MapView, we have to check if the file exists.
File mfile = null; File mfile = null;
if (isMapView) { if (isMapView) {
String mfpath = graphFilePath.substring(0, graphFilePath.lastIndexOf(".map")) + ".mapfg"; String mfpath = graphFilePath.substring(0, graphFilePath.lastIndexOf(".map"))
+ ".mapfg";
mfile = new File(mfpath); mfile = new File(mfpath);
if (!mfile.exists()) { if (!mfile.exists()) {
if (JOptionPane.showConfirmDialog(this, if (JOptionPane.showConfirmDialog(this,
"The associated mapsforge (.mapfg) file has not been found, do you want to specify it manually?", "The associated mapsforge (.mapfg) file has not been found, do you want to specify it manually?",
"File not found", JOptionPane.YES_NO_CANCEL_OPTION) == JOptionPane.YES_OPTION) { "File not found",
JOptionPane.YES_NO_CANCEL_OPTION) == JOptionPane.YES_OPTION) {
JFileChooser chooser = new JFileChooser(mfile.getParentFile()); JFileChooser chooser = new JFileChooser(mfile.getParentFile());
if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
mfile = chooser.getSelectedFile(); mfile = chooser.getSelectedFile();
@ -445,14 +449,17 @@ public class MainWindow extends JFrame {
catch (Exception exception) { catch (Exception exception) {
progressBar.setVisible(false); progressBar.setVisible(false);
progressBar = null; progressBar = null;
JOptionPane.showMessageDialog(MainWindow.this, "Unable to read graph from the selected file."); JOptionPane.showMessageDialog(MainWindow.this,
"Unable to read graph from the selected file.");
exception.printStackTrace(System.out); exception.printStackTrace(System.out);
return; return;
} }
String info = graph.getMapId(); String info = graph.getMapId();
if (graph.getMapName() != null && !graph.getMapName().isEmpty()) { if (graph.getMapName() != null && !graph.getMapName().isEmpty()) {
info += " - " + graph.getMapName(); // The \u200e character is the left-to-right mark, we need to avoid issue with
// name that are right-to-left (e.g. arabic names).
info += " - " + graph.getMapName() + "\u200e";
} }
info += ", " + graph.getNodes().size() + " nodes"; info += ", " + graph.getNodes().size() + " nodes";
graphInfoPanel.setText(info); graphInfoPanel.setText(info);
@ -477,8 +484,10 @@ public class MainWindow extends JFrame {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser(); JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Graph files", "mapgr"); FileNameExtensionFilter filter = new FileNameExtensionFilter("Graph files",
File mapFolder = new File(preferences.get(DEFAULT_MAP_FOLDER_KEY, DEFAULT_MAP_FOLDER_INSA)); "mapgr");
File mapFolder = new File(
preferences.get(DEFAULT_MAP_FOLDER_KEY, DEFAULT_MAP_FOLDER_INSA));
if (!mapFolder.exists()) { if (!mapFolder.exists()) {
mapFolder = new File(System.getProperty("user.dir")); mapFolder = new File(System.getProperty("user.dir"));
} }
@ -489,16 +498,18 @@ public class MainWindow extends JFrame {
// Check... // Check...
if (chooser.getSelectedFile().exists()) { if (chooser.getSelectedFile().exists()) {
preferences.put(DEFAULT_MAP_FOLDER_KEY, chooser.getSelectedFile().getParent()); preferences.put(DEFAULT_MAP_FOLDER_KEY,
chooser.getSelectedFile().getParent());
} }
DataInputStream stream; DataInputStream stream;
try { try {
stream = new DataInputStream( stream = new DataInputStream(new BufferedInputStream(
new BufferedInputStream(new FileInputStream(chooser.getSelectedFile()))); new FileInputStream(chooser.getSelectedFile())));
} }
catch (IOException e1) { catch (IOException e1) {
JOptionPane.showMessageDialog(MainWindow.this, "Cannot open the selected file."); JOptionPane.showMessageDialog(MainWindow.this,
"Cannot open the selected file.");
return; return;
} }
loadGraph(new BinaryGraphReaderInsa2018(stream)); loadGraph(new BinaryGraphReaderInsa2018(stream));
@ -514,8 +525,10 @@ public class MainWindow extends JFrame {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser(); JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Path & compressed path files", "path"); FileNameExtensionFilter filter = new FileNameExtensionFilter(
File pathFolder = new File(preferences.get(DEFAULT_PATH_FOLDER_KEY, DEFAULT_PATH_FOLDER_INSA)); "Path & compressed path files", "path");
File pathFolder = new File(
preferences.get(DEFAULT_PATH_FOLDER_KEY, DEFAULT_PATH_FOLDER_INSA));
if (!pathFolder.exists()) { if (!pathFolder.exists()) {
pathFolder = new File(System.getProperty("user.dir")); pathFolder = new File(System.getProperty("user.dir"));
} }
@ -525,16 +538,18 @@ public class MainWindow extends JFrame {
// Check & Update // Check & Update
if (chooser.getSelectedFile().exists()) { if (chooser.getSelectedFile().exists()) {
preferences.put(DEFAULT_PATH_FOLDER_KEY, chooser.getSelectedFile().getParent()); preferences.put(DEFAULT_PATH_FOLDER_KEY,
chooser.getSelectedFile().getParent());
} }
BinaryPathReader reader; BinaryPathReader reader;
try { try {
reader = new BinaryPathReader(new DataInputStream( reader = new BinaryPathReader(new DataInputStream(new BufferedInputStream(
new BufferedInputStream(new FileInputStream(chooser.getSelectedFile())))); new FileInputStream(chooser.getSelectedFile()))));
} }
catch (IOException e1) { catch (IOException e1) {
JOptionPane.showMessageDialog(MainWindow.this, "Cannot open the selected file."); JOptionPane.showMessageDialog(MainWindow.this,
"Cannot open the selected file.");
return; return;
} }
try { try {
@ -547,7 +562,8 @@ public class MainWindow extends JFrame {
return; return;
} }
catch (Exception exception) { catch (Exception exception) {
JOptionPane.showMessageDialog(MainWindow.this, "Unable to read path from the selected file."); JOptionPane.showMessageDialog(MainWindow.this,
"Unable to read path from the selected file.");
return; return;
} }
} }
@ -561,7 +577,8 @@ public class MainWindow extends JFrame {
closeItem.addActionListener(new ActionListener() { closeItem.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
MainWindow.this.dispatchEvent(new WindowEvent(MainWindow.this, WindowEvent.WINDOW_CLOSING)); MainWindow.this.dispatchEvent(
new WindowEvent(MainWindow.this, WindowEvent.WINDOW_CLOSING));
} }
}); });
@ -602,7 +619,8 @@ public class MainWindow extends JFrame {
})); }));
graphLockItems.add(drawGraphBWItem); graphLockItems.add(drawGraphBWItem);
JMenuItem drawGraphMapsforgeItem = new JMenuItem("Redraw (Map)", KeyEvent.VK_M); JMenuItem drawGraphMapsforgeItem = new JMenuItem("Redraw (Map)", KeyEvent.VK_M);
drawGraphMapsforgeItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, ActionEvent.ALT_MASK)); drawGraphMapsforgeItem
.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, ActionEvent.ALT_MASK));
drawGraphMapsforgeItem.addActionListener(baf.createBlockingAction(new ActionListener() { drawGraphMapsforgeItem.addActionListener(baf.createBlockingAction(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
@ -677,8 +695,9 @@ public class MainWindow extends JFrame {
private JPanel createStatusBar() { private JPanel createStatusBar() {
// create the status bar panel and shove it down the bottom of the frame // create the status bar panel and shove it down the bottom of the frame
JPanel statusPanel = new JPanel(); JPanel statusPanel = new JPanel();
statusPanel.setBorder(new CompoundBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.GRAY), statusPanel.setBorder(
new EmptyBorder(0, 15, 0, 15))); new CompoundBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.GRAY),
new EmptyBorder(0, 15, 0, 15)));
statusPanel.setPreferredSize(new Dimension(getWidth(), 38)); statusPanel.setPreferredSize(new Dimension(getWidth(), 38));
statusPanel.setLayout(new BorderLayout()); statusPanel.setLayout(new BorderLayout());
@ -695,8 +714,8 @@ public class MainWindow extends JFrame {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (currentThread.isRunning()) { if (currentThread.isRunning()) {
int confirmed = JOptionPane.showConfirmDialog(MainWindow.this, int confirmed = JOptionPane.showConfirmDialog(MainWindow.this,
"Are you sure you want to kill the running thread?", "Kill Confirmation", "Are you sure you want to kill the running thread?",
JOptionPane.YES_NO_OPTION); "Kill Confirmation", JOptionPane.YES_NO_OPTION);
if (confirmed == JOptionPane.YES_OPTION) { if (confirmed == JOptionPane.YES_OPTION) {
currentThread.interrupt(); currentThread.interrupt();
} }
@ -708,8 +727,8 @@ public class MainWindow extends JFrame {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
long seconds = currentThread.getDuration().getSeconds(); long seconds = currentThread.getDuration().getSeconds();
threadTimerLabel threadTimerLabel.setText(String.format("%02d:%02d:%02d", seconds / 3600,
.setText(String.format("%02d:%02d:%02d", seconds / 3600, seconds / 60 % 60, seconds % 60)); seconds / 60 % 60, seconds % 60));
} }
}); });
threadTimer.setInitialDelay(0); threadTimer.setInitialDelay(0);