ru.sbtqa.monte.media.mpo.MPOImageSplitter Maven / Gradle / Ivy
/* @(#)MPOImageSplitter.java
* Copyright © 2011-2012 Werner Randelshofer, Switzerland.
* You may only use this software in accordance with the license terms.
*/
package ru.sbtqa.monte.media.mpo;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import ru.sbtqa.monte.media.gui.BackgroundTask;
/**
* MPOImageSplitter.
*
* @author Werner Randelshofer
* @version $Id: MPOImageSplitter.java 364 2016-11-09 19:54:25Z werner $
*/
public class MPOImageSplitter extends javax.swing.JPanel {
private final static long serialVersionUID = 1L;
private class Handler implements DropTargetListener {
/**
* Called when a drag operation has encountered the
* DropTarget
.
*
* @param dtde the DropTargetDragEvent
*/
@Override
public void dragEnter(DropTargetDragEvent event) {
if (event.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
event.acceptDrag(DnDConstants.ACTION_COPY);
} else {
event.rejectDrag();
}
}
/**
* The drag operation has departed the DropTarget
without
* dropping.
*
* @param dte the DropTargetEvent
*/
@Override
public void dragExit(DropTargetEvent event) {
// Nothing to do
}
/**
* Called when a drag operation is ongoing on the
* DropTarget
.
*
* @param dtde the DropTargetDragEvent
*/
@Override
public void dragOver(DropTargetDragEvent event) {
if (event.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
event.acceptDrag(DnDConstants.ACTION_COPY);
} else {
event.rejectDrag();
}
}
/**
* The drag operation has terminated with a drop on this
* DropTarget
. This method is responsible for undertaking
* the transfer of the data associated with the gesture. The
* DropTargetDropEvent
provides a means to obtain a
* Transferable
object that represents the data object(s)
* to be transfered.
* From this method, the DropTargetListener
shall accept or
* reject the drop via the acceptDrop(int dropAction) or rejectDrop()
* methods of the DropTargetDropEvent
parameter.
*
* Subsequent to acceptDrop(), but not before,
* DropTargetDropEvent
's getTransferable() method may be
* invoked, and data transfer may be performed via the returned
* Transferable
's getTransferData() method.
*
* At the completion of a drop, an implementation of this method is
* required to signal the success/failure of the drop by passing an
* appropriate boolean
to the
* DropTargetDropEvent
's dropComplete(boolean success)
* method.
*
* Note: The actual processing of the data transfer is not required to
* finish before this method returns. It may be deferred until later.
*
* @param dtde the DropTargetDropEvent
*/
@Override
@SuppressWarnings("unchecked")
public void drop(DropTargetDropEvent event) {
if (event.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
event.acceptDrop(DnDConstants.ACTION_COPY);
try {
List files = (List) event.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
splitMPOFiles(files);
} catch (IOException e) {
JOptionPane.showConfirmDialog(
MPOImageSplitter.this,
"Could not access the dropped data.",
"MPOImageSplitter: Drop Failed",
JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
} catch (UnsupportedFlavorException e) {
JOptionPane.showConfirmDialog(
MPOImageSplitter.this,
"Unsupported data flavor.",
"MPOImageSplitter: Drop Failed",
JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
}
} else {
event.rejectDrop();
}
}
/**
* Called if the user has modified the current drop gesture.
*
* @param dtde the DropTargetDragEvent
*/
@Override
public void dropActionChanged(DropTargetDragEvent event) {
// Nothing to do
}
}
private Handler handler = new Handler();
/**
* Creates new form MPOImageSplitter
*/
public MPOImageSplitter() {
initComponents();
new DropTarget(this, handler);
new DropTarget(label, handler);
}
public void splitMPOFiles(final List files) {
label.setEnabled(false);
new BackgroundTask() {
@Override
protected void construct() throws Exception {
for (File f : files) {
MPOFiles.splitMPOFile(f);
}
}
@Override
protected void finished() {
label.setEnabled(true);
}
}.start();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// //GEN-BEGIN:initComponents
private void initComponents() {
label = new javax.swing.JLabel();
setLayout(new java.awt.BorderLayout());
label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
label.setText("Drop MPO file here.");
add(label, java.awt.BorderLayout.CENTER);
}// //GEN-END:initComponents
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame("MPO Image Splitter");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new MPOImageSplitter());
f.setSize(200, 200);
f.setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel label;
// End of variables declaration//GEN-END:variables
}