From 72f3c375e9fc6ff953fc9f161fd957df505ae638 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sat, 19 Mar 2016 21:03:12 +0000 Subject: [PATCH] Improved support for DnD and startup file args --- source/net/filebot/ui/list/ListPanel.java | 107 ++++++++++-------- source/net/filebot/ui/sfv/SfvPanel.java | 19 ++++ .../ui/subtitle/SubtitleDropTarget.java | 2 +- .../filebot/ui/subtitle/SubtitlePanel.java | 20 ++++ 4 files changed, 100 insertions(+), 48 deletions(-) diff --git a/source/net/filebot/ui/list/ListPanel.java b/source/net/filebot/ui/list/ListPanel.java index 0efabefb..6960f7a1 100644 --- a/source/net/filebot/ui/list/ListPanel.java +++ b/source/net/filebot/ui/list/ListPanel.java @@ -3,10 +3,13 @@ package net.filebot.ui.list; import static java.awt.Font.*; import static java.lang.Math.*; import static net.filebot.Logging.*; +import static net.filebot.Logging.log; import static net.filebot.media.MediaDetection.*; +import static net.filebot.util.ui.SwingUI.*; import java.awt.BorderLayout; import java.awt.Font; +import java.awt.datatransfer.Transferable; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.text.NumberFormat; @@ -18,7 +21,6 @@ import java.util.logging.Level; import javax.script.Bindings; import javax.script.SimpleBindings; -import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JLabel; @@ -29,14 +31,17 @@ import javax.swing.JTextField; import javax.swing.KeyStroke; import javax.swing.SpinnerNumberModel; +import com.google.common.eventbus.Subscribe; + import net.filebot.ResourceManager; import net.filebot.format.ExpressionFormat; import net.filebot.ui.FileBotList; import net.filebot.ui.FileBotListExportHandler; import net.filebot.ui.transfer.LoadAction; import net.filebot.ui.transfer.SaveAction; +import net.filebot.ui.transfer.TransferablePolicy; +import net.filebot.ui.transfer.TransferablePolicy.TransferAction; import net.filebot.util.ExceptionUtilities; -import net.filebot.util.ui.SwingUI; import net.miginfocom.swing.MigLayout; public class ListPanel extends JComponent { @@ -71,7 +76,7 @@ public class ListPanel extends JComponent { add(fromSpinner, "gap related, wmax 14mm, sizegroup spinner, sizegroupy editor"); add(new JLabel("To:"), "gap 5mm"); add(toSpinner, "gap related, wmax 14mm, sizegroup spinner, sizegroupy editor"); - add(new JButton(createAction), "gap 7mm, gapafter indent, wrap paragraph"); + add(newButton("Create", ResourceManager.getIcon("action.export"), this::create), "gap 7mm, gapafter indent, wrap paragraph"); add(list, "grow"); @@ -82,62 +87,70 @@ public class ListPanel extends JComponent { list.add(buttonPanel, BorderLayout.SOUTH); - SwingUI.installAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), createAction); + installAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), newAction("Create", this::create)); } - private AbstractAction createAction = new AbstractAction("Create", ResourceManager.getIcon("action.export")) { + public void create(ActionEvent evt) { + // clear selection + list.getListComponent().clearSelection(); - @Override - public void actionPerformed(ActionEvent evt) { + int from = fromSpinnerModel.getNumber().intValue(); + int to = toSpinnerModel.getNumber().intValue(); - // clear selection - list.getListComponent().clearSelection(); + try { + ExpressionFormat format = new ExpressionFormat(textField.getText()); - int from = fromSpinnerModel.getNumber().intValue(); - int to = toSpinnerModel.getNumber().intValue(); + // pad episode numbers with zeros (e.g. %02d) so all numbers have the same number of digits + NumberFormat numberFormat = NumberFormat.getIntegerInstance(); + numberFormat.setMinimumIntegerDigits(max(2, Integer.toString(max(from, to)).length())); + numberFormat.setGroupingUsed(false); - try { - ExpressionFormat format = new ExpressionFormat(textField.getText()); + List names = new ArrayList(); - // pad episode numbers with zeros (e.g. %02d) so all numbers have the same number of digits - NumberFormat numberFormat = NumberFormat.getIntegerInstance(); - numberFormat.setMinimumIntegerDigits(max(2, Integer.toString(max(from, to)).length())); - numberFormat.setGroupingUsed(false); + int min = min(from, to); + int max = max(from, to); - List names = new ArrayList(); + for (int i = min; i <= max; i++) { + Bindings bindings = new SimpleBindings(); - int min = min(from, to); - int max = max(from, to); + // strings + bindings.put("i", numberFormat.format(i)); - for (int i = min; i <= max; i++) { - Bindings bindings = new SimpleBindings(); + // numbers + bindings.put("index", i); + bindings.put("from", from); + bindings.put("to", to); - // strings - bindings.put("i", numberFormat.format(i)); - - // numbers - bindings.put("index", i); - bindings.put("from", from); - bindings.put("to", to); - - names.add(format.format(bindings)); - } - - if (signum(to - from) < 0) { - Collections.reverse(names); - } - - // try to match title from the first five names - Collection title = getSeriesNameMatcher(true).matchAll((names.size() < 5 ? names : names.subList(0, 4)).toArray(new String[0])); - - list.setTitle(title.isEmpty() ? "List" : title.iterator().next()); - - list.getModel().clear(); - list.getModel().addAll(names); - } catch (Exception e) { - log.log(Level.WARNING, ExceptionUtilities.getMessage(e), e); + names.add(format.format(bindings)); } + + if (signum(to - from) < 0) { + Collections.reverse(names); + } + + // try to match title from the first five names + Collection title = getSeriesNameMatcher(true).matchAll((names.size() < 5 ? names : names.subList(0, 4)).toArray(new String[0])); + + list.setTitle(title.isEmpty() ? "List" : title.iterator().next()); + + list.getModel().clear(); + list.getModel().addAll(names); + } catch (Exception e) { + log.log(Level.WARNING, ExceptionUtilities.getMessage(e), e); } - }; + } + + @Subscribe + public void handle(Transferable transferable) { + TransferablePolicy handler = list.getTransferablePolicy(); + + try { + if (handler != null && handler.accept(transferable)) { + handler.handleTransferable(transferable, TransferAction.ADD); + } + } catch (Exception e) { + debug.log(Level.WARNING, "Failed to handle transferable: " + transferable, e); + } + } } diff --git a/source/net/filebot/ui/sfv/SfvPanel.java b/source/net/filebot/ui/sfv/SfvPanel.java index 9b597d0c..5fa0be7a 100644 --- a/source/net/filebot/ui/sfv/SfvPanel.java +++ b/source/net/filebot/ui/sfv/SfvPanel.java @@ -1,11 +1,13 @@ package net.filebot.ui.sfv; import static java.lang.Math.*; +import static net.filebot.Logging.*; import static net.filebot.ui.sfv.ChecksumTableModel.*; import static net.filebot.ui.transfer.BackgroundFileTransferablePolicy.*; import static net.filebot.util.FileUtilities.*; import static net.filebot.util.ui.SwingUI.*; +import java.awt.datatransfer.Transferable; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.beans.PropertyChangeEvent; @@ -17,6 +19,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ExecutorService; +import java.util.logging.Level; import javax.swing.AbstractAction; import javax.swing.ButtonGroup; @@ -29,6 +32,8 @@ import javax.swing.KeyStroke; import javax.swing.SwingUtilities; import javax.swing.border.TitledBorder; +import com.google.common.eventbus.Subscribe; + import net.filebot.ResourceManager; import net.filebot.hash.HashType; import net.filebot.ui.SelectDialog; @@ -36,6 +41,7 @@ import net.filebot.ui.transfer.DefaultTransferHandler; import net.filebot.ui.transfer.LoadAction; import net.filebot.ui.transfer.SaveAction; import net.filebot.ui.transfer.TransferablePolicy; +import net.filebot.ui.transfer.TransferablePolicy.TransferAction; import net.filebot.util.FileUtilities; import net.miginfocom.swing.MigLayout; @@ -127,6 +133,19 @@ public class SfvPanel extends JComponent { } } + @Subscribe + public void handle(Transferable transferable) { + TransferablePolicy handler = getTransferablePolicy(); + + try { + if (handler != null && handler.accept(transferable)) { + handler.handleTransferable(transferable, TransferAction.ADD); + } + } catch (Exception e) { + debug.log(Level.WARNING, "Failed to handle transferable: " + transferable, e); + } + } + private final SaveAction saveAction = new ChecksumTableSaveAction(); private final LoadAction loadAction = new LoadAction(this::getTransferablePolicy); diff --git a/source/net/filebot/ui/subtitle/SubtitleDropTarget.java b/source/net/filebot/ui/subtitle/SubtitleDropTarget.java index 305691eb..4ddf1add 100644 --- a/source/net/filebot/ui/subtitle/SubtitleDropTarget.java +++ b/source/net/filebot/ui/subtitle/SubtitleDropTarget.java @@ -45,7 +45,7 @@ import net.filebot.web.VideoHashSubtitleService; abstract class SubtitleDropTarget extends JButton { - private enum DropAction { + public enum DropAction { Accept, Cancel } diff --git a/source/net/filebot/ui/subtitle/SubtitlePanel.java b/source/net/filebot/ui/subtitle/SubtitlePanel.java index a8ee5060..e0e765f6 100644 --- a/source/net/filebot/ui/subtitle/SubtitlePanel.java +++ b/source/net/filebot/ui/subtitle/SubtitlePanel.java @@ -12,8 +12,10 @@ import java.awt.Dialog.ModalityType; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; +import java.awt.datatransfer.Transferable; import java.awt.event.ActionEvent; import java.awt.geom.Path2D; +import java.io.File; import java.net.URI; import java.util.ArrayList; import java.util.Collection; @@ -38,6 +40,8 @@ import javax.swing.JTextField; import javax.swing.SwingUtilities; import javax.swing.border.TitledBorder; +import com.google.common.eventbus.Subscribe; + import net.filebot.Language; import net.filebot.ResourceManager; import net.filebot.Settings; @@ -46,6 +50,8 @@ import net.filebot.media.MediaDetection; import net.filebot.ui.AbstractSearchPanel; import net.filebot.ui.LanguageComboBox; import net.filebot.ui.SelectDialog; +import net.filebot.ui.subtitle.SubtitleDropTarget.DropAction; +import net.filebot.ui.transfer.FileTransferable; import net.filebot.util.ui.LabelProvider; import net.filebot.util.ui.LinkButton; import net.filebot.util.ui.SimpleLabelProvider; @@ -74,6 +80,20 @@ public class SubtitlePanel extends AbstractSearchPanel files = FileTransferable.getFilesFromTransferable(transferable); + + if (files != null && files.size() > 0 && target.getDropAction(files) != DropAction.Cancel) { + target.handleDrop(files); + } + } catch (Exception e) { + debug.log(Level.WARNING, "Failed to handle transferable: " + transferable, e); + } + } + private final SubtitleDropTarget uploadDropTarget = new SubtitleDropTarget.Upload() { @Override