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

103 lines
2.7 KiB
Java
Raw Normal View History

2016-12-07 10:42:01 +00:00
package com.bytezone.diskbrowser.duplicates;
import java.io.File;
import java.util.List;
2016-12-07 10:42:01 +00:00
import javax.swing.SwingWorker;
2016-12-07 10:42:01 +00:00
import com.bytezone.diskbrowser.utilities.Utility;
2016-12-12 08:31:58 +00:00
public class DuplicateSwingWorker extends SwingWorker<Void, ProgressState>
2016-12-07 10:42:01 +00:00
{
2016-12-09 11:31:03 +00:00
private final int rootFolderNameLength;
2016-12-12 08:31:58 +00:00
private final RootFolderData rootFolderData;
2016-12-07 10:42:01 +00:00
2016-12-12 23:46:09 +00:00
public DuplicateSwingWorker (RootFolderData rootFolderData)
2016-12-07 10:42:01 +00:00
{
2016-12-12 23:46:09 +00:00
this.rootFolderData = rootFolderData;
rootFolderNameLength = rootFolderData.rootFolder.getAbsolutePath ().length ();
2016-12-08 01:19:18 +00:00
2016-12-12 23:46:09 +00:00
rootFolderData.dialog.setLocationRelativeTo (null);
rootFolderData.dialog.setVisible (true);
2016-12-09 11:31:03 +00:00
}
2016-12-08 01:19:18 +00:00
private void traverse (File directory)
2016-12-07 10:42:01 +00:00
{
File[] files = directory.listFiles ();
2016-12-09 11:31:03 +00:00
2016-12-07 10:42:01 +00:00
if (files == null || files.length == 0)
{
System.out.println ("Empty folder : " + directory.getAbsolutePath ());
return;
}
for (File file : files)
{
2016-12-09 11:31:03 +00:00
String fileName = file.getName ().toLowerCase ();
2016-12-07 10:42:01 +00:00
if (file.isDirectory ())
{
2016-12-12 10:53:28 +00:00
rootFolderData.progressState.incrementFolders ();
2016-12-08 01:19:18 +00:00
traverse (file);
2016-12-07 10:42:01 +00:00
}
else if (Utility.validFileType (fileName) && file.length () > 0)
2016-12-07 10:42:01 +00:00
{
2016-12-12 10:53:28 +00:00
rootFolderData.progressState.incrementType (file, fileName);
2016-12-09 11:31:03 +00:00
checkDuplicates (file, fileName);
2016-12-12 10:53:28 +00:00
if ((rootFolderData.progressState.totalDisks % 500) == 0)
publish (rootFolderData.progressState);
2016-12-07 10:42:01 +00:00
}
}
}
private void checkDuplicates (File file, String filename)
2016-12-07 10:42:01 +00:00
{
2016-12-09 11:31:03 +00:00
String rootName = file.getAbsolutePath ().substring (rootFolderNameLength);
2016-12-12 23:46:09 +00:00
DiskDetails diskDetails =
new DiskDetails (file, rootName, filename, rootFolderData.doChecksums);
2016-12-07 10:42:01 +00:00
2016-12-12 08:31:58 +00:00
if (rootFolderData.fileNameMap.containsKey (filename))
rootFolderData.fileNameMap.get (filename).addDuplicateName (diskDetails);
2016-12-09 11:31:03 +00:00
else
2016-12-12 08:31:58 +00:00
rootFolderData.fileNameMap.put (filename, diskDetails);
2016-12-07 10:42:01 +00:00
2016-12-12 23:46:09 +00:00
if (rootFolderData.doChecksums)
2016-12-12 07:43:19 +00:00
{
long checksum = diskDetails.getChecksum ();
2016-12-12 08:31:58 +00:00
if (rootFolderData.checksumMap.containsKey (checksum))
rootFolderData.checksumMap.get (checksum).addDuplicateChecksum (diskDetails);
2016-12-12 07:43:19 +00:00
else
2016-12-12 08:31:58 +00:00
rootFolderData.checksumMap.put (checksum, diskDetails);
2016-12-12 07:43:19 +00:00
}
2016-12-07 10:42:01 +00:00
}
@Override
protected void done ()
2016-12-07 10:42:01 +00:00
{
try
2016-12-07 10:42:01 +00:00
{
2016-12-12 23:46:09 +00:00
if (!rootFolderData.showTotals)
rootFolderData.dialog.setVisible (false);
rootFolderData.window.setTableData (rootFolderData);
}
catch (Exception e)
{
e.printStackTrace ();
2016-12-07 10:42:01 +00:00
}
}
@Override
protected Void doInBackground () throws Exception
{
2016-12-12 23:46:09 +00:00
traverse (rootFolderData.rootFolder);
2016-12-12 10:53:28 +00:00
rootFolderData.progressState.print ();
return null;
}
@Override
protected void process (List<ProgressState> chunks)
{
2016-12-12 23:46:09 +00:00
rootFolderData.progressPanel.repaint ();
}
2016-12-07 10:42:01 +00:00
}