Merge lp:~tapaal-contributor/tapaal/save-file-browser-location into lp:tapaal

Proposed by Lena Ernstsen
Status: Merged
Approved by: Jiri Srba
Approved revision: 1174
Merged at revision: 1172
Proposed branch: lp:~tapaal-contributor/tapaal/save-file-browser-location
Merge into: lp:tapaal
Diff against target: 227 lines (+40/-23)
4 files modified
src/net/tapaal/Preferences.java (+14/-0)
src/pipe/gui/widgets/filebrowser/FileBrowser.java (+7/-7)
src/pipe/gui/widgets/filebrowser/NativeFileBrowser.java (+9/-8)
src/pipe/gui/widgets/filebrowser/NativeFileBrowserFallback.java (+10/-8)
To merge this branch: bzr merge lp:~tapaal-contributor/tapaal/save-file-browser-location
Reviewer Review Type Date Requested Status
Jiri Srba Approve
Review via email: mp+427672@code.launchpad.net

Commit message

Saves the location of the file browser when saving or opening a new net

To post a comment you must log in.
Revision history for this message
Jiri Srba (srba) wrote :

Does not seem to work for me. Run the GUI, click on open file icon (it shows the root of HD by default). Open a file in e.g. Downloads and quit the GUI. Now run the GUI again, click on the open file icon and it does not offer Downloads but root of HD again. After this is fixed, we also need a separate branch for 3.9 series (on launchpad) and for cpn-gui-dev (on github).

review: Needs Fixing
1174. By Lena Ernstsen

fixed not saving locarion after first time opening the file browser

Revision history for this message
Jiri Srba (srba) wrote :

