Refactoring

This commit is contained in:
Denis Molony 2016-12-13 21:03:15 +11:00
parent dd5432c788
commit 973c06d7ac
7 changed files with 140 additions and 185 deletions

View File

@ -1,36 +0,0 @@
package com.bytezone.diskbrowser.duplicates;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JCheckBox;
class CheckBoxActionListener implements ActionListener
{
DiskDetails diskDetails;
List<DiskDetails> disksSelected;
JButton deleteButton;
JButton clearButton;
public CheckBoxActionListener (DiskDetails diskDetails, List<DiskDetails> disksSelected,
JButton deleteButton, JButton clearButton)
{
this.diskDetails = diskDetails;
this.disksSelected = disksSelected;
this.deleteButton = deleteButton;
this.clearButton = clearButton;
}
@Override
public void actionPerformed (ActionEvent e)
{
if (((JCheckBox) e.getSource ()).isSelected ())
disksSelected.add (diskDetails);
else
disksSelected.remove (diskDetails);
deleteButton.setEnabled (disksSelected.size () > 0);
clearButton.setEnabled (disksSelected.size () > 0);
}
}

View File

@ -7,7 +7,7 @@ import javax.swing.SwingWorker;
import com.bytezone.diskbrowser.utilities.Utility;
public class DuplicateSwingWorker extends SwingWorker<Void, ProgressState>
public class DuplicateSwingWorker extends SwingWorker<Void, RootFolderData>
{
private final int rootFolderNameLength;
private final RootFolderData rootFolderData;
@ -15,7 +15,7 @@ public class DuplicateSwingWorker extends SwingWorker<Void, ProgressState>
public DuplicateSwingWorker (RootFolderData rootFolderData)
{
this.rootFolderData = rootFolderData;
rootFolderNameLength = rootFolderData.rootFolder.getAbsolutePath ().length ();
rootFolderNameLength = rootFolderData.getRootFolder ().getAbsolutePath ().length ();
rootFolderData.dialog.setLocationRelativeTo (null);
rootFolderData.dialog.setVisible (true);
@ -37,16 +37,16 @@ public class DuplicateSwingWorker extends SwingWorker<Void, ProgressState>
if (file.isDirectory ())
{
rootFolderData.progressState.incrementFolders ();
rootFolderData.incrementFolders ();
traverse (file);
}
else if (Utility.validFileType (fileName) && file.length () > 0)
{
rootFolderData.progressState.incrementType (file, fileName);
rootFolderData.incrementType (file, fileName);
checkDuplicates (file, fileName);
if ((rootFolderData.progressState.totalDisks % 500) == 0)
publish (rootFolderData.progressState);
if ((rootFolderData.totalDisks % 500) == 0)
publish (rootFolderData);
}
}
}
@ -90,13 +90,14 @@ public class DuplicateSwingWorker extends SwingWorker<Void, ProgressState>
@Override
protected Void doInBackground () throws Exception
{
traverse (rootFolderData.rootFolder);
rootFolderData.progressState.print ();
traverse (rootFolderData.getRootFolder ());
publish (rootFolderData);
rootFolderData.print ();
return null;
}
@Override
protected void process (List<ProgressState> chunks)
protected void process (List<RootFolderData> chunks)
{
rootFolderData.progressPanel.repaint ();
}

View File

