import java.awt.*;
import java.net.*;
import java.awt.Image;
import java.lang.*;
import java.applet.Applet;
import java.awt.image.ImageFilter;
import java.awt.image.RGBImageFilter;
import java.awt.image.ImageProducer;
import java.awt.image.FilteredImageSource;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.ImageIcon;





public class GPIOpanel extends JPanel 
{
    Color activeColor = java.awt.Color.red;
    Color lowColor = java.awt.Color.black;
    java.awt.Image baseIconImage;
    int L=14;
    int T=40;
    int S1=30;
    int S2=100;
    int W=106;
    int H=24;
    java.awt.Color ForegColor = new java.awt.Color(102,102,153);
    java.awt.Color BackgColor = new java.awt.Color(204,204,204);

		
    public GPIOpanel(java.awt.Image baseIconImage, int pn)
    {
      
        unSelIcon.setImage(makeBullet(java.awt.Color.gray, baseIconImage));
        SelIcon.setImage(makeBullet(activeColor, baseIconImage));
		pinName.setHorizontalAlignment(javax.swing.JLabel.LEFT);
		pinName.setText("GPIO"+pn);
		pinName.setForeground(ForegColor);
		add(pinName);
		pinName.setBounds(L,0,60,24);
		output.setText("output");
		output.setRolloverEnabled(true);
		output.setForeground(ForegColor);
		directionPanel.add(output);
		add(output);
		output.setBounds(L,T,W,H);
		output.setIcon(unSelIcon);
		output.setSelectedIcon(SelIcon);
		output.setDisabledIcon(unSelIcon);
		output.setDisabledSelectedIcon(SelIcon);
		input.setText("input");
		input.setRolloverEnabled(true);
		input.setForeground(ForegColor);
		directionPanel.add(input);
		add(input);
		input.setBounds(L,T+S1,W,H);
		input.setIcon(unSelIcon);
		input.setSelectedIcon(SelIcon);
		input.setDisabledIcon(unSelIcon);
		input.setDisabledSelectedIcon(SelIcon);
		activehigh.setText("active high");
		activehigh.setRolloverEnabled(true);
		activehigh.setForeground(ForegColor);
		activetypePanel.add(activehigh);
		add(activehigh);
		activehigh.setBounds(L,T+S2,W,H);
		activehigh.setIcon(unSelIcon);
		activehigh.setSelectedIcon(SelIcon);
		activehigh.setDisabledIcon(unSelIcon);
		activehigh.setDisabledSelectedIcon(SelIcon);
		activelow.setText("active low");
		activelow.setRolloverEnabled(true);
		activelow.setForeground(ForegColor);
		activetypePanel.add(activelow);
		add(activelow);
		activelow.setBounds(L,T+S2+S1,W,H);
		activelow.setIcon(unSelIcon);
		activelow.setSelectedIcon(SelIcon);
		activelow.setDisabledIcon(unSelIcon);
		activelow.setDisabledSelectedIcon(SelIcon);
		active.setText("active");
		active.setRolloverEnabled(true);
		active.setForeground(ForegColor);
		statePanel.add(active);
		add(active);
		active.setBounds(L,T+2*S2,W,H);
		active.setIcon(unSelIcon);
		active.setSelectedIcon(SelIcon);
		active.setDisabledIcon(unSelIcon);
		active.setDisabledSelectedIcon(SelIcon);
		inactive.setText("inactive");
		inactive.setRolloverEnabled(true);
		inactive.setForeground(ForegColor);
		statePanel.add(inactive);
		add(inactive);
		inactive.setBounds(L,T+2*S2+S1,W,H);
		inactive.setIcon(unSelIcon);
		inactive.setSelectedIcon(SelIcon);
		inactive.setDisabledIcon(unSelIcon);
		inactive.setDisabledSelectedIcon(SelIcon);
    }

    public void init()
    {
    }
    

	javax.swing.ButtonGroup directionPanel = new javax.swing.ButtonGroup();
	javax.swing.ButtonGroup activetypePanel = new javax.swing.ButtonGroup();
	javax.swing.ButtonGroup statePanel = new javax.swing.ButtonGroup();

	javax.swing.JLabel pinName = new javax.swing.JLabel();
	javax.swing.JRadioButton activehigh = new javax.swing.JRadioButton();
	javax.swing.JRadioButton activelow = new javax.swing.JRadioButton();
	javax.swing.JRadioButton input = new javax.swing.JRadioButton();
	javax.swing.JRadioButton output = new javax.swing.JRadioButton();
	javax.swing.JRadioButton inactive = new javax.swing.JRadioButton();
	javax.swing.JRadioButton active = new javax.swing.JRadioButton();
	javax.swing.ImageIcon SelIcon = new javax.swing.ImageIcon();
    javax.swing.ImageIcon unSelIcon = new javax.swing.ImageIcon();



	void updateGPIOfunction(boolean function)
	{
		if (!function) {
            activehigh.setEnabled(false);
		    activelow.setEnabled(false);
            input.setEnabled(false);
		    output.setEnabled(false);
            active.setEnabled(false);
		    inactive.setEnabled(false);
        } else {		
            activehigh.setEnabled(true);
		    activelow.setEnabled(true);
            input.setEnabled(true);
		    output.setEnabled(true);
        }
	    this.repaint();
		
    }

	void updateGPIOdirection(boolean direction)
	{
        if (direction) output.setSelected(true);
        else input.setSelected(true);
		if (input.isSelected() || !input.isEnabled()) {
            active.setEnabled(false);
		    inactive.setEnabled(false);
		} else {
            active.setEnabled(true);
		    inactive.setEnabled(true);
        }
	    this.repaint();
    }

	void updateGPIOactivelevel(boolean activetype)
	{
        if (!activetype) activehigh.setSelected(true);
        else activelow.setSelected(true);
	    this.repaint();
    }

	void updateGPIOstate(boolean state)
	{
        if (state) active.setSelected(true);
        else inactive.setSelected(true);
	    this.repaint();
    }

	long getGPIOdirection()
	{
		return output.isSelected()?1:0;
	}

	long getGPIOactivelevel()
	{
		return activelow.isSelected()?1:0;
	}

	long getGPIOstate()
	{
		return active.isSelected()?1:0;
	}


   /**
     * Create a bullet bitmap from a new foreground color and a color image.
     */
    private synchronized Image makeBullet(Color fg, Image sourceImage)
    {

	ImageFilter imgf = new HueFilter(fg);
	ImageProducer Origp = sourceImage.getSource();
	ImageProducer imgp = new FilteredImageSource(Origp,
						     imgf);
	Image bulletImage = this.createImage(imgp);
	return bulletImage;
    }
}


    
    
