mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-26 22:41:59 -04:00
consistent handling of overwrite confirmation dialog.
This commit is contained in:
parent
0ff369fa68
commit
f8aef591d3
@ -1,7 +1,7 @@
|
||||
package de.neemann.digital.builder.ATF1502;
|
||||
|
||||
import de.neemann.digital.builder.ExpressionToFileExporter;
|
||||
import de.neemann.digital.gui.Main;
|
||||
import de.neemann.digital.gui.SaveAsHelper;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
@ -25,7 +25,7 @@ public class CreateCHN implements ExpressionToFileExporter.PostProcess {
|
||||
|
||||
@Override
|
||||
public File execute(File file) throws IOException {
|
||||
File chnFile = Main.checkSuffix(file, "chn");
|
||||
File chnFile = SaveAsHelper.checkSuffix(file, "chn");
|
||||
|
||||
try (Writer chn = new OutputStreamWriter(new FileOutputStream(chnFile), "UTF-8")) {
|
||||
chn.write("1 4 1 0 \r\n"
|
||||
|
@ -2,7 +2,7 @@ package de.neemann.digital.builder.tt2;
|
||||
|
||||
import de.neemann.digital.builder.ExpressionToFileExporter;
|
||||
import de.neemann.digital.core.element.Keys;
|
||||
import de.neemann.digital.gui.Main;
|
||||
import de.neemann.digital.gui.SaveAsHelper;
|
||||
import de.neemann.digital.gui.Settings;
|
||||
import de.neemann.digital.lang.Lang;
|
||||
|
||||
@ -56,7 +56,7 @@ public class StartATF1502Fitter implements ExpressionToFileExporter.PostProcess
|
||||
|
||||
SwingUtilities.invokeLater(() -> JOptionPane.showMessageDialog(parent, message, Lang.get("msg_fitterResult"), JOptionPane.INFORMATION_MESSAGE));
|
||||
|
||||
return Main.checkSuffix(file, "jed");
|
||||
return SaveAsHelper.checkSuffix(file, "jed");
|
||||
} catch (IOException e) {
|
||||
throw new IOException(Lang.get("err_errorRunningFitter"), e);
|
||||
}
|
||||
|
@ -409,46 +409,28 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave, E
|
||||
if (lastFilename == null && library.getRootFilePath() != null)
|
||||
fc.setCurrentDirectory(library.getRootFilePath());
|
||||
|
||||
boolean repeat;
|
||||
do {
|
||||
repeat = false;
|
||||
if (fc.showSaveDialog(Main.this) == JFileChooser.APPROVE_OPTION) {
|
||||
|
||||
final File selectedFile = fc.getSelectedFile();
|
||||
|
||||
if (selectedFile.exists()) {
|
||||
Object[] options = {Lang.get("btn_overwrite"), Lang.get("btn_newName")};
|
||||
int res = JOptionPane.showOptionDialog(Main.this,
|
||||
Lang.get("msg_fileExists", selectedFile.getName()),
|
||||
Lang.get("msg_warning"),
|
||||
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
|
||||
null, options, options[0]);
|
||||
if (res == 1) {
|
||||
repeat = true;
|
||||
continue;
|
||||
final SaveAsHelper saveAsHelper = new SaveAsHelper(Main.this, fc, "dig");
|
||||
saveAsHelper.checkOverwrite(
|
||||
file -> {
|
||||
if (library.isFileAccessible(file))
|
||||
saveFile(file, false);
|
||||
else {
|
||||
Object[] options = {Lang.get("btn_saveAnyway"), Lang.get("btn_newName"), Lang.get("cancel")};
|
||||
int res = JOptionPane.showOptionDialog(Main.this,
|
||||
Lang.get("msg_fileNotAccessible"),
|
||||
Lang.get("msg_warning"),
|
||||
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
|
||||
null, options, options[0]);
|
||||
switch (res) {
|
||||
case 0:
|
||||
saveFile(file, true);
|
||||
break;
|
||||
case 1:
|
||||
saveAsHelper.retryFileSelect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (library.isFileAccessible(selectedFile))
|
||||
saveFile(selectedFile, false);
|
||||
else {
|
||||
Object[] options = {Lang.get("btn_saveAnyway"), Lang.get("btn_newName"), Lang.get("cancel")};
|
||||
int res = JOptionPane.showOptionDialog(Main.this,
|
||||
Lang.get("msg_fileNotAccessible"),
|
||||
Lang.get("msg_warning"),
|
||||
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
|
||||
null, options, options[0]);
|
||||
switch (res) {
|
||||
case 0:
|
||||
saveFile(selectedFile, true);
|
||||
break;
|
||||
case 1:
|
||||
repeat = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (repeat);
|
||||
);
|
||||
}
|
||||
}.setActive(allowAll);
|
||||
|
||||
@ -969,7 +951,6 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave, E
|
||||
}
|
||||
|
||||
private void saveFile(File filename, boolean toPrefs) {
|
||||
filename = checkSuffix(filename, "dig");
|
||||
try {
|
||||
circuitComponent.getCircuit().save(filename);
|
||||
stoppedState.enter();
|
||||
@ -996,21 +977,6 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave, E
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the given suffix to the file
|
||||
*
|
||||
* @param filename filename
|
||||
* @param suffix suffix
|
||||
* @return the file name with the given suffix
|
||||
*/
|
||||
public static File checkSuffix(File filename, String suffix) {
|
||||
String name = filename.getName();
|
||||
int p = name.lastIndexOf('.');
|
||||
if (p >= 0)
|
||||
name = name.substring(0, p);
|
||||
return new File(filename.getParentFile(), name + "." + suffix);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the window position manager
|
||||
*/
|
||||
@ -1076,23 +1042,20 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave, E
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
JFileChooser fc = new JFileChooser();
|
||||
if (filename != null)
|
||||
fc.setSelectedFile(checkSuffix(filename, suffix));
|
||||
fc.setSelectedFile(SaveAsHelper.checkSuffix(filename, suffix));
|
||||
|
||||
if (lastExportDirectory != null)
|
||||
fc.setCurrentDirectory(lastExportDirectory);
|
||||
|
||||
fc.addChoosableFileFilter(new FileNameExtensionFilter(name, suffix));
|
||||
if (fc.showSaveDialog(Main.this) == JFileChooser.APPROVE_OPTION) {
|
||||
|
||||
lastExportDirectory = fc.getSelectedFile().getParentFile();
|
||||
|
||||
try (OutputStream out = new FileOutputStream(checkSuffix(fc.getSelectedFile(), suffix))) {
|
||||
new Export(circuitComponent.getCircuit(), exportFactory).export(out);
|
||||
|
||||
} catch (IOException e1) {
|
||||
new ErrorMessage(Lang.get("msg_errorWritingFile")).addCause(e1).show(Main.this);
|
||||
}
|
||||
}
|
||||
new SaveAsHelper(Main.this, fc, suffix).checkOverwrite(
|
||||
file -> {
|
||||
lastExportDirectory = file.getParentFile();
|
||||
try (OutputStream out = new FileOutputStream(file)) {
|
||||
new Export(circuitComponent.getCircuit(), exportFactory).export(out);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
121
src/main/java/de/neemann/digital/gui/SaveAsHelper.java
Normal file
121
src/main/java/de/neemann/digital/gui/SaveAsHelper.java
Normal file
@ -0,0 +1,121 @@
|
||||
package de.neemann.digital.gui;
|
||||
|
||||
import de.neemann.digital.lang.Lang;
|
||||
import de.neemann.gui.ErrorMessage;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Helper to handle the overwrite conformation
|
||||
* <p>
|
||||
* Created by hneemann on 04.04.17.
|
||||
*/
|
||||
public final class SaveAsHelper {
|
||||
private final Component parent;
|
||||
private final JFileChooser fc;
|
||||
private final String suffix;
|
||||
private boolean repeat;
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
*
|
||||
* @param parent the parent
|
||||
* @param fc the file chooser
|
||||
*/
|
||||
public SaveAsHelper(Component parent, JFileChooser fc) {
|
||||
this(parent, fc, null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
*
|
||||
* @param parent the parent
|
||||
* @param fc the file chooser
|
||||
* @param suffix the suffix to enforce
|
||||
*/
|
||||
public SaveAsHelper(Component parent, JFileChooser fc, String suffix) {
|
||||
this.parent = parent;
|
||||
this.fc = fc;
|
||||
this.suffix = suffix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses the JFileChooser to select a file and checks, if the file exists.
|
||||
* Uses the gicen interface to save the file.
|
||||
*
|
||||
* @param saveAs used to save the file
|
||||
*/
|
||||
public void checkOverwrite(SaveAs saveAs) {
|
||||
do {
|
||||
repeat = false;
|
||||
if (fc.showSaveDialog(parent) == JFileChooser.APPROVE_OPTION) {
|
||||
|
||||
final File selectedFile = checkSuffix(fc.getSelectedFile(), suffix);
|
||||
|
||||
if (selectedFile.exists()) {
|
||||
Object[] options = {Lang.get("btn_overwrite"), Lang.get("btn_newName")};
|
||||
int res = JOptionPane.showOptionDialog(parent,
|
||||
Lang.get("msg_fileExists", selectedFile.getName()),
|
||||
Lang.get("msg_warning"),
|
||||
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
|
||||
null, options, options[0]);
|
||||
if (res == 1) {
|
||||
repeat = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
saveAs.saveAs(selectedFile);
|
||||
} catch (IOException e) {
|
||||
new ErrorMessage(Lang.get("msg_errorWritingFile")).addCause(e).show(parent);
|
||||
}
|
||||
}
|
||||
} while (repeat);
|
||||
}
|
||||
|
||||
/**
|
||||
* if called user can select an other name
|
||||
*/
|
||||
public void retryFileSelect() {
|
||||
repeat = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the given suffix to the file
|
||||
*
|
||||
* @param filename filename
|
||||
* @param suffix suffix
|
||||
* @return the file name with the given suffix
|
||||
*/
|
||||
public static File checkSuffix(File filename, String suffix) {
|
||||
if (suffix == null)
|
||||
return filename;
|
||||
|
||||
String name = filename.getName();
|
||||
int p = name.lastIndexOf('.');
|
||||
if (p >= 0)
|
||||
name = name.substring(0, p);
|
||||
return new File(filename.getParentFile(), name + "." + suffix);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Used to encapsulate the save action
|
||||
*/
|
||||
public interface SaveAs {
|
||||
/**
|
||||
* Interface to implement the save operation
|
||||
*
|
||||
* @param file the file to write
|
||||
* @throws IOException IOException
|
||||
*/
|
||||
void saveAs(File file) throws IOException;
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -8,6 +8,7 @@ import de.neemann.digital.core.element.Rotation;
|
||||
import de.neemann.digital.core.io.IntFormat;
|
||||
import de.neemann.digital.core.memory.DataField;
|
||||
import de.neemann.digital.core.memory.ROM;
|
||||
import de.neemann.digital.gui.SaveAsHelper;
|
||||
import de.neemann.digital.gui.components.testing.TestDataEditor;
|
||||
import de.neemann.digital.gui.sync.NoSync;
|
||||
import de.neemann.digital.lang.Lang;
|
||||
@ -355,14 +356,12 @@ public final class EditorFactory {
|
||||
JFileChooser fc = new JFileChooser();
|
||||
fc.setSelectedFile(attr.getFile(ROM.LAST_DATA_FILE_KEY));
|
||||
fc.setFileFilter(new FileNameExtensionFilter("hex", "hex"));
|
||||
if (fc.showSaveDialog(panel) == JFileChooser.APPROVE_OPTION) {
|
||||
attr.setFile(ROM.LAST_DATA_FILE_KEY, fc.getSelectedFile());
|
||||
try {
|
||||
data.saveTo(fc.getSelectedFile());
|
||||
} catch (IOException e1) {
|
||||
new ErrorMessage(Lang.get("msg_errorWritingFile")).addCause(e1).show(panel);
|
||||
}
|
||||
}
|
||||
new SaveAsHelper(panel, fc, "hex").checkOverwrite(
|
||||
file -> {
|
||||
attr.setFile(ROM.LAST_DATA_FILE_KEY, file);
|
||||
data.saveTo(file);
|
||||
}
|
||||
);
|
||||
}
|
||||
}.createJButton());
|
||||
return panel;
|
||||
|
@ -4,10 +4,10 @@ import de.neemann.digital.core.Model;
|
||||
import de.neemann.digital.core.ModelEvent;
|
||||
import de.neemann.digital.core.ModelStateObserver;
|
||||
import de.neemann.digital.core.Signal;
|
||||
import de.neemann.digital.gui.SaveAsHelper;
|
||||
import de.neemann.digital.gui.components.OrderMerger;
|
||||
import de.neemann.digital.gui.sync.Sync;
|
||||
import de.neemann.digital.lang.Lang;
|
||||
import de.neemann.gui.ErrorMessage;
|
||||
import de.neemann.gui.ToolTipAction;
|
||||
|
||||
import javax.swing.*;
|
||||
@ -16,8 +16,6 @@ import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@ -86,16 +84,8 @@ public class DataSetDialog extends JDialog implements ModelStateObserver {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
JFileChooser fileChooser = new JFileChooser();
|
||||
fileChooser.setFileFilter(new FileNameExtensionFilter("Comma Separated Values", "csv"));
|
||||
if (fileChooser.showSaveDialog(DataSetDialog.this) == JFileChooser.APPROVE_OPTION) {
|
||||
File file = fileChooser.getSelectedFile();
|
||||
if (!file.getName().endsWith(".csv"))
|
||||
file = new File(file.getParentFile(), file.getName() + ".csv");
|
||||
try {
|
||||
dataSet.saveCSV(file);
|
||||
} catch (IOException e1) {
|
||||
new ErrorMessage(Lang.get("msg_errorSavingData")).addCause(e1).show(DataSetDialog.this);
|
||||
}
|
||||
}
|
||||
new SaveAsHelper(DataSetDialog.this, fileChooser, "csv")
|
||||
.checkOverwrite(file -> dataSet.saveCSV(file));
|
||||
}
|
||||
}.setToolTip(Lang.get("menu_saveData_tt")).createJMenuItem());
|
||||
setJMenuBar(bar);
|
||||
|
@ -33,6 +33,7 @@ import de.neemann.digital.draw.elements.Circuit;
|
||||
import de.neemann.digital.draw.library.ElementLibrary;
|
||||
import de.neemann.digital.draw.shapes.ShapeFactory;
|
||||
import de.neemann.digital.gui.Main;
|
||||
import de.neemann.digital.gui.SaveAsHelper;
|
||||
import de.neemann.digital.gui.components.AttributeDialog;
|
||||
import de.neemann.digital.gui.components.ElementOrderer;
|
||||
import de.neemann.digital.lang.Lang;
|
||||
@ -240,10 +241,10 @@ public class TableDialog extends JDialog {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
JFileChooser fc = new JFileChooser();
|
||||
if (TableDialog.this.filename != null)
|
||||
fc.setSelectedFile(Main.checkSuffix(TableDialog.this.filename, "tru"));
|
||||
fc.setSelectedFile(SaveAsHelper.checkSuffix(TableDialog.this.filename, "tru"));
|
||||
if (fc.showOpenDialog(TableDialog.this) == JFileChooser.APPROVE_OPTION) {
|
||||
try {
|
||||
File file = Main.checkSuffix(fc.getSelectedFile(), "tru");
|
||||
File file = fc.getSelectedFile();
|
||||
TruthTable truthTable = TruthTable.readFromFile(file);
|
||||
setModel(new TruthTableTableModel(truthTable));
|
||||
TableDialog.this.filename = file;
|
||||
@ -259,36 +260,14 @@ public class TableDialog extends JDialog {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
JFileChooser fc = new JFileChooser();
|
||||
if (TableDialog.this.filename != null)
|
||||
fc.setSelectedFile(Main.checkSuffix(TableDialog.this.filename, "tru"));
|
||||
fc.setSelectedFile(SaveAsHelper.checkSuffix(TableDialog.this.filename, "tru"));
|
||||
|
||||
boolean repeat;
|
||||
do {
|
||||
repeat = false;
|
||||
if (fc.showSaveDialog(TableDialog.this) == JFileChooser.APPROVE_OPTION) {
|
||||
final File selectedFile = fc.getSelectedFile();
|
||||
|
||||
if (selectedFile.exists()) {
|
||||
Object[] options = {Lang.get("btn_overwrite"), Lang.get("btn_newName")};
|
||||
int res = JOptionPane.showOptionDialog(TableDialog.this,
|
||||
Lang.get("msg_fileExists", selectedFile.getName()),
|
||||
Lang.get("msg_warning"),
|
||||
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
|
||||
null, options, options[0]);
|
||||
if (res == 1) {
|
||||
repeat = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
File file = Main.checkSuffix(selectedFile, "tru");
|
||||
new SaveAsHelper(TableDialog.this, fc, "tru").checkOverwrite(
|
||||
file -> {
|
||||
model.getTable().save(file);
|
||||
TableDialog.this.filename = file;
|
||||
} catch (IOException e1) {
|
||||
new ErrorMessage().addCause(e1).show(TableDialog.this);
|
||||
}
|
||||
}
|
||||
} while (repeat);
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
@ -312,15 +291,9 @@ public class TableDialog extends JDialog {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
JFileChooser fc = new JFileChooser();
|
||||
if (TableDialog.this.filename != null)
|
||||
fc.setSelectedFile(Main.checkSuffix(TableDialog.this.filename, "hex"));
|
||||
if (fc.showSaveDialog(TableDialog.this) == JFileChooser.APPROVE_OPTION) {
|
||||
try {
|
||||
File file = Main.checkSuffix(fc.getSelectedFile(), "hex");
|
||||
model.getTable().saveHex(file);
|
||||
} catch (IOException e1) {
|
||||
new ErrorMessage().addCause(e1).show(TableDialog.this);
|
||||
}
|
||||
}
|
||||
fc.setSelectedFile(SaveAsHelper.checkSuffix(TableDialog.this.filename, "hex"));
|
||||
new SaveAsHelper(TableDialog.this, fc, "hex")
|
||||
.checkOverwrite(file -> model.getTable().saveHex(file));
|
||||
}
|
||||
}.setToolTip(Lang.get("menu_table_exportHex_tt")).createJMenuItem());
|
||||
|
||||
@ -512,7 +485,7 @@ public class TableDialog extends JDialog {
|
||||
if (filename == null)
|
||||
filename = new File("circuit." + suffix);
|
||||
else
|
||||
filename = Main.checkSuffix(filename, suffix);
|
||||
filename = SaveAsHelper.checkSuffix(filename, suffix);
|
||||
|
||||
JFileChooser fileChooser = new JFileChooser();
|
||||
fileChooser.setFileFilter(new FileNameExtensionFilter("JEDEC", suffix));
|
||||
@ -521,7 +494,7 @@ public class TableDialog extends JDialog {
|
||||
try {
|
||||
expressionExporter.getPinMapping().addAll(model.getTable().getPins());
|
||||
new BuilderExpressionCreator(expressionExporter.getBuilder(), ExpressionModifier.IDENTITY).create(lastGeneratedExpressions);
|
||||
expressionExporter.export(Main.checkSuffix(fileChooser.getSelectedFile(), suffix));
|
||||
expressionExporter.export(SaveAsHelper.checkSuffix(fileChooser.getSelectedFile(), suffix));
|
||||
} catch (ExpressionException | FormatterException | IOException | FuseMapFillerException | PinMapException e) {
|
||||
new ErrorMessage(Lang.get("msg_errorDuringCalculation")).addCause(e).show(this);
|
||||
}
|
||||
|
@ -729,7 +729,6 @@ Die Icons stammen aus dem Tango Desktop Project.</string>
|
||||
<string name="msg_test_N_Passed">{0}: ok</string>
|
||||
<string name="msg_test_N_Failed">{0}: Fehler</string>
|
||||
<string name="msg_testExp_N0_found_N1">E: {0} / F: {1}</string>
|
||||
<string name="msg_errorSavingData">Speichern der Daten fehlgeschlagen!</string>
|
||||
<string name="msg_creatingHelp">Fehler bei der Erzeugung der Hilfe!</string>
|
||||
<string name="msg_clipboardContainsNoImportableData">In der Zwischenablage befinden sich keine importierbaren Daten!</string>
|
||||
<string name="msg_selectAnEmptyFolder">Wählen Sie einen leeren Ordner aus!</string>
|
||||
|
@ -716,7 +716,6 @@ The icons are taken from the Tango Desktop Project.</string>
|
||||
<string name="msg_test_N_Passed">{0} passed</string>
|
||||
<string name="msg_test_N_Failed">{0} failed</string>
|
||||
<string name="msg_testExp_N0_found_N1">E: {0} / F: {1}</string>
|
||||
<string name="msg_errorSavingData">Error writing the data!</string>
|
||||
<string name="msg_creatingHelp">Error creating the help!</string>
|
||||
<string name="msg_clipboardContainsNoImportableData">The clipboard contains no importable data!</string>
|
||||
<string name="msg_selectAnEmptyFolder">Select an empty folder!</string>
|
||||
|
Loading…
x
Reference in New Issue
Block a user