Browse Source

Create a FileUtils class to simplify file opening/saving.

Holt59 3 years ago
parent
commit
0030ab7aef

+ 4
- 45
src/main/org/insa/graphics/MainWindow.java View File

@@ -19,7 +19,6 @@ import java.io.IOException;
19 19
 import java.io.PrintStream;
20 20
 import java.util.ArrayList;
21 21
 import java.util.List;
22
-import java.util.prefs.Preferences;
23 22
 
24 23
 import javax.swing.BorderFactory;
25 24
 import javax.swing.Box;
@@ -42,7 +41,6 @@ import javax.swing.Timer;
42 41
 import javax.swing.UIManager;
43 42
 import javax.swing.border.CompoundBorder;
44 43
 import javax.swing.border.EmptyBorder;
45
-import javax.swing.filechooser.FileNameExtensionFilter;
46 44
 
47 45
 import org.insa.algo.AbstractSolution;
48 46
 import org.insa.algo.AlgorithmFactory;
@@ -70,6 +68,8 @@ import org.insa.graphics.drawing.Drawing;
70 68
 import org.insa.graphics.drawing.GraphPalette;
71 69
 import org.insa.graphics.drawing.components.BasicDrawing;
72 70
 import org.insa.graphics.drawing.components.MapViewDrawing;
71
+import org.insa.graphics.utils.FileUtils;
72
+import org.insa.graphics.utils.FileUtils.FolderType;
73 73
 
