/*
 * @(#)JPDFView.java  1.0  2008-09-19
 *
 * Copyright (c) 2008 Werner Randelshofer, Immensee, Switzerland.
 * All rights reserved.
 *
 * You may not use, copy or modify this file, except in compliance with the
 * license agreement you entered into with Werner Randelshofer.
 * For details see accompanying license terms.
 */
package ch.randelshofer.view;

import ch.randelshofer.pdf.JPDFFilePanel;
import com.sun.pdfview.PDFFile;
import java.awt.*;
import java.awt.dnd.*;
import java.io.*;

import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;

/**
 * View for Adobe Acrobat files (.PDF).
 *
 * @author Werner Randelshofer, Hausmatt 10, CH-6405 Immensee, Switzerland
 * @version 1.1 2002-02-10 Adapted for AcrobatBean 1.1.
 * <br>1.0 2000-10-09
 */
public class JPDFView extends AbstractView {

    private RandomAccessFile rafFile;
    private JPDFFilePanel viewer;

    /** Creates new form AdobeAcrobatPDFView */
    public JPDFView() {
        initComponents();
    }

    /**
     * Sets the command context for the bean. The receiver loads
     * the image data provided by the data handler and displays it.
     */
    @Override
    public void setCommandContext(String verb, DataHandler dh)
            throws IOException {
        try {
            dispose();
            viewer = new JPDFFilePanel();

            File file = null;
            DataSource source = dh.getDataSource();
            if (source instanceof FileDataSource) {
                file = ((FileDataSource) source).getFile();
            } else {
                throw new IOException("cannot retrieve File from data source");
            }
            rafFile = new RandomAccessFile(file, "r");
            FileChannel channel = rafFile.getChannel();
            ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY,
                    0, channel.size());
            PDFFile pdfFile = new PDFFile(buf);


            viewer.setPDF(pdfFile);
            add(viewer, BorderLayout.CENTER);


        } catch (IOException e) {
            throw e;
        } catch (Exception e) {
            IOException ioe = new IOException(e.toString());
            ioe.initCause(e);
            throw ioe;
        }
    }

    @Override
    public void addNotify() {
        super.addNotify();
        try {
            if (viewer != null) {
//                viewer.activate();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean hasScrollPane() {
        return true;
    }

    protected void updateViewProperties() {
        getViewProperties().put("title", " as PDF");
    }

    public String toString() {
        return "Java PDF-Renderer View";
    }

    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setLayout(new java.awt.BorderLayout());
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Disposes all resources allocated by this viewer.
     * The viewer must not be used after this method has been called.
     */
    @Override
    public void dispose() {
        if (viewer != null) {
            removeAll();
//            viewer.deactivate();
//            viewer.destroy();
            viewer = null;
        }
        if (rafFile != null) {
            try {
                rafFile.close();
            } catch (IOException ex) {
                // oh well
            }
            rafFile = null;
        }
    }

    /**
     * This method adds the given drop target listener to
     * all components of the viewer.
     */
    public void addDropTargetListener(DropTargetListener listener) {
        new DropTarget(this, listener);
        new DropTarget(viewer, listener);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
