/* BTreeAlgThread.java */ /** BTree Animation June.4 Friday 2004 Implemented by Jung-Ho, Yoo & Chang-Hwan, Park in Seoul, S.Korea **/ import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.util.*; import ciips.animation.*; /** * The BTreeAlgThread class contol the menu of Applet and it generates the data. Its parent class is AlgThread and it implements * ItemListener,ActionListener and InputCompleteListener interface. *

* @see AlgThread */ public class BTreeAlgThread extends AlgThread implements ItemListener, ActionListener, InputCompleteListener { BTree tree; IntList int_list; static String[] BTreeDataSets = { "Data Set 0", "Data Set 1", "Data Set 2"}; private static String base_data_file_name = "bt"; static String[] options = { "3-Array BTree","4-Array BTree" }; static final int BT_DISPLAY = 0; static final int T234_DISPLAY = 1; static final int ADD_NODE = 1; static final int DELETE_NODE = 2; static final int PRINT_PANEL = 3; static final int ARRY3=3; static final int ARRY4=4; 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 = 5; } /** It allow the user input menu, if the argument is true. **/ static { ciips.animation.UIFrame.allowUI( true ); } static Font font = new Font("Courier", Font.PLAIN, 12); static private final int N_HISTORY_PANELS = 2; 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 final String[] actions = { "Add", "Delete", "Print" }; private static final int ADD_ACTION = 0; private static final int DEL_ACTION = 1; private static final int PRINT_ACTION = 2; Menu actionMenu = null; private static InputDialog AddDialog, DeleteDialog; 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; /** * Creates and call the setParms() in the AlgThread class. * @param frame The AlgAnimFrame object. * @see AlgAnimFrame * @see AlgThread */ public BTreeAlgThread( AlgAnimFrame frame ) { super(); setParms( frame, base_data_file_name, N_HISTORY_PANELS ); f = frame; this_seq = seq++; System.out.println("BTreeAlgThread " + this_seq + " constructed OK"); } /** * Implements the loadData method in abstract class of AlgThread. */ 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("BTReeAlgThread:loadData - user input selected"); UIFrame uif = frame.getUIFrame(); int_list = uif.getIntList(); if( DEBUG ) System.out.println("BTAlgThread: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 = 5, titley = 5; static ComBox final_tree = new ComBox( titlex, titley, "Final BTree", font ); /** To use control run(), it is dangerous to use stop() for control thread. */ private static boolean threadState=true; private static int nArray=3; private ComBox adding; private int k; private Graphics g; private DrawingPanel dpAfter; private boolean opt[]; private int data; public void run() { while(threadState) { // Create the lock object in this thread next_action = new NextAction(); //int k = getDataPanelCount(); k = getDataPanelCount(); // Capture reference to the current panel dpAfter = frame.getCurrentPanel(); // Ensure that an off-screen graphics buffer is built g = dpAfter.getOffScreenGraphics(); if ( dpAfter == null ) { System.out.println( "run: null drawingPanel"); return; } f.clearPanels(); tree = new BTree(nArray); opt = f.getOptions(); tree.setBTDisplay( !opt[BT_DISPLAY] ); generateData(); if ( int_list != null ) { if( DEBUG ) System.out.println( "BTAlgThread:run int list " + int_list ); 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("BTAlgThread: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("BTreeAlgThread: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( "BTAlgThread(" + this_seq + "):inputComplete " + src + " H " + src.hashCode() + " val " + next_value ); if ( src == AddDialog ) { if( DEBUG ) System.out.println( "BTAlgThread: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( "BTAlgThread:inputComplete - it's an DELETE" ); // next_action.notify(); } else { System.out.println("BTAlgThread:inputComplete - src " + src + " unknown"); } } }