   /*
    * @(#) CommandPanel.java 1.0 96/09/20 
    * Copyright (c) 1998 Lawrence Rodrigues
    */

import java.io.*;
import java.io.InputStream;
import java.awt.*;
import java.awt.event.*;

  /** CommandPanel Class
    * @version 1.0  20  Sep 1998
    * @author Lawarnce Rodrigues
    * This class creates a panel with a number of sub panels.
    **/
  public class CommandPanel  extends Panel {
     public int width, height;
     public DispBtnsPanel dispPanel;
     public StatsPanel statsPanel;
     public Cine cinePanel;
     public String[] fileList;
     private DisplayController dispMgr;

     public  CommandPanel(int wd, int hgt, String[] list ){
        width =wd;
        height = hgt;
        fileList = list;
        createUI();
     }

     private void createUI() {
        dispPanel = new DispBtnsPanel(width/4, height/3);
        statsPanel = new StatsPanel(width/4, height/3);
        cinePanel = new Cine(width/4, height/3);
        GridBagLayout    gb = new GridBagLayout() ;
        GridBagConstraints c = new GridBagConstraints();
        setLayout(gb);
        setBackground(Color.lightGray);

        c.fill = GridBagConstraints.BOTH;
        c.weighty = 1.0; c.weightx = 1.0;

        c.insets = new Insets(5,5, 5,5);

        c.gridwidth = GridBagConstraints.REMAINDER;

        gb.setConstraints(dispPanel, c);
        add(dispPanel);
        gb.setConstraints(cinePanel, c);
        add(statsPanel);
        gb.setConstraints(statsPanel, c);
        add(cinePanel);
    }


    public void paint(Graphics g){
       Rectangle bBox = getBounds();
       g.setColor(Color.cyan);
       g.draw3DRect(0,0, bBox.width-2, bBox.height-1, true);
    }

    /* Here you register for call back.
     * Without this, the clicking of buttons will not have any
     * effect.
     */
   public void registerForDisplayCallback(DisplayController callbackObj){
      dispMgr = callbackObj;
    }
/**
  * This panel will contain the user interface components for
  * top level panel. This will also handle events related to
  * those UI components. To get a callback from the event handler,
  * a object that implements DisplayController interface need
  * to register with this panel.
  */
class DispBtnsPanel  extends Panel {
      public int width, height;
      private Button dispButton, loadPixButton;
      public List imageList;

      public  DispBtnsPanel(int wd, int hgt){
         width =wd;
         height = hgt;
         createUI();
      }

      private void createUI() {
         imageList = new List(4, false);
         if(fileList != null){
            for(int i=0;i<fileList.length;i++){
                imageList.addItem(fileList[i]);
           }
         } else {System.out.println("Filelist is null"); }
         imageList.addActionListener(
            new ActionListener() {
               public void actionPerformed(ActionEvent e){
                  int imageId = imageList.getSelectedIndex();
                  if(dispMgr != null) dispMgr.displayImage(imageId);

               }

            }
         );

         imageList.setBackground(Color.lightGray);
         imageList.select(0);


         //Create a display buttons panel
         Panel dispPanel = new Panel();
         dispPanel.setLayout(new GridLayout(3,1));
         dispButton = new Button("Render");
         dispButton.addActionListener(
             new ActionListener(){
                public void actionPerformed(ActionEvent e){
                   int imageId = imageList.getSelectedIndex();
                   if(dispMgr != null) dispMgr.displayImage(imageId);
                }
             }
         );

         loadPixButton = new Button("Grab Pixels");
         loadPixButton.addActionListener(
             new ActionListener(){
                public void actionPerformed(ActionEvent e){
                   int imageId = imageList.getSelectedIndex();
                   dispMgr.loadPixels(imageId);
                }
            }

         );
     

         dispPanel.add(dispButton);
         dispPanel.add(loadPixButton);
         GridBagLayout    gb = new GridBagLayout() ;
         GridBagConstraints c = new GridBagConstraints();
         setLayout(gb);
         setBackground(Color.lightGray);

         c.fill = GridBagConstraints.BOTH;
         c.weighty = 0.0; c.weightx = 0.0;
         c.gridx =0;
         c.gridheight =1;
         c.insets = new Insets(5,5, 5,5);
         Label dispLabel = new Label("Image Selection");
         c.gridwidth = GridBagConstraints.REMAINDER;

         gb.setConstraints(dispLabel, c);
         add(dispLabel);

         c.gridwidth = 1;
         gb.setConstraints(imageList, c);
         add(imageList);
         c.gridheight = 1;
         c.gridx =1;
         c.gridwidth = GridBagConstraints.REMAINDER;
         gb.setConstraints(dispPanel, c);
         add(dispPanel);
   }


   public void paint(Graphics g){
      Rectangle bBox = getBounds();
      g.setColor(Color.cyan);
      g.draw3DRect(1,1,bBox.width-2, bBox.height-2,  true);
   }

  }

  class StatsPanel  extends Panel {

      public int width, height;
      public TextField redmin, redmax, redave;
      public TextField greenmin, greenmax, greenave;
      public TextField bluemin, bluemax, blueave;
      TextField imgname;

      public  StatsPanel(int wd, int hgt){
              width =wd;
              height = hgt;
              createUI();
      }

      private void createUI() {

          Label title = new Label("Pixel Statistics");
          Panel titleP = new Panel();
          titleP.setLayout(new GridLayout(2,1));
          titleP.add(title);
          Label imageName = new Label("Image ");
          imgname = new TextField(12);
          Panel nameP = new Panel();
          nameP.setBackground(Color.gray);
          nameP.add(imageName);
          nameP.add(imgname);
          titleP.add(nameP);

          Label col = new Label("Color");
          Label min = new Label("Min");
          Label max = new Label("Max");
          Label ave = new Label("mean");

          Label red  = new Label("Red");
          Label green  = new Label("Green");
          Label blue  = new Label("Blue");
          redmax = new TextField(4);
          redmin = new TextField(4);
          redave = new TextField(4);

          greenmax = new TextField(4);
          greenmin = new TextField(4);
          greenave = new TextField(4);

          bluemax = new TextField(4);
          bluemin = new TextField(4);
          blueave = new TextField(4);
       
          Panel statP = new Panel();
          statP.setLayout(new GridLayout(4,4));
          statP.setBackground(Color.gray);
          statP.add(col); statP.add(min); statP.add(max); statP.add(ave);

          statP.add(red); statP.add(redmin); statP.add(redmax); statP.add(redave);

          statP.add(green); statP.add(greenmin); statP.add(greenmax); statP.add(greenave);

          statP.add(blue); statP.add(bluemin); statP.add(bluemax); statP.add(blueave);


          GridBagLayout    gb = new GridBagLayout() ;
          GridBagConstraints c = new GridBagConstraints();
          setLayout(gb);
          setBackground(Color.lightGray);

          c.fill = GridBagConstraints.BOTH;
          c.weighty = 1.0; c.weightx = 1.0;

          c.insets = new Insets(5,5, 5,5);

          c.gridwidth = GridBagConstraints.REMAINDER;

          gb.setConstraints(titleP, c);
          add(titleP);
          gb.setConstraints(statP, c);
          add(statP);
    }

    public void paint(Graphics g){
       Rectangle bBox = getBounds();
       g.setColor(Color.cyan);
       g.draw3DRect(1,1,bBox.width-2, bBox.height-2,  true);
    }
   }
 }


