/* AVLTreeAlgThread.java */ import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.util.*; import ciips.animation.*; public class AVLTreeAlgThread extends AlgThread implements ItemListener, ActionListener, InputCompleteListener { AVLTree tree; IntList int_list; static String[] AVLTreeDataSets = { "Data Set 0", "Data Set 1", "Data Set 2"}; private static String base_data_file_name = "avl"; static String[] options = { " -- " }; static final int AVLT_DISPLAY = 0; static final int ADD_NODE = 1; static final int DELETE_NODE = 2; static final int PRINT_PANEL = 3; private boolean avlt_display = true; private AlgAnimFrame f; static final int x_offset = 10; static final int y_offset = 50; /** Set the number of comment panel lines needed **/ static { AlgAnimFrame.COMPANEL_LINES = 3; } static { ciips.animation.UIFrame.allowUI( true ); } static Font font = new Font("Courier", Font.PLAIN, 12); static private final int N_HISTORY_PANELS = 3; 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; public Image in_image; public AVLTreeAlgThread( AlgAnimFrame frame ) { super(); // setParms( frame, AVLTreeDataSets, N_HISTORY_PANELS ); setParms( frame, base_data_file_name, N_HISTORY_PANELS ); f = frame; AVLShowDescriptionImage(f); this_seq = seq++; System.out.println("AVLTreeAlgThread " + this_seq + " constructed OK"); } /** load number from input file **/ public boolean loadData( int choice ) { String fn; DataInputStream inStream; String line = null; System.out.println("loadData: choice " + choice ); if ( choice == AlgAnimFrame.UI_SELECTED ) { System.out.println("AVLTReeAlgThread:loadData - user input selected"); UIFrame uif = frame.getUIFrame(); int_list = uif.getIntList(); if( DEBUG ) System.out.println("AVLTAlgThread:loadData " + int_list ); } else { fn = base_data_file_name + choice; System.out.println("loadData: filename ["+fn+"]"); try { URL dataURL = new URL(frame.getApplet().getCodeBase(), fn); int_list = new NamedIntList( dataURL ); } catch (IOException e) { System.out.println("Data file or source file not found"); } catch (NullPointerException e) {} } return int_list != null; } static private int titlex = 2, titley = 2; static ComBox final_tree = new ComBox( titlex, titley, "Final AVL Tree", font ); /** basic method inherited from Thread class **/ public void run() { ComBox adding; ComBox balance_box; System.out.println("AVLTreeAlgThread:run - enter"); // Create the lock object in this thread next_action = new NextAction(); int k = getDataPanelCount(); // Capture reference to the current panel DrawingPanel 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(); f.Highlight(114); tree = new AVLTree(); boolean opt[] = f.getOptions(); if( opt != null ) { tree.setAVLTDisplay( !opt[AVLT_DISPLAY] ); } generateData(); if ( int_list != null ) { for(k = 0 ; k < int_list.size() ; k++) { // Get the next one to insert int data = int_list.elementAt( k ); dpAfter.removeAllCom(); // AlgAnimFrame.showText( 1, "Adding new node " + data ); adding = new ComBox( titlex, titley, "Adding " + data, font ); dpAfter.addCom( adding ); // Insert into the tree tree.InsertNode( data ); // tree.drawAVLTree(); System.out.println("AVLTreeAlgThread:run - tree drawn"); dpAfter.removeCom( adding ); if( DEBUG ) System.out.println( "AVLTAlgThread:First loop run!!!!!!!!!!!!!!!" ); } AlgAnimFrame.showText( 1, "Final AVL Tree" ); dpAfter.addCom( final_tree ); // finish creating tree dpAfter.repaint(); balance_box = new ComBox(150,150, "balance here!!"); dpAfter.addCom(balance_box); dpAfter.removeCom(balance_box); if( DEBUG ) System.out.println( "AVLTAlgThread:run waiting .." ); try { do { // Wait until stop is hit Thread.sleep( 1000L ); if( DEBUG ) System.out.println( "AVLTAlgThread(" + this_seq + "):run - next_action " + next_action.next_action ); switch( next_action.next_action ) { case ADD_ITEM: System.out.println("AVLTreeAlgThread:run - adding extra item " + next_value ); // Disable more additions for a while? dpAfter.removeAllCom(); adding = new ComBox( titlex, titley, "Adding " + next_value, font ); dpAfter.addCom( adding ); // Insert into the tree tree.InsertNode( next_value ); System.out.println("AVLTreeAlgThread:run - tree drawn"); dpAfter.removeCom( adding ); dpAfter.addCom( final_tree ); dpAfter.repaint(); next_action.next_action = WAITING; break; case DELETE_ITEM: // Can we find this item? System.out.println("AVLTreeAlgThread:run - deleting " + next_value ); dpAfter.removeAllCom(); adding = new ComBox( titlex, titley, "Deleting " + next_value, font ); AVLNode z = tree.FindNode( next_value ); if ( z != null ) { String del_caption = "Deleting " + next_value; AlgAnimFrame.showText(1, del_caption); adding = new ComBox( titlex, titley, del_caption, font ); dpAfter.addCom( adding ); tree.DeleteNode( z, frame ); tree.drawAVLTree(); dpAfter.removeAllCom(); dpAfter.addCom( final_tree ); dpAfter.repaint(); } else { AlgAnimFrame.showText(1, "Node " + next_value + " isn't in the tree!"); } next_action.next_action = WAITING; break; } } while( next_action.next_action != STOP ); } catch( InterruptedException e ) { System.out.println("AVLTreeAlgThread:run - int exception " + e.getMessage() ); } f.finishAlg(); } else { System.out.println("Data file not found\n"); } } private void optionAction( int index, boolean on ) { if( DEBUG ) System.out.println( "AVLTAlgThread:optionAction - " + index + (on?" on":" off") ); if ( index < 0 ) { } else { switch( index ) { case AVLT_DISPLAY: avlt_display = on; if ( tree != null ) tree.setAVLTDisplay( on ); break; } } } private final String[] actions = { "Add", "Delete" }; private static final int ADD_ACTION = 0; private static final int DEL_ACTION = 1; Menu actionMenu = null; /** get command from actionListener **/ public Menu getActionMenu() { actionMenu = new Menu( "Actions" ); int n = actions.length; for( int 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("AVLTAlgThread: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(); } } else { System.out.println("AVLTreeAlgThread:actionPerformed - unknown action"); } } /** When user input incomed, calls command dialog to add or delete **/ 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( "AVLTAlgThread(" + this_seq + "):inputComplete " + src + " H " + src.hashCode() + " val " + next_value ); //if( DEBUG ) System.out.println( "AVLTAlgThread:inputComplete aD " + AddDialog + // " H " + AddDialog.hashCode() ); if ( src == AddDialog ) { if( DEBUG ) System.out.println( "AVLTAlgThread: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( "AVLTAlgThread:inputComplete - it's an DELETE" ); // next_action.notify(); } else { System.out.println("AVLTAlgThread:inputComplete - src " + src + " unknown"); } } public void describeColor(Graphics g){ } /** displays image about each nodecolor **/ void AVLShowDescriptionImage(AlgAnimFrame f){ DrawingPanel dpexam = new DrawingPanel(310,56); URL codeBase = f.getApplet().getCodeBase(); try { URL img_src = new URL(codeBase, "./Describe.gif"); in_image=f.getApplet().getImage(img_src); dpexam.setImage(in_image); System.out.println("describe Image loaded"); }catch ( Exception e ) { System.out.println("Cannot get describe Image" );} } }