Create a FileUtils class to simplify file opening/saving.

This commit is contained in:
Holt59 2018-03-11 14:19:38 +01:00
parent 8661e63728
commit 0030ab7aef
3 changed files with 123 additions and 53 deletions

View File

@ -19,7 +19,6 @@ import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.prefs.Preferences;
import javax.swing.BorderFactory; import javax.swing.BorderFactory;
import javax.swing.Box; import javax.swing.Box;
@ -42,7 +41,6 @@ import javax.swing.Timer;
import javax.swing.UIManager; import javax.swing.UIManager;
import javax.swing.border.CompoundBorder; import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.insa.algo.AbstractSolution; import org.insa.algo.AbstractSolution;
import org.insa.algo.AlgorithmFactory; import org.insa.algo.AlgorithmFactory;
@ -70,6 +68,8 @@ import org.insa.graphics.drawing.Drawing;
import org.insa.graphics.drawing.GraphPalette; import org.insa.graphics.drawing.GraphPalette;
import org.insa.graphics.drawing.components.BasicDrawing; import org.insa.graphics.drawing.components.BasicDrawing;
import org.insa.graphics.drawing.components.MapViewDrawing; import org.insa.graphics.drawing.components.MapViewDrawing;
import org.insa.graphics.utils.FileUtils;
import org.insa.graphics.utils.FileUtils.FolderType;
public class MainWindow extends JFrame { public class MainWindow extends JFrame {
@ -88,15 +88,6 @@ public class MainWindow extends JFrame {
*/ */
private static final int THREAD_TIMER_DELAY = 1000; // in milliseconds private static final int THREAD_TIMER_DELAY = 1000; // in milliseconds
private static final String DEFAULT_MAP_FOLDER_KEY = "DefaultMapFolder";
private static final String DEFAULT_MAP_FOLDER_INSA = "/home/commetud/...";
private static final String DEFAULT_PATH_FOLDER_KEY = "DefaultPathFolder";
private static final String DEFAULT_PATH_FOLDER_INSA = "/home/commetud/...";
// Preferences
private Preferences preferences = Preferences.userRoot().node(getClass().getName());
// Current graph. // Current graph.
protected Graph graph; protected Graph graph;
@ -303,25 +294,9 @@ public class MainWindow extends JFrame {
ActionListener openMapActionListener = new ActionListener() { ActionListener openMapActionListener = new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser(); JFileChooser chooser = FileUtils.createFileChooser(FolderType.Map);
FileNameExtensionFilter filter = new FileNameExtensionFilter("Graph files",
"mapgr");
File mapFolder = new File(
preferences.get(DEFAULT_MAP_FOLDER_KEY, DEFAULT_MAP_FOLDER_INSA));
if (!mapFolder.exists()) {
mapFolder = new File(System.getProperty("user.dir"));
}
chooser.setCurrentDirectory(mapFolder);
chooser.setFileFilter(filter);
if (chooser.showOpenDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION) { if (chooser.showOpenDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION) {
graphFilePath = chooser.getSelectedFile().getAbsolutePath(); graphFilePath = chooser.getSelectedFile().getAbsolutePath();
// Check...
if (chooser.getSelectedFile().exists()) {
preferences.put(DEFAULT_MAP_FOLDER_KEY,
chooser.getSelectedFile().getParent());
}
DataInputStream stream; DataInputStream stream;
try { try {
stream = new DataInputStream(new BufferedInputStream( stream = new DataInputStream(new BufferedInputStream(
@ -659,24 +634,8 @@ public class MainWindow extends JFrame {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser(); JFileChooser chooser = FileUtils.createFileChooser(FolderType.PathInput);
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"Path & compressed path files", "path");
File pathFolder = new File(
preferences.get(DEFAULT_PATH_FOLDER_KEY, DEFAULT_PATH_FOLDER_INSA));
if (!pathFolder.exists()) {
pathFolder = new File(System.getProperty("user.dir"));
}
chooser.setCurrentDirectory(pathFolder);
chooser.setFileFilter(filter);
if (chooser.showOpenDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION) { if (chooser.showOpenDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION) {
// Check & Update
if (chooser.getSelectedFile().exists()) {
preferences.put(DEFAULT_PATH_FOLDER_KEY,
chooser.getSelectedFile().getParent());
}
BinaryPathReader reader; BinaryPathReader reader;
try { try {
reader = new BinaryPathReader(new DataInputStream(new BufferedInputStream( reader = new BinaryPathReader(new DataInputStream(new BufferedInputStream(

View File

@ -37,6 +37,8 @@ import org.insa.graph.Path;
import org.insa.graph.io.BinaryPathWriter; import org.insa.graph.io.BinaryPathWriter;
import org.insa.graphics.drawing.Drawing; import org.insa.graphics.drawing.Drawing;
import org.insa.graphics.drawing.overlays.PathOverlay; import org.insa.graphics.drawing.overlays.PathOverlay;
import org.insa.graphics.utils.FileUtils;
import org.insa.graphics.utils.FileUtils.FolderType;
public class PathsPanel extends JPanel implements DrawingChangeListener, GraphChangeListener { public class PathsPanel extends JPanel implements DrawingChangeListener, GraphChangeListener {
@ -216,18 +218,15 @@ public class PathsPanel extends JPanel implements DrawingChangeListener, GraphCh
saveButton.addActionListener(new ActionListener() { saveButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
String filepath = System.getProperty("user.dir"); String filepath = String.format("path_%s_%d_%d.path",
filepath += File.separator + String.format("path_%s_%d_%d.path",
path.getGraph().getMapId().toLowerCase().replaceAll("[^a-z0-9_]", "_"), path.getGraph().getMapId().toLowerCase().replaceAll("[^a-z0-9_]", "_"),
path.getOrigin().getId(), path.getDestination().getId()); path.getOrigin().getId(), path.getDestination().getId());
JFileChooser fileChooser = new JFileChooser(); JFileChooser chooser = FileUtils.createFileChooser(FolderType.PathOutput,
fileChooser.setSelectedFile(new File(filepath)); filepath);
fileChooser.setApproveButtonText("Save");
fileChooser.setToolTipText("Save");
if (fileChooser if (chooser
.showSaveDialog(getTopLevelAncestor()) == JFileChooser.APPROVE_OPTION) { .showSaveDialog(getTopLevelAncestor()) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile(); File file = chooser.getSelectedFile();
try { try {
BinaryPathWriter writer = new BinaryPathWriter(new DataOutputStream( BinaryPathWriter writer = new BinaryPathWriter(new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(file)))); new BufferedOutputStream(new FileOutputStream(file))));
@ -284,6 +283,7 @@ public class PathsPanel extends JPanel implements DrawingChangeListener, GraphCh
/* /*
* (non-Javadoc) * (non-Javadoc)
*
* @see java.lang.Object#toString() * @see java.lang.Object#toString()
*/ */
public String toString() { public String toString() {

View File

@ -0,0 +1,111 @@
package org.insa.graphics.utils;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.EnumMap;
import java.util.Map;
import java.util.prefs.Preferences;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
public class FileUtils {
// Preferences
private static Preferences preferences = Preferences.userRoot().node(FileUtils.class.getName());
public enum FolderType {
Map, PathInput, PathOutput
}
private static class PreferencesEntry {
public String key;
public String value;
public PreferencesEntry(String key, String value) {
this.key = key;
this.value = value;
}
}
// Map folder type -> PreferencesEntry
private static final Map<FolderType, PreferencesEntry> folderToEntry = new EnumMap<>(
FolderType.class);
// Map folder type -> File Filter
private static final Map<FolderType, FileFilter> folderToFilter = new EnumMap<>(
FolderType.class);
static {
// Populate folderToEntry
folderToEntry.put(FolderType.Map,
new PreferencesEntry("DefaultMapFolder", "/home/commetud/..."));
folderToEntry.put(FolderType.PathInput,
new PreferencesEntry("DefaultPathInputFolder", "/home/commetud/..."));
folderToEntry.put(FolderType.PathOutput,
new PreferencesEntry("DefaultPathOutputsFolder", "paths"));
// Populate folderToFilter
folderToFilter.put(FolderType.Map, new FileNameExtensionFilter("Graph files", "mapgr"));
folderToFilter.put(FolderType.PathInput, new FileNameExtensionFilter("Path files", "path"));
folderToFilter.put(FolderType.PathOutput,
new FileNameExtensionFilter("Path files", "path"));
}
/**
* @return A File instance pointing to the preferred folder for the given type.
*/
public static File getPreferredFolder(FolderType folderType) {
PreferencesEntry entry = folderToEntry.get(folderType);
File folder = new File(preferences.get(entry.key, entry.value));
if (!folder.exists()) {
folder = new File(System.getProperty("user.dir"));
}
return folder;
}
public static void updatePreferredFolder(FolderType folderType, File newPreferredFolder) {
PreferencesEntry entry = folderToEntry.get(folderType);
preferences.put(entry.key, newPreferredFolder.getAbsolutePath());
}
/**
* @return A FileFilter corresponding to input graph files.
*/
public static FileFilter getFileFilter(FolderType folderType) {
return folderToFilter.get(folderType);
}
/**
* @param folderType
* @return
*/
public static JFileChooser createFileChooser(FolderType folderType, String defaultFileName) {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(getPreferredFolder(folderType));
if (defaultFileName != null) {
chooser.setSelectedFile(new File(chooser.getCurrentDirectory().getAbsolutePath()
+ File.separator + defaultFileName));
}
chooser.setFileFilter(getFileFilter(folderType));
chooser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals(JFileChooser.APPROVE_SELECTION)) {
if (chooser.getSelectedFile().exists()) {
updatePreferredFolder(folderType,
chooser.getSelectedFile().getParentFile());
}
}
}
});
return chooser;
}
public static JFileChooser createFileChooser(FolderType folderType) {
return createFileChooser(folderType, null);
}
}