/*
 * @(#)ZipView.java  1.0  2002-02-11
 *
 * Copyright (c) 1999 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.activation.*;
import ch.randelshofer.gui.*;

import java.awt.*;
import java.awt.dnd.*;
import java.io.*;
import java.util.zip.*;

import javax.swing.*;
import javax.swing.tree.*;
import javax.activation.*;

/**
 * View for ZIP files.
 *
 * @author  Werner Randelshofer
 * @version 1.0 2002-02-011
 */
public class ZipView extends AbstractView implements ViewFactory {
    private ZipTreeModel treeModel;
    
    /** Creates new form ZipView */
    public ZipView() {
        initComponents();
        explorer.setViewFactory(this);
    }
    
    /** 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.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        explorer = new ch.randelshofer.gui.Explorer();

        setLayout(new java.awt.BorderLayout());

        add(explorer, java.awt.BorderLayout.CENTER);

    }//GEN-END:initComponents
    
    protected void updateViewProperties() {
        getViewProperties().put("title", " as Zip Archive");
    }
    /**
     * 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(explorer, listener);
    }
    
    /**
     * Disposes all resources allocated by this viewer.
     * The viewer must not be used after this method has been called.
     */
    public void dispose() {
        if (explorer.getTreeModel() instanceof ZipTreeModel) {
            ((ZipTreeModel) explorer.getTreeModel()).dispose();
        }
    }
    
    /**
     * Sets the command context for the bean. The receiver loads
     * the data provided by the data handler and displays it.
     */
    public void setCommandContext(String verb, DataHandler dh)
    throws IOException {
        dispose();
        if (dh != null) {
            DataSource source = dh.getDataSource();
            if (source instanceof FileDataSource) {
                treeModel = new ZipTreeModel();
                treeModel.setModel(((FileDataSource) source).getFile());
                explorer.setTreeModel(treeModel);
            } else {
                treeModel = new ZipTreeModel();
                treeModel.setModel(source.getInputStream());
                explorer.setTreeModel(treeModel);
            }
        }
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private ch.randelshofer.gui.Explorer explorer;
    // End of variables declaration//GEN-END:variables
    
    public String toString() {
        return "ZIP Archive View";
    }
    
    /**
     * Sets the value of the viewer to value.
     *
     * @param parent This is the component into which the viewer will be
     * embedded.
     * @param value This is the object to be displayed.
     */
    public Component getComponent(Component parent, Object value) {
        DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) value;
        ZipTreeModel.ZipNode zipNode = (ZipTreeModel.ZipNode) treeNode.getUserObject();
        if (treeNode.getAllowsChildren() == true) {
            return null;
        } else {
            ZipEntry zipEntry = zipNode.getZipEntry();
            DataSource dataSource = treeModel.createDataSource(zipNode);
            
            DataHandler dataHandler = new DataHandler(dataSource);
            CommandInfo[] infos = dataHandler.getAllCommands();
            if (infos.length > 0) {
                try {
                    return (Component) infos[0].getCommandObject(dataHandler, getClass().getClassLoader());
                } catch (Exception e) {
                    e.printStackTrace();
                    return new JLabel(e.toString());
                }
            } else {
                return new JLabel(zipEntry.toString());
            }
        }
    }

    public boolean hasScrollPane() {
        return true;
    }

}