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

360 lines
12 KiB
Java
Raw Normal View History

2016-12-12 08:31:58 +00:00
package com.bytezone.diskbrowser.duplicates;
2020-02-02 10:17:49 +00:00
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
2016-12-15 00:01:42 +00:00
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
2021-06-06 06:27:30 +00:00
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
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-15 00:01:42 +00:00
import javax.swing.JButton;
2016-12-16 20:45:08 +00:00
import javax.swing.JFrame;
2016-12-12 23:46:09 +00:00
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
2020-02-08 09:54:50 +00:00
// -----------------------------------------------------------------------------------//
2021-06-06 06:27:30 +00:00
public class RootFolderData implements PropertyChangeListener
2020-02-08 09:54:50 +00:00
// -----------------------------------------------------------------------------------//
2016-12-12 08:31:58 +00:00
{
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-15 00:01:42 +00:00
public DisksWindow disksWindow;
2016-12-12 23:46:09 +00:00
2020-02-02 10:17:49 +00:00
public final List<DiskTableSelectionListener> listeners = new ArrayList<> ();
2016-12-12 23:46:09 +00:00
public boolean doChecksums;
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
2016-12-15 00:01:42 +00:00
// Progress dialog
ProgressPanel progressPanel;
2016-12-16 20:45:08 +00:00
public JFrame dialogTotals;
2016-12-15 00:01:42 +00:00
JPanel southPanel;
JButton btnCancel;
JButton btnOK;
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
2016-12-15 00:01:42 +00:00
private void createWindows ()
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
2016-12-12 23:46:09 +00:00
{
2016-12-15 00:01:42 +00:00
southPanel = new JPanel ();
btnCancel = new JButton ("Cancel");
btnOK = new JButton ("OK");
2016-12-12 23:46:09 +00:00
progressPanel = new ProgressPanel ();
2021-04-24 03:01:58 +00:00
progressPanel.setPreferredSize (new Dimension (560, 380));
2016-12-13 10:03:15 +00:00
2016-12-16 20:45:08 +00:00
dialogTotals = new JFrame ("Disk Totals");
2016-12-15 00:01:42 +00:00
dialogTotals.add (progressPanel, BorderLayout.CENTER);
2016-12-15 07:27:38 +00:00
southPanel.add (btnCancel); // needs to be here for the pack()
2016-12-15 00:01:42 +00:00
dialogTotals.add (southPanel, BorderLayout.SOUTH);
2016-12-13 22:26:47 +00:00
dialogTotals.pack ();
dialogTotals.setLocationRelativeTo (null);
2016-12-15 00:01:42 +00:00
btnCancel.addActionListener (new ActionListener ()
{
@Override
public void actionPerformed (ActionEvent e)
{
progressPanel.cancelled = true;
dialogTotals.setVisible (false);
}
});
btnOK.addActionListener (new ActionListener ()
{
@Override
public void actionPerformed (ActionEvent e)
{
dialogTotals.setVisible (false);
}
});
}
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
2016-12-15 00:01:42 +00:00
public void count (boolean doChecksums)
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
2016-12-15 00:01:42 +00:00
{
if (dialogTotals == null)
createWindows ();
clear ();
setButton (btnCancel);
this.doChecksums = doChecksums;
progressPanel.cancelled = false;
disksWindow = new DisksWindow (this);
dialogTotals.setVisible (true);
new DuplicateSwingWorker (this).execute (); // start SwingWorker
}
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
2016-12-15 00:01:42 +00:00
public void done () // SwingWorker has completed
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
2016-12-15 00:01:42 +00:00
{
print ();
dialogTotals.repaint ();
dialogTotals.setVisible (false);
if (progressPanel.cancelled)
disksWindow = null;
else
{
disksWindow.setTableData (this);
setButton (btnOK);
}
}
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
2016-12-15 00:01:42 +00:00
private void setButton (JButton button)
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
2016-12-15 00:01:42 +00:00
{
southPanel.removeAll ();
southPanel.add (button);
dialogTotals.revalidate ();
dialogTotals.repaint ();
2016-12-12 23:46:09 +00:00
}
2016-12-12 08:31:58 +00:00
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
2018-07-23 07:39:25 +00:00
String getRootFolderPathText ()
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
2018-07-23 07:39:25 +00:00
{
String text = rootFolder.getAbsolutePath ();
String homeDir = System.getProperty ("user.home");
if (text.startsWith (homeDir))
text = text.replace (homeDir, "~");
return text;
}
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
2016-12-15 00:01:42 +00:00
private void clear ()
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
2016-12-15 00:01:42 +00:00
{
2021-05-12 08:14:49 +00:00
typeTotals = new int[4][Utility.getTotalSuffixes ()];
2016-12-13 10:03:15 +00:00
totalDisks = 0;
totalFolders = 0;
checksumMap.clear ();
fileNameMap.clear ();
}
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
2016-12-13 10:03:15 +00:00
public File getRootFolder ()
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
2016-12-13 10:03:15 +00:00
{
return rootFolder;
}
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
2016-12-13 10:03:15 +00:00
public void incrementFolders ()
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
2016-12-13 10:03:15 +00:00
{
++totalFolders;
}
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
2021-05-22 10:15:20 +00:00
public void incrementType (File file, String fileName)
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
2016-12-13 10:03:15 +00:00
{
2021-05-22 10:15:20 +00:00
int pos = Utility.getSuffixNo (fileName);
2016-12-13 10:03:15 +00:00
if (pos >= 0)
{
2021-05-22 10:15:20 +00:00
int cmp = fileName.endsWith (".zip") ? 2 : fileName.endsWith (".gz") ? 1 : 0;
2016-12-13 10:03:15 +00:00
typeTotals[cmp][pos]++;
typeTotals[3][pos]++;
++totalDisks;
}
else
2021-05-22 10:15:20 +00:00
System.out.println ("no suffix: " + fileName);
2016-12-13 22:26:47 +00:00
2021-05-22 10:15:20 +00:00
checkDuplicates (file, fileName);
2016-12-13 22:26:47 +00:00
}
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
2021-05-22 10:15:20 +00:00
private void checkDuplicates (File file, String fileName)
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
2016-12-13 22:26:47 +00:00
{
String rootName = file.getAbsolutePath ().substring (rootFolderNameLength);
2021-05-22 10:15:20 +00:00
DiskDetails diskDetails = new DiskDetails (file, rootName, fileName, doChecksums);
2016-12-13 22:26:47 +00:00
2021-05-22 10:15:20 +00:00
if (fileNameMap.containsKey (fileName))
fileNameMap.get (fileName).addDuplicateName (diskDetails);
2016-12-13 22:26:47 +00:00
else
2021-05-22 10:15:20 +00:00
fileNameMap.put (fileName, diskDetails);
2016-12-13 22:26:47 +00:00
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
}
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
2016-12-16 20:45:08 +00:00
public List<DiskDetails> listDuplicates (long checksum)
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
2016-12-16 20:45:08 +00:00
{
2020-02-02 10:17:49 +00:00
List<DiskDetails> list = new ArrayList<> ();
2016-12-16 20:45:08 +00:00
DiskDetails original = checksumMap.get (checksum);
if (original != null)
{
list.add (original);
for (DiskDetails dd : original.getDuplicateChecksums ())
list.add (dd);
}
return list;
}
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
2016-12-15 00:01:42 +00:00
public int getTotalType (int type)
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
2016-12-15 00:01:42 +00:00
{
return typeTotals[0][type] + typeTotals[1][type] + typeTotals[2][type];
}
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
2016-12-13 10:03:15 +00:00
public void print ()
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
2016-12-13 10:03:15 +00:00
{
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++)
{
2021-05-12 08:14:49 +00:00
System.out.printf ("%14.14s ", Utility.getSuffix (i) + " ...........");
2016-12-13 10:03:15 +00:00
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]);
2016-12-15 07:27:38 +00:00
if (doChecksums)
{
System.out.printf ("Unique checksums: %,7d%n", checksumMap.size ());
System.out.printf ("Duplicate disks : %,7d%n", totalDisks - checksumMap.size ());
}
2016-12-13 10:03:15 +00:00
}
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
2021-06-06 06:27:30 +00:00
private void rootDirectoryChanged (File oldRootFolder, File newRootFolder)
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
{
rootFolder = newRootFolder;
rootFolderNameLength = rootFolder.getAbsolutePath ().length ();
disksWindow = null; // force a recount
}
// ---------------------------------------------------------------------------------//
2021-06-06 06:27:30 +00:00
@Override
public void propertyChange (PropertyChangeEvent evt)
// ---------------------------------------------------------------------------------//
{
if (evt.getPropertyName ().equals ("RootDirectory"))
rootDirectoryChanged ((File) evt.getOldValue (), (File) evt.getNewValue ());
}
// ---------------------------------------------------------------------------------//
2019-10-24 03:13:01 +00:00
@Override
public String toString ()
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
2019-10-24 03:13:01 +00:00
{
StringBuilder text = new StringBuilder ();
text.append (String.format ("Root folder ....... %s%n", rootFolder));
text.append (String.format ("Disks ............. %,d%n", totalDisks));
text.append (String.format ("Folders ........... %,d", totalFolders));
return text.toString ();
}
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
2016-12-12 23:46:09 +00:00
class ProgressPanel extends JPanel
2020-02-08 09:54:50 +00:00
// ---------------------------------------------------------------------------------//
2016-12-12 23:46:09 +00:00
{
2016-12-15 00:01:42 +00:00
public volatile boolean cancelled;
2016-12-12 23:46:09 +00:00
@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++)
2021-05-22 10:15:20 +00:00
if (typeTotals[3][i] > 0)
{
line = String.format ("%14.14s %,7d %,7d %,7d %,7d",
Utility.getSuffix (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;
}
2016-12-13 10:03:15 +00:00
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-16 20:45:08 +00:00
if (doChecksums)
{
line = String.format ("duplicates ... %,7d%n",
totalDisks - checksumMap.size ());
y += lineHeight + 10;
g.drawString (line, x, y);
}
2016-12-12 23:46:09 +00:00
}
}
2016-12-12 08:31:58 +00:00
}