Skip to content

Commit eb906b2

Browse files
committed
Fix #1726 Feature request: remember last save location (dir)
1 parent 90a5fe6 commit eb906b2

File tree

2 files changed

+72
-25
lines changed

2 files changed

+72
-25
lines changed

modules/DesktopExport/src/main/java/org/gephi/desktop/io/export/api/GraphFileExporterUI.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,16 @@ public void actionPerformed(ActionEvent e) {
156156
graphSettings.setVisibleOnlyGraph(visibleOnlyGraph);
157157
southPanel.add(graphSettings, BorderLayout.CENTER);
158158

159+
File lastPathDir = null;
160+
if (lastPath != null) {
161+
lastPathDir = new File(lastPath).getParentFile();
162+
while (lastPathDir != null && !lastPathDir.exists()) {
163+
lastPathDir = lastPathDir.getParentFile();
164+
}
165+
}
166+
159167
//Optionable file chooser
160-
final JFileChooser chooser = new JFileChooser(lastPath) {
168+
final JFileChooser chooser = new JFileChooser(lastPathDir) {
161169

162170
@Override
163171
protected JDialog createDialog(Component parent) throws HeadlessException {
@@ -252,10 +260,13 @@ public void propertyChange(PropertyChangeEvent evt) {
252260
if (lastFileFilter != null) {
253261
defaultFileFilter = lastFileFilter;
254262
}
255-
263+
256264
chooser.setFileFilter(defaultFileFilter);
257265

258266
selectedFile = new File(chooser.getCurrentDirectory(), "Untitled" + defaultFileFilter.getExtensions().get(0));
267+
if (lastPathDir != null && lastPathDir.exists() && lastPathDir.isDirectory()) {
268+
selectedFile = new File(lastPath);
269+
}
259270
chooser.setSelectedFile(selectedFile);
260271

261272
//Show

modules/DesktopProject/src/main/java/org/gephi/desktop/project/ProjectControllerUIImpl.java

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Development and Distribution License("CDDL") (collectively, the
4242
package org.gephi.desktop.project;
4343

4444
import java.io.File;
45+
import java.io.IOException;
4546
import java.util.ArrayList;
4647
import java.util.Arrays;
4748
import java.util.List;
@@ -183,38 +184,39 @@ public void saveAsProject() {
183184
String lastPathDefault = NbPreferences.forModule(ProjectControllerUIImpl.class).get(LAST_PATH_DEFAULT, null);
184185
String lastPath = NbPreferences.forModule(ProjectControllerUIImpl.class).get(LAST_PATH, lastPathDefault);
185186

187+
File lastPathDir = null;
188+
if (lastPath != null) {
189+
lastPathDir = new File(lastPath).getParentFile();
190+
while (lastPathDir != null && !lastPathDir.exists()) {
191+
lastPathDir = lastPathDir.getParentFile();
192+
}
193+
}
194+
186195
//File chooser
187-
final JFileChooser chooser = new JFileChooser(lastPath);
196+
final JFileChooser chooser = new JFileChooser(lastPathDir) {
197+
@Override
198+
public void approveSelection() {
199+
if (canExport(this)) {
200+
super.approveSelection();
201+
}
202+
}
203+
};
188204
chooser.addChoosableFileFilter(filter);
205+
206+
if (lastPathDir != null && lastPathDir.exists() && lastPathDir.isDirectory()) {
207+
chooser.setSelectedFile(new File(lastPath));
208+
}
209+
189210
int returnFile = chooser.showSaveDialog(null);
190211
if (returnFile == JFileChooser.APPROVE_OPTION) {
191212
File file = chooser.getSelectedFile();
213+
file = FileUtil.normalizeFile(file);
192214

193215
//Save last path
194216
NbPreferences.forModule(ProjectControllerUIImpl.class).put(LAST_PATH, file.getAbsolutePath());
195217

196218
//File management
197219
try {
198-
if (!file.getPath().endsWith(".gephi")) {
199-
file = new File(file.getPath() + ".gephi");
200-
}
201-
if (!file.exists()) {
202-
if (!file.createNewFile()) {
203-
String failMsg = NbBundle.getMessage(
204-
ProjectControllerUIImpl.class,
205-
"SaveAsProject_SaveFailed", new Object[]{file.getPath()});
206-
JOptionPane.showMessageDialog(null, failMsg);
207-
return;
208-
}
209-
} else {
210-
String overwriteMsg = NbBundle.getMessage(
211-
ProjectControllerUIImpl.class,
212-
"SaveAsProject_Overwrite", new Object[]{file.getPath()});
213-
if (JOptionPane.showConfirmDialog(null, overwriteMsg) != JOptionPane.OK_OPTION) {
214-
return;
215-
}
216-
}
217-
file = FileUtil.normalizeFile(file);
218220
final String SaveAsFileName = file.getName();
219221
//File exist now, Save project
220222
Project project = controller.getCurrentProject();
@@ -237,6 +239,40 @@ public void run() {
237239
}
238240
}
239241

242+
private boolean canExport(JFileChooser chooser) {
243+
File file = chooser.getSelectedFile();
244+
245+
if (!file.getPath().endsWith(".gephi")) {
246+
file = new File(file.getPath() + ".gephi");
247+
chooser.setSelectedFile(file);
248+
}
249+
250+
try {
251+
if (!file.exists()) {
252+
if (!file.createNewFile()) {
253+
String failMsg = NbBundle.getMessage(
254+
ProjectControllerUIImpl.class,
255+
"SaveAsProject_SaveFailed", new Object[]{file.getPath()});
256+
JOptionPane.showMessageDialog(null, failMsg);
257+
return false;
258+
}
259+
} else {
260+
String overwriteMsg = NbBundle.getMessage(
261+
ProjectControllerUIImpl.class,
262+
"SaveAsProject_Overwrite", new Object[]{file.getPath()});
263+
if (JOptionPane.showConfirmDialog(null, overwriteMsg) != JOptionPane.OK_OPTION) {
264+
return false;
265+
}
266+
}
267+
} catch (IOException ex) {
268+
NotifyDescriptor.Message msg = new NotifyDescriptor.Message(ex.getMessage(), NotifyDescriptor.WARNING_MESSAGE);
269+
DialogDisplayer.getDefault().notifyLater(msg);
270+
return false;
271+
}
272+
273+
return true;
274+
}
275+
240276
public boolean closeCurrentProject() {
241277
if (controller.getCurrentProject() != null) {
242278

@@ -459,9 +495,9 @@ public void openFile(FileImporterBuilder[] builders) {
459495
} else {
460496
DialogFileFilter gephiFilter = new DialogFileFilter(NbBundle.getMessage(ProjectControllerUIImpl.class, "OpenProject_filechooser_filter"));
461497
gephiFilter.addExtension(".gephi");
462-
498+
463499
filters.add(gephiFilter);
464-
500+
465501
graphFilter.addExtension(".gephi");
466502
fileTypes = Arrays.asList(importControllerUI.getImportController().getFileTypes());
467503
}

0 commit comments

Comments
 (0)