* improved Mac compatibility regarding DELETE key

This commit is contained in:
Reinhard Pointner 2014-07-24 12:10:47 +00:00
parent 2a4af5a995
commit c6bbd4db54
3 changed files with 109 additions and 136 deletions

View File

@ -1,7 +1,5 @@
package net.filebot.ui;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
@ -24,137 +22,120 @@ import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.swing.EventListModel;
public class FileBotList<E> extends JComponent {
protected EventList<E> model = new BasicEventList<E>();
protected JList list = new JList(new EventListModel<E>(model));
protected JScrollPane listScrollPane = new JScrollPane(list);
public FileBotList() {
setLayout(new BorderLayout());
setBorder(new TitledBorder(getTitle()));
list.setCellRenderer(new DefaultFancyListCellRenderer());
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
list.setTransferHandler(new DefaultTransferHandler(null, null));
list.setDragEnabled(false);
add(listScrollPane, BorderLayout.CENTER);
// Shortcut DELETE, disabled by default
getRemoveAction().setEnabled(false);
TunedUtilities.installAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), removeHook);
TunedUtilities.installAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, KeyEvent.ALT_DOWN_MASK), removeHook);
TunedUtilities.installAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0), removeHook);
TunedUtilities.installAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, KeyEvent.ALT_DOWN_MASK), removeHook);
}
public EventList<E> getModel() {
return model;
}
public void setModel(EventList<E> model) {
this.model = model;
list.setModel(new EventListModel<E>(model));
}
public JList getListComponent() {
return list;
}
public JScrollPane getListScrollPane() {
return listScrollPane;
}
@Override
public DefaultTransferHandler getTransferHandler() {
return (DefaultTransferHandler) list.getTransferHandler();
}
public void setTransferablePolicy(TransferablePolicy transferablePolicy) {
getTransferHandler().setTransferablePolicy(transferablePolicy);
}
public TransferablePolicy getTransferablePolicy() {
return getTransferHandler().getTransferablePolicy();
}
public void setExportHandler(TextFileExportHandler exportHandler) {
getTransferHandler().setExportHandler(exportHandler);
// enable drag if export handler is available
list.setDragEnabled(exportHandler != null);
}
public TextFileExportHandler getExportHandler() {
return (TextFileExportHandler) getTransferHandler().getExportHandler();
}
public String getTitle() {
return (String) getClientProperty("title");
}
public void setTitle(String title) {
putClientProperty("title", title);
if (getBorder() instanceof TitledBorder) {
TitledBorder border = (TitledBorder) getBorder();
border.setTitle(title);
repaint();
}
}
private final AbstractAction defaultRemoveAction = new AbstractAction("Remove") {
@Override
public void actionPerformed(ActionEvent e) {
int index = list.getSelectedIndex();
Object values[] = list.getSelectedValues();
for (Object value : values)
getModel().remove(value);
int maxIndex = list.getModel().getSize() - 1;
if (index > maxIndex)
index = maxIndex;
list.setSelectedIndex(index);
}
};
private Action removeAction = defaultRemoveAction;
public Action getRemoveAction() {
return removeAction;
}
public void setRemoveAction(Action action) {
this.removeAction = action;
}
private final AbstractAction removeHook = new AbstractAction("Remove") {
@Override
public void actionPerformed(ActionEvent e) {
if (getRemoveAction() != null && getRemoveAction().isEnabled()) {
@ -162,5 +143,5 @@ public class FileBotList<E> extends JComponent {
}
}
};
}

View File

