From e5681ef18bf7f8d35d6be932d55736a789a55b3a Mon Sep 17 00:00:00 2001 From: Denis Molony Date: Mon, 12 Dec 2016 19:31:58 +1100 Subject: [PATCH] Refactoring --- .../duplicates/DiskTableModel.java | 6 +-- ...Handler.java => DuplicateSwingWorker.java} | 41 ++++++------------- .../duplicates/DuplicateWindow.java | 5 ++- .../duplicates/RootFolderData.java | 19 +++++++++ .../diskbrowser/gui/DuplicateAction.java | 12 +++--- 5 files changed, 43 insertions(+), 40 deletions(-) rename src/com/bytezone/diskbrowser/duplicates/{DuplicateHandler.java => DuplicateSwingWorker.java} (73%) create mode 100644 src/com/bytezone/diskbrowser/duplicates/RootFolderData.java diff --git a/src/com/bytezone/diskbrowser/duplicates/DiskTableModel.java b/src/com/bytezone/diskbrowser/duplicates/DiskTableModel.java index 931f05d..2a41d14 100644 --- a/src/com/bytezone/diskbrowser/duplicates/DiskTableModel.java +++ b/src/com/bytezone/diskbrowser/duplicates/DiskTableModel.java @@ -17,10 +17,10 @@ public class DiskTableModel extends AbstractTableModel Map checkSumMap; List lines = new ArrayList (); - public DiskTableModel (DuplicateHandler duplicateHandler) + public DiskTableModel (RootFolderData rootFolderData) { - fileNameMap = duplicateHandler.getFileNameMap (); - checkSumMap = duplicateHandler.getChecksumMap (); + fileNameMap = rootFolderData.fileNameMap; + checkSumMap = rootFolderData.checksumMap; for (String key : fileNameMap.keySet ()) { diff --git a/src/com/bytezone/diskbrowser/duplicates/DuplicateHandler.java b/src/com/bytezone/diskbrowser/duplicates/DuplicateSwingWorker.java similarity index 73% rename from src/com/bytezone/diskbrowser/duplicates/DuplicateHandler.java rename to src/com/bytezone/diskbrowser/duplicates/DuplicateSwingWorker.java index 38a31b0..2025758 100644 --- a/src/com/bytezone/diskbrowser/duplicates/DuplicateHandler.java +++ b/src/com/bytezone/diskbrowser/duplicates/DuplicateSwingWorker.java @@ -3,10 +3,7 @@ package com.bytezone.diskbrowser.duplicates; import java.awt.Dimension; import java.awt.Graphics; import java.io.File; -import java.util.HashMap; import java.util.List; -import java.util.Map; -import java.util.TreeMap; import javax.swing.JDialog; import javax.swing.JPanel; @@ -14,7 +11,7 @@ import javax.swing.SwingWorker; import com.bytezone.diskbrowser.utilities.Utility; -public class DuplicateHandler extends SwingWorker +public class DuplicateSwingWorker extends SwingWorker { private final File rootFolder; private final int rootFolderNameLength; @@ -23,21 +20,17 @@ public class DuplicateHandler extends SwingWorker private final JDialog dialog; private final ProgressPanel progressPanel; private final boolean doChecksums; + private final RootFolderData rootFolderData; - // list of checksum -> DiskDetails - private final Map checksumMap = new HashMap (); - - // list of unique disk names -> DiskDetails - private final Map fileNameMap = - new TreeMap (); - - public DuplicateHandler (File rootFolder, DuplicateWindow owner, boolean doChecksums) + public DuplicateSwingWorker (File rootFolder, DuplicateWindow owner, boolean doChecksums) { this.rootFolder = rootFolder; this.owner = owner; this.doChecksums = doChecksums; rootFolderNameLength = rootFolder.getAbsolutePath ().length (); + rootFolderData = new RootFolderData (); + dialog = new JDialog (owner); progressPanel = new ProgressPanel (); progressPanel.setPreferredSize (new Dimension (485, 300)); @@ -48,16 +41,6 @@ public class DuplicateHandler extends SwingWorker dialog.setVisible (true); } - public Map getFileNameMap () - { - return fileNameMap; - } - - public Map getChecksumMap () - { - return checksumMap; - } - File getRootFolder () { return rootFolder; @@ -98,18 +81,18 @@ public class DuplicateHandler extends SwingWorker String rootName = file.getAbsolutePath ().substring (rootFolderNameLength); DiskDetails diskDetails = new DiskDetails (file, rootName, filename, doChecksums); - if (fileNameMap.containsKey (filename)) - fileNameMap.get (filename).addDuplicateName (diskDetails); + if (rootFolderData.fileNameMap.containsKey (filename)) + rootFolderData.fileNameMap.get (filename).addDuplicateName (diskDetails); else - fileNameMap.put (filename, diskDetails); + rootFolderData.fileNameMap.put (filename, diskDetails); if (doChecksums) { long checksum = diskDetails.getChecksum (); - if (checksumMap.containsKey (checksum)) - checksumMap.get (checksum).addDuplicateChecksum (diskDetails); + if (rootFolderData.checksumMap.containsKey (checksum)) + rootFolderData.checksumMap.get (checksum).addDuplicateChecksum (diskDetails); else - checksumMap.put (checksum, diskDetails); + rootFolderData.checksumMap.put (checksum, diskDetails); } } @@ -119,7 +102,7 @@ public class DuplicateHandler extends SwingWorker try { dialog.setVisible (false); - owner.setDuplicateHandler (this); + owner.setTableModel (new DiskTableModel (rootFolderData)); } catch (Exception e) { diff --git a/src/com/bytezone/diskbrowser/duplicates/DuplicateWindow.java b/src/com/bytezone/diskbrowser/duplicates/DuplicateWindow.java index 62024a9..867136c 100644 --- a/src/com/bytezone/diskbrowser/duplicates/DuplicateWindow.java +++ b/src/com/bytezone/diskbrowser/duplicates/DuplicateWindow.java @@ -70,9 +70,10 @@ public class DuplicateWindow extends JFrame setDefaultCloseOperation (HIDE_ON_CLOSE); } - public void setDuplicateHandler (DuplicateHandler duplicateHandler) + // called from DuplicateSwingWorker + public void setTableModel (DiskTableModel diskTableModel) { - table.setModel (new DiskTableModel (duplicateHandler)); + table.setModel (diskTableModel); int[] columnWidths = { 300, 300, 30, 40, 40, 40, 100 }; TableColumnModel tcm = table.getColumnModel (); diff --git a/src/com/bytezone/diskbrowser/duplicates/RootFolderData.java b/src/com/bytezone/diskbrowser/duplicates/RootFolderData.java new file mode 100644 index 0000000..58ad0d3 --- /dev/null +++ b/src/com/bytezone/diskbrowser/duplicates/RootFolderData.java @@ -0,0 +1,19 @@ +package com.bytezone.diskbrowser.duplicates; + +import java.util.HashMap; +import java.util.Map; +import java.util.TreeMap; + +public class RootFolderData +{ + // list of checksum -> DiskDetails + public final Map checksumMap = new HashMap (); + + // list of unique disk names -> DiskDetails + public final Map fileNameMap = new TreeMap (); + + public RootFolderData () + { + + } +} \ No newline at end of file diff --git a/src/com/bytezone/diskbrowser/gui/DuplicateAction.java b/src/com/bytezone/diskbrowser/gui/DuplicateAction.java index 13f5eaa..f124166 100644 --- a/src/com/bytezone/diskbrowser/gui/DuplicateAction.java +++ b/src/com/bytezone/diskbrowser/gui/DuplicateAction.java @@ -13,7 +13,7 @@ import javax.swing.KeyStroke; import com.bytezone.common.DefaultAction; import com.bytezone.diskbrowser.duplicates.DiskDetails; -import com.bytezone.diskbrowser.duplicates.DuplicateHandler; +import com.bytezone.diskbrowser.duplicates.DuplicateSwingWorker; import com.bytezone.diskbrowser.duplicates.DuplicateWindow; import com.bytezone.diskbrowser.gui.RootDirectoryAction.RootDirectoryChangeListener; @@ -50,17 +50,17 @@ public class DuplicateAction extends DefaultAction implements RootDirectoryChang if (window == null) { Object[] options = { "Generate checksums", "Disk names only", "Cancel" }; - int n = JOptionPane.showOptionDialog (null, + int option = JOptionPane.showOptionDialog (null, "This command will list all of the disks in the root folder (including\n" + "nested folders). If you wish to generate a checksum for each disk, it\n" - + "will slow the process down considerably.\n\n" + + "may slow the process down considerably.\n\n" + "Do you wish to generate checksums?", "Generate Disk Listing", JOptionPane.YES_NO_CANCEL_OPTION, - JOptionPane.QUESTION_MESSAGE, null, options, options[2]); - if (n < 2) + JOptionPane.QUESTION_MESSAGE, null, options, options[1]); // just disk names + if (option < 2) { window = new DuplicateWindow (rootFolder, listeners); - new DuplicateHandler (rootFolder, window, n == 0).execute (); + new DuplicateSwingWorker (rootFolder, window, option == 0).execute (); } } else