/*
 * Copyright (C) 1999-2006 <a href="mailto:adrian.paschke@gmx.de">Adrian Paschke</a>
 * 
 * The RBSLM Environment is part of the Rule Based Service Level Agreement (RBSLA)
 * framework, available at 
 * <a href=" http://ibis.in.tum.de/staff/paschke/rbsla/index.htm">RBSLA</a>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package rbsla.environment.view;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.Hashtable;

import javax.swing.JComponent;
import javax.swing.JPanel;

public class GraphicTrueFalseViewer implements Viewer
{
    final static String defaultFalseImageUrl = "rules/image/computer_tower_smoking_lg_wm.gif";

    final static String defaultTrueImageUrl = "rules/image/computer_info_scroll_lg_wm.gif";

    ImagePanel imagePanel;

    Hashtable viewProperties;

    // TODO GraphicTrueFalseViewer Mnchmal erscheint Text im Bild
    public Hashtable getViewerPropertieNames()
    {
        Hashtable trueFalseViewerProperties;
        trueFalseViewerProperties = new Hashtable();
        trueFalseViewerProperties.put("trueImagePath", defaultTrueImageUrl);
        trueFalseViewerProperties.put("falseImagePath", defaultFalseImageUrl);
        return trueFalseViewerProperties;
    }

    /*
     * the default constructor be called from Class.forname
     */
    public JComponent getContentPane(Hashtable _viewProperties)
    {
        viewProperties = _viewProperties;

        // Create and set up the content pane.
        String falseImagePath = (String) viewProperties.get("falseImagePath");
        Image falseImage = Toolkit.getDefaultToolkit().getImage(falseImagePath);
        imagePanel = new ImagePanel(falseImage);
        return imagePanel;
    }

    /**
     * Creates a chart.
     * 
     * @param dataset
     *            the dataset.
     * 
     * @return a chart.
     */
    public void dataChanged(String result, String debugText) throws Throwable
    {
        String trueImagePath = (String) viewProperties.get("trueImagePath");
        String falseImagePath = (String) viewProperties.get("falseImagePath");
        Image image;
        if (result.indexOf("true") > 0)
            image = Toolkit.getDefaultToolkit().getImage(trueImagePath);
        else
            image = Toolkit.getDefaultToolkit().getImage(falseImagePath);
        imagePanel.setImage(image);
    }
}

class ImagePanel extends JPanel
{
    Image image;

    public ImagePanel(Image bild)
    {
        super(new BorderLayout());
        //this.setPreferredSize(new java.awt.Dimension(200, 200));
    }

    public void setImage(Image _image)
    {
        // repaint, if new image
        // compared by pointer
        if (!_image.equals(image))
        {
            image = _image;
            repaint();
        }
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, this);
    }
}


