92 lines
2.9 KiB
Java
92 lines
2.9 KiB
Java
package com.bytezone.diskbrowser.gui;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.nio.file.FileAlreadyExistsException;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.StandardOpenOption;
|
|
|
|
import javax.swing.JFileChooser;
|
|
import javax.swing.JOptionPane;
|
|
|
|
import com.bytezone.diskbrowser.utilities.DefaultAction;
|
|
|
|
// -----------------------------------------------------------------------------------//
|
|
public abstract class AbstractSaveAction extends DefaultAction
|
|
// -----------------------------------------------------------------------------------//
|
|
{
|
|
JFileChooser fileChooser;
|
|
String title;
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
public AbstractSaveAction (String text, String tip, String title)
|
|
// ---------------------------------------------------------------------------------//
|
|
{
|
|
super (text, tip);
|
|
this.title = title;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
void setSelectedFile (File file)
|
|
// ---------------------------------------------------------------------------------//
|
|
{
|
|
if (fileChooser == null)
|
|
{
|
|
fileChooser = new JFileChooser ();
|
|
fileChooser.setDialogTitle (title);
|
|
}
|
|
|
|
fileChooser.setSelectedFile (file);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
void saveBuffer (byte[] buffer)
|
|
// ---------------------------------------------------------------------------------//
|
|
{
|
|
if (fileChooser.showSaveDialog (null) == JFileChooser.APPROVE_OPTION)
|
|
{
|
|
File file = fileChooser.getSelectedFile ();
|
|
try
|
|
{
|
|
Files.write (file.toPath (), buffer, StandardOpenOption.CREATE_NEW);
|
|
JOptionPane.showMessageDialog (null,
|
|
String.format ("File %s saved", file.getName ()));
|
|
}
|
|
catch (FileAlreadyExistsException e)
|
|
{
|
|
JOptionPane.showMessageDialog (null,
|
|
"File " + file.getName () + " already exists", "Failed",
|
|
JOptionPane.ERROR_MESSAGE);
|
|
}
|
|
catch (IOException e)
|
|
{
|
|
e.printStackTrace ();
|
|
JOptionPane.showMessageDialog (null, "File failed to save - " + e.getMessage (),
|
|
"Failed", JOptionPane.ERROR_MESSAGE);
|
|
}
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
void saveFile (Path sourcePath)
|
|
// ---------------------------------------------------------------------------------//
|
|
{
|
|
if (fileChooser.showSaveDialog (null) == JFileChooser.APPROVE_OPTION)
|
|
{
|
|
File file = fileChooser.getSelectedFile ();
|
|
try
|
|
{
|
|
Files.copy (sourcePath, file.toPath ());
|
|
JOptionPane.showMessageDialog (null,
|
|
String.format ("File %s saved", file.getName ()));
|
|
}
|
|
catch (IOException e)
|
|
{
|
|
e.printStackTrace ();
|
|
JOptionPane.showMessageDialog (null, "File failed to save");
|
|
}
|
|
}
|
|
}
|
|
}
|