import java.awt.*; import Tree; import Display; // This class define the user interface i.e. the buttons and what is // done when each button is pressed. class CtrlPanel extends Panel { private MainWin Parent; public Button demo,insert,delete,next,clear,load,exit; private ExitDlg exitdlg = null; // for Exit confirmation private InputDlg inputdlg = null; // for getting input from the user private Font BtFont = new Font("TimesRoman",Font.BOLD,16); private Color BtFore = Color.blue; private Color BtBack = new Color(180,180,180); // constructor create the buttons initialize font,color and state // of them (enable/disable). public CtrlPanel(MainWin p) { Parent = p; demo = new Button("Demo"); insert = new Button("Insert"); delete = new Button("Delete"); next = new Button("Next"); clear = new Button("Clear"); load = new Button("Load"); exit = new Button("Exit"); demo.setFont(BtFont); insert.setFont(BtFont); delete.setFont(BtFont); next.setFont(BtFont); clear.setFont(BtFont); load.setFont(BtFont); exit.setFont(BtFont); demo.setForeground(BtFore); insert.setForeground(BtFore); delete.setForeground(BtFore); next.setForeground(BtFore); clear.setForeground(BtFore); load.setForeground(BtFore); exit.setForeground(BtFore); demo.setBackground(BtBack); insert.setBackground(BtBack); delete.setBackground(BtBack); next.setBackground(BtBack); clear.setBackground(BtBack); load.setBackground(BtBack); exit.setBackground(BtBack); setLayout(new GridLayout(0,1)); add(demo); add(insert); add(delete); add(next); add(clear); add(load); add(exit); delete.disable(); next.disable(); clear.disable(); } // This method respones to a buttons press. Acording to the button // it execute the proper operation. public boolean action(Event evnt, Object obj) { Object target = evnt.target; if (target == demo) startDemo(); // perform the demo script. if (target == insert) { openInputDlg(InputDlg.INSERT); // open the input dialog window return true; } if (target == delete) { openInputDlg(InputDlg.DELETE); // open the input dialog window return true; } if (target == next) { Parent.tree.nextStep(); // execute step in the current operation return true; } if (target == clear) { Parent.ClearTree(); // clears the tree and change the buttons demo.enable(); // state as needed. insert.enable(); delete.disable(); next.disable(); clear.disable(); load.enable(); return true; } if (target == load) startLoad(); // load a RB tree if (target == exit) { openExitDlg(); // open the exit dialog for exit confirmation return true; } return true; } // This is the demo script i.e. it perform insertion and deleion // operations on the tree, they are executed without the user // interference. private void startDemo() { load.disable(); demo.disable(); Parent.tree.fastRB_Insert(50); Parent.tree.fastRB_Insert(40); Parent.tree.fastRB_Insert(30); Parent.tree.fastRB_Insert(20); Parent.tree.fastRB_Insert(70); Parent.tree.fastRB_Insert(90); Parent.tree.fastRB_Insert(80); Parent.tree.fastRB_Insert(10); Parent.tree.fastRB_Insert(60); Parent.tree.fastRB_Delete(40); Parent.tree.fastRB_Insert(40); Parent.tree.fastRB_Insert(65); Parent.tree.fastRB_Insert(45); Parent.tree.fastRB_Insert(5); Parent.tree.fastRB_Delete(30); Parent.tree.fastRB_Delete(40); delete.enable(); clear.enable(); } // This method open the input dialog, it also create it in the first // time it is called. private void openInputDlg(int kind) { if (inputdlg == null) inputdlg = new InputDlg(Parent); inputdlg.setInputFor(kind); inputdlg.show(); } // This is the load script i.e. it perform insertion and deleion // operations on the tree, they are executed without the user // interference. private void startLoad() { load.disable(); demo.disable(); Parent.tree.load(3); /* Argument indicates which tree */ delete.enable(); clear.enable(); } // This method create and open the exit dialog. private void openExitDlg() { exitdlg = new ExitDlg(Parent,"Exit"); exitdlg.show(); } // This method dispose the exit and input dialog public void Free() { if (inputdlg != null) inputdlg.dispose(); if (exitdlg != null) exitdlg.dispose(); } } // This class define the Exit confirmation dialog. class ExitDlg extends Dialog { MainWin Parent; Button yes,no; Label message = new Label("Exit Now?"); // constructor create the dialog and all it's components, and initialize // there color,font and size. public ExitDlg(MainWin p,String title) { super(p,title,true); Parent = p; yes = new Button("Yes"); no = new Button("No"); Font f = new Font("TimesRomn",Font.BOLD,18); yes.setFont(f); no.setFont(f); message.setFont(f); yes.setForeground(Color.red); no.setForeground(Color.red); GridBagLayout gridlayout = new GridBagLayout(); GridBagConstraints constraints = new GridBagConstraints(); setLayout(gridlayout); constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.CENTER; constraints.ipadx = 20; constraints.ipady = 20; constraints.gridwidth = GridBagConstraints.REMAINDER; gridlayout.setConstraints(message,constraints); add(message); constraints.ipadx = 0; constraints.ipady = 0; constraints.gridwidth = 1; constraints.gridheight = GridBagConstraints.REMAINDER; gridlayout.setConstraints(yes,constraints); add(yes); gridlayout.setConstraints(no,constraints); add(no); resize(160,120); } // this method define what is done when the buttons are pressed. public boolean action(Event evnt,Object obj) { Object target = evnt.target; if (target == no) { dispose(); return true; } if (target == yes) Parent.dispose(); return (true); } } // This class define input dialog for getting input from the user. class InputDlg extends Dialog { public final static int INSERT = 0; // constants for identifing public final static int DELETE = 1; // what the input for. private MainWin Parent; private TextField data; private Button ok,cancel; private int inputFor; private String header; private Label message = new Label("Enter integer (0-999)."); // constructor create the dialog all it's components, and // initialize there color, font and shape. public InputDlg(MainWin p) { super(p,true); Parent = p; data = new TextField(8); ok = new Button("O.K."); cancel = new Button("Cancel"); Font f = new Font("TimesRoman",Font.BOLD,20); ok.setFont(f); cancel.setFont(f); message.setFont(f); data.setFont(f); ok.setForeground(Color.red); cancel.setForeground(Color.red); GridBagLayout gridlayout = new GridBagLayout(); GridBagConstraints constraints = new GridBagConstraints(); setLayout(gridlayout); constraints.anchor = GridBagConstraints.CENTER; constraints.insets = new Insets(30,15,10,0); constraints.gridwidth = GridBagConstraints.REMAINDER; gridlayout.setConstraints(message,constraints); add(message); constraints.anchor = GridBagConstraints.CENTER; constraints.insets = new Insets(0,10,10,0); constraints.gridwidth = GridBagConstraints.REMAINDER; gridlayout.setConstraints(data,constraints); add(data); constraints.insets = new Insets(0,20,10,0); constraints.ipadx = 15; constraints.ipady = 0; constraints.gridwidth = 1; constraints.gridheight = GridBagConstraints.REMAINDER; gridlayout.setConstraints(ok,constraints); add(ok); gridlayout.setConstraints(cancel,constraints); add(cancel); resize(220,220); } // define the action that are done when each of the buttons are // pressed. public boolean action(Event event, Object obj) { int inp; Object target = event.target; if (target == ok) { try { inp = Integer.parseInt((data.getText()).trim()); if ((inp >= 0) && (inp <= 999)) { hide(); Parent.display.repaint(); sendInput(inp); } } catch (NumberFormatException e) { } } else if (target == cancel) { hide(); } data.setText(""); return true; } // This method draw the string header that tells if the input // is for insertion or deletion. public void paint(Graphics g) { g.setFont(new Font("TimesRoman",Font.BOLD,20)); g.drawString(header,30,65); } // This method set the inputFor variable that tell for what is the // input. public void setInputFor(int kind) { if (kind == INSERT) inputFor = INSERT; else if (kind == DELETE) inputFor = DELETE; } // this method shows the dialog it's update the header string for // the paint method. public void show() { if (inputFor == INSERT) { header = "Insert element."; data.requestFocus(); super.show(); } else if (inputFor == DELETE) { header = "Delete element."; data.requestFocus(); super.show(); } } // This method 'send' the input to the tree, it start an operation // by calling to the tree RB_Insert or RB_Delete methods. public void sendInput(int inputdata) { switch (inputFor) { case INSERT: if (Parent.tree.RB_Insert(inputdata)) { // update the ctrlpanel's buttons state. Parent.ctrlpanel.demo.disable(); Parent.ctrlpanel.insert.disable(); Parent.ctrlpanel.delete.disable(); Parent.ctrlpanel.next.enable(); Parent.ctrlpanel.load.disable(); } break; case DELETE: if (Parent.tree.RB_Delete(inputdata)) { Parent.ctrlpanel.insert.disable(); Parent.ctrlpanel.delete.disable(); Parent.ctrlpanel.next.enable(); } break; } } }