@@ -42,6 +42,7 @@ Development and Distribution License("CDDL") (collectively, the
4242package org .gephi .desktop .project ;
4343
4444import java .io .File ;
45+ import java .io .IOException ;
4546import java .util .ArrayList ;
4647import java .util .Arrays ;
4748import 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