/*
 * @(#)PBMView.java  2.0  2008-08-02
 *
 * Copyright (c) 2005-2010 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.ilbm.ColorCyclingMemoryImageSource;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.activation.DataHandler;
import java.awt.Image;
import java.io.IOException;
import org.monte.media.pbm.PBMDecoder;
import ch.randelshofer.util.PropertyModel;
import java.awt.event.ItemEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.ImageProducer;
import java.beans.PropertyChangeEvent;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.AbstractAction;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JSeparator;
import javax.swing.KeyStroke;
import org.jhotdraw.util.ResourceBundleUtil;

/**
 * This viewer displays images of type IFF PBM.
 *
 * @version 2.0 2010-08-02 Added support for color cycling.
 * <br>1.0 2005-05-04 Created.
 */
public class PBMView extends ILBMView {

    /** Initializes the Form */
    public PBMView() {
    }

    @Override
    public void setCommandContext(String verb, DataHandler dh)
            throws IOException {
        dispose();
        if (dh != null) {
            InputStream in = dh.getInputStream();
            try {
                PBMDecoder decoder = new PBMDecoder(in);
                ArrayList<ColorCyclingMemoryImageSource> v = decoder.produce();
                if (v.size() > 0) {
                    imageSource = v.get(0);
                    imagePanel.setImage(getToolkit().createImage(imageSource));
                    colorCyclingItem.setEnabled(imageSource.isColorCyclingAvailable());
                    blendColorCyclesItem.setEnabled(imageSource.isColorCyclingAvailable());
                }
            } finally {
                in.close();
            }
        }
    }

    @Override
    public String getViewName() {
        return "IFF PBM View";
    }

    @Override
    public String toString() {
        StringBuffer buf = new StringBuffer();
        buf.append(super.toString());

        Image image = imagePanel.getImage();
        Object value;
        if (image != null) {
            buf.append("\n\nPBM:");

            value = image.getProperty("screenMode", this);
            if (value != Image.UndefinedProperty && value != null) {
                buf.append("\n   Screen Mode: ");
                buf.append(value);
            }
            value = image.getProperty("nbPlanes", this);
            if (value != Image.UndefinedProperty && value != null) {
                buf.append("\n   Number of Planes: ");
                buf.append(value);
            }
            value = image.getProperty("colorCycling", this);
            if (value != Image.UndefinedProperty && value != null) {
                buf.append("\n   Color Cycles: ");
                buf.append(value);
            }
        }
        return buf.toString();
    }
}
