dmolony-DiskBrowser/src/com/bytezone/diskbrowser/gui/AbstractSaveAction.java

59 lines
2.0 KiB
Java
Raw Normal View History

2021-05-19 09:28:04 +00:00
package com.bytezone.diskbrowser.gui;
2021-05-20 10:13:06 +00:00
import java.io.File;
import java.io.IOException;
2021-05-21 03:34:33 +00:00
import java.nio.file.FileAlreadyExistsException;
2021-05-20 10:13:06 +00:00
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
2021-05-19 09:28:04 +00:00
import javax.swing.JFileChooser;
2021-05-20 10:13:06 +00:00
import javax.swing.JOptionPane;
2021-05-19 09:28:04 +00:00
import com.bytezone.diskbrowser.utilities.DefaultAction;
2021-05-20 02:44:40 +00:00
// -----------------------------------------------------------------------------------//
2021-05-19 09:28:04 +00:00
public abstract class AbstractSaveAction extends DefaultAction
2021-05-20 02:44:40 +00:00
// -----------------------------------------------------------------------------------//
2021-05-19 09:28:04 +00:00
{
protected JFileChooser fileChooser = new JFileChooser ();
2021-05-19 09:28:04 +00:00
2021-05-20 02:44:40 +00:00
// ---------------------------------------------------------------------------------//
2021-05-21 04:36:44 +00:00
public AbstractSaveAction (String menuText, String tip, String dialogTitle)
2021-05-20 02:44:40 +00:00
// ---------------------------------------------------------------------------------//
2021-05-19 09:28:04 +00:00
{
2021-05-21 04:36:44 +00:00
super (menuText, tip);
fileChooser.setDialogTitle (dialogTitle);
2021-05-20 10:13:06 +00:00
}
// ---------------------------------------------------------------------------------//
void setSelectedFile (File file)
// ---------------------------------------------------------------------------------//
{
fileChooser.setSelectedFile (file);
}
// ---------------------------------------------------------------------------------//
void saveBuffer (byte[] buffer)
// ---------------------------------------------------------------------------------//
{
2021-05-21 23:59:06 +00:00
File file = fileChooser.getSelectedFile ();
try
{
Files.write (file.toPath (), buffer, StandardOpenOption.CREATE_NEW);
JOptionPane.showMessageDialog (null, String.format ("File %s saved", file.getName ()));
2021-05-21 23:59:06 +00:00
}
catch (FileAlreadyExistsException e)
2021-05-20 10:13:06 +00:00
{
JOptionPane.showMessageDialog (null, "File " + file.getName () + " already exists", "Failed",
JOptionPane.ERROR_MESSAGE);
2021-05-21 23:59:06 +00:00
}
catch (IOException e)
{
e.printStackTrace ();
JOptionPane.showMessageDialog (null, "File failed to save - " + e.getMessage (), "Failed",
JOptionPane.ERROR_MESSAGE);
2021-05-20 10:13:06 +00:00
}
2021-05-19 09:28:04 +00:00
}
}