dmolony-DiskBrowser/src/com/bytezone/diskbrowser/duplicates/RootFolderData.java

195 lines
5.3 KiB
Java
Raw Normal View History

2016-12-12 08:31:58 +00:00
package com.bytezone.diskbrowser.duplicates;
2016-12-13 10:03:15 +00:00
import java.awt.Color;
2016-12-12 23:46:09 +00:00
import java.awt.Dimension;
2016-12-13 10:03:15 +00:00
import java.awt.Font;
2016-12-12 23:46:09 +00:00
import java.awt.Graphics;
2016-12-13 10:03:15 +00:00
import java.awt.Graphics2D;
2016-12-12 23:46:09 +00:00
import java.io.File;
import java.util.ArrayList;
2016-12-12 08:31:58 +00:00
import java.util.HashMap;
2016-12-12 23:46:09 +00:00
import java.util.List;
2016-12-12 08:31:58 +00:00
import java.util.Map;
import java.util.TreeMap;
2016-12-12 23:46:09 +00:00
import javax.swing.JDialog;
import javax.swing.JPanel;
import com.bytezone.diskbrowser.gui.DuplicateAction.DiskTableSelectionListener;
2016-12-13 10:03:15 +00:00
import com.bytezone.diskbrowser.utilities.Utility;
2016-12-12 23:46:09 +00:00
2016-12-12 08:31:58 +00:00
public class RootFolderData
{
2016-12-13 22:26:47 +00:00
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);
2016-12-13 10:03:15 +00:00
private File rootFolder;
2016-12-13 22:26:47 +00:00
private int rootFolderNameLength;
2016-12-12 23:46:09 +00:00
2016-12-13 10:03:15 +00:00
final Map<Long, DiskDetails> checksumMap = new HashMap<Long, DiskDetails> ();
final Map<String, DiskDetails> fileNameMap = new TreeMap<String, DiskDetails> ();
2016-12-12 23:46:09 +00:00
2016-12-13 10:03:15 +00:00
final ProgressPanel progressPanel;
2016-12-13 22:26:47 +00:00
public JDialog dialogTotals;
public DisksWindow windowDisks;
2016-12-12 23:46:09 +00:00
public final List<DiskTableSelectionListener> listeners =
new ArrayList<DiskTableSelectionListener> ();
public boolean doChecksums;
public boolean showTotals;
2016-12-13 10:03:15 +00:00
int totalDisks;
int totalFolders;
// total files for each suffix (uncompressed, .gz, .zip, total)
int[][] typeTotals;
2016-12-12 23:46:09 +00:00
public RootFolderData ()
{
progressPanel = new ProgressPanel ();
2016-12-13 03:36:26 +00:00
progressPanel.setPreferredSize (new Dimension (560, 300));
2016-12-13 10:03:15 +00:00
2016-12-13 22:26:47 +00:00
dialogTotals = new JDialog (windowDisks);
dialogTotals.add (progressPanel);
dialogTotals.setTitle ("Disk Totals");
dialogTotals.pack ();
dialogTotals.setLocationRelativeTo (null);
2016-12-12 23:46:09 +00:00
}
2016-12-12 08:31:58 +00:00
2016-12-13 10:03:15 +00:00
public void setRootFolder (File rootFolder)
{
this.rootFolder = rootFolder;
typeTotals = new int[4][Utility.suffixes.size ()];
totalDisks = 0;
totalFolders = 0;
2016-12-13 22:26:47 +00:00
windowDisks = null;
2016-12-13 10:03:15 +00:00
checksumMap.clear ();
fileNameMap.clear ();
2016-12-13 22:26:47 +00:00
rootFolderNameLength = rootFolder.getAbsolutePath ().length ();
2016-12-13 10:03:15 +00:00
}
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);
2016-12-13 22:26:47 +00:00
checkDuplicates (file, filename);
}
private void checkDuplicates (File file, String filename)
{
String rootName = file.getAbsolutePath ().substring (rootFolderNameLength);
DiskDetails diskDetails = new DiskDetails (file, rootName, filename, doChecksums);
if (fileNameMap.containsKey (filename))
fileNameMap.get (filename).addDuplicateName (diskDetails);
else
fileNameMap.put (filename, diskDetails);
if (doChecksums)
{
long checksum = diskDetails.getChecksum ();
if (checksumMap.containsKey (checksum))
checksumMap.get (checksum).addDuplicateChecksum (diskDetails);
else
checksumMap.put (checksum, diskDetails);
}
2016-12-13 10:03:15 +00:00
}
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 ());
}
2016-12-12 23:46:09 +00:00
class ProgressPanel extends JPanel
{
@Override
protected void paintComponent (Graphics graphics)
{
super.paintComponent (graphics);
2016-12-13 10:03:15 +00:00
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);
2016-12-12 23:46:09 +00:00
}
}
2016-12-12 08:31:58 +00:00
}