4343import org .sbolstandard .core2 .ComponentDefinition ;
4444import org .sbolstandard .core2 .SBOLConversionException ;
4545import org .sbolstandard .core2 .SBOLDocument ;
46+ import org .sbolstandard .core2 .SBOLReader ;
4647import org .sbolstandard .core2 .SBOLValidationException ;
4748import org .sbolstandard .core2 .SBOLWriter ;
4849import org .sbolstandard .core2 .Sequence ;
@@ -367,7 +368,7 @@ void openDocument(DocumentIO documentIO) throws SBOLValidationException, IOExcep
367368 }
368369 }
369370
370- private void saveAs () throws SBOLValidationException , FileNotFoundException , SBOLConversionException {
371+ private void saveAs () throws IOException , Exception {
371372 ComponentDefinitionBox root = new ComponentDefinitionBox ();
372373 SBOLDocument doc = editor .getDesign ().createDocument (root );
373374 if (SBOLUtils .rootCalledUnamedPart (root .cd , this )) {
@@ -377,15 +378,21 @@ private void saveAs() throws SBOLValidationException, FileNotFoundException, SBO
377378
378379 File file = SBOLUtils .selectFile (this , fc );
379380 if (file == null ) {
381+ JOptionPane .showMessageDialog (this , "File selection failed." );
380382 return ;
381383 }
382384
383- String fileName = file .getName ();
384- if (!fileName .contains ("." )) {
385- file = new File (file + ".xml" );
386- }
385+ if (file .exists ()) {
386+ int selection = chooseSaveOption ();
387+ saveOption (SBOLReader .read (file ), doc , root .cd , selection , file );
388+ } else {
389+ String fileName = file .getName ();
390+ if (!fileName .contains ("." )) {
391+ file = new File (file + ".xml" );
392+ }
387393
388- SBOLWriter .write (doc , new FileOutputStream (file ));
394+ SBOLWriter .write (doc , new FileOutputStream (file ));
395+ }
389396 }
390397
391398 private void export () throws FileNotFoundException , SBOLConversionException , IOException , SBOLValidationException {
@@ -401,6 +408,11 @@ private void export() throws FileNotFoundException, SBOLConversionException, IOE
401408 if (file == null ) {
402409 return ;
403410 }
411+ if (file .exists ()) {
412+ JOptionPane .showMessageDialog (this , "This file already exists." );
413+ return ;
414+ }
415+
404416 String fileName = file .getName ();
405417
406418 SBOLDocument doc = editor .getDesign ().createDocument (null );
@@ -540,13 +552,24 @@ boolean saveIntoExistingFile() throws Exception {
540552 selection = 0 ;
541553 }
542554 } else {
543- String [] options = { "Cancel" , "Overwrite Document" , "New Version" , "Overwrite Parts" };
544- selection = JOptionPane .showOptionDialog (this ,
545- "You are saving into an existing SBOL file. Would you like to overwrite the document, overwrite the parts, or create new versions of parts that already exist in the document?" ,
546- "Save Options" , JOptionPane .DEFAULT_OPTION , JOptionPane .QUESTION_MESSAGE , null , options ,
547- options [3 ]);
555+ selection = chooseSaveOption ();
548556 }
549557
558+ return saveOption (doc , currentDesign , currentRootCD , selection , SBOLUtils .setupFile ());
559+ }
560+
561+ private int chooseSaveOption () {
562+ int selection ;
563+ String [] options = { "Cancel" , "Overwrite Document" , "New Version" , "Overwrite Parts" };
564+ selection = JOptionPane .showOptionDialog (this ,
565+ "You are saving into an existing SBOL file. Would you like to overwrite the document, overwrite the parts, or create new versions of parts that already exist in the document?" ,
566+ "Save Options" , JOptionPane .DEFAULT_OPTION , JOptionPane .QUESTION_MESSAGE , null , options , options [3 ]);
567+ return selection ;
568+ }
569+
570+ private boolean saveOption (SBOLDocument doc , SBOLDocument currentDesign , ComponentDefinition currentRootCD ,
571+ int selection , File file ) throws SBOLValidationException , Exception , FileNotFoundException ,
572+ SBOLConversionException , IOException {
550573 switch (selection ) {
551574 case 0 : // canceled
552575 updateEnabledButtons (true );
@@ -570,7 +593,7 @@ boolean saveIntoExistingFile() throws Exception {
570593 throw new IllegalArgumentException ();
571594 }
572595
573- documentIO .write (doc );
596+ SBOLWriter .write (doc , file );
574597 updateEnabledButtons (false );
575598 return true ;
576599 }
0 commit comments