/*
 * Created on 09.09.2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package TUGLaby;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseListener;
import java.util.HashMap;

import javax.swing.JPanel;

/**
 * @author user
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class SensorChoosePanel extends JPanel {

    private HashMap brick_images_;

    /**
     *  
     */
    private Image current_image_;

    private int internal_state_;

    public final static int WALL = 1;
    public final static int FREE = 2;

    public SensorChoosePanel(HashMap brick_images) {
        super();
        // TODO Auto-generated constructor stub
        brick_images_ = brick_images;
        this.setBackground(Color.green);
        internal_state_ = FREE;
        current_image_ = (Image) brick_images_.get("FREE_GROUND");
        this.setToolTipText("Anklicken um den Sensortyp zu ändern.");
        initMouseListener();
    }    
    
    public SensorChoosePanel(HashMap brick_images, int init_state) {
        super();
        // TODO Auto-generated constructor stub
        brick_images_ = brick_images;
        this.setBackground(Color.green);
        setInternalState( init_state );
    
        this.setToolTipText("Anklicken um den Sensortyp zu ändern.");
        initMouseListener();
    }
    
    private void setInternalState( int new_state ) {
        // MUST BE REWRITTEN IF A NEW CONST IS ADDED !!!
        if( (new_state >= WALL) && (new_state <=FREE))
            internal_state_ = new_state;
        paintState();
    }
    
    public int getInternalState() {
        return internal_state_;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.drawImage(current_image_, 0, 0, this);

    }
    private void paintState() {
        if (internal_state_ == WALL) {

            current_image_ = (Image) brick_images_.get("WALL_GROUND");
          
        } else if (internal_state_ == FREE) {
      
            current_image_ = (Image) brick_images_.get("FREE_GROUND");
         
        }
        SensorChoosePanel.this.repaint();
      
    }
    private void initMouseListener() {
        this.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                if (internal_state_ == FREE) {
                    internal_state_ = WALL;
                } else if (internal_state_ == WALL) {
                    internal_state_ = FREE;
                }
                paintState();
            }
        });
    }
}
