refactoring

This commit is contained in:
Denis Molony 2021-05-20 20:13:06 +10:00
parent 5ea96a260d
commit 13647213c6
4 changed files with 98 additions and 89 deletions

View File

@ -1,6 +1,13 @@
package com.bytezone.diskbrowser.gui;
import java.io.File;
import java.io.IOException;
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;
@ -9,12 +16,68 @@ public abstract class AbstractSaveAction extends DefaultAction
// -----------------------------------------------------------------------------------//
{
JFileChooser fileChooser;
String title;
// ---------------------------------------------------------------------------------//
public AbstractSaveAction (String text, String tip)
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 (IOException e)
{
e.printStackTrace ();
JOptionPane.showMessageDialog (null, "File failed to save");
}
}
}
// ---------------------------------------------------------------------------------//
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");
}
}
}
}

View File

@ -2,25 +2,23 @@ package com.bytezone.diskbrowser.gui;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import com.bytezone.diskbrowser.disk.Disk;
import com.bytezone.diskbrowser.disk.FormattedDisk;
// -----------------------------------------------------------------------------------//
class SaveDiskAction extends AbstractSaveAction implements DiskSelectionListener
// -----------------------------------------------------------------------------------//
{
FormattedDisk disk;
FormattedDisk formattedDisk;
// ---------------------------------------------------------------------------------//
SaveDiskAction ()
// ---------------------------------------------------------------------------------//
{
super ("Save converted disk...", "Save converted disk");
super ("Save converted disk...", "Save converted disk", "Save converted disk");
}
// ---------------------------------------------------------------------------------//
@ -28,35 +26,18 @@ class SaveDiskAction extends AbstractSaveAction implements DiskSelectionListener
public void actionPerformed (ActionEvent evt)
// ---------------------------------------------------------------------------------//
{
if (disk == null)
if (formattedDisk == null)
{
JOptionPane.showMessageDialog (null, "No disk selected");
return;
}
if (fileChooser == null)
{
fileChooser = new JFileChooser ();
fileChooser.setDialogTitle ("Save converted disk");
}
Disk disk = formattedDisk.getDisk ();
int blocks = disk.getTotalBlocks ();
String suffix = blocks <= 560 ? ".dsk" : ".hdv";
fileChooser.setSelectedFile (new File (disk.getName () + ".dsk"));
if (fileChooser.showSaveDialog (null) == JFileChooser.APPROVE_OPTION)
{
File file = fileChooser.getSelectedFile ();
try
{
Files.copy (disk.getDisk ().getFile ().toPath (), file.toPath ());
JOptionPane.showMessageDialog (null,
String.format ("File %s saved", file.getName ()));
}
catch (IOException e)
{
e.printStackTrace ();
JOptionPane.showMessageDialog (null, "Disk failed to save");
}
}
setSelectedFile (new File (formattedDisk.getName () + suffix));
saveFile (disk.getFile ().toPath ());
}
// ---------------------------------------------------------------------------------//
@ -64,7 +45,7 @@ class SaveDiskAction extends AbstractSaveAction implements DiskSelectionListener
public void diskSelected (DiskSelectedEvent event)
// ---------------------------------------------------------------------------------//
{
this.disk = event.getFormattedDisk ();
setEnabled (disk != null && disk.isTempDisk ());
formattedDisk = event.getFormattedDisk ();
setEnabled (formattedDisk != null && formattedDisk.isTempDisk ());
}
}

View File

@ -2,11 +2,7 @@ package com.bytezone.diskbrowser.gui;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import com.bytezone.diskbrowser.applefile.AppleFileSource;
@ -21,7 +17,7 @@ class SaveFileAction extends AbstractSaveAction implements FileSelectionListener
SaveFileAction ()
// ---------------------------------------------------------------------------------//
{
super ("Save file...", "Save currently selected file");
super ("Save file...", "Save currently selected file", "Save File");
}
// ---------------------------------------------------------------------------------//
@ -35,30 +31,8 @@ class SaveFileAction extends AbstractSaveAction implements FileSelectionListener
return;
}
if (fileChooser == null)
{
fileChooser = new JFileChooser ();
fileChooser.setDialogTitle ("Save File");
}
fileChooser.setSelectedFile (new File (appleFileSource.getUniqueName () + ".bin"));
if (fileChooser.showSaveDialog (null) == JFileChooser.APPROVE_OPTION)
{
File file = fileChooser.getSelectedFile ();
try
{
Files.write (file.toPath (), appleFileSource.getDataSource ().getBuffer (),
StandardOpenOption.CREATE_NEW);
JOptionPane.showMessageDialog (null,
String.format ("File %s saved", file.getName ()));
}
catch (IOException e)
{
e.printStackTrace ();
JOptionPane.showMessageDialog (null, "File failed to save");
}
}
setSelectedFile (new File (appleFileSource.getUniqueName () + ".bin"));
saveBuffer (appleFileSource.getDataSource ().getBuffer ());
}
// ---------------------------------------------------------------------------------//

View File

@ -2,11 +2,7 @@ package com.bytezone.diskbrowser.gui;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
// -----------------------------------------------------------------------------------//
@ -19,7 +15,7 @@ class SaveSectorsAction extends AbstractSaveAction implements SectorSelectionLis
SaveSectorsAction ()
// ---------------------------------------------------------------------------------//
{
super ("Save sectors...", "Save currently selected sectors");
super ("Save sectors...", "Save currently selected sectors", "Save sectors");
this.setEnabled (false);
}
@ -34,31 +30,26 @@ class SaveSectorsAction extends AbstractSaveAction implements SectorSelectionLis
return;
}
if (fileChooser == null)
{
fileChooser = new JFileChooser ();
fileChooser.setDialogTitle ("Save sectors");
}
setSelectedFile (new File ("savedSectors.bin"));
saveBuffer (event.getFormattedDisk ().getDisk ().readBlocks (event.getSectors ()));
fileChooser.setSelectedFile (new File ("savedSectors.bin"));
if (fileChooser.showSaveDialog (null) == JFileChooser.APPROVE_OPTION)
{
File file = fileChooser.getSelectedFile ();
try
{
byte[] buffer =
event.getFormattedDisk ().getDisk ().readBlocks (event.getSectors ());
Files.write (file.toPath (), buffer, StandardOpenOption.CREATE_NEW);
JOptionPane.showMessageDialog (null,
String.format ("File %s saved", file.getName ()));
}
catch (IOException e)
{
e.printStackTrace ();
JOptionPane.showMessageDialog (null, "File failed to save");
}
}
// if (fileChooser.showSaveDialog (null) == JFileChooser.APPROVE_OPTION)
// {
// File file = fileChooser.getSelectedFile ();
// try
// {
// byte[] buffer =
// event.getFormattedDisk ().getDisk ().readBlocks (event.getSectors ());
// Files.write (file.toPath (), buffer, StandardOpenOption.CREATE_NEW);
// JOptionPane.showMessageDialog (null,
// String.format ("File %s saved", file.getName ()));
// }
// catch (IOException e)
// {
// e.printStackTrace ();
// JOptionPane.showMessageDialog (null, "File failed to save");
// }
// }
}
// ---------------------------------------------------------------------------------//