package Deadlock;

import java.awt.*;

public class CheckerBox extends Frame implements MyConstants {
    // This class is used to create the Deadlock Checker GUI
    MenuBar menubar;                
    Menu file, help;                
    Button load, run, okay, view;    
    TextArea messages;
    CheckboxGroup tests;
    Checkbox[] test;                
    Font font;
    FileDialog select;

    // The layout manager for each of the containers.
    GridBagLayout gridbag = new GridBagLayout();
    
    public CheckerBox() {
        super("Deadlock Checker Version "+VERSION);
        this.setBackground(MAUVE);
        this.setForeground(BLACK);
        // Choose a large font 
        font = new Font("Dialog",Font.BOLD,14);
        this.setFont(font);
        // Create the menubar.  Tell the frame about it.
        menubar = new MenuBar();
        this.setMenuBar(menubar);
        // Create the file menu. Add to menubar.
        file = new Menu("File");
        file.add(new MenuItem("Load network"));
        file.add(new MenuItem("Quit"));
        menubar.add(file);
        // Create Help menu; add an item; add to menubar
        help = new Menu("Help");
        help.add(new MenuItem("Deadlock Checker"));
        help.add(new MenuItem(SDDTEST));
        help.add(new MenuItem(SDD3TEST));
        help.add(new MenuItem(CSDDTEST));
        help.add(new MenuItem(FSDDTEST));
        help.add(new MenuItem(CPOTEST));
        help.add(new MenuItem(DIVTEST));
        menubar.add(help);
        // Display the help menu in a special reserved place.
        menubar.setHelpMenu(help);

        // Create checkboxes
        tests = new CheckboxGroup();
        test = new Checkbox[6];
        test[0] = new Checkbox(SDDTEST,tests,true);
        test[1] = new Checkbox(SDD3TEST,tests,true);
        test[2] = new Checkbox(CSDDTEST,tests,false);
        test[3] = new Checkbox(FSDDTEST,tests,false);
        test[4] = new Checkbox(CPOTEST,tests,false);
        test[5] = new Checkbox(DIVTEST,tests,false);
        test[0].setBackground(MAUVE);
        test[1].setBackground(MAUVE);
        test[2].setBackground(MAUVE);
        test[3].setBackground(MAUVE);
        test[4].setBackground(MAUVE);
        test[5].setBackground(MAUVE);
        tests.setCurrent(test[0]);
        // Create pushbuttons
        okay = new Button("Begin Analysis");
        okay.setBackground(BEIGE);
        load = new Button("Load Network");
        load.setBackground(BEIGE);
        run = new Button("Run/Abort");
        run.setBackground(BEIGE);
        view = new Button("View Network");
        view.setBackground(BEIGE);
        
        messages = new TextArea(20,MESSAGELENGTH);
        messages.setEditable(false);
        messages.setBackground(BEIGE);

        // Create a file selection dialog box
        select = new FileDialog(this, "Select File", FileDialog.LOAD);
 
        // Use the constrain() convenience method
        // to add components to the panel and to specify their 
        // GridBagConstraints values.
        this.setLayout(gridbag);

        constrain(this, test[0], 0, 0, 3, 1);
        constrain(this, test[1], 0, 1, 3, 1);
        constrain(this, test[2], 0, 2, 3, 1);
        constrain(this, test[3], 3, 0, 3, 1);
        constrain(this, test[4], 3, 1, 3, 1);
        constrain(this, test[5], 3, 2, 3, 1);
        constrain(this, load, 0, 4, 1, 1, GridBagConstraints.NONE,
              GridBagConstraints.CENTER, 0.3, 0.0, 0, 0, 0, 0);
        constrain(this, run, 1, 4, 1, 1, GridBagConstraints.NONE,
              GridBagConstraints.CENTER, 0.3, 0.0, 0, 0, 0, 0);
        constrain(this, view, 2, 4, 2, 1, GridBagConstraints.NONE,
              GridBagConstraints.CENTER, 0.3, 0.0, 0, 0, 0, 0);
        constrain(this, okay, 4, 4, 2, 1, GridBagConstraints.NONE,
              GridBagConstraints.CENTER, 0.3, 0.0, 0, 0, 0, 0);
        constrain(this, messages, 0, 5, 6, 1);
    }
    
    public void constrain(Container container, Component component, 
                  int grid_x, int grid_y, int grid_width, int grid_height,
                  int fill, int anchor, double weight_x, double weight_y,
                  int top, int left, int bottom, int right)
    {
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = grid_x; c.gridy = grid_y;
        c.gridwidth = grid_width; c.gridheight = grid_height;
        c.fill = fill; c.anchor = anchor;
        c.weightx = weight_x; c.weighty = weight_y;
        if (top+bottom+left+right > 0)
            c.insets = new Insets(top, left, bottom, right);
        
        ((GridBagLayout)container.getLayout()).setConstraints(component, c);
        container.add(component);
    }
    
    public void constrain(Container container, Component component, 
                  int grid_x, int grid_y, int grid_width, int grid_height) {
        constrain(container, component, grid_x, grid_y, 
              grid_width, grid_height, GridBagConstraints.NONE, 
              GridBagConstraints.NORTHWEST, 0.0, 0.0, 0, 0, 0, 0);
    }
    
    public void constrain(Container container, Component component, 
                  int grid_x, int grid_y, int grid_width, int grid_height,
                  int top, int left, int bottom, int right) {
        constrain(container, component, grid_x, grid_y, 
              grid_width, grid_height, GridBagConstraints.NONE, 
              GridBagConstraints.NORTHWEST, 
              0.0, 0.0, top, left, bottom, right);
    }
	    
        public static void main(String[] args) {
        Frame f = new CheckerBox();
        f.pack();
        f.show();
    }
}
