Export file

This commit is contained in:
Denis Molony 2016-12-13 14:36:26 +11:00
parent 7526a81176
commit 40090db4e7
6 changed files with 97 additions and 23 deletions

View File

@ -0,0 +1,39 @@
package com.bytezone.diskbrowser.duplicates;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JTable;
public class CSVFileWriter
{
public static void write (DiskTableModel diskTableModel, JTable table)
{
String csvFile =
System.getProperty ("user.home") + File.separator + "DiskBrowser.csv";
FileWriter writer;
try
{
writer = new FileWriter (csvFile);
writer.append (String
.format ("Path,Name,Type,Size,Duplicate Name, Duplicate Data, Checksum%n"));
for (int i = 0; i < table.getRowCount (); i++)
{
int actualRow = table.convertRowIndexToModel (i);
String line = diskTableModel.getCSV (actualRow);
writer.append (line);
}
writer.flush ();
writer.close ();
}
catch (IOException e)
{
e.printStackTrace ();
}
}
}

View File

@ -9,7 +9,7 @@ import com.bytezone.common.ComputeCRC32;
public class DiskDetails
{
private final File file;
private final long checksum;
private long checksum;
private final String rootName; // full path without the root folder
private final String shortName; // file name in lower case
@ -84,17 +84,17 @@ public class DiskDetails
return shortName;
}
public long calculateChecksum ()
{
checksum = ComputeCRC32.getChecksumValue (file);
return checksum;
}
public long getChecksum ()
{
return checksum;
}
// public boolean delete ()
// {
// // return file.delete ();
// return false;
// }
@Override
public String toString ()
{

View File

@ -81,11 +81,26 @@ public class DiskTableModel extends AbstractTableModel
}
}
public String getCSV (int rowIndex)
{
TableLine line = lines.get (rowIndex);
return String.format ("\"%s\",\"%s\",%s,%d,%s,%s,%d%n", line.path, line.shortName,
line.type, line.size, line.duplicateNames, line.duplicateChecksums,
line.checksum);
}
void updateChecksum (int rowIndex)
{
TableLine line = lines.get (rowIndex);
line.checksum = line.diskDetails.calculateChecksum ();
fireTableCellUpdated (rowIndex, 6);
}
class TableLine
{
private final String shortName;
private final String path;
private final long checksum;
private long checksum;
private final int duplicateNames;
private final int duplicateChecksums;
final DiskDetails diskDetails;

View File

@ -27,12 +27,12 @@ public class DuplicateWindow extends JFrame
private final JButton btnExport = new JButton ("Export");
private final JButton btnHide = new JButton ("Close");
// private final JLabel lblTotalDisks = new JLabel ();
private final JPanel topPanel = new JPanel ();
private final List<JCheckBox> boxes = new ArrayList<JCheckBox> ();
private TableRowSorter<DiskTableModel> sorter;
private final CheckBoxActionListener checkBoxActionListener =
new CheckBoxActionListener ();
private DiskTableModel diskTableModel;
public DuplicateWindow (RootFolderData rootFolderData)
{
@ -44,7 +44,6 @@ public class DuplicateWindow extends JFrame
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
table.setFillsViewportHeight (true);
table.setAutoCreateRowSorter (true);
table.setShowGrid (true);
table.setGridColor (Color.LIGHT_GRAY);
@ -71,6 +70,15 @@ public class DuplicateWindow extends JFrame
}
});
btnExport.addActionListener (new ActionListener ()
{
@Override
public void actionPerformed (ActionEvent e)
{
createCSV ();
}
});
scrollPane.setPreferredSize (new Dimension (1200, 700));
setDefaultCloseOperation (HIDE_ON_CLOSE);
}
@ -78,7 +86,7 @@ public class DuplicateWindow extends JFrame
// called from DuplicateSwingWorker
public void setTableData (final RootFolderData rootFolderData)
{
DiskTableModel diskTableModel = new DiskTableModel (rootFolderData);
diskTableModel = new DiskTableModel (rootFolderData);
table.setModel (diskTableModel);
// lblTotalDisks.setText (diskTableModel.getRowCount () + "");
@ -113,6 +121,10 @@ public class DuplicateWindow extends JFrame
DiskTableModel diskTableModel = (DiskTableModel) table.getModel ();
DiskDetails diskDetails = diskTableModel.lines.get (actualRow).diskDetails;
long checksum = diskDetails.getChecksum ();
if (checksum == 0)
diskTableModel.updateChecksum (actualRow);
for (DiskTableSelectionListener listener : rootFolderData.listeners)
listener.diskSelected (diskDetails);
}
@ -140,11 +152,17 @@ public class DuplicateWindow extends JFrame
pack ();
setLocationRelativeTo (null);
btnExport.setEnabled (true);
if (!rootFolderData.showTotals)
setVisible (true);
}
private void createCSV ()
{
CSVFileWriter.write (diskTableModel, table);
}
private String getFilterText ()
{
StringBuilder filterText = new StringBuilder ();

View File

@ -11,8 +11,9 @@ import com.bytezone.diskbrowser.utilities.Utility;
public class ProgressState
{
private static final String header = " type uncmp .gz .zip";
private static final String line = "-------------- ------- ------- -------";
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);
@ -20,7 +21,7 @@ public class ProgressState
int totalFolders;
// total files for each suffix (uncompressed, .gz, .zip)
final int[][] typeTotals = new int[3][suffixes.size ()];
final int[][] typeTotals = new int[4][suffixes.size ()];
public void incrementFolders ()
{
@ -43,6 +44,7 @@ public class ProgressState
else if (filename.endsWith (".zip"))
cmp = 2;
typeTotals[cmp][pos]++;
typeTotals[3][pos]++;
++totalDisks;
}
else
@ -64,13 +66,13 @@ public class ProgressState
g.drawString (header, x, y);
y += lineHeight + 10;
int grandTotal[] = new int[3];
int grandTotal[] = new int[4];
for (int i = 0; i < typeTotals[0].length; i++)
{
line = String.format ("%14.14s %,7d %,7d %,7d",
line = String.format ("%14.14s %,7d %,7d %,7d %,7d",
Utility.suffixes.get (i) + " ...........", typeTotals[0][i], typeTotals[1][i],
typeTotals[2][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];
@ -78,8 +80,8 @@ public class ProgressState
y += lineHeight;
}
line = String.format ("Total %,7d %,7d %,7d%n%n", grandTotal[0],
grandTotal[1], grandTotal[2]);
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);
}
@ -89,7 +91,7 @@ public class ProgressState
System.out.printf ("%nFolders ...... %,7d%n", totalFolders);
System.out.printf ("Disks ........ %,7d%n%n", totalDisks);
int grandTotal[] = new int[3];
int grandTotal[] = new int[4];
System.out.println (header);
System.out.println (line);
@ -105,7 +107,7 @@ public class ProgressState
}
System.out.println (line);
System.out.printf ("Total %,7d %,7d %,7d%n%n", grandTotal[0],
grandTotal[1], grandTotal[2]);
System.out.printf ("Total %,7d %,7d %,7d %,7d%n%n", grandTotal[0],
grandTotal[1], grandTotal[2], grandTotal[3]);
}
}

View File

@ -41,7 +41,7 @@ public class RootFolderData
{
dialog = new JDialog (window);
progressPanel = new ProgressPanel ();
progressPanel.setPreferredSize (new Dimension (485, 300));
progressPanel.setPreferredSize (new Dimension (560, 300));
dialog.add (progressPanel);
dialog.setTitle ("Disk Totals");
dialog.pack ();