/* RBTreeAlgThread.java */ import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.util.*; import ciips.animation.*; import ciips.animation.tree.*; public class BinTreeAlgThread extends AlgThread implements ItemListener, ActionListener, InputCompleteListener { BinTree tree; IntList int_list; static String[] BinTreeDataSets = { "Data Set 0", "Data Set 1", "Data Set 2"}; private static String base_data_file_name = "bt"; static String[] options = { "Option 1" }; static final int ADD_NODE = 1; static final int DELETE_NODE = 2; static final int PRINT_PANEL = 3; private AlgAnimFrame f; private DrawingPanel dpAfter /* = frame.getCurrentPanel() */; static final int x_offset = 10; static final int y_offset = 50; /** Set the number of comment panel lines needed **/ static { AlgAnimFrame.COMPANEL_LINES = 5; } static { ciips.animation.UIFrame.allowUI( true ); } static Font font = new Font("Courier", Font.PLAIN, 12); static private final int N_HISTORY_PANELS = 4; private static final int WAITING = 0; private static final int ADD_ITEM = 1; private static final int DELETE_ITEM = 2; private static final int STOP = 3; private class NextAction { int next_action = WAITING; public NextAction() { }; public void setNextAction( int x ) { next_action = x; } } private NextAction next_action = new NextAction(); private int next_value = -1; static private int seq = 0; // Sequence number for this thread private int this_seq; static private final boolean DEBUG = true; static private final boolean DEBUGI = false; public BinTreeAlgThread( AlgAnimFrame frame ) { super(); if( frame.getDataFileName() != null ) { base_data_file_name = frame.getDataFileName(); } setParms( frame, base_data_file_name, N_HISTORY_PANELS ); f = frame; this_seq = seq++; System.out.println("BinTreeAlgThread " + this_seq + " constructed OK"); } public boolean loadData( int choice ) { String fn; DataInputStream inStream; String line = null; if( DEBUG ) System.out.println("loadData: choice " + choice ); if ( choice == AlgAnimFrame.UI_SELECTED ) { System.out.println("BinTreeAlgThread:loadData - user input selected"); UIFrame uif = frame.getUIFrame(); int_list = uif.getIntList(); if( DEBUG ) System.out.println("RBTAlgThread:loadData " + int_list ); } else { int_list = frame.getNamedIntList( choice ); } return int_list != null; } static private int titlex = 5, titley = 5; static private final String final_string = "Final Tree"; static ComBox final_tree = new ComBox( titlex, titley, final_string, font ); private ComBox operation_name = new ComBox( titlex, titley, "Title ..", font ); private void preparePanels( String opn, int data ) { dpAfter.clear(); dpAfter.removeAllCom(); AlgAnimFrame.showText( 1, opn + data ); operation_name.setText( opn + data ); dpAfter.addCom( operation_name ); } private void finalDisplay( ) { dpAfter.removeAllCom(); AlgAnimFrame.showText( 1, final_string ); operation_name.setText( final_string ); dpAfter.addCom( operation_name ); // finish creating tree tree.updateNodePositions(); dpAfter.clear(); dpAfter.redraw(); } public void run() { if( DEBUG ) System.out.println("BinTreeAlgThread:run - enter"); // Create the lock object in this thread next_action = new NextAction(); int k = getDataPanelCount(); // Capture reference to the current panel dpAfter = frame.getCurrentPanel(); // Ensure that an off-screen graphics buffer is built Graphics g = dpAfter.getOffScreenGraphics(); if ( dpAfter == null ) { System.out.println( "run: null drawingPanel"); return; } f.clearPanels(); tree = new BinTree(); boolean opt[] = f.getOptions(); generateData(); if ( int_list != null ) { if( DEBUG ) System.out.println( "RBTAlgThread:run int list " + int_list ); if( DEBUG ) System.out.println( "RBTAlgThread:run int list " + int_list.size() + " items" ); for(k=0;k= 0 ) { switch( index ) { case ADD_ACTION: if( DEBUG ) System.out.println("ADD action"); // Don't create a new one! if ( AddDialog == null ) AddDialog = new InputDialog( frame, "Enter value of item to add" ); AddDialog.show(); if( DEBUG ) System.out.println("RBTAlgThread:actPerf - wait for input" ); break; case DEL_ACTION: if( DEBUG ) System.out.println("DEL action"); // Don't create a new one! if ( DeleteDialog == null ) DeleteDialog = new InputDialog( frame, "Enter value of item to delete" ); DeleteDialog.show(); break; case PRINT_ACTION: System.out.println("PRINT action"); break; } } else { System.out.println("BinTreeAlgThread:actionPerformed - unknown action"); } } public void inputComplete( InputDialogEvent e ) { InputDialog src = (InputDialog)(e.getSource() ); String t = e.getText(); next_value = Integer.parseInt( t.trim() ); if( DEBUG ) System.out.println( "BinTreeAlgThread(" + this_seq + "):inputComplete " + src + " H " + src.hashCode() + " val " + next_value ); //if( DEBUG ) System.out.println( "BinTreeAlgThread:inputComplete aD " + AddDialog + // " H " + AddDialog.hashCode() ); if ( src == AddDialog ) { if( DEBUG ) System.out.println( "BinTreeAlgThread:inputComplete - it's an ADD" ); next_action.setNextAction( ADD_ITEM ); // next_action.notify(); } else if ( src == DeleteDialog ) { next_action.setNextAction( DELETE_ITEM ); if( DEBUG ) System.out.println( "BinTreeAlgThread:inputComplete - it's an DELETE" ); // next_action.notify(); } else { System.out.println("BinTreeAlgThread:inputComplete - src " + src + " unknown"); } } }