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

117 lines
3.6 KiB
Java
Raw Normal View History

2016-12-07 10:56:25 +00:00
package com.bytezone.diskbrowser.duplicates;
import java.awt.BorderLayout;
2016-12-10 07:36:44 +00:00
import java.awt.Color;
import java.awt.Rectangle;
2016-12-07 10:56:25 +00:00
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;
import javax.swing.*;
2016-12-10 07:36:44 +00:00
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
2016-12-09 12:12:35 +00:00
import javax.swing.table.JTableHeader;
2016-12-10 07:36:44 +00:00
import javax.swing.table.TableRowSorter;
import com.bytezone.diskbrowser.gui.DuplicateAction;
import com.bytezone.diskbrowser.gui.DuplicateAction.DiskTableSelectionListener;
2016-12-07 10:56:25 +00:00
public class DuplicateWindow extends JFrame
{
2016-12-09 11:31:03 +00:00
private final JTable table;
2016-12-07 10:56:25 +00:00
2016-12-10 07:36:44 +00:00
private final JButton btnExport = new JButton ("Export");
private final JButton btnHide = new JButton ("Close");
2016-12-07 10:56:25 +00:00
2016-12-11 21:32:18 +00:00
// private DuplicateHandler duplicateHandler;
2016-12-10 07:36:44 +00:00
private final List<DiskTableSelectionListener> listeners;
2016-12-08 01:19:18 +00:00
2016-12-10 07:36:44 +00:00
public DuplicateWindow (File rootFolder,
List<DuplicateAction.DiskTableSelectionListener> listeners)
2016-12-07 10:56:25 +00:00
{
super ("Duplicate Disk Detection - " + rootFolder.getAbsolutePath ());
2016-12-10 07:36:44 +00:00
this.listeners = listeners;
2016-12-07 10:56:25 +00:00
2016-12-09 11:31:03 +00:00
table = new JTable ();
JScrollPane scrollPane =
new JScrollPane (table, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
2016-12-07 10:56:25 +00:00
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
2016-12-10 07:36:44 +00:00
2016-12-09 11:31:03 +00:00
table.setFillsViewportHeight (true);
2016-12-10 07:36:44 +00:00
table.setAutoCreateRowSorter (true);
2016-12-09 11:31:03 +00:00
2016-12-10 07:36:44 +00:00
table.setShowGrid (true);
table.setGridColor (Color.LIGHT_GRAY);
2016-12-09 11:31:03 +00:00
add (scrollPane, BorderLayout.CENTER);
2016-12-07 10:56:25 +00:00
JPanel panel = new JPanel ();
2016-12-10 07:36:44 +00:00
panel.add (btnHide);
panel.add (btnExport);
2016-12-07 10:56:25 +00:00
add (panel, BorderLayout.SOUTH);
2016-12-10 07:36:44 +00:00
btnHide.setEnabled (true);
btnExport.setEnabled (false);
2016-12-07 10:56:25 +00:00
2016-12-10 07:36:44 +00:00
btnHide.addActionListener (new ActionListener ()
2016-12-07 10:56:25 +00:00
{
@Override
public void actionPerformed (ActionEvent e)
{
DuplicateWindow.this.setVisible (false);
}
});
2016-12-10 07:36:44 +00:00
setSize (1200, 700);
2016-12-07 10:56:25 +00:00
setLocationRelativeTo (null);
setDefaultCloseOperation (HIDE_ON_CLOSE);
}
2016-12-09 11:31:03 +00:00
public void setDuplicateHandler (DuplicateHandler duplicateHandler)
2016-12-07 10:56:25 +00:00
{
2016-12-11 21:32:18 +00:00
// this.duplicateHandler = duplicateHandler;
2016-12-10 07:36:44 +00:00
2016-12-09 11:31:03 +00:00
table.setModel (new DiskTableModel (duplicateHandler));
2016-12-10 07:36:44 +00:00
int[] columnWidths = { 300, 300, 30, 40, 40, 40, 100 };
2016-12-10 07:36:44 +00:00
for (int i = 0; i < columnWidths.length; i++)
table.getColumnModel ().getColumn (i).setPreferredWidth (columnWidths[i]);
final TableRowSorter<DiskTableModel> sorter =
new TableRowSorter<DiskTableModel> ((DiskTableModel) table.getModel ());
table.setRowSorter (sorter);
ListSelectionModel listSelectionModel = table.getSelectionModel ();
listSelectionModel.addListSelectionListener (new ListSelectionListener ()
{
@Override
public void valueChanged (ListSelectionEvent e)
{
if (e.getValueIsAdjusting ())
return;
ListSelectionModel lsm = (ListSelectionModel) e.getSource ();
if (lsm.isSelectionEmpty ())
return;
table.scrollRectToVisible (
new Rectangle (table.getCellRect (lsm.getMinSelectionIndex (), 0, true)));
int selectedRow = table.getSelectedRow ();
int actualRow = sorter.convertRowIndexToModel (selectedRow);
DiskTableModel diskTableModel = (DiskTableModel) table.getModel ();
DiskDetails diskDetails = diskTableModel.lines.get (actualRow).diskDetails;
for (DiskTableSelectionListener listener : listeners)
listener.diskSelected (diskDetails);
}
});
2016-12-09 12:12:35 +00:00
JTableHeader header = table.getTableHeader ();
header.setFont (header.getFont ().deriveFont ((float) 13.0));
2016-12-09 11:31:03 +00:00
setVisible (true);
2016-12-07 10:56:25 +00:00
}
2016-12-09 11:31:03 +00:00
}