/*
 * @(#)EightSVXView.java  1.2  2006-07-20
 *
 * Copyright (c) 1999-2006 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 org.monte.media.eightsvx.EightSVXDecoder;


import java.awt.*;
import java.awt.dnd.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import java.applet.AudioClip;


import java.util.Enumeration;
import java.util.Vector;

import java.io.IOException;
import java.io.InputStream;

import javax.activation.DataHandler;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JPanel;
import javax.swing.JScrollPane;


/**
 * @author  Werner Randelshofer, Hausmatt 10, CH-6405 Immensee, Switzerland
 * @version 1.2 2006-07-20 Reworked for Java 1.5.
 * <br> 1.1 2002-02-10 Support for DnD added.
 * <br> 1.0.1 2000-09-25
 * <br> 1.0 1999-10-19
 */
public class EightSVXView extends AbstractView implements AudioClip, ActionListener {

    private JList list_;
    private DefaultListModel samples_;
    private JScrollPane scrollPane;

    public EightSVXView() {
        setLayout(new BorderLayout(0, 0));
        samples_ = new DefaultListModel();
        list_ = new JList(samples_);
        add("Center", scrollPane = new JScrollPane(list_));

        JPanel controls = new JPanel();
        //controls.setLayout(new FlowLayout()); // --> already set by java.awt.Panel
        JButton button = new JButton("Play");
        button.setActionCommand("play");
        button.addActionListener(this);
        controls.add(button);
        button = new JButton("Loop");
        button.setActionCommand("loop");
        button.addActionListener(this);
        controls.add(button);
        button = new JButton("Stop");
        button.setActionCommand("stop");
        button.addActionListener(this);
        controls.add(button);
        add("South", controls);
    }

    public void actionPerformed(ActionEvent event) {
        String command = event.getActionCommand();
        if (command.equals("play")) {
            stop();
            play();
        } else if (command.equals("loop")) {
            stop();
            loop();
        } else if (command.equals("stop")) {
            stop();
        }
    }

    public void play() {
        AudioClip clip = (AudioClip) list_.getSelectedValue();
        if (clip != null) {
            setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
            clip.play();
            setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        }
    }

    public void loop() {
        AudioClip clip = (AudioClip) list_.getSelectedValue();
        if (clip != null) {
            setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
            clip.loop();
            setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        }
    }

    public void stop() {
        Enumeration enm = samples_.elements();
        while (enm.hasMoreElements()) {
            ((AudioClip) enm.nextElement()).stop();
        }
    }

    /** Sets the command context for the bean. The receiver loads
     * the image data provided by the data handler and displays it. */
    public void setCommandContext(String verb, DataHandler dh)
            throws IOException {
        stop();
        samples_.removeAllElements();
        if (dh != null) {
            InputStream in = dh.getInputStream();
            try {
                EightSVXDecoder decoder = new EightSVXDecoder(in);
                Vector v = decoder.getSamples();
                Enumeration enm = v.elements();
                while (enm.hasMoreElements()) {
                    samples_.addElement(enm.nextElement());
                    list_.setSelectedIndex(0);
                }
            } finally {
                in.close();
            }
        }
    }

    public void removeNotify() {
        stop();
        super.removeNotify();
    }

    /**
     * Adds menu items to the "View" menu.
     *
     * When the viewer is added to a container with a
     * JMenuBar. Then the container asks the viewer to
     * add his menu items to the "View" menu of the container.
     */
    public void mergeViewMenuItems(JMenu m) {
    }

    /**
     * Adds menu items to the "Settings" menu.
     *
     * When the viewer is added to a container with a
     * JMenuBar. Then the container asks the viewer to
     * add his menu items to the "Settings" menu of the container.
     */
    public void mergeSettingsMenuItems(JMenu m) {
    }

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

    protected void updateApplicationProperties() {
    }

    /**
     * Frees all resources allocated by this viewer.
     */
    public void dispose() {
        stop();
    }

    /**
     * Returns true if this viewer has a scrollpane of its own.
     * The container that contains this viewers uses this method to
     * determine if it should wrap a scrollpane around the viewer
     * or not.
     *
     * @return  true if this viewer has a scrollpane of its own.
     */
    public boolean hasScrollPane() {
        return true;
    }

    /**
     * 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(list_, listener);
        new DropTarget(scrollPane, listener);
    }

    public String toString() {
        return "IFF 8SVX View";
    }
}
