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

Subscribers

People subscribed via source and target branches