@ -36,7 +36,7 @@ public class DuplicateWindow extends JFrame
public DuplicateWindow (RootFolderData rootFolderData)
{
super ("Disk List - " + rootFolderData.rootFolder.getAbsolutePath ());
super ("Disk List - " + rootFolderData.getRootFolder ().getAbsolutePath ());
table = new JTable ();
JScrollPane scrollPane =
@ -75,7 +75,7 @@ public class DuplicateWindow extends JFrame
@Override
public void actionPerformed (ActionEvent e)
{
createCSV ();
CSVFileWriter.write (diskTableModel, table);
}
});
@ -88,7 +88,6 @@ public class DuplicateWindow extends JFrame
{
diskTableModel = new DiskTableModel (rootFolderData);
table.setModel (diskTableModel);
// lblTotalDisks.setText (diskTableModel.getRowCount () + "");
int[] columnWidths = { 300, 300, 30, 40, 40, 40, 100 };
TableColumnModel tcm = table.getColumnModel ();
@ -132,7 +131,7 @@ public class DuplicateWindow extends JFrame
for (int i = 0; i < Utility.suffixes.size (); i++)
{
int total = rootFolderData.progressState.getTotalType (i);
int total = rootFolderData.getTotalType (i);
JCheckBox btn =
new JCheckBox (String.format ("%s (%,d)", Utility.suffixes.get (i), total));
topPanel.add (btn);
@ -158,11 +157,6 @@ public class DuplicateWindow extends JFrame
setVisible (true);
}
private void createCSV ()
{
CSVFileWriter.write (diskTableModel, table);
}
private String getFilterText ()
{
StringBuilder filterText = new StringBuilder ();

View File

@ -1,113 +0,0 @@
package com.bytezone.diskbrowser.duplicates;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.File;
import java.util.List;
import com.bytezone.diskbrowser.utilities.Utility;
public class ProgressState
{
private static final String header =
" type uncmp .gz .zip total";
private static final String line = "-------------- ------- ------- ------- -------";
private static final List<String> suffixes = Utility.suffixes;
private static final Font font = new Font ("Monospaced", Font.BOLD, 15);
int totalDisks;
int totalFolders;
// total files for each suffix (uncompressed, .gz, .zip)
final int[][] typeTotals = new int[4][suffixes.size ()];
public void incrementFolders ()
{
++totalFolders;
}
public int getTotalType (int type)
{
return typeTotals[0][type] + typeTotals[1][type] + typeTotals[2][type];
}
public void incrementType (File file, String filename)
{
int pos = Utility.getSuffixNo (filename);
if (pos >= 0)
{
int cmp = 0;
if (filename.endsWith (".gz"))
cmp = 1;
else if (filename.endsWith (".zip"))
cmp = 2;
typeTotals[cmp][pos]++;
typeTotals[3][pos]++;
++totalDisks;
}
else
System.out.println ("no suffix: " + filename);
}
void paintComponent (Graphics graphics)
{
Graphics2D g = (Graphics2D) graphics;
g.setColor (Color.BLACK);
g.setFont (font);
int x = 55;
int y = 25;
int lineHeight = 23;
String line;
g.drawString (header, x, y);
y += lineHeight + 10;
int grandTotal[] = new int[4];
for (int i = 0; i < typeTotals[0].length; i++)
{
line = String.format ("%14.14s %,7d %,7d %,7d %,7d",
Utility.suffixes.get (i) + " ...........", typeTotals[0][i], typeTotals[1][i],
typeTotals[2][i], typeTotals[3][i]);
g.drawString (line, x, y);
for (int j = 0; j < typeTotals.length; j++)
grandTotal[j] += typeTotals[j][i];
y += lineHeight;
}
line = String.format ("Total %,7d %,7d %,7d %,7d%n%n", grandTotal[0],
grandTotal[1], grandTotal[2], grandTotal[3]);
y += 10;
g.drawString (line, x, y);
}
public void print ()
{
System.out.printf ("%nFolders ...... %,7d%n", totalFolders);
System.out.printf ("Disks ........ %,7d%n%n", totalDisks);
int grandTotal[] = new int[4];
System.out.println (header);
System.out.println (line);
for (int i = 0; i < typeTotals[0].length; i++)
{
System.out.printf ("%14.14s ", Utility.suffixes.get (i) + " ...........");
for (int j = 0; j < typeTotals.length; j++)
{
System.out.printf ("%,7d ", typeTotals[j][i]);
grandTotal[j] += typeTotals[j][i];
}
System.out.println ();
}
System.out.println (line);
System.out.printf ("Total %,7d %,7d %,7d %,7d%n%n", grandTotal[0],
grandTotal[1], grandTotal[2], grandTotal[3]);
}
}

View File

@ -1,7 +1,10 @@
package com.bytezone.diskbrowser.duplicates;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
@ -13,20 +16,17 @@ import javax.swing.JDialog;
import javax.swing.JPanel;
import com.bytezone.diskbrowser.gui.DuplicateAction.DiskTableSelectionListener;
import com.bytezone.diskbrowser.utilities.Utility;
public class RootFolderData
{
public File rootFolder;
private File rootFolder;
// list of checksum -> DiskDetails
public final Map<Long, DiskDetails> checksumMap = new HashMap<Long, DiskDetails> ();
// list of unique disk names -> DiskDetails
public final Map<String, DiskDetails> fileNameMap = new TreeMap<String, DiskDetails> ();
public final ProgressState progressState = new ProgressState ();
public final ProgressPanel progressPanel;
final Map<Long, DiskDetails> checksumMap = new HashMap<Long, DiskDetails> ();
final Map<String, DiskDetails> fileNameMap = new TreeMap<String, DiskDetails> ();
final ProgressPanel progressPanel;
public JDialog dialog;
public DuplicateWindow window;
public final List<DiskTableSelectionListener> listeners =
@ -35,25 +35,136 @@ public class RootFolderData
public boolean doChecksums;
public boolean showTotals;
public JDialog dialog;
private static final String header =
" type uncmp .gz .zip total";
private static final String line = "-------------- ------- ------- ------- -------";
private static final Font font = new Font ("Monospaced", Font.BOLD, 15);
int totalDisks;
int totalFolders;
// total files for each suffix (uncompressed, .gz, .zip, total)
int[][] typeTotals;
public RootFolderData ()
{
dialog = new JDialog (window);
progressPanel = new ProgressPanel ();
progressPanel.setPreferredSize (new Dimension (560, 300));
dialog = new JDialog (window);
dialog.add (progressPanel);
dialog.setTitle ("Disk Totals");
dialog.pack ();
}
public void setRootFolder (File rootFolder)
{
this.rootFolder = rootFolder;
typeTotals = new int[4][Utility.suffixes.size ()];
totalDisks = 0;
totalFolders = 0;
window = null;
checksumMap.clear ();
fileNameMap.clear ();
}
public File getRootFolder ()
{
return rootFolder;
}
public void incrementFolders ()
{
++totalFolders;
}
public int getTotalType (int type)
{
return typeTotals[0][type] + typeTotals[1][type] + typeTotals[2][type];
}
public void incrementType (File file, String filename)
{
int pos = Utility.getSuffixNo (filename);
if (pos >= 0)
{
int cmp = 0;
if (filename.endsWith (".gz"))
cmp = 1;
else if (filename.endsWith (".zip"))
cmp = 2;
typeTotals[cmp][pos]++;
typeTotals[3][pos]++;
++totalDisks;
}
else
System.out.println ("no suffix: " + filename);
}
public void print ()
{
System.out.printf ("%nFolders ...... %,7d%n", totalFolders);
System.out.printf ("Disks ........ %,7d%n%n", totalDisks);
int grandTotal[] = new int[4];
System.out.println (header);
System.out.println (line);
for (int i = 0; i < typeTotals[0].length; i++)
{
System.out.printf ("%14.14s ", Utility.suffixes.get (i) + " ...........");
for (int j = 0; j < typeTotals.length; j++)
{
System.out.printf ("%,7d ", typeTotals[j][i]);
grandTotal[j] += typeTotals[j][i];
}
System.out.println ();
}
System.out.println (line);
System.out.printf ("Total %,7d %,7d %,7d %,7d%n%n", grandTotal[0],
grandTotal[1], grandTotal[2], grandTotal[3]);
System.out.printf ("Unique checksums: %,d%n", checksumMap.size ());
}
class ProgressPanel extends JPanel
{
@Override
protected void paintComponent (Graphics graphics)
{
super.paintComponent (graphics);
progressState.paintComponent (graphics);
Graphics2D g = (Graphics2D) graphics;
g.setColor (Color.BLACK);
g.setFont (font);
int x = 55;
int y = 25;
int lineHeight = 23;
String line;
g.drawString (header, x, y);
y += lineHeight + 10;
int grandTotal[] = new int[4];
for (int i = 0; i < typeTotals[0].length; i++)
{
line = String.format ("%14.14s %,7d %,7d %,7d %,7d",
Utility.suffixes.get (i) + " ...........", typeTotals[0][i], typeTotals[1][i],
typeTotals[2][i], typeTotals[3][i]);
g.drawString (line, x, y);
for (int j = 0; j < typeTotals.length; j++)
grandTotal[j] += typeTotals[j][i];
y += lineHeight;
}
line = String.format ("Total %,7d %,7d %,7d %,7d%n%n", grandTotal[0],
grandTotal[1], grandTotal[2], grandTotal[3]);
y += 10;
g.drawString (line, x, y);
}
}
}

View File

@ -26,7 +26,7 @@ public class CountDisksAction extends DefaultAction implements RootDirectoryChan
int mask = Toolkit.getDefaultToolkit ().getMenuShortcutKeyMask ();
putValue (Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke (KeyEvent.VK_I, mask));
setEnabled (rootFolderData.rootFolder != null);
setEnabled (rootFolderData.getRootFolder () != null);
}
@Override
@ -45,8 +45,7 @@ public class CountDisksAction extends DefaultAction implements RootDirectoryChan
@Override
public void rootDirectoryChanged (File rootFolder)
{
rootFolderData.rootFolder = rootFolder;
rootFolderData.setRootFolder (rootFolder);
setEnabled (rootFolder != null);
rootFolderData.window = null;
}
}

View File

@ -31,15 +31,14 @@ public class DuplicateAction extends DefaultAction implements RootDirectoryChang
setIcon (Action.LARGE_ICON_KEY, "save_delete_32.png");
int mask = Toolkit.getDefaultToolkit ().getMenuShortcutKeyMask ();
putValue (Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke (KeyEvent.VK_L, mask));
setEnabled (rootFolderData.rootFolder != null);
setEnabled (rootFolderData.getRootFolder () != null);
}
@Override
public void rootDirectoryChanged (File rootFolder)
{
rootFolderData.rootFolder = rootFolder;
rootFolderData.setRootFolder (rootFolder);
setEnabled (rootFolder != null);
rootFolderData.window = null;
}
@Override