package edu.princeton.toy.choosers; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import edu.princeton.swing.*; import edu.princeton.toy.lang.*; /** * TAutoCompleteChooserPane manages the options related to the autocomplete feature of toy. * * @author btsang * @version 7.1 */ public class TAutoCompleteChooserPane extends JPanel { private static final String CLASS_STRING = TAutoCompleteChooserPane.class.toString(); /** * The command doesn't do anything at present. */ public static final String UPDATE_COMMAND = CLASS_STRING + "#updateCommand"; private ButtonModel buttonModels[]; private ButtonGroup group; /** * Creates a new TAutoCompleteChooserPane which has only the given autoCompleters available. */ public TAutoCompleteChooserPane(String autoCompleterNames[]) { super(new GridBagLayout()); int length = autoCompleterNames.length; buttonModels = new ButtonModel[length]; group = new ButtonGroup(); add( new JLabel("Available AutoCompleters"), new GridBagConstraints( 0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.SOUTHWEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0 ) ); for (int ctr = 0; ctr < length; ctr++) { if (autoCompleterNames[ctr] == null) throw new NullPointerException(); JRadioButton radioButton = new JRadioButton(autoCompleterNames[ctr], null, ctr == 0); buttonModels[ctr] = radioButton.getModel(); group.add(radioButton); add( radioButton, new GridBagConstraints( 0, ctr + 1, 1, 1, 0.0, (ctr == length - 1)?1.0:0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0 ) ); } } /** * Returns the index of the autoCompleter which should be used when editing a TOY program. * * @return The index of the autoCompleter which should be used when editing a TOY program. */ public int getAutoCompleterId() { for (int ctr = 0; ctr < buttonModels.length; ctr++) if (buttonModels[ctr].isSelected()) return ctr; return 0; } /** * Sets the index of the autoCompleter which should be used when editing a TOY program. * * @param autoCompleterId The index of the autoCompleter which should be used when editing a * TOY program. If this is invalid, an IllegalArgumentException will be thrown. */ public void setAutoCompleterId(int autoCompleterId) { buttonModels[autoCompleterId].setSelected(true); } /** * Performs a command based on the argument. * * @param command A string representing the command. Note that pointer equality (not string * equality) is tested here, so it is important to use the string constants defined in this * class. An IllegalArgumentException will be thrown if the argument is invalid. * @return True iff the command was executed sucessfully. */ public synchronized boolean doCommand(String command, Object extraInfo) { //////////////////////////////////////////////////////////////////////////////////////////// if (command == UPDATE_COMMAND) { return true; } throw new IllegalArgumentException(); } }