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; 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.JFileChooser;
import javax.swing.JOptionPane;
import com.bytezone.diskbrowser.utilities.DefaultAction; import com.bytezone.diskbrowser.utilities.DefaultAction;
@ -9,12 +16,68 @@ public abstract class AbstractSaveAction extends DefaultAction
// -----------------------------------------------------------------------------------// // -----------------------------------------------------------------------------------//
{ {
JFileChooser fileChooser; JFileChooser fileChooser;
String title;
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
public AbstractSaveAction (String text, String tip) public AbstractSaveAction (String text, String tip, String title)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
super (text, tip); 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.awt.event.ActionEvent;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import com.bytezone.diskbrowser.disk.Disk;
import com.bytezone.diskbrowser.disk.FormattedDisk; import com.bytezone.diskbrowser.disk.FormattedDisk;
// -----------------------------------------------------------------------------------// // -----------------------------------------------------------------------------------//
class SaveDiskAction extends AbstractSaveAction implements DiskSelectionListener class SaveDiskAction extends AbstractSaveAction implements DiskSelectionListener
// -----------------------------------------------------------------------------------// // -----------------------------------------------------------------------------------//
{ {
FormattedDisk disk; FormattedDisk formattedDisk;
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
SaveDiskAction () 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) public void actionPerformed (ActionEvent evt)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
if (disk == null) if (formattedDisk == null)
{ {
JOptionPane.showMessageDialog (null, "No disk selected"); JOptionPane.showMessageDialog (null, "No disk selected");
return; return;
} }
if (fileChooser == null) Disk disk = formattedDisk.getDisk ();
{ int blocks = disk.getTotalBlocks ();
fileChooser = new JFileChooser (); String suffix = blocks <= 560 ? ".dsk" : ".hdv";
fileChooser.setDialogTitle ("Save converted disk");
}
fileChooser.setSelectedFile (new File (disk.getName () + ".dsk")); setSelectedFile (new File (formattedDisk.getName () + suffix));
saveFile (disk.getFile ().toPath ());
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");
}
}
} }
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
@ -64,7 +45,7 @@ class SaveDiskAction extends AbstractSaveAction implements DiskSelectionListener
public void diskSelected (DiskSelectedEvent event) public void diskSelected (DiskSelectedEvent event)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
this.disk = event.getFormattedDisk (); formattedDisk = event.getFormattedDisk ();
setEnabled (disk != null && disk.isTempDisk ()); setEnabled (formattedDisk != null && formattedDisk.isTempDisk ());
} }
} }

View File

@ -2,11 +2,7 @@ package com.bytezone.diskbrowser.gui;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.io.File; 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 javax.swing.JOptionPane;
import com.bytezone.diskbrowser.applefile.AppleFileSource; import com.bytezone.diskbrowser.applefile.AppleFileSource;
@ -21,7 +17,7 @@ class SaveFileAction extends AbstractSaveAction implements FileSelectionListener
SaveFileAction () 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; return;
} }
if (fileChooser == null) setSelectedFile (new File (appleFileSource.getUniqueName () + ".bin"));
{ saveBuffer (appleFileSource.getDataSource ().getBuffer ());
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");
}
}
} }
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//

View File

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