package edu.csusb.danby.applet;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/** The class DFrame extends JFrame by adding a
*	button panel and a menu bar.
* @version Wed Jul 24 08:16:55 PDT 2002
*/
public class DFrame extends JFrame implements WindowListener{

    private JSplitPane split;
    private JPanel controlPanel;
    private JComponent component1;
    private JComponent component2;
    private JMenuBar menuBar;
    private JFrame frame;
    private Container contentPane;

    public static void main(String[] args){
    
     DFrame sfg = new DFrame("trial", new JPanel());
    }


/**
* Single pane version of constructor
* @param title is the desired title for the frame
* @param aComponent1 is the component
* @param title is the title for the window
*/
  public DFrame(String title, JComponent aComponent1){
    super(title);
    frame= this;

    component1 = aComponent1;
    component1.setPreferredSize(new Dimension(240,200));
    this.addWindowListener(this);
    // Need content pane for JFrame
    contentPane = this.getContentPane();
    contentPane.setLayout(new BorderLayout());
    
/* *****************
        Now we set up the panel
   **************** */ 
 
    contentPane.add(component1,BorderLayout.CENTER); 
    //addControlPanel();
    validate();
    pack();
    setLocation(20,20);
    setVisible(true);
    validateTree();
  }

/**
* A two-panel constructor for  the frame, using JSplitPane
* @param title is the desired title for the frame
* @param aComponent1 is the left-hand component
* @param aComponent2 is the right-hand component.
*/
  public DFrame(String title, JComponent aComponent1, JComponent aComponent2){
    super(title);
    component1 = aComponent1;
    component2 = aComponent2;
    frame = this;
    this.addWindowListener(this);
    // Need content pane for JFrame
    contentPane = this.getContentPane();
    contentPane.setLayout(new BorderLayout());

/* *****************
                Now we set up the panels in the split pane
   **************** */

    split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, component1,  component2);
    split.setAlignmentY(0.5F);
    split.setDividerSize(6);
    split.setContinuousLayout(true);
    split.setPreferredSize(new Dimension(240,200));
    contentPane.add(split,BorderLayout.CENTER);
    component1.setPreferredSize(new Dimension(120,200));
    component2.setPreferredSize(new Dimension(120,200));
    split.resetToPreferredSizes();
    split.setDividerLocation(0.5);
   
    
    pack();
    validate();
    setLocation(20,20);
    setVisible( true );
  }


    /**
    * setControlPanel is used to add the control panel at bottom of frame
    * @param aControlPanel the controlPanel to be added
    */	
    public void setControlPanel(JPanel aControlPanel){
        invalidate();
        if (controlPanel != null) {
            contentPane.remove( controlPanel);
            }
        controlPanel=aControlPanel;
        contentPane.add(controlPanel, BorderLayout.SOUTH);
        validate();
        //pack();
        super.repaint();
    }


    /**
    * setControlPanelItem is used to add individual items to ControlPanel
    * @param item  the item to be added
    */ 
    public void setControlPanelItem(JComponent item){
        if (controlPanel == null){
            System.out.println(" tried to add "+item+" to null control panel");
        }
        else{
            controlPanel.invalidate();
            controlPanel.add(item);
            controlPanel.validate();
        }
    }


/**
* setLeftComponent adds the component to the left side of the split panel
* @param component the component to be added
*/
    public void setLeftComponent(JComponent component){
        split.setLeftComponent(component);
        validateTree();
        repaint();
        }

/**
*  setRightComponent adds the component to the right side of the split panel
* @param component the component to be added
*/
     public void setRightComponent(JComponent component){
        split.setRightComponent(component);
        validateTree();
        repaint();
        }

/** 
* setComponet sets the component in the frame for the single panel version
* @param component the component to be added
*/
    public void setComponent(JComponent component){
        invalidate();	
        contentPane.remove(component1);
        component1 = component;
        contentPane.add(component1);
        validateTree();
        repaint();
    }
    
    
    /**
    * set the split at desired position between 0 and 1
    */
    public void setSplitDividerLocation(double position){
        if( split != null){
            split.setDividerLocation(position);
        }
    }
            
    
    


// Window Listener Events. Only windowClosing is implemented
public void windowClosing(WindowEvent e) {this.dispose(); }
public void windowOpened(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}


}