74 74
 public class MainWindow extends JFrame {
75 75
 
@@ -88,15 +88,6 @@ public class MainWindow extends JFrame {
88 88
      */
89 89
     private static final int THREAD_TIMER_DELAY = 1000; // in milliseconds
90 90
 
91
-    private static final String DEFAULT_MAP_FOLDER_KEY = "DefaultMapFolder";
92
-    private static final String DEFAULT_MAP_FOLDER_INSA = "/home/commetud/...";
93
-
94
-    private static final String DEFAULT_PATH_FOLDER_KEY = "DefaultPathFolder";
95
-    private static final String DEFAULT_PATH_FOLDER_INSA = "/home/commetud/...";
96
-
97
-    // Preferences
98
-    private Preferences preferences = Preferences.userRoot().node(getClass().getName());
99
-
100 91
     // Current graph.
101 92
     protected Graph graph;
102 93
 
@@ -303,25 +294,9 @@ public class MainWindow extends JFrame {
303 294
         ActionListener openMapActionListener = new ActionListener() {
304 295
             @Override
305 296
             public void actionPerformed(ActionEvent e) {
306
-                JFileChooser chooser = new JFileChooser();
307
-                FileNameExtensionFilter filter = new FileNameExtensionFilter("Graph files",
308
-                        "mapgr");
309
-                File mapFolder = new File(
310
-                        preferences.get(DEFAULT_MAP_FOLDER_KEY, DEFAULT_MAP_FOLDER_INSA));
311
-                if (!mapFolder.exists()) {
312
-                    mapFolder = new File(System.getProperty("user.dir"));
313
-                }
314
-                chooser.setCurrentDirectory(mapFolder);
315
-                chooser.setFileFilter(filter);
297
+                JFileChooser chooser = FileUtils.createFileChooser(FolderType.Map);
316 298
                 if (chooser.showOpenDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION) {
317 299
                     graphFilePath = chooser.getSelectedFile().getAbsolutePath();
318
-
319
-                    // Check...
320
-                    if (chooser.getSelectedFile().exists()) {
321
-                        preferences.put(DEFAULT_MAP_FOLDER_KEY,
322
-                                chooser.getSelectedFile().getParent());
323
-                    }
324
-
325 300
                     DataInputStream stream;
326 301
                     try {
327 302
                         stream = new DataInputStream(new BufferedInputStream(
@@ -659,24 +634,8 @@ public class MainWindow extends JFrame {
659 634
 
660 635
             @Override
661 636
             public void actionPerformed(ActionEvent e) {
662
-                JFileChooser chooser = new JFileChooser();
663
-                FileNameExtensionFilter filter = new FileNameExtensionFilter(
664
-                        "Path & compressed path files", "path");
665
-                File pathFolder = new File(
666
-                        preferences.get(DEFAULT_PATH_FOLDER_KEY, DEFAULT_PATH_FOLDER_INSA));
667
-                if (!pathFolder.exists()) {
668
-                    pathFolder = new File(System.getProperty("user.dir"));
669
-                }
670
-                chooser.setCurrentDirectory(pathFolder);
671
-                chooser.setFileFilter(filter);
637
+                JFileChooser chooser = FileUtils.createFileChooser(FolderType.PathInput);
672 638
                 if (chooser.showOpenDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION) {
673
-
674
-                    // Check & Update
675
-                    if (chooser.getSelectedFile().exists()) {
676
-                        preferences.put(DEFAULT_PATH_FOLDER_KEY,
677
-                                chooser.getSelectedFile().getParent());
678
-                    }
679
-
680 639
                     BinaryPathReader reader;
681 640
                     try {
682 641
                         reader = new BinaryPathReader(new DataInputStream(new BufferedInputStream(

+ 8
- 8
src/main/org/insa/graphics/PathsPanel.java View File

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

+ 111
- 0
src/main/org/insa/graphics/utils/FileUtils.java View File

@@ -0,0 +1,111 @@
1
+package org.insa.graphics.utils;
2
+
3
+import java.awt.event.ActionEvent;
4
+import java.awt.event.ActionListener;
5
+import java.io.File;
6
+import java.util.EnumMap;
7
+import java.util.Map;
8
+import java.util.prefs.Preferences;
9
+
10
+import javax.swing.JFileChooser;
11
+import javax.swing.filechooser.FileFilter;
12
+import javax.swing.filechooser.FileNameExtensionFilter;
13
+
14
+public class FileUtils {
15
+
16
+    // Preferences
17
+    private static Preferences preferences = Preferences.userRoot().node(FileUtils.class.getName());
18
+
19
+    public enum FolderType {
20
+        Map, PathInput, PathOutput
21
+    }
22
+
23
+    private static class PreferencesEntry {
24
+        public String key;
25
+        public String value;
26
+
27
+        public PreferencesEntry(String key, String value) {
28
+            this.key = key;
29
+            this.value = value;
30
+        }
31
+    }
32
+
33
+    // Map folder type -> PreferencesEntry
34
+    private static final Map<FolderType, PreferencesEntry> folderToEntry = new EnumMap<>(
35
+            FolderType.class);
36
+
37
+    // Map folder type -> File Filter
38
+    private static final Map<FolderType, FileFilter> folderToFilter = new EnumMap<>(
39
+            FolderType.class);
40
+
41
+    static {
42
+        // Populate folderToEntry
43
+        folderToEntry.put(FolderType.Map,
44
+                new PreferencesEntry("DefaultMapFolder", "/home/commetud/..."));
45
+        folderToEntry.put(FolderType.PathInput,
46
+                new PreferencesEntry("DefaultPathInputFolder", "/home/commetud/..."));
47
+        folderToEntry.put(FolderType.PathOutput,
48
+                new PreferencesEntry("DefaultPathOutputsFolder", "paths"));
49
+
50
+        // Populate folderToFilter
51
+        folderToFilter.put(FolderType.Map, new FileNameExtensionFilter("Graph files", "mapgr"));
52
+        folderToFilter.put(FolderType.PathInput, new FileNameExtensionFilter("Path files", "path"));
53
+        folderToFilter.put(FolderType.PathOutput,
54
+                new FileNameExtensionFilter("Path files", "path"));
55
+    }
56
+
57
+    /**
58
+     * @return A File instance pointing to the preferred folder for the given type.
59
+     */
60
+    public static File getPreferredFolder(FolderType folderType) {
61
+        PreferencesEntry entry = folderToEntry.get(folderType);
62
+        File folder = new File(preferences.get(entry.key, entry.value));
63
+        if (!folder.exists()) {
64
+            folder = new File(System.getProperty("user.dir"));
65
+        }
66
+        return folder;
67
+    }
68
+
69
+    public static void updatePreferredFolder(FolderType folderType, File newPreferredFolder) {
70
+        PreferencesEntry entry = folderToEntry.get(folderType);
71
+        preferences.put(entry.key, newPreferredFolder.getAbsolutePath());
72
+    }
73
+
74
+    /**
75
+     * @return A FileFilter corresponding to input graph files.
76
+     */
77
+    public static FileFilter getFileFilter(FolderType folderType) {
78
+        return folderToFilter.get(folderType);
79
+    }
80
+
81
+    /**
82
+     * @param folderType
83
+     * @return
84
+     */
85
+    public static JFileChooser createFileChooser(FolderType folderType, String defaultFileName) {
86
+        JFileChooser chooser = new JFileChooser();
87
+        chooser.setCurrentDirectory(getPreferredFolder(folderType));
88
+        if (defaultFileName != null) {
89
+            chooser.setSelectedFile(new File(chooser.getCurrentDirectory().getAbsolutePath()
90
+                    + File.separator + defaultFileName));
91
+        }
92
+        chooser.setFileFilter(getFileFilter(folderType));
93
+        chooser.addActionListener(new ActionListener() {
94
+            @Override
95
+            public void actionPerformed(ActionEvent e) {
96
+                if (e.getActionCommand().equals(JFileChooser.APPROVE_SELECTION)) {
97
+                    if (chooser.getSelectedFile().exists()) {
98
+                        updatePreferredFolder(folderType,
99
+                                chooser.getSelectedFile().getParentFile());
100
+                    }
101
+                }
102
+            }
103
+        });
104
+        return chooser;
105
+    }
106
+
107
+    public static JFileChooser createFileChooser(FolderType folderType) {
108
+        return createFileChooser(folderType, null);
109
+    }
110
+
111
+}

Loading…
Cancel
Save