/* UserInputFrame.java User Input Frame - allows users to provide data Note: need to reset text_complete when text is changed */ package ciips.animation; import java.awt.*; import java.awt.event.*; import java.applet.*; import java.io.*; import java.net.*; import java.lang.reflect.*; /** * User input frame which pops up when user input is allowed * @see AlgAnimFrame */ public class UIFrame extends Frame implements ActionListener { private AlgAnimFrame parent; private static final boolean DEBUG = true; private static boolean user_input_allowed = false; private static int DEF_ROWS = 10, DEF_COLUMNS = 30; private static int DEF_X_LOC = 100, DEF_Y_LOC = 100; private Dimension pref_size = new Dimension( 200, 100 ); private Panel text_input; private TextArea ta = null; private Button text_ok = null; private boolean text_complete = false; private TextField status = null; private static final String def_status = "Enter list of numbers"; private Panel file_input; public static void allowUI( boolean UI_allowed ) { user_input_allowed = UI_allowed; } public static boolean isUIAllowed() { return user_input_allowed; } public UIFrame( AlgAnimFrame parent_frame, String algname ) { if ( !user_input_allowed ) return; // Don't create a user input frame setTitle("User Input for " + algname); setLocation( DEF_X_LOC, DEF_Y_LOC ); if( DEBUG ) System.out.println("UIFrame - User input allowed "); setLayout( new BorderLayout() ); // setFont(helv14); // Make the text input panel text_input = new Panel(); // text_input.setLayout( new GridLayout(4,1) ); GridBagLayout gb = new GridBagLayout(); GridBagConstraints gc = new GridBagConstraints(); gc.gridx = 0; gc.gridy = GridBagConstraints.RELATIVE; gc.fill = GridBagConstraints.HORIZONTAL; gc.weighty = 1.0; text_input.setLayout( gb ); TextField prompt = new TextField( "Type desired input, click OK" ); prompt.setEditable( false ); gb.setConstraints( prompt, gc ); text_input.add( prompt ); ta = new TextArea( DEF_ROWS, DEF_COLUMNS ); gc.weighty = 10.0; gb.setConstraints( ta, gc ); text_input.add( ta ); status = new TextField( def_status ); status.setEditable( false ); gb.setConstraints( status, gc ); text_input.add( status ); text_ok = new Button( "OK" ); gc.weighty = 1.0; gb.setConstraints( text_ok, gc ); text_input.add( text_ok ); text_ok.addActionListener( this ); // text_input.pack(); add( "North", text_input ); file_input = new Panel(); Label f_input = new Label( "File Input" ); file_input.add( f_input ); Label not_imp = new Label( "Not implemented yet" ); file_input.add( not_imp ); add( BorderLayout.SOUTH, file_input ); pack(); validate(); // setVisible( false ); } public void setVisible( boolean visible ) { if ( !user_input_allowed ) return; if ( visible ) { if( DEBUG ) System.out.println("User Input Window Visible"); validate(); } super.setVisible( visible ); status.setText( def_status ); } public Dimension getPreferredSize() { return pref_size; } public Dimension preferredSize() { return pref_size; } public String getTextInput() { try { while ( !text_complete ) { System.out.println("UIFrame: waiting"); Thread.sleep( 1000L ); } } catch( InterruptedException e ) {}; if( text_complete ) { text_complete = false; // Grey out text? return ta.getText(); } else return null; } public IntList getIntList() { IntList int_list = null; try { String s = getTextInput(); if( s != null ) { int_list = new IntList( s ); } } catch( Exception e ) { status.setText( e.getMessage() ); System.out.println("UIFrame:getIntList - exception (" + e.getMessage() + ")" ); } super.setVisible( int_list == null ); if ( int_list == null ) repaint(); return int_list; } public void actionPerformed( ActionEvent e ) { if( DEBUG ) System.out.println("UIFrame:actionPerformed - from " + e.getSource() ); if ( e.getSource() == text_ok ) { text_complete = true; } } } // class UIFrame