Tested and works.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/net/tapaal/Preferences.java'
2--- src/net/tapaal/Preferences.java 2016-11-14 09:58:40 +0000
3+++ src/net/tapaal/Preferences.java 2022-08-01 09:36:36 +0000
4@@ -329,4 +329,18 @@
5
6 return object;
7 }
8+
9+ public void setFileBrowserLocation(String location) {
10+ final String key = "file.location";
11+
12+ if (location == null || location.equals("")) {
13+ pref.remove(key);
14+ } else {
15+ pref.put(key, location);
16+ }
17+ }
18+
19+ public String getFileBrowserLocation() {
20+ return pref.get("file.location", null);
21+ }
22 }
23
24=== modified file 'src/pipe/gui/widgets/filebrowser/FileBrowser.java'
25--- src/pipe/gui/widgets/filebrowser/FileBrowser.java 2020-05-02 16:56:19 +0000
26+++ src/pipe/gui/widgets/filebrowser/FileBrowser.java 2022-08-01 09:36:36 +0000
27@@ -5,12 +5,14 @@
28 import dk.aau.cs.util.JavaUtil;
29 import pipe.gui.CreateGui;
30
31+import net.tapaal.Preferences;
32+
33
34 public abstract class FileBrowser {
35 //Used for latest open dialog path
36 //Default value null makes the open dialog open default folder, For Windows, My Documents, For *nix ~ , etc
37 //XXX 2018-05-23 moved from CreateGUI, refactor with regards to usage with lastPath local var in this class
38- public static String userPath = null;
39+ public static String userPath = Preferences.getInstance().getFileBrowserLocation();
40 static String lastSavePath = ".";
41 static String lastOpenPath = ".";
42
43@@ -22,18 +24,14 @@
44 return constructor(filetype, ext, "", path);
45 }
46 public static FileBrowser constructor(String filetype, final String ext, final String optionalExt, String path) {
47-
48- if(JavaUtil.getJREMajorVersion() >= 7){
49-
50+ if (JavaUtil.getJREMajorVersion() >= 7) {
51 return new NativeFileBrowser(filetype, ext, optionalExt, path);
52- }else{
53+ } else {
54 /*if(path != null) {
55 newObject.lastPath = path;
56 }*/
57 return new NativeFileBrowserFallback(filetype, ext, optionalExt, path);
58 }
59-
60-
61 }
62
63 public abstract File openFile();
64@@ -50,4 +48,6 @@
65 public abstract String saveFile(String suggestedName);
66 public abstract File saveFileToDir();
67
68+
69+
70 }
71
72=== modified file 'src/pipe/gui/widgets/filebrowser/NativeFileBrowser.java'
73--- src/pipe/gui/widgets/filebrowser/NativeFileBrowser.java 2020-11-27 12:35:35 +0000
74+++ src/pipe/gui/widgets/filebrowser/NativeFileBrowser.java 2022-08-01 09:36:36 +0000
75@@ -9,6 +9,7 @@
76
77 import javax.swing.*;
78 import pipe.gui.CreateGui;
79+import net.tapaal.Preferences;
80
81 class NativeFileBrowser extends FileBrowser {
82 private final FileDialog fc;
83@@ -56,7 +57,7 @@
84
85
86 public File openFile() {
87- if(specifiedPath == null) specifiedPath = lastOpenPath;
88+ if(specifiedPath == null) specifiedPath = Preferences.getInstance().getFileBrowserLocation();
89 fc.setDirectory(specifiedPath);
90 //This is needed for Windows
91 if(optionalExt.equals("")) fc.setFile(ext.equals("")? "":("*."+ext));
92@@ -66,13 +67,13 @@
93 fc.setVisible(true);
94 String selectedFile = fc.getFile();
95 String selectedDir = fc.getDirectory();
96- lastOpenPath = selectedDir;
97+ Preferences.getInstance().setFileBrowserLocation(selectedDir);
98 File file = selectedFile == null? null:new File(selectedDir + selectedFile);
99 return file;
100 }
101
102 public File[] openFiles() {
103- if(specifiedPath == null) specifiedPath = lastOpenPath;
104+ if(specifiedPath == null) specifiedPath = Preferences.getInstance().getFileBrowserLocation();
105 fc.setDirectory(specifiedPath);
106 //This is needed for Windows
107 if(optionalExt.equals("")) fc.setFile(ext.equals("")? "":("*."+ext));
108@@ -81,12 +82,12 @@
109 fc.setMode(FileDialog.LOAD);
110 fc.setVisible(true);
111 File[] selectedFiles = fc.getFiles();
112- lastOpenPath = fc.getDirectory();
113+ Preferences.getInstance().setFileBrowserLocation(fc.getDirectory());
114 return selectedFiles;
115 }
116
117 public String saveFile(String suggestedName) {
118- if(specifiedPath == null) specifiedPath = lastSavePath;
119+ if(specifiedPath == null) specifiedPath = Preferences.getInstance().getFileBrowserLocation();
120 fc.setDirectory(specifiedPath);
121 fc.setFile(suggestedName + (suggestedName.endsWith("."+ext)? "":"."+ext));
122 fc.setMode(FileDialog.SAVE);
123@@ -103,7 +104,7 @@
124 }
125
126 String file = fc.getFile() == null? null: fc.getDirectory() + fc.getFile();
127- lastSavePath = fc.getDirectory();
128+ Preferences.getInstance().setFileBrowserLocation(fc.getDirectory());
129
130 if(file == null){
131 return file;
132@@ -150,14 +151,14 @@
133 //So we make a JFileChooser in which we can control it
134 if(System.getProperty("os.name").startsWith("Windows")) {
135 File selectedDir = null;
136- if (specifiedPath == null) specifiedPath = lastSavePath;
137+ if (specifiedPath == null) specifiedPath = Preferences.getInstance().getFileBrowserLocation();
138 JFileChooser c = new JFileChooser(specifiedPath);
139 c.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
140 c.setDialogTitle("Choose target directory for export");
141 int rVal = c.showSaveDialog(c);
142 if (rVal == JFileChooser.APPROVE_OPTION) {
143 selectedDir = c.getSelectedFile();
144- lastSavePath = selectedDir.getPath();
145+ Preferences.getInstance().setFileBrowserLocation(selectedDir.getPath());
146 }
147
148 return selectedDir;
149
150=== modified file 'src/pipe/gui/widgets/filebrowser/NativeFileBrowserFallback.java'
151--- src/pipe/gui/widgets/filebrowser/NativeFileBrowserFallback.java 2020-05-05 18:17:52 +0000
152+++ src/pipe/gui/widgets/filebrowser/NativeFileBrowserFallback.java 2022-08-01 09:36:36 +0000
153@@ -10,6 +10,8 @@
154 import javax.swing.JFileChooser;
155 import javax.swing.JOptionPane;
156 import javax.swing.filechooser.FileNameExtensionFilter;
157+
158+import net.tapaal.Preferences;
159 import pipe.gui.CreateGui;
160
161 class NativeFileBrowserFallback extends FileBrowser {
162@@ -75,7 +77,7 @@
163 }
164
165 public File openFile() {
166- if(specifiedPath == null) specifiedPath = lastOpenPath;
167+ if(specifiedPath == null) specifiedPath = Preferences.getInstance().getFileBrowserLocation();
168 fc.setDirectory(specifiedPath);
169 //This is needed for windows
170 if(optionalExt.equals("")) fc.setFile(ext.equals("")? "":("*."+ext));
171@@ -84,13 +86,13 @@
172 fc.setVisible(true);
173 String selectedFile = fc.getFile();
174 String selectedDir = fc.getDirectory();
175- lastOpenPath = selectedDir;
176+ Preferences.getInstance().setFileBrowserLocation(selectedDir);
177 File file = selectedFile == null? null:new File(selectedDir + selectedFile);
178 return file;
179 }
180
181 public File[] openFiles() {
182- if(specifiedPath == null) specifiedPath = lastOpenPath;
183+ if(specifiedPath == null) specifiedPath = Preferences.getInstance().getFileBrowserLocation();
184 if(new File(specifiedPath).exists()) fileChooser.setCurrentDirectory(new File(specifiedPath));
185 /*if (lastPath != null) {
186 File path = new File(lastPath);
187@@ -104,20 +106,20 @@
188 filesArray = fileChooser.getSelectedFiles();
189 }
190 //They should all come from the same directory so we just take one
191- lastOpenPath = filesArray[0].getAbsolutePath();
192+ Preferences.getInstance().setFileBrowserLocation(filesArray[0].getAbsolutePath());
193 return filesArray;
194 }
195
196
197 public String saveFile(String suggestedName) {
198- if(specifiedPath == null) specifiedPath = lastSavePath;
199+ if(specifiedPath == null) specifiedPath = Preferences.getInstance().getFileBrowserLocation();
200 fc.setDirectory(specifiedPath);
201 fc.setFile(suggestedName + (suggestedName.endsWith("."+ext)? "":"."+ext));
202 fc.setMode(FileDialog.SAVE);
203 fc.setVisible(true);
204
205 String file = fc.getFile() == null? null: fc.getDirectory() + fc.getFile();
206- lastSavePath = fc.getDirectory();
207+ Preferences.getInstance().setFileBrowserLocation(fc.getDirectory());
208
209 if(file == null){
210 return file;
211@@ -163,14 +165,14 @@
212 //So we make a JFileChooser in which we can control it
213 if(System.getProperty("os.name").startsWith("Windows")) {
214 File selectedDir = null;
215- if (specifiedPath == null) specifiedPath = lastSavePath;
216+ if (specifiedPath == null) specifiedPath = Preferences.getInstance().getFileBrowserLocation();
217 JFileChooser c = new JFileChooser(specifiedPath);
218 c.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
219 c.setDialogTitle("Choose target directory for export");
220 int rVal = c.showSaveDialog(c);
221 if (rVal == JFileChooser.APPROVE_OPTION) {
222 selectedDir = c.getSelectedFile();
223- lastSavePath = selectedDir.getPath();
224+ Preferences.getInstance().setFileBrowserLocation(selectedDir.getPath());
225 }
226
227 return selectedDir;

Subscribers

People subscribed via source and target branches