ru.sbtqa.monte.ilbmdemo.ILBMViewer Maven / Gradle / Ivy
/* @(#)ILBMViewer.java
* Copyright © 2012 Werner Randelshofer, Switzerland.
* You may only use this software in accordance with the license terms.
*/
package ru.sbtqa.monte.ilbmdemo;
import java.awt.Image;
import java.awt.Toolkit;
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.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import ru.sbtqa.monte.media.gui.Worker;
import ru.sbtqa.monte.media.ilbm.ColorCyclingMemoryImageSource;
import ru.sbtqa.monte.media.ilbm.ILBMDecoder;
import ru.sbtqa.monte.media.io.ByteArrayImageInputStream;
import ru.sbtqa.monte.media.io.IOStreams;
/**
* ILBMViewer.
*
* @author Werner Randelshofer
* @version $Id: ILBMViewer.java 364 2016-11-09 19:54:25Z werner $
*/
public class ILBMViewer 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);
showILBMImages(files);
} catch (IOException e) {
JOptionPane.showConfirmDialog(
ILBMViewer.this,
"Could not access the dropped data.",
"ILBMViewer: Drop Failed",
JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
} catch (UnsupportedFlavorException e) {
JOptionPane.showConfirmDialog(
ILBMViewer.this,
"Unsupported data flavor.",
"ILBMViewer: 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 ILBMViewer
*/
public ILBMViewer() {
initComponents();
new DropTarget(this, handler);
new DropTarget(label, handler);
}
protected BufferedImage getAmigaPicture(File f) throws IOException {
return ImageIO.read(f);
}
protected BufferedImage getAmigaPictureViaByteArray(File f) throws IOException {
return getAmigaPicture(getData(f));
}
protected byte[] getData(File f) throws IOException {
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
FileInputStream in = new FileInputStream(f)) {
IOStreams.copy(in, out);
return out.toByteArray();
}
}
protected BufferedImage getAmigaPicture(byte[] data) throws IOException {
return ImageIO.read(new ByteArrayImageInputStream(data));
}
protected Image getAmigaPictureWithColorCycling(File f) throws IOException {
FileInputStream in = new FileInputStream(f);
try {
ColorCyclingMemoryImageSource ccmis = new ILBMDecoder(in).produce().get(0);
if (ccmis.isColorCyclingAvailable()) {
ccmis.start();
}
return Toolkit.getDefaultToolkit().createImage(ccmis);
} finally {
in.close();
}
}
public void showILBMImages(final List files) {
label.setEnabled(false);
if (label.getIcon() instanceof ImageIcon) {
ImageIcon icon = (ImageIcon) label.getIcon();
label.setIcon(null);
label.setDisabledIcon(null);
icon.getImage().flush();
}
new Worker() {
@Override
protected Image construct() throws Exception {
for (File f : files) {
//return getAmigaPicture(f);
//return getAmigaPictureViaByteArray(f);
return getAmigaPictureWithColorCycling(f);
}
return null;
}
@Override
protected void done(Image value) {
if (value == null) {
failed(new IOException("Could not load image."));
return;
}
label.setText(null);
ImageIcon icon = new ImageIcon(value);
label.setIcon(icon);
label.setDisabledIcon(icon);
SwingUtilities.getWindowAncestor(ILBMViewer.this).pack();
}
@Override
protected void failed(Throwable error) {
label.setText("Error
" + error.getMessage());
SwingUtilities.getWindowAncestor(ILBMViewer.this).pack();
}
@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 ILBM 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("ILBM Image Viewer");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new ILBMViewer());
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
}