Merge lp:~tapaal-contributor/tapaal/fix1945984 into lp:~tapaal-contributor/tapaal/cpn-gui-dev

Proposed by Kenneth Yrke Jørgensen
Status: Merged
Approved by: Jiri Srba
Approved revision: 1487
Merged at revision: 1496
Proposed branch: lp:~tapaal-contributor/tapaal/fix1945984
Merge into: lp:~tapaal-contributor/tapaal/cpn-gui-dev
Diff against target: 224 lines (+176/-32)
1 file modified
src/pipe/gui/widgets/filebrowser/FileBrowser.java (+176/-32)
To merge this branch: bzr merge lp:~tapaal-contributor/tapaal/fix1945984
Reviewer Review Type Date Requested Status
Jiri Srba Approve
Review via email: mp+413439@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Jiri Srba (srba) wrote :

I could not reproduce the bug on mac but after this patch saving and opening files still works on mac, so I approve.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/pipe/gui/widgets/filebrowser/FileBrowser.java'
2--- src/pipe/gui/widgets/filebrowser/FileBrowser.java 2021-11-18 11:18:54 +0000
3+++ src/pipe/gui/widgets/filebrowser/FileBrowser.java 2021-12-21 09:05:52 +0000
4@@ -1,42 +1,186 @@
5 package pipe.gui.widgets.filebrowser;
6
7+import pipe.gui.CreateGui;
8+
9+import javax.swing.*;
10+import java.awt.*;
11 import java.io.File;
12-
13-import dk.aau.cs.util.JavaUtil;
14-import pipe.gui.CreateGui;
15-
16-
17-public abstract class FileBrowser {
18+import java.io.IOException;
19+import java.util.regex.Matcher;
20+import java.util.regex.Pattern;
21+
22+
23+public class FileBrowser {
24 //Used for latest open dialog path
25 //Default value null makes the open dialog open default folder, For Windows, My Documents, For *nix ~ , etc
26- //XXX 2018-05-23 moved from CreateGUI, refactor with regards to usage with lastPath local var in this class
27+ //XXX 2018-05-23 moved from CreateGUI, refactor with regards to usage with lastPath local var in this class
28 public static String userPath = null;
29 static String lastSavePath = ".";
30 static String lastOpenPath = ".";
31-
32- public static FileBrowser constructor(String filetype, final String ext) {
33- return constructor(filetype, ext, null);
34- }
35-
36- public static FileBrowser constructor(String filetype, final String ext, String path) {
37- return constructor(filetype, ext, "", path);
38- }
39- public static FileBrowser constructor(String filetype, final String ext, final String optionalExt, String path) {
40- return new NativeFileBrowser(filetype, ext, optionalExt, path);
41- }
42-
43- public abstract File openFile();
44-
45- public abstract File[] openFiles();
46-
47- public String saveFile(){
48- if(CreateGui.getAppGui().getCurrentTabName().endsWith(".tapn"))
49- return saveFile(CreateGui.getAppGui().getCurrentTabName().replaceAll(".tapn", ""));
50- else
51- return saveFile(CreateGui.getAppGui().getCurrentTabName().replaceAll(".xml", ""));
52- }
53-
54- public abstract String saveFile(String suggestedName);
55- public abstract File saveFileToDir();
56+ protected final FileDialog fileDialog;
57+ protected final String ext;
58+ protected final String optionalExt;
59+ protected String specifiedPath;
60+
61+ private FileBrowser(String filetype, final String ext, final String optionalExt, String path) {
62+ fileDialog = new FileDialog(CreateGui.getAppGui(), filetype);
63+ this.ext = ext;
64+ this.optionalExt = optionalExt;
65+ this.specifiedPath = path;
66+
67+ if (filetype == null) {
68+ filetype = "file";
69+ }
70+
71+ // Setup filter if extension specified
72+ //This is needed for Linux and Mac
73+ if (!ext.equals("")) {
74+ if (!optionalExt.equals("")) {
75+ fileDialog.setFilenameFilter((dir, name) -> name.endsWith(ext) || name.endsWith(optionalExt));
76+ } else {
77+ fileDialog.setFilenameFilter((dir, name) -> name.endsWith(ext));
78+ }
79+ }
80+ }
81+
82+ public static FileBrowser constructor(String filetype, final String ext) {
83+ return constructor(filetype, ext, null);
84+ }
85+
86+ public static FileBrowser constructor(String filetype, final String ext, String path) {
87+ return constructor(filetype, ext, "", path);
88+ }
89+
90+ public static FileBrowser constructor(String filetype, final String ext, final String optionalExt, String path) {
91+ return new FileBrowser(filetype, ext, optionalExt, path);
92+ }
93+
94+ public File openFile() {
95+ if (specifiedPath == null) specifiedPath = lastOpenPath;
96+ fileDialog.setDirectory(specifiedPath);
97+ //This is needed for Windows
98+ if (optionalExt.equals("")) {
99+ fileDialog.setFile(ext.equals("") ? "" : ("*." + ext));
100+ } else {
101+ fileDialog.setFile(ext.equals("") ? "" : ("*." + ext + ";*." + optionalExt));
102+ }
103+ fileDialog.setMode(FileDialog.LOAD);
104+ fileDialog.setMultipleMode(false);
105+ fileDialog.setVisible(true);
106+ String selectedFile = fileDialog.getFile();
107+ String selectedDir = fileDialog.getDirectory();
108+ lastOpenPath = selectedDir;
109+ File file = selectedFile == null ? null : new File(selectedDir + selectedFile);
110+ return file;
111+ }
112+
113+ public File[] openFiles() {
114+ if (specifiedPath == null) specifiedPath = lastOpenPath;
115+ fileDialog.setDirectory(specifiedPath);
116+ //This is needed for Windows
117+ if (optionalExt.equals("")) {
118+ fileDialog.setFile(ext.equals("") ? "" : ("*." + ext));
119+ } else {
120+ fileDialog.setFile(ext.equals("") ? "" : ("*." + ext + ";*." + optionalExt));
121+ }
122+ fileDialog.setMultipleMode(true);
123+ fileDialog.setMode(FileDialog.LOAD);
124+ fileDialog.setVisible(true);
125+ File[] selectedFiles = fileDialog.getFiles();
126+ lastOpenPath = fileDialog.getDirectory();
127+ return selectedFiles;
128+ }
129+
130+ public String saveFile() {
131+ if (CreateGui.getAppGui().getCurrentTabName().endsWith(".tapn")) {
132+ return saveFile(CreateGui.getAppGui().getCurrentTabName().replaceAll(".tapn", ""));
133+ } else {
134+ return saveFile(CreateGui.getAppGui().getCurrentTabName().replaceAll(".xml", ""));
135+ }
136+ }
137+
138+ public String saveFile(String suggestedName) {
139+ if (specifiedPath == null) specifiedPath = lastSavePath;
140+
141+ fileDialog.setDirectory(specifiedPath);
142+ fileDialog.setFile(suggestedName + (suggestedName.endsWith("." + ext) ? "" : "." + ext));
143+ fileDialog.setMode(FileDialog.SAVE);
144+ fileDialog.setVisible(true);
145+
146+ // user canceled
147+ if (fileDialog.getFile() == null) {
148+ return null;
149+ }
150+
151+ // Fixes bug:1648076 for OS X
152+ if (fileDialog.getDirectory().endsWith(suggestedName + "." + ext + "/")) {
153+ fileDialog.setDirectory(fileDialog.getDirectory().replaceAll(suggestedName + "." + ext + "/", ""));
154+ }
155+
156+ String file = fileDialog.getDirectory() + fileDialog.getFile();
157+ lastSavePath = fileDialog.getDirectory();
158+
159+ // Windows does not enforce file ending on save
160+ if (!file.endsWith("." + ext)) {
161+
162+ Pattern p = Pattern.compile(".*\\.(.*)");
163+ Matcher m = p.matcher(file);
164+ String newName = file + "." + ext;
165+
166+ // I guess this tries to replace any existing file ending? I don't think this is safe --kyrke
167+ if (m.matches()) {
168+ newName = file.substring(0, file.length() - m.group(1).length()) + ext;
169+ }
170+ File destination = new File(newName);
171+
172+ // Overwrite dialog is already shown, but since we changed the named file, we need to show it again
173+ if (destination.exists()) {
174+ int overRide = JOptionPane.showConfirmDialog(CreateGui.getAppGui(), newName + "\nDo you want to overwrite this file?");
175+ switch (overRide) {
176+ case JOptionPane.NO_OPTION:
177+ return saveFile(suggestedName); // Reopen dialog to select new name
178+ case JOptionPane.YES_OPTION:
179+ file = newName;
180+ break;
181+ default:
182+ return null;
183+ }
184+ } else {
185+ file = newName;
186+ }
187+ }
188+ return file;
189+ }
190+
191+ public File saveFileToDir() {
192+ //In Windows the native FileDialog only works with files
193+ //So we make a JFileChooser in which we can control it
194+ if (System.getProperty("os.name").startsWith("Windows")) {
195+ File selectedDir = null;
196+ if (specifiedPath == null) specifiedPath = lastSavePath;
197+ JFileChooser c = new JFileChooser(specifiedPath);
198+ c.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
199+ c.setDialogTitle("Choose target directory for export");
200+ int rVal = c.showSaveDialog(c);
201+ if (rVal == JFileChooser.APPROVE_OPTION) {
202+ selectedDir = c.getSelectedFile();
203+ lastSavePath = selectedDir.getPath();
204+ }
205+
206+ return selectedDir;
207+ } else {
208+ //For Mac we can set Directories only
209+ //For linux a save dialog only shows directories
210+ System.setProperty("apple.awt.fileDialogForDirectories", "true");
211+ String selection = saveFile("Choose Directory");
212+ System.setProperty("apple.awt.fileDialogForDirectories", "false");
213+ if (selection != null) {
214+ return new File(fileDialog.getDirectory());
215+ } else {
216+ return null;
217+ }
218+ }
219+
220+ }
221
222 }
223
224=== removed file 'src/pipe/gui/widgets/filebrowser/NativeFileBrowser.java'

Subscribers

People subscribed via source and target branches