@ -1,7 +1,5 @@
package net.filebot.ui.sfv;
import static java.lang.Math.*;
import static net.filebot.ui.sfv.ChecksumTableModel.*;
import static net.filebot.ui.transfer.BackgroundFileTransferablePolicy.*;
@ -39,47 +37,45 @@ import net.filebot.util.FileUtilities;
import net.filebot.util.ui.TunedUtilities;
import net.miginfocom.swing.MigLayout;
public class SfvPanel extends JComponent {
private final ChecksumComputationService computationService = new ChecksumComputationService();
private final ChecksumTable table = new ChecksumTable();
private final ChecksumTableTransferablePolicy transferablePolicy = new ChecksumTableTransferablePolicy(table.getModel(), computationService);
private final ChecksumTableExportHandler exportHandler = new ChecksumTableExportHandler(table.getModel());
public SfvPanel() {
table.setTransferHandler(new DefaultTransferHandler(transferablePolicy, exportHandler));
JPanel contentPane = new JPanel(new MigLayout("insets 0, nogrid, fill", "", "[fill]10px[bottom, pref!]4px"));
contentPane.setBorder(new TitledBorder("SFV"));
setLayout(new MigLayout("insets dialog, fill"));
add(contentPane, "grow");
contentPane.add(new JScrollPane(table), "grow, wrap");
contentPane.add(new JButton(loadAction), "gap left 15px");
contentPane.add(new JButton(saveAction));
contentPane.add(new JButton(clearAction), "gap right 40px");
// hash function toggle button group
ButtonGroup group = new ButtonGroup();
for (HashType hash : HashType.values()) {
JToggleButton button = new ChecksumButton(new ChangeHashTypeAction(hash));
group.add(button);
contentPane.add(button);
}
contentPane.add(new TotalProgressPanel(computationService), "gap left 35px:push, gap right 7px, hidemode 1");
// cancel and restart computations whenever the hash function has been changed
table.getModel().addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (HASH_TYPE_PROPERTY.equals(evt.getPropertyName())) {
@ -87,189 +83,176 @@ public class SfvPanel extends JComponent {
}
}
});
putClientProperty("transferablePolicy", transferablePolicy);
// Shortcut DELETE
TunedUtilities.installAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), removeAction);
TunedUtilities.installAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0), removeAction);
}
protected void restartComputation(HashType hash) {
// cancel all running computations
computationService.reset();
ChecksumTableModel model = table.getModel();
// calculate new hashes, one executor for each checksum column
Map<File, ExecutorService> executors = new HashMap<File, ExecutorService>(4);
for (ChecksumRow row : model.rows()) {
for (ChecksumCell cell : row.values()) {
if (cell.getChecksum(hash) == null && cell.getRoot().isDirectory()) {
cell.putTask(new ChecksumComputationTask(new File(cell.getRoot(), cell.getName()), hash));
ExecutorService executor = executors.get(cell.getRoot());
if (executor == null) {
executor = computationService.newExecutor();
executors.put(cell.getRoot(), executor);
}
// start computation
executor.execute(cell.getTask());
}
}
}
// start shutdown sequence for all created executors
for (ExecutorService executor : executors.values()) {
executor.shutdown();
}
}
private final SaveAction saveAction = new ChecksumTableSaveAction();
private final LoadAction loadAction = new LoadAction(transferablePolicy);
private final AbstractAction clearAction = new AbstractAction("Clear", ResourceManager.getIcon("action.clear")) {
@Override
public void actionPerformed(ActionEvent e) {
transferablePolicy.reset();
computationService.reset();
table.getModel().clear();
}
};
private final AbstractAction removeAction = new AbstractAction("Remove") {
@Override
public void actionPerformed(ActionEvent e) {
if (table.getSelectedRowCount() < 1)
return;
int[] rows = table.getSelectedRows();
if (rows.length <= 0) {
// no rows selected
return;
}
// first selected row
int selectedRow = table.getSelectedRow();
// convert view index to model index
for (int i = 0; i < rows.length; i++) {
rows[i] = table.getRowSorter().convertRowIndexToModel(rows[i]);
}
// remove selected rows
table.getModel().remove(rows);
// update computation service task count
computationService.purge();
// auto select next row
selectedRow = min(selectedRow, table.getRowCount() - 1);
table.getSelectionModel().setSelectionInterval(selectedRow, selectedRow);
}
};
protected class ChangeHashTypeAction extends AbstractAction implements PropertyChangeListener {
private ChangeHashTypeAction(HashType hash) {
super(hash.toString());
putValue(HASH_TYPE_PROPERTY, hash);
// initialize selected state
propertyChange(new PropertyChangeEvent(this, HASH_TYPE_PROPERTY, null, table.getModel().getHashType()));
transferablePolicy.addPropertyChangeListener(this);
table.getModel().addPropertyChangeListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
table.getModel().setHashType((HashType) getValue(HASH_TYPE_PROPERTY));
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (LOADING_PROPERTY.equals(evt.getPropertyName())) {
// update enabled state
setEnabled(!(Boolean) evt.getNewValue());
} else if (HASH_TYPE_PROPERTY.equals(evt.getPropertyName())) {
// update selected state
// update selected state
putValue(SELECTED_KEY, evt.getNewValue() == getValue(HASH_TYPE_PROPERTY));
}
}
}
protected class ChecksumTableSaveAction extends SaveAction {
private File selectedColumn = null;
public ChecksumTableSaveAction() {
super(exportHandler);
}
@Override
public ChecksumTableExportHandler getExportHandler() {
return (ChecksumTableExportHandler) super.getExportHandler();
}
@Override
protected boolean canExport() {
return selectedColumn != null && super.canExport();
}
@Override
protected void export(File file) throws IOException {
getExportHandler().export(file, selectedColumn);
}
@Override
protected String getDefaultFileName() {
return getExportHandler().getDefaultFileName(selectedColumn);
}
@Override
protected File getDefaultFolder() {
// use the column root as default folder in the file dialog
return selectedColumn;
}
@Override
public void actionPerformed(ActionEvent e) {
List<File> options = new ArrayList<File>();
// filter out verification file columns
for (File file : table.getModel().getChecksumColumns()) {
if (file.isDirectory())
options.add(file);
}
// can't export anything
if (options.isEmpty()) {
return;
}
try {
if (options.size() == 1) {
// auto-select option if there is only one option
@ -277,20 +260,20 @@ public class SfvPanel extends JComponent {
} else if (options.size() > 1) {
// user must select one option
SelectDialog<File> selectDialog = new SelectDialog<File>(SwingUtilities.getWindowAncestor(SfvPanel.this), options) {
@Override
protected String convertValueToString(Object value) {
return FileUtilities.getFolderName((File) value);
}
};
selectDialog.getHeaderLabel().setText("Select checksum column:");
selectDialog.setLocationRelativeTo(SfvPanel.this);
selectDialog.setVisible(true);
this.selectedColumn = selectDialog.getSelectedValue();
}
if (this.selectedColumn != null) {
// continue if a column was selected
super.actionPerformed(e);
@ -301,5 +284,5 @@ public class SfvPanel extends JComponent {
}
}
}
}

View File

@ -95,11 +95,20 @@ public final class TunedUtilities {
public static void installAction(JComponent component, KeyStroke keystroke, Action action) {
Object key = action.getValue(Action.NAME);
if (key == null)
if (key == null) {
throw new IllegalArgumentException("Action must have a name");
}
component.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(keystroke, key);
component.getActionMap().put(key, action);
// automatically add Mac OS X compatibility (on Mac the BACKSPACE key is called DELETE, and there is no DELETE key)
if (keystroke.getKeyCode() == KeyEvent.VK_DELETE) {
KeyStroke macKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, keystroke.getModifiers(), keystroke.isOnKeyRelease());
Object macKey = "mac." + action.getValue(Action.NAME);
component.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(macKeyStroke, macKey);
component.getActionMap().put(macKey, action);
}
}
public static UndoManager installUndoSupport(JTextComponent component) {