Skip to content

Commit 17087db

Browse files
committed
Save as now gives same options prompt when saving into existing file.
1 parent 65b67c6 commit 17087db

3 files changed

Lines changed: 39 additions & 16 deletions

File tree

src/main/java/edu/utah/ece/async/sboldesigner/sbol/SBOLUtils.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,6 @@ public static File selectFile(java.awt.Component parent, JFileChooser fc) {
286286
}
287287

288288
File file = fc.getSelectedFile();
289-
if (file.exists()) {
290-
JOptionPane.showMessageDialog(parent, "You cannot select this file, it already exists.");
291-
return null;
292-
}
293289

294290
Preferences.userRoot().node("path").put("path", file.getPath());
295291

src/main/java/edu/utah/ece/async/sboldesigner/sbol/editor/SBOLDesign.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,10 @@ private void expandCombinatorial() throws SBOLValidationException, SBOLConversio
13761376
if (file == null) {
13771377
return;
13781378
}
1379+
if (file.exists()) {
1380+
JOptionPane.showMessageDialog(panel, "This file already exists.");
1381+
return;
1382+
}
13791383

13801384
doc = CombinatorialExpansionUtil.createCombinatorialDesign(doc);
13811385

src/main/java/edu/utah/ece/async/sboldesigner/sbol/editor/SBOLDesignerPanel.java

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.sbolstandard.core2.ComponentDefinition;
4444
import org.sbolstandard.core2.SBOLConversionException;
4545
import org.sbolstandard.core2.SBOLDocument;
46+
import org.sbolstandard.core2.SBOLReader;
4647
import org.sbolstandard.core2.SBOLValidationException;
4748
import org.sbolstandard.core2.SBOLWriter;
4849
import 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

Comments
 